469,282 Members | 1,949 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,282 developers. It's quick & easy.

A way to get aligned allocated memory?


Is there a way to get aligned dynamically allocated memory?
(provided that the requested memory size is a power of 2.)

For example, if I request 128 bytes of memory, can I implement
an allocator that allocates 128 bytes with 128-byte alignment?

Of course I know that it is possible by allocating twice the requested
size or more, but I would like to know if it is possible without excessive
memory allocation.
Jul 23 '05 #1
7 2178
serikas wrote:
Is there a way to get aligned dynamically allocated memory?
(provided that the requested memory size is a power of 2.)

For example, if I request 128 bytes of memory, can I implement
an allocator that allocates 128 bytes with 128-byte alignment?

Of course I know that it is possible by allocating twice the requested
size or more, but I would like to know if it is possible without excessive
memory allocation.


The answer is "no" because there are no mechanisms in the C++ _language_
that control alignment. There may be mechanisms provided by your compiler
or your OS, but they are not part of the language.

V
Jul 23 '05 #2
serikas wrote:
Is there a way to get aligned dynamically allocated memory?
(provided that the requested memory size is a power of 2.) For example, if I request 128 bytes of memory, can I implement
an allocator that allocates 128 bytes with 128-byte alignment? Of course I know that it is possible by allocating twice the requested size or more, but I would like to know if it is possible without excessive memory allocation.


The answer is "yes" but there are no mechanisms in the C++ _language_
that control alignment. There are mechanisms provided by your compiler
or your OS and there are some very portable general solutions. The C++
Standard: "The alignment of a complete object type is an
implementation-defined integer value representing a number of bytes; an
object is allocated at an address that meets the alignment requirements
of its object type."

A.

Jul 23 '05 #3
"serikas" <se*****@hotmail.com> wrote

Is there a way to get aligned dynamically allocated memory?
(provided that the requested memory size is a power of 2.)

For example, if I request 128 bytes of memory, can I implement
an allocator that allocates 128 bytes with 128-byte alignment?

Of course I know that it is possible by allocating twice the requested
size or more, but I would like to know if it is possible without excessive
memory allocation.


If all items are same size then I would solve it this way:
Initially (in ctor of your allocator class) allocate a twice large
block and find the right aligned offset position (adress) for
the first. And, from then on use realloc on the memory block
and keep a counter or the total lenght.
But that means: only the allocator can release the memory block,
and you cannot physically delete released gaps, so use a flag
in the data hdr to indicate whether its free or not, and reuse them.
Jul 23 '05 #4

<ab*********@spambob.com> skrev i en meddelelse
news:11**********************@f14g2000cwb.googlegr oups.com...
serikas wrote:
Is there a way to get aligned dynamically allocated memory?
(provided that the requested memory size is a power of 2.)

For example, if I request 128 bytes of memory, can I implement
an allocator that allocates 128 bytes with 128-byte alignment?

Of course I know that it is possible by allocating twice the

requested
size or more, but I would like to know if it is possible without

excessive
memory allocation.


The answer is "yes" but there are no mechanisms in the C++ _language_
that control alignment. There are mechanisms provided by your compiler
or your OS and there are some very portable general solutions. The C++
Standard: "The alignment of a complete object type is an
implementation-defined integer value representing a number of bytes; an
object is allocated at an address that meets the alignment requirements
of its object type."

A.


Yes?? How? Of course you can allocate aligned depending on the requirement
of some type, but I doubt you can have alignment guaranteed to be some power
of 2.

/Peter
Jul 23 '05 #5

"Uenal Mutlu" <52***************@t-online.de> skrev i en meddelelse
news:d4*************@news.t-online.com...
"serikas" <se*****@hotmail.com> wrote

Is there a way to get aligned dynamically allocated memory?
(provided that the requested memory size is a power of 2.)

For example, if I request 128 bytes of memory, can I implement
an allocator that allocates 128 bytes with 128-byte alignment?

Of course I know that it is possible by allocating twice the requested
size or more, but I would like to know if it is possible without
excessive
memory allocation.


If all items are same size then I would solve it this way:
Initially (in ctor of your allocator class) allocate a twice large
block and find the right aligned offset position (adress) for
the first. And, from then on use realloc on the memory block
and keep a counter or the total lenght.
But that means: only the allocator can release the memory block,
and you cannot physically delete released gaps, so use a flag
in the data hdr to indicate whether its free or not, and reuse them.

That will not work. realloc will typically move the block.

/Peter
Jul 23 '05 #6
"Peter Koch Larsen" wrote
"Uenal Mutlu"
"serikas" wrote

Is there a way to get aligned dynamically allocated memory?
(provided that the requested memory size is a power of 2.)

For example, if I request 128 bytes of memory, can I implement
an allocator that allocates 128 bytes with 128-byte alignment?

Of course I know that it is possible by allocating twice the requested
size or more, but I would like to know if it is possible without
excessive
memory allocation.


If all items are same size then I would solve it this way:
Initially (in ctor of your allocator class) allocate a twice large
block and find the right aligned offset position (adress) for
the first. And, from then on use realloc on the memory block
and keep a counter or the total lenght.
But that means: only the allocator can release the memory block,
and you cannot physically delete released gaps, so use a flag
in the data hdr to indicate whether its free or not, and reuse them.

That will not work. realloc will typically move the block.


You are right, a bad<tm> idea of mine.
The idea would only function for a fixed nbr of items preallocated
with just one alloc request, and of course no realloc'ing afterwards.
Jul 23 '05 #7
"Peter Koch Larsen" <pk*****@mailme.dk> wrote in message
news:Qm*****************@news000.worldonline.dk...

<ab*********@spambob.com> skrev i en meddelelse
news:11**********************@f14g2000cwb.googlegr oups.com...
serikas wrote:
Is there a way to get aligned dynamically allocated memory?
(provided that the requested memory size is a power of 2.)
For example, if I request 128 bytes of memory, can I implement
an allocator that allocates 128 bytes with 128-byte alignment?

Of course I know that it is possible by allocating twice the

requested
size or more, but I would like to know if it is possible without

excessive
memory allocation.


The answer is "yes" but there are no mechanisms in the C++ _language_
that control alignment. There are mechanisms provided by your compiler
or your OS and there are some very portable general solutions. The C++
Standard: "The alignment of a complete object type is an
implementation-defined integer value representing a number of bytes; an
object is allocated at an address that meets the alignment requirements
of its object type."

A.


Yes?? How? Of course you can allocate aligned depending on the requirement
of some type, but I doubt you can have alignment guaranteed to be some

power of 2.

/Peter


This post may be beneath you, but I always add whatever memory is necessary
to make my structure fill out the word. So, if my structure begins with a
short, I add a padding of two bytes before or after the first element.

Hope this helps.

- dan
Jul 23 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

74 posts views Thread by ballpointpenthief | last post: by
11 posts views Thread by Divick | last post: by
9 posts views Thread by Spoon | last post: by
29 posts views Thread by K. Jennings | last post: by
15 posts views Thread by shaanxxx | last post: by
6 posts views Thread by Praetorian | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.