RE: How to align static data structures in a drive r?

To my knowledge alignment is sorted out by the compiler, which makes sense to me, since it needs to create the code to access the structures.

I’ve only ever had to turn off alignment, i.e. I set it to 1 byte alignment to access tightly packed structures. Therefore I’m not sure exactly how to solve your problem. However I think that the pack pragma is what you are looking for.

Have a look at the following URL for more info

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclang/html/_predir_pack.asp

Since you are dealing with a short you may need to do some casting magic. By that I mean allocate it as a block of ULONGLONGs (I can’t remember for sure if that is an 8 byte type) or something that is 8 byte aligned. Then access it as a series of shorts via a cast.

Richard McNally


From: xxxxx@lists.osr.com [mailto:xxxxx@lists.osr.com] On Behalf Of xxxxx@conexant.com
Sent: Tuesday, 1 February 2005 12:28 PM
To: Windows System Software Devs Interest List
Subject: [ntdev] How to align static data structures in a driver?

Hi All,

Say I have a huge structure of shorts declared globally:

static short myArray={1, 2, 3, 4, 5, …, 16000}

I want myArray[0] to be 8-Byte aligned. Is there a way to tell the DDK compiler this requirement? I know in later application compilers there is a __declspec(align) directive. Will this also work in a driver?

Who actually does the alignment in this case, is it the compiler, or the linker, or both in conjunction enforce the alignment?

Thanks,
James
********************** Legal Disclaimer ****************************
“This email may contain confidential and privileged material for the sole use of the intended recipient. Any unauthorized review, use or distribution by others is strictly prohibited. If you have received the message in error, please advise the sender by reply email and delete the message. Thank you.”
**********************************************************************

Questions? First check the Kernel Driver FAQ at http://www.osronline.com/article.cfm?id=256

You are currently subscribed to ntdev as: xxxxx@dsto.defence.gov.au
To unsubscribe send a blank email to xxxxx@lists.osr.com

The align declspec operator would appear to be just what he needs, and is
supported in compiler versions greater than 1300, whatever that means.

__declspec(align(8)) USHORT array = {1,2,3,4};

Otherwise put the array in a struct and use
#pragma pack (push, 8)
struct ushortArray {
USHORT array[1];
}
#pragma pack (pop)
However I think that the default packing is already 8, and just wrapping the
array in a struct would do it.

=====================
Mark Roddy
Windows .NET/XP/2000 Consulting
Hollis Technology Solutions 603-321-1032
www.hollistech.com

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of
McNally, Richard
Sent: Sunday, February 06, 2005 11:16 PM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] How to align static data structures in a drive r?

To my knowledge alignment is sorted out by the compiler,
which makes sense to me, since it needs to create the code to
access the structures.

I’ve only ever had to turn off alignment, i.e. I set it to 1
byte alignment to access tightly packed structures. Therefore
I’m not sure exactly how to solve your problem. However I
think that the pack pragma is what you are looking for.

Have a look at the following URL for more info

http://msdn.microsoft.com/library/default.asp?url=/library/en-
us/vclang/html/_predir_pack.asp

Since you are dealing with a short you may need to do some
casting magic. By that I mean allocate it as a block of
ULONGLONGs (I can’t remember for sure if that is an 8 byte
type) or something that is 8 byte aligned. Then access it as
a series of shorts via a cast.

Richard McNally


From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of
xxxxx@conexant.com
Sent: Tuesday, 1 February 2005 12:28 PM
To: Windows System Software Devs Interest List
Subject: [ntdev] How to align static data structures in a driver?

Hi All,

Say I have a huge structure of shorts declared globally:

static short myArray={1, 2, 3, 4, 5, …, 16000}

I want myArray[0] to be 8-Byte aligned. Is there a way to
tell the DDK compiler this requirement? I know in later
application compilers there is a __declspec(align) directive.
Will this also work in a driver?

Who actually does the alignment in this case, is it the
compiler, or the linker, or both in conjunction enforce the alignment?

Thanks,
James
********************** Legal Disclaimer
**************************** “This email may contain
confidential and privileged material for the sole use of the
intended recipient. Any unauthorized review, use or
distribution by others is strictly prohibited. If you have
received the message in error, please advise the sender by
reply email and delete the message. Thank you.”
**********************************************************************

Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256

You are currently subscribed to ntdev as:
xxxxx@dsto.defence.gov.au
To unsubscribe send a blank email to xxxxx@lists.osr.com


Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256

You are currently subscribed to ntdev as:
xxxxx@hollistech.com To unsubscribe send a blank email to
xxxxx@lists.osr.com

#pragma pack doesn’t help in this case, as the structure will receive
the alignment of the only member, which is a short. There’s nothing to
guarantee the linker will align the start of the structure to a multiple
of 8 bytes. So __declspec(align(8)) is the best if it’s available,
otherwise the OP will need to use a union with a data type that requires
8-byte alignment (such as double).

Chuck

----- Original Message -----
From: “Mark Roddy”
To: “Windows System Software Devs Interest List”
Sent: Monday, February 07, 2005 11:46 AM
Subject: RE: [ntdev] How to align static data structures in a drive r?

> The align declspec operator would appear to be just what he needs, and
> is
> supported in compiler versions greater than 1300, whatever that means.
>
> __declspec(align(8)) USHORT array = {1,2,3,4};
>
> Otherwise put the array in a struct and use
> #pragma pack (push, 8)
> struct ushortArray {
> USHORT array[1];
> }
> #pragma pack (pop)
> However I think that the default packing is already 8, and just
> wrapping the
> array in a struct would do it.
>
>
> =====================
> Mark Roddy
> Windows .NET/XP/2000 Consulting
> Hollis Technology Solutions 603-321-1032
> www.hollistech.com
>
>> -----Original Message-----
>> From: xxxxx@lists.osr.com
>> [mailto:xxxxx@lists.osr.com] On Behalf Of
>> McNally, Richard
>> Sent: Sunday, February 06, 2005 11:16 PM
>> To: Windows System Software Devs Interest List
>> Subject: RE: [ntdev] How to align static data structures in a drive
>> r?
>>
>> To my knowledge alignment is sorted out by the compiler,
>> which makes sense to me, since it needs to create the code to
>> access the structures.
>>
>> I’ve only ever had to turn off alignment, i.e. I set it to 1
>> byte alignment to access tightly packed structures. Therefore
>> I’m not sure exactly how to solve your problem. However I
>> think that the pack pragma is what you are looking for.
>>
>> Have a look at the following URL for more info
>>
>> http://msdn.microsoft.com/library/default.asp?url=/library/en-
>> us/vclang/html/predir_pack.asp
>>
>> Since you are dealing with a short you may need to do some
>> casting magic. By that I mean allocate it as a block of
>> ULONGLONGs (I can’t remember for sure if that is an 8 byte
>> type) or something that is 8 byte aligned. Then access it as
>> a series of shorts via a cast.
>>
>> Richard McNally
>>
>>
_______________________________________
>> From: xxxxx@lists.osr.com
>> [mailto:xxxxx@lists.osr.com] On Behalf Of
>> xxxxx@conexant.com
>> Sent: Tuesday, 1 February 2005 12:28 PM
>> To: Windows System Software Devs Interest List
>> Subject: [ntdev] How to align static data structures in a driver?
>>
>> Hi All,
>>
>> Say I have a huge structure of shorts declared globally:
>>
>> static short myArray={1, 2, 3, 4, 5, …, 16000}
>>
>> I want myArray[0] to be 8-Byte aligned. Is there a way to
>> tell the DDK compiler this requirement? I know in later
>> application compilers there is a __declspec(align) directive.
>> Will this also work in a driver?
>>
>> Who actually does the alignment in this case, is it the
>> compiler, or the linker, or both in conjunction enforce the
>> alignment?
>>
>> Thanks,
>> James
>> Legal Disclaimer
>>
******“This email may contain
>> confidential and privileged material for the sole use of the
>> intended recipient. Any unauthorized review, use or
>> distribution by others is strictly prohibited. If you have
>> received the message in error, please advise the sender by
>> reply email and delete the message. Thank you.”
>> **********************************************************************
>> —
>> Questions? First check the Kernel Driver FAQ at
>> http://www.osronline.com/article.cfm?id=256
>>
>> You are currently subscribed to ntdev as:
>> xxxxx@dsto.defence.gov.au
>> To unsubscribe send a blank email to xxxxx@lists.osr.com
>>
>> —
>> Questions? First check the Kernel Driver FAQ at
>> http://www.osronline.com/article.cfm?id=256
>>
>> You are currently subscribed to ntdev as:
>> xxxxx@hollistech.com To unsubscribe send a blank email to
>> xxxxx@lists.osr.com
>>
>
>
>
>
> —
> Questions? First check the Kernel Driver FAQ at
> http://www.osronline.com/article.cfm?id=256
>
> You are currently subscribed to ntdev as: xxxxx@cbatson.com
> To unsubscribe send a blank email to xxxxx@lists.osr.com
>

The default packing IS 8. I checked in some code I wrote for some
portable middleware that ran on WIN32 among others and was eventually
assimilated by the IBM borg. Never be a programmer for hire. I would
be independently wealthy if *I* had sold that code to IBM. To quote an
(in)famous list member, “BUT, I’m NOT bitter!”

Mark Roddy wrote:

The align declspec operator would appear to be just what he needs, and is
supported in compiler versions greater than 1300, whatever that means.

__declspec(align(8)) USHORT array = {1,2,3,4};

Otherwise put the array in a struct and use
#pragma pack (push, 8)
struct ushortArray {
USHORT array[1];
}
#pragma pack (pop)
However I think that the default packing is already 8, and just wrapping the
array in a struct would do it.

=====================
Mark Roddy
Windows .NET/XP/2000 Consulting
Hollis Technology Solutions 603-321-1032
www.hollistech.com

>-----Original Message-----
>From: xxxxx@lists.osr.com
>[mailto:xxxxx@lists.osr.com] On Behalf Of
>McNally, Richard
>Sent: Sunday, February 06, 2005 11:16 PM
>To: Windows System Software Devs Interest List
>Subject: RE: [ntdev] How to align static data structures in a drive r?
>
>To my knowledge alignment is sorted out by the compiler,
>which makes sense to me, since it needs to create the code to
>access the structures.
>
>I’ve only ever had to turn off alignment, i.e. I set it to 1
>byte alignment to access tightly packed structures. Therefore
>I’m not sure exactly how to solve your problem. However I
>think that the pack pragma is what you are looking for.
>
>Have a look at the following URL for more info
>
>http://msdn.microsoft.com/library/default.asp?url=/library/en-
>us/vclang/html/predir_pack.asp
>
>Since you are dealing with a short you may need to do some
>casting magic. By that I mean allocate it as a block of
>ULONGLONGs (I can’t remember for sure if that is an 8 byte
>type) or something that is 8 byte aligned. Then access it as
>a series of shorts via a cast.
>
>Richard McNally
>
>
_______________________________________
>From: xxxxx@lists.osr.com
>[mailto:xxxxx@lists.osr.com] On Behalf Of
>xxxxx@conexant.com
>Sent: Tuesday, 1 February 2005 12:28 PM
>To: Windows System Software Devs Interest List
>Subject: [ntdev] How to align static data structures in a driver?
>
>Hi All,
>
>Say I have a huge structure of shorts declared globally:
>
>static short myArray={1, 2, 3, 4, 5, …, 16000}
>
>I want myArray[0] to be 8-Byte aligned. Is there a way to
>tell the DDK compiler this requirement? I know in later
>application compilers there is a __declspec(align) directive.
>Will this also work in a driver?
>
>Who actually does the alignment in this case, is it the
>compiler, or the linker, or both in conjunction enforce the alignment?
>
>Thanks,
>James
>********************** Legal Disclaimer
>**************************** “This email may contain
>confidential and privileged material for the sole use of the
>intended recipient. Any unauthorized review, use or
>distribution by others is strictly prohibited. If you have
>received the message in error, please advise the sender by
>reply email and delete the message. Thank you.”
>**********************************************************************
>—
>Questions? First check the Kernel Driver FAQ at
>http://www.osronline.com/article.cfm?id=256
>
>You are currently subscribed to ntdev as:
>xxxxx@dsto.defence.gov.au
>To unsubscribe send a blank email to xxxxx@lists.osr.com
>
>—
>Questions? First check the Kernel Driver FAQ at
>http://www.osronline.com/article.cfm?id=256
>
>You are currently subscribed to ntdev as:
>xxxxx@hollistech.com To unsubscribe send a blank email to
>xxxxx@lists.osr.com
>


Questions? First check the Kernel Driver FAQ at http://www.osronline.com/article.cfm?id=256

You are currently subscribed to ntdev as: xxxxx@yahoo.com
To unsubscribe send a blank email to xxxxx@lists.osr.com

Union it with a double. Problem solved.

Chuck Batson wrote:

#pragma pack doesn’t help in this case, as the structure will receive
the alignment of the only member, which is a short. There’s nothing to
guarantee the linker will align the start of the structure to a multiple
of 8 bytes. So __declspec(align(8)) is the best if it’s available,
otherwise the OP will need to use a union with a data type that requires
8-byte alignment (such as double).

Chuck

----- Original Message ----- From: “Mark Roddy”
> To: “Windows System Software Devs Interest List”
> Sent: Monday, February 07, 2005 11:46 AM
> Subject: RE: [ntdev] How to align static data structures in a drive r?
>
>
>> The align declspec operator would appear to be just what he needs, and is
>> supported in compiler versions greater than 1300, whatever that means.
>>
>> __declspec(align(8)) USHORT array = {1,2,3,4};
>>
>> Otherwise put the array in a struct and use
>> #pragma pack (push, 8)
>> struct ushortArray {
>> USHORT array[1];
>> }
>> #pragma pack (pop)
>> However I think that the default packing is already 8, and just
>> wrapping the
>> array in a struct would do it.
>>
>>
>> =====================
>> Mark Roddy
>> Windows .NET/XP/2000 Consulting
>> Hollis Technology Solutions 603-321-1032
>> www.hollistech.com
>>
>>> -----Original Message-----
>>> From: xxxxx@lists.osr.com
>>> [mailto:xxxxx@lists.osr.com] On Behalf Of
>>> McNally, Richard
>>> Sent: Sunday, February 06, 2005 11:16 PM
>>> To: Windows System Software Devs Interest List
>>> Subject: RE: [ntdev] How to align static data structures in a drive r?
>>>
>>> To my knowledge alignment is sorted out by the compiler,
>>> which makes sense to me, since it needs to create the code to
>>> access the structures.
>>>
>>> I’ve only ever had to turn off alignment, i.e. I set it to 1
>>> byte alignment to access tightly packed structures. Therefore
>>> I’m not sure exactly how to solve your problem. However I
>>> think that the pack pragma is what you are looking for.
>>>
>>> Have a look at the following URL for more info
>>>
>>> http://msdn.microsoft.com/library/default.asp?url=/library/en-
>>> us/vclang/html/predir_pack.asp
>>>
>>> Since you are dealing with a short you may need to do some
>>> casting magic. By that I mean allocate it as a block of
>>> ULONGLONGs (I can’t remember for sure if that is an 8 byte
>>> type) or something that is 8 byte aligned. Then access it as
>>> a series of shorts via a cast.
>>>
>>> Richard McNally
>>>
>>>
_______________________________________
>>> From: xxxxx@lists.osr.com
>>> [mailto:xxxxx@lists.osr.com] On Behalf Of
>>> xxxxx@conexant.com
>>> Sent: Tuesday, 1 February 2005 12:28 PM
>>> To: Windows System Software Devs Interest List
>>> Subject: [ntdev] How to align static data structures in a driver?
>>>
>>> Hi All,
>>>
>>> Say I have a huge structure of shorts declared globally:
>>>
>>> static short myArray={1, 2, 3, 4, 5, …, 16000}
>>>
>>> I want myArray[0] to be 8-Byte aligned. Is there a way to
>>> tell the DDK compiler this requirement? I know in later
>>> application compilers there is a __declspec(align) directive.
>>> Will this also work in a driver?
>>>
>>> Who actually does the alignment in this case, is it the
>>> compiler, or the linker, or both in conjunction enforce the alignment?
>>>
>>> Thanks,
>>> James
>>> Legal Disclaimer
>>>
******“This email may contain
>>> confidential and privileged material for the sole use of the
>>> intended recipient. Any unauthorized review, use or
>>> distribution by others is strictly prohibited. If you have
>>> received the message in error, please advise the sender by
>>> reply email and delete the message. Thank you.”
>>> **********************************************************************
>>> —
>>> Questions? First check the Kernel Driver FAQ at
>>> http://www.osronline.com/article.cfm?id=256
>>>
>>> You are currently subscribed to ntdev as:
>>> xxxxx@dsto.defence.gov.au
>>> To unsubscribe send a blank email to xxxxx@lists.osr.com
>>>
>>> —
>>> Questions? First check the Kernel Driver FAQ at
>>> http://www.osronline.com/article.cfm?id=256
>>>
>>> You are currently subscribed to ntdev as:
>>> xxxxx@hollistech.com To unsubscribe send a blank email to
>>> xxxxx@lists.osr.com
>>>
>>
>>
>>
>>
>> —
>> Questions? First check the Kernel Driver FAQ at
>> http://www.osronline.com/article.cfm?id=256
>>
>> You are currently subscribed to ntdev as: xxxxx@cbatson.com
>> To unsubscribe send a blank email to xxxxx@lists.osr.com
>>
>
>
> —
> Questions? First check the Kernel Driver FAQ at
> http://www.osronline.com/article.cfm?id=256
>
> You are currently subscribed to ntdev as: xxxxx@yahoo.com
> To unsubscribe send a blank email to xxxxx@lists.osr.com
>

> Say I have a huge structure of shorts declared globally:

static short myArray={1, 2, 3, 4, 5, …, 16000}

I want myArray[0] to be 8-Byte aligned. Is there a way to tell the DDK
compiler this requirement? I know in later application compilers there is a
__declspec(align) directive. Will this also work in a driver?

Probably declspec align should work. There isn’t much magic about the DDK
vs the SDK.

Alternately declare a long just in front of your array of shorts, assuming
packing is at least 8 bytes.

Alternately put your array of shorts in a structure, assuming packing is at
least 8 bytes.

Loren

> #pragma pack doesn’t help in this case, as the structure will receive

the alignment of the only member, which is a short. There’s nothing to
guarantee the linker will align the start of the structure to a multiple
of 8 bytes. So __declspec(align(8)) is the best if it’s available,
otherwise the OP will need to use a union with a data type that requires
8-byte alignment (such as double).

Er, no. the packing is also the structure alignment, so /Zp8 will insure a
structure starts on an 8 byte boundary.

Loren

Loren,

Please try the following:

#include <stdio.h>

short z1, z2, z3;

#pragma pack(push, 8)
struct {
short a;
} a;
#pragma pack(pop)

struct {
short b;
} b;

short c;

short d[5] = { 0 };

#pragma pack(push, 8)
struct {
short e[5];
} e = { 0 };
#pragma pack(pop)

int main(int argc, char * * argv)
{
printf(“Compiler version is %d.\n”, _MSC_VER);
printf(“Address of a = 0x%p\n”, &a);
printf(“Address of e = 0x%p\n”, &e);
return 0;
}

Here’s the results I get:

Compiler version is 1310.
Address of a = 0x00428516
Address of e = 0x004284EC

Which are clearly not 8-byte aligned.

Chuck

----- Original Message -----
From: “Loren Wilton”
To: “Windows System Software Devs Interest List”
Sent: Monday, February 07, 2005 3:28 PM
Subject: Re: [ntdev] How to align static data structures in a drive r?

>> #pragma pack doesn’t help in this case, as the structure will receive
>> the alignment of the only member, which is a short. There’s nothing
>> to
>> guarantee the linker will align the start of the structure to a
>> multiple
>> of 8 bytes. So __declspec(align(8)) is the best if it’s available,
>> otherwise the OP will need to use a union with a data type that
>> requires
>> 8-byte alignment (such as double).
>
> Er, no. the packing is also the structure alignment, so /Zp8 will
> insure a
> structure starts on an 8 byte boundary.
>
> Loren</stdio.h>

(For the list archives, in the hopes that this advice is not
followed…)

Declaring a LONG just in front of your array of shorts is a bad idea if
you want to ensure alignment, as it relies on two things:

  1. The order the compiler will put locals on the stack.

  2. The idea that the compiler will keep the order of the local variables
    relative positions.

Neither idea has *ever* been guaranteed, and I think you will find that
compiling with full optimizations will likely move things around in
complex functions (i.e. with many local variables of different types).

Just don’t do it! :slight_smile:
.

-----Original Message-----
From: Loren Wilton [mailto:xxxxx@earthlink.net]
Sent: Monday, February 07, 2005 12:27 AM
Subject: Re: How to align static data structures in a drive r?

Say I have a huge structure of shorts declared globally:

static short myArray={1, 2, 3, 4, 5, …, 16000}

I want myArray[0] to be 8-Byte aligned. Is there a way to tell the DDK
compiler this requirement? I know in later application compilers there
is a
__declspec(align) directive. Will this also work in a driver?

Probably declspec align should work. There isn’t much magic about the
DDK vs the SDK.

Alternately declare a long just in front of your array of shorts,
assuming packing is at least 8 bytes.

Alternately put your array of shorts in a structure, assuming packing is
at least 8 bytes.

Loren