Confusión sobre la estructura typedef en c ++
Pregunta
Tengo un programa que escribió mi profesor que simula la forma en que la memoria escribe en el caché L2. Tiene un par de lugares donde se supone que debo llenar el espacio en blanco. Lo primero que se supone que debo hacer es borrar el bit válido de cada entrada de caché. Él nos ha dado lo siguiente:
//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];
Entonces, según tengo entendido, borrar el bit válido solo significa configurarlo en cero. El bit válido es el bit 31 de V_D_TAG, por lo que debería usar una maestría en bits. Quiero hacer algo en la línea de "V_D_TAG = V_D_TAG & 0x80000000;", ¿Creo que? Pero lo que no entiendo es cómo puedo pasar y hacerlo para cada entrada de caché. Veo la matriz de entradas de caché (l2_cache), pero no veo cómo se relaciona el V_D_TAG.
¿Alguien puede explicarme a mí?
Solución
typedef struct es redundante en c ++, al igual que el #define
Estoy viendo que podrían ser estáticos const.
Para aclararlos a todos, querrías hacer
for(int i = 0; i < L2_NUM_CACHE_ENTRIES; i++)
l2_cache[i].v_d_tag &= 0x80000000;
Otros consejos
La estructura se define de manera C, ya que en C es un idioma común a Typedef declarar una estructura para que pueda usarse como un tipo sin tener que escribir struct L2_CACHE_ENTRY
en cada referencia. Este idioma ya no es necesario en C ++, ya que el struct
La etiqueta funcionará como un tipo individual.
En resumen, en C ++ puedes tratar
typedef struct {
uint32_t v_d_tag;
uint32_t cache_line[WORDS_PER_CACHE_LINE];
} L2_CACHE_ENTRY;
exactamente el mismo que
struct L2_CACHE_ENTRY{
uint32_t v_d_tag;
uint32_t cache_line[WORDS_PER_CACHE_LINE];
};