Re: byte alignment/dynamic memory from heap x64

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



The 16-byte alignment for SLIST_HEADER applies only to x64/IA64, and, on
those platforms,
pool allocations are always 16-bytes aligned.
As long as your structure is `casted` directly from the returned value from
ExAllocatePool[*],
and, you do not change the declaration of SLIST_HEADER in the WDK/DDK
headers,
the compiler will take care of that for you.
For example, this will prove the compiler right or wrong.

typedef struct _Align16 {
BYTE OneChar;
SLIST_HEADER Header;
} Align16;

C_ASSERT(FIELD_OFFSET(Align16,Header) == 0x10);

--

--
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of any included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm


"Mike L" <MikeL@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:5211EC28-341F-4607-96C6-B7ACB0B2D103@xxxxxxxxxxxxxxxx

My IM driver uses SLIST_ENTRY and SLIST_HEADER(s) and they are declared in
a
struct and when my driver is loaded I dynamically allocate memory using
these
structs.

for example(not my real struct...)

struct X {

int y;
PNDIS_BUFFER pBuf;
....
SLIST_HEADER ListHeader;
SLIST_ENTRY ListEntry;
...
};

The documention for x64 states that SLIST_ENTRY and SLIST_HEADER must be
16
byte aligned.

How do I force this to happen ?

I have tried the below 3 methods( and with all 3 methods the byte
alignment
seems also to be 8 bytes instead of 16):

1) Method 1 - ExAllocatePoolWithTag with NonPagedPoolCacheAligned
2) Method 2 - pragma pack(push, 16)
3) __declspec(align(16)) - Documentation states that this will not work
for
dynamic memory from the heap, so I assume that is why it didn't work.

Any advice is appreciated, I might have a bug in my code for one of the
above methods, so I really just need someone to tell me how this is
normally
done and I can figure out the details. I saw another function
_align_memory, but did not want to link an NDIS driver to the c runtime
for
memory management, but maybe that is another alternative.

thanks,
Mike





.



Relevant Pages

  • Re: A malloc question
    ... reliably store 100 chars followed by a struct in this memory, ... the alignment of p+100 may not be suitable for the struct. ... the paddings for memory alignment are ... getting proper alignment for the UserStruct part! ...
    (comp.lang.c)
  • Re: A malloc question
    ... reliably store 100 chars followed by a struct in this memory, ... the alignment of p+100 may not be suitable for the struct. ... the paddings for memory alignment are ... malloc() will see a request of 24 byte allocation. ...
    (comp.lang.c)
  • Re: Structure size directives
    ... alignment requirements, it's a terrible idea to use e.g. ... "If you use #pragma pack to align struct or union members on ... System programmers who are concerned about code bloat et al can just ...
    (comp.lang.c)
  • Re: A malloc question
    ... reliably store 100 chars followed by a struct in this memory, ... the alignment of p+100 may not be suitable for the struct. ... the paddings for memory alignment are ... getting proper alignment for the UserStruct part! ...
    (comp.lang.c)
  • Re: byte alignment/dynamic memory from heap x64
    ... pool allocations are always 16-bytes aligned. ... struct and when my driver is loaded I dynamically allocate memory using ... dynamic memory from the heap, so I assume that is why it didn't work. ...
    (microsoft.public.development.device.drivers)