ExAllocatePoolWithTag and Alignment

MSDN says InterlockedCompareExchange should only use 32bit aligned parameters. The parameter I need to pass to InterlockedCompareExchange is a member of a struct in a non-paged pool allocated previously by ExAllocatePoolWithTag(). How do I ensure the parameter is aligned? The MSDN doc for ExAllocatePoolWithTag() POOL_TYPE says those xxxAligned type are for internal use only. How do i guarantee a member of a struct is aligned when it is stored in an allocated buffer?

from MSDN “Memory allocations of less than PAGE_SIZE are not necessarily page-aligned but are aligned to 8-byte boundaries in 32-bit systems and to 16-byte boundaries in 64-bit systems.”

First, by default, structure fields are aligned on their “natural boundaries” by MSVC in the WDK environment. So, 32-bit fields are aligned on a natural 32-bit boundary, 64-bit fields on a 64-bit boundary, etc.

Second, if you’re using VS2015 and the Win10 WDK, you can use “alignas” (for example: alignas(4)) to align fields the way you want.

Finally, there are MSFT-specific alignment forcing attributes, that work at LEAST in the 64-bit compiler. I’m not at the office as I write this, and a casual check via My Friend Google fails to turn-up what I’m thinking of… but it’s something like pointer_alignment (that’s not really the attribute, just something LIKE the attribute)… Google around and you’ll find it. I’ve used it recently, and if you want and can’t find it, I’ll grab it at work if you post back.

Personally, I vote for using “alignas” given that it’s standard C++ as of C++ 11.

Peter
OSR
@OSRDrivers

note that this is true only with the default 8-byte packing in effect. If alternate structure packing is in effect due to a compiler option or a pragma pack pre-processor directive, then the behaviour will be different.

IMHO the only two valid settings for packing are the 8 byte default and 1 byte for on disk / wire structures. All other settings are evil and cause non-obvious performance issues or even outright bugs.

Using the default packing, and default allocators, it will be impossible for you to get an alignment problem that prevents the use of Interlocked functions on x86 / x64, but as Peter points out if you are worried, you can now specify it explicitly.


From: xxxxx@lists.osr.com on behalf of xxxxx@osr.com
Sent: November 29, 2015 8:46 AM
To: Windows System Software Devs Interest List
Subject: RE:[ntdev] ExAllocatePoolWithTag and Alignment

First, by default, structure fields are aligned on their “natural boundaries” by MSVC in the WDK environment. So, 32-bit fields are aligned on a natural 32-bit boundary, 64-bit fields on a 64-bit boundary, etc.

Second, if you’re using VS2015 and the Win10 WDK, you can use “alignas” (for example: alignas(4)) to align fields the way you want.

Finally, there are MSFT-specific alignment forcing attributes, that work at LEAST in the 64-bit compiler. I’m not at the office as I write this, and a casual check via My Friend Google fails to turn-up what I’m thinking of… but it’s something like pointer_alignment (that’s not really the attribute, just something LIKE the attribute)… Google around and you’ll find it. I’ve used it recently, and if you want and can’t find it, I’ll grab it at work if you post back.

Personally, I vote for using “alignas” given that it’s standard C++ as of C++ 11.

Peter
OSR
@OSRDrivers


NTDEV is sponsored by OSR

Visit the list at: http://www.osronline.com/showlists.cfm?list=ntdev

OSR is HIRING!! See http://www.osr.com/careers

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer

> Finally, there are MSFT-specific alignment forcing attributes, that work at LEAST in the 64-bit

#pragma pack(1) around the structure :slight_smile:


Maxim S. Shatskih
Microsoft MVP on File System And Storage
xxxxx@storagecraft.com
http://www.storagecraft.com