3 ms·
The Linux kernel, for example, uses the following definitions for page-table entries in different levels of amd64's four-level page tables: typedef struct
by nelhage 16y ago
The Linux kernel, for example, uses the following definitions for page-table
entries in different levels of amd64's four-level page tables:
typedef struct { unsigned long pte; } pte_t;
typedef struct { unsigned long pmd; } pmd_t;
typedef struct { unsigned long pud; } pud_t;
typedef struct { unsigned long pgd; } pgd_t;
That way, even though they're all represented as 64-bit values, the compiler
will catch you if you try to mix-and-match different types without doing
appropriate work.
Passing small structs in registers means that Linux can do that, and pass around
`pte_t`s as parameters, without having to worry about paying a runtime cost for
their typechecking.
- paulbaumgart 16y agoThat's a neat trick. Thanks for pointing that out.
- caf 16y agoThis is a nice demonstration that C is actually (moderately) strongly typed - it's just that the keyword that introduces a new type is "struct", not "typedef".