Board index » cppbuilder » Allocating aligned memory

Allocating aligned memory


2004-07-20 08:54:37 PM
cppbuilder4
This is actually a follow up on a very old thread so I copied the old stuff
from google (see below) :
Bottom Line :
I need inside a structure a data member to be aligned on a 8-BYTE boundary.
I tried #pragma option -a8
This seems to work BUT ONLY when I compile in debug mode !
When I switch the setting to compile for release ( speed etc ) the data
member is aligned on 4 bytes instead of 8.
It's frustrating to see that the implementation is not full proof and
correct working happens only by accident.
It looks like in previous builds the data member was aligned on 8 bytes more
by accident.
A few members added and the 8 bytes alignment only happens when compiled for
debug.
Any ideas on a full proof way ?
FYI, I compile using BCB 5 + SP1
// ---------------------------------------
// The old thread :
// ---------------------------------------
// ---------------------------------------
Discussies:borland.public.cppbuilder.students
Datum:2003-04-26 08:17:56 PST
I have this situation (cut to the essence) :
And added // comments (The way I understand it. Please correct me if I'm
wrong)
#pragma option -a8 // Makes sure that Data Members that are 8 bytes long
are aligned on 8 bytes
// Data with type sizes of less than 8
bytes are aligned on their own type size.
typedef struct _PTSB
{
SPT Srb ; // struct
unsigned __int64 Reserved ; // Filler, QUAD aligned
(because 8 bytes and compile switch -a8)
UCHAR SenseBuf[32] ; // If Filler is QUAD
aligned then this buffer is also Quad aligned (as filler IS Quad)
UCHAR DataBuf[64 * 1024] ; // Set Buffer to 64 K
(which is enough) // QUAD Aligned because SenseBuf is multiple of Quad
} PTSB;
#pragma option -a
No the questions.
1. I am correct to assume that PTSB.DataBuf will ALWAYS point to 8 byte
aligned memory ?
2. What do I need to do to make it 16 byte aligned memory (for extra safety
in my project - certain driver issues)
#pragma option -a16 // This is a given ;-)
But what do I use to make sure that SenseBuf and DataBuff are 16 byte
aligned ?
Thanks
// ---------------------------------------
Discussies:borland.public.cppbuilder.students
Datum:2003-04-26 12:51:39 PST
I guess one of the questions is :
What is the syntax for a 16 bytes variable ?
__int128 doesn't work ?
// ---------------------------------------
Van:Remy Lebeau \(TeamB\) ( XXXX@XXXXX.COM )
Onderwerp:Re: Allocating aligned memory
View this article only
Discussies:borland.public.cppbuilder.students
Datum:2003-04-26 15:40:43 PST
"Peter" < XXXX@XXXXX.COM >wrote in message
Quote
What is the syntax for a 16 bytes variable ?
The largest data type available is 64-bit, there aren't any native 128-bit
data types (that I know of, anyway).
Gambit
// ---------------------------------------
Van:Rudy Velthuis (TeamB) ( XXXX@XXXXX.COM )
Onderwerp:Re: Allocating aligned memory
View this article only
Discussies:borland.public.cppbuilder.students
Datum:2003-04-27 00:39:02 PST
In <3eab09f7$ XXXX@XXXXX.COM >, Remy Lebeau (TeamB) wrote:
Quote

"Peter" < XXXX@XXXXX.COM >wrote in message
news: XXXX@XXXXX.COM ...

>What is the syntax for a 16 bytes variable ?

The largest data type available is 64-bit, there aren't any native
128-bit data types (that I know of, anyway).
Long double is 80 bit, so it will align on a 16 byte boundary.
--
Rudy Velthuis (TeamB)
// ---------------------------------------
Van:Peter ( XXXX@XXXXX.COM )
Onderwerp:Re: Allocating aligned memory
View this article only
Discussies:borland.public.cppbuilder.students
Datum:2003-04-27 02:47:05 PST
Quote
Long double is 80 bit, so it will align on a 16 byte boundary.
Are you sure of this ?
Because the help file says :
Help>>Paragraph (16-byte)
Help>>(Command-line switch: -a16)
Help>>Paragraph alignment aligns non-character data at 128-bit word
(16-byte) boundaries.
Help>>Data with type sizes of less than 16 bytes are aligned on their type
size.
So a long double is not 16 bytes and I guess there is no alignment on 10
bytes ... so the alignment will happen on 8 bytes (or not) ?
// ---------------------------------------
Van:Rudy Velthuis (TeamB) ( XXXX@XXXXX.COM )
Onderwerp:Re: Allocating aligned memory
View this article only
Discussies:borland.public.cppbuilder.students
Datum:2003-04-27 08:37:04 PST
In <3eaba65a$ XXXX@XXXXX.COM >, Peter wrote:
Quote

>Long double is 80 bit, so it will align on a 16 byte boundary.

Are you sure of this ?
It seems I was wrong, and that they only align on 8 bytes. I must have
remembered wrong.
--
Rudy Velthuis (TeamB)
"Ask her to wait a moment - I am almost done."
-- Carl Friedrich Gauss (1777-1855), while working, when informed that
his wife is dying
Bericht 7 van deze discussie
Van:Rudy Velthuis (TeamB) ( XXXX@XXXXX.COM )
Onderwerp:Re: Allocating aligned memory
// ---------------------------------------
Discussies:borland.public.cppbuilder.students
Datum:2003-04-26 13:06:18 PST
In <3eaaa230$ XXXX@XXXXX.COM >, Peter wrote:
Quote
#pragma option -a16 // This is a given ;-)
But what do I use to make sure that SenseBuf and DataBuff are 16
byte aligned ?
I guess you can use a datatype that is>8 bytes long, like long double
(which is 10 bytes in size):
long double Reserved;
That way the entire rest of the structure will be 16 byte aligned as well.
--
Rudy Velthuis (TeamB)
 
 

Re:Allocating aligned memory

never mind, I made a work-around
I allocate the structure twice now, with a 4 byte shift the second time, and
I pick the best one.
like so :
struct NewStruct
{
union
{
OldStruct A ;
struct
{
DWORD filler ;
OldStruct B ;
} ;
} ;
} ;
I check the critical member in both A and B and use one of the two structs
accordingly
-------------------------------------------------------------
"Peter" < XXXX@XXXXX.COM >wrote in message
Quote
This is actually a follow up on a very old thread so I copied the old
stuff
from google (see below) :

Bottom Line :
I need inside a structure a data member to be aligned on a 8-BYTE
boundary.
I tried #pragma option -a8
This seems to work BUT ONLY when I compile in debug mode !
When I switch the setting to compile for release ( speed etc ) the data
member is aligned on 4 bytes instead of 8.

It's frustrating to see that the implementation is not full proof and
correct working happens only by accident.
It looks like in previous builds the data member was aligned on 8 bytes
more
by accident.
A few members added and the 8 bytes alignment only happens when compiled
for
debug.

Any ideas on a full proof way ?
FYI, I compile using BCB 5 + SP1


// ---------------------------------------
// The old thread :
// ---------------------------------------
// ---------------------------------------

Discussies:borland.public.cppbuilder.students
Datum:2003-04-26 08:17:56 PST

I have this situation (cut to the essence) :
And added // comments (The way I understand it. Please correct me if I'm
wrong)



#pragma option -a8 // Makes sure that Data Members that are 8 bytes long
are aligned on 8 bytes
// Data with type sizes of less than 8
bytes are aligned on their own type size.

typedef struct _PTSB
{
SPT Srb ; // struct
unsigned __int64 Reserved ; // Filler, QUAD aligned
(because 8 bytes and compile switch -a8)
UCHAR SenseBuf[32] ; // If Filler is QUAD
aligned then this buffer is also Quad aligned (as filler IS Quad)
UCHAR DataBuf[64 * 1024] ; // Set Buffer to 64 K
(which is enough) // QUAD Aligned because SenseBuf is multiple of Quad
} PTSB;

#pragma option -a


No the questions.

1. I am correct to assume that PTSB.DataBuf will ALWAYS point to 8 byte
aligned memory ?
2. What do I need to do to make it 16 byte aligned memory (for extra
safety
in my project - certain driver issues)
#pragma option -a16 // This is a given ;-)
But what do I use to make sure that SenseBuf and DataBuff are 16 byte
aligned ?

Thanks

// ---------------------------------------
Discussies:borland.public.cppbuilder.students
Datum:2003-04-26 12:51:39 PST

I guess one of the questions is :

What is the syntax for a 16 bytes variable ?
__int128 doesn't work ?


// ---------------------------------------
Van:Remy Lebeau \(TeamB\) ( XXXX@XXXXX.COM )
Onderwerp:Re: Allocating aligned memory

View this article only
Discussies:borland.public.cppbuilder.students
Datum:2003-04-26 15:40:43 PST

"Peter" < XXXX@XXXXX.COM >wrote in message
news: XXXX@XXXXX.COM ...

>What is the syntax for a 16 bytes variable ?

The largest data type available is 64-bit, there aren't any native 128-bit
data types (that I know of, anyway).


Gambit

// ---------------------------------------
Van:Rudy Velthuis (TeamB) ( XXXX@XXXXX.COM )
Onderwerp:Re: Allocating aligned memory

View this article only
Discussies:borland.public.cppbuilder.students
Datum:2003-04-27 00:39:02 PST

In <3eab09f7$ XXXX@XXXXX.COM >, Remy Lebeau (TeamB) wrote:

>
>"Peter" < XXXX@XXXXX.COM >wrote in message
>news: XXXX@XXXXX.COM ...
>
>>What is the syntax for a 16 bytes variable ?
>
>The largest data type available is 64-bit, there aren't any native
>128-bit data types (that I know of, anyway).

Long double is 80 bit, so it will align on a 16 byte boundary.
--
Rudy Velthuis (TeamB)

// ---------------------------------------
Van:Peter ( XXXX@XXXXX.COM )
Onderwerp:Re: Allocating aligned memory

View this article only
Discussies:borland.public.cppbuilder.students
Datum:2003-04-27 02:47:05 PST

>Long double is 80 bit, so it will align on a 16 byte boundary.

Are you sure of this ?
Because the help file says :

Help>>Paragraph (16-byte)
Help>>(Command-line switch: -a16)
Help>>Paragraph alignment aligns non-character data at 128-bit word
(16-byte) boundaries.
Help>>Data with type sizes of less than 16 bytes are aligned on their
type
size.

So a long double is not 16 bytes and I guess there is no alignment on 10
bytes ... so the alignment will happen on 8 bytes (or not) ?

// ---------------------------------------
Van:Rudy Velthuis (TeamB) ( XXXX@XXXXX.COM )
Onderwerp:Re: Allocating aligned memory

View this article only
Discussies:borland.public.cppbuilder.students
Datum:2003-04-27 08:37:04 PST

In <3eaba65a$ XXXX@XXXXX.COM >, Peter wrote:

>
>>Long double is 80 bit, so it will align on a 16 byte boundary.
>
>Are you sure of this ?

It seems I was wrong, and that they only align on 8 bytes. I must have
remembered wrong.
--
Rudy Velthuis (TeamB)

"Ask her to wait a moment - I am almost done."
-- Carl Friedrich Gauss (1777-1855), while working, when informed that
his wife is dying

Bericht 7 van deze discussie
Van:Rudy Velthuis (TeamB) ( XXXX@XXXXX.COM )
Onderwerp:Re: Allocating aligned memory

// ---------------------------------------
Discussies:borland.public.cppbuilder.students
Datum:2003-04-26 13:06:18 PST

In <3eaaa230$ XXXX@XXXXX.COM >, Peter wrote:

>#pragma option -a16 // This is a given ;-)
>But what do I use to make sure that SenseBuf and DataBuff are 16
>byte aligned ?

I guess you can use a datatype that is>8 bytes long, like long double
(which is 10 bytes in size):

long double Reserved;

That way the entire rest of the structure will be 16 byte aligned as well.
--
Rudy Velthuis (TeamB)