At about the time of 3/21/2007 2:08 AM, cman stated the following:
What does this kind of typedef accomplish?
typedef struct { unsigned long pte_low; } pte_t;
typedef struct { unsigned long pgd; } pgd_t;
typedef struct { unsigned long pgprot; } pgprot_t
I am familiar with "typedef int NUMBER", but how does it work with
structures.
Tilak
This allows you to define a structure type of pte_t, pgd_t, or pgprot_t
which you can use in your code. Ex:
struct pte_t { unsigned long pte_low; };
This defines the struct pte_t to the compiler, so when you need to
declare a variable to it:
struct pte_t varname;
Where as if you do:
typedef struct { unsigned long pte_low; } pte_t;
Then you can do:
pte_t varname;
with no struct tag because the compiler knows that pte_t is a structure
type when it encountered the typedef. Some people here will advise you
against doing this, and I forgot what the reason was. Here's some
real-world code that uses that format:
/* **** public types */
/* data transfer structure */
/* **note: if using a block algorithm such as AES, then
the output buffer must be at least as big as the input
buffer, and should be a multiple of the blocksize. */
typedef struct crypto_data_tag
{
uint32 sizein; /* datablock buffer size for input */
uint32 sizeout; /* datablock buffer size for output */
uint8 *datain; /* pointer to input datablock buffer */
uint8 *dataout; /* pointer to output datablock buffer */
} crypto_data_t;
/* crypto key data */
/* sub-structure used by other structures */
typedef struct crypto_key_tag
{
uint8 key[CRYPTO_KEYSIZE]; /* master crypto key */
uint8 salt[CRYPTO_SALTSIZE]; /* salt */
uint8 rand[CRYPTO_RANDSIZE]; /* random generator seed */
} crypto_key_t;
/* info transfer structure */
/* fully datafill this structure when initializing a crypto state */
typedef struct crypto_keydata_tag
{
int algorithm; /* crypto algorithm to use */
int keysize; /* key size constant */
uint32 nonce; /* nonce counter */
uint32 skip; /* nonce counter skip */
crypto_key_t key; /* key information data */
} crypto_keydata_t;
I think that some of the regulars hate me now after posting this...but
FWIW, I see ALOT of real-world code that does things this way. The
*ONE* problem that I have ever ran into doing it this way is linked
lists. You get stuff like this below:
typedef struct data_tag
{
void *data;
unsigned long size;
struct data_tag *next;
struct data_tag *prev;
} data_t;
Because it's not a complete type, you *MUST* use the struct identifier
with your initial tag as you would with a normal structure as shown
below. The only reason to have the initial structure name like _tag is
to solve linked list issues when doing it this way. Other than that,
you don't need it.
struct data_t
{
void *data;
unsigned long size;
struct data_t *next;
struct data_t *prev;
};
--
Daniel Rudy
Email address has been base64 encoded to reduce spam
Decode email address using b64decode or uudecode -m
Why geeks like computers: look chat date touch grep make unzip
strip view finger mount fcsk more fcsk yes spray umount sleep