简体   繁体   中英

Writing a C structure using bitfields

I want to convert this 8 Byte structure:

nt!_POOL_HEADER
   +0x000 PreviousSize     : Pos 0, 9 Bits
   +0x000 PoolIndex        : Pos 9, 7 Bits
   +0x002 BlockSize        : Pos 0, 9 Bits
   +0x002 PoolType         : Pos 9, 7 Bits
   +0x000 Ulong1           : Uint4B
   +0x004 PoolTag          : Uint4B
   +0x004 AllocatorBackTraceIndex : Uint2B
   +0x006 PoolTagHash      : Uint2B

Into a C structure like this:

struct _POOL_HEADER {
    PreviousSize;    // what SIZE do i Need to specify here ?
    PoolIndex;       // what SIZE do i Need to specify here ?
    BlockSize;       // what SIZE do i Need to specify here ?
    PoolType;        // what SIZE do i Need to specify here ?
    unsigned int Ulong1;
    unsigned int PoolTag;
    unsigned short AllocatorBackTraceIndex;
    unsigned short PoolTagHash;
};

I know this can be done with bitfields but how? This is on x86.

Assuming int to be 32 bits this would do:

struct _POOL_HEADER {
  int PreviousSize :9;
  int PoolIndex :7;
  int BlockSize :9;
  int PoolType :7;
  ...

The part above uses 4 bytes.

If you want to stick to 8 bytes all in all you need to change the lower part,

  unsigned int Ulong1;
  unsigned int PoolTag;
  unsigned short AllocatorBackTraceIndex;
  unsigned short PoolTagHash;
};

as your proposal uses more then the 4 bytes left, namely 12 bytes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM