A way to get aligned allocated memory?

S

serikas

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.
 
V

Victor Bazarov

serikas said:
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
 
A

abecedarian

serikas said:
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.
 
P

Peter Koch Larsen

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
 
U

Uenal Mutlu

serikas said:
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.
 
P

Peter Koch Larsen

Uenal Mutlu said:
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
 
U

Uenal Mutlu

"Uenal Mutlu"
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.
 
D

Dan Elliott

Peter Koch Larsen said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,667
Latest member
DaniloB294

Latest Threads

Top