Confused definition for pte_t and __pte (x)

typedef struct { unsigned long pte; } pte_t;

#define __pte(x)        ((pte_t) { (x) } )

Why not use 'typedef unsigned long pte_t' directly?

Why is '{}' used here? It looks weird.

I know that without them gcc will report an error. However, how does it work?

+4
source share
1 answer

I do not know the library, but I would argue that the intention is to prevent automatic conversion between pte_tand integer types.

I mean, a typedefis just an alias for the type, so:

typedef unsigned long pte_t;

pte_t x = 3; //ok
char y = x; //ok

But structure is a new type, therefore:

typedef struct { unsigned long pte; } pte_t;

pte_t x = 3; //error!
char y = x; //error!

Then create some functions, macros or whatever, to get / set the inner field pte_tand do.

UPDATE: , . - ​​Linux, :

#define pte_val(x)      ((x).pte)

.

, , :

typedef struct { unsigned long pte_low, pte_high; } pte_t;
#define pte_val(x)    ((x).pte_low | ((unsigned long long)(x).pte_high << 32))

? . . .

+2

All Articles