2011-01-23 92 views
2

我有一個教授寫的程序模擬了內存寫入L2緩存的方式。它有幾個地方我應該填補空白。我應該做的第一件事是清除每個緩存條目的有效位。他給了我們以下幾點:對C++中typedef結構的混淆

//number of cache entries (2^11) 

#define L2_NUM_CACHE_ENTRIES (1<<11) 

/*************************************************** 

This struct defines the structure of a single cache 
entry in the L2 cache. It has the following fields: 
v_d_tag: 32-bit unsigned word containing the 
valid (v) bit at bit 31 (leftmost bit), 
the dirty bit (d) at bit 30, and the tag 
in bits 0 through 15 (the 16 rightmost bits) 
cache_line: an array of 8 words, constituting a single 
cache line. 
****************************************************/ 

Typedef struct { 

uint32_t v_d_tag; 

uint32_t cache_line[WORDS_PER_CACHE_LINE]; 

} L2_CACHE_ENTRY; 

//The L2 is just an array cache entries 

L2_CACHE_ENTRY l2_cache[L2_NUM_CACHE_ENTRIES]; 

所以,據我瞭解,清除有效位只是意味着將其設置爲零。有效位是v_d_tag的第31位,所以我應該使用位掩碼 - 我想按照「v_d_tag = v_d_tag & 0x80000000;」的方式做一些事情,我想?但我不明白的是,我如何通過併爲每個緩存條目執行此操作。我看到緩存條目數組(l2_cache),但我看不到v_d_tag與此有關。

有人可以解釋給我嗎?

+7

什麼是你的問題與你的標題呢? – 2011-01-23 21:01:23

+0

好吧,我相當肯定,我對代碼不瞭解的內容與typedef設置有關,我不確定如何簡潔地將我的問題的具體內容用於標題,因此我隨它一起去了。對不起,如果我違反了禮儀,這不是我的意圖 - 我只是不知道我在說什麼。 ^^; – amb691 2011-01-24 05:34:28

回答

4

typedef struct在C++中是多餘的,正如#define我看到的,它們可能是static const int。

爲了清除所有這些,你會想要做

for(int i = 0; i < L2_NUM_CACHE_ENTRIES; i++) 
    l2_cache[i].v_d_tag &= 0x80000000; 
-1

的結構是在C時尚定義,因爲在C中,它是一種常見的成語的typedef聲明一個結構,這樣它可以作爲一種類型使用,而不必在每個參考文獻上寫struct L2_CACHE_ENTRY。這個成語在C++中不再需要,因爲struct標籤將作爲單獨的類型工作。

總之,在C++中,你可以把

typedef struct { 

uint32_t v_d_tag; 

uint32_t cache_line[WORDS_PER_CACHE_LINE]; 

} L2_CACHE_ENTRY; 

完全一樣

struct L2_CACHE_ENTRY{ 

uint32_t v_d_tag; 

uint32_t cache_line[WORDS_PER_CACHE_LINE]; 

};