Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is aligned memory allocation?

Tags:

I also want to know whether glibc malloc() does this.

like image 321
Anonymous Avatar asked Oct 22 '10 05:10

Anonymous


People also ask

What does it mean for memory to be aligned?

Alignment refers to the arrangement of data in memory, and specifically deals with the issue of accessing data as proper units of information from main memory. First we must conceptualize main memory as a contiguous block of consecutive memory locations. Each location contains a fixed number of bits.

What is the meaning of memory allocation?

Memory allocation is the process of reserving a partial or complete portion of computer memory for the execution of programs and processes. Memory allocation is achieved through a process known as memory management.

What is alignment in malloc?

Regular malloc aligns memory suitable for any object type (which, in practice, means that it is aligned to alignof(max_align_t)). This function is useful for over-aligned allocations, such as to SSE, cache line, or VM page boundary.

Why do we align memory?

An aligned 32-bit read will require information stored in the same address in all four memory systems, so all systems can supply data simultaneously. An unaligned 32-bit read would require some memory systems to return data from one address, and some to return data from the next higher address.


2 Answers

Suppose that you have the structure.

struct S {     short a;     int b;     char c, d; }; 

Without alignment, it would be laid out in memory like this (assuming a 32-bit architecture):

 0 1 2 3 4 5 6 7 |a|a|b|b|b|b|c|d|  bytes |       |       |  words 

The problem is that on some CPU architectures, the instruction to load a 4-byte integer from memory only works on word boundaries. So your program would have to fetch each half of b with separate instructions.

But if the memory was laid out as:

 0 1 2 3 4 5 6 7 8 9 A B |a|a| | |b|b|b|b|c|d| | | |       |       |       | 

Then access to b becomes straightforward. (The disadvantage is that more memory is required, because of the padding bytes.)

Different data types have different alignment requirements. It's common for char to be 1-byte aligned, short to be 2-byte aligned, and 4-byte types (int, float, and pointers on 32-bit systems) to be 4-byte aligned.

malloc is required by the C standard to return a pointer that's properly aligned for any data type.

glibc malloc on x86-64 returns 16-byte-aligned pointers.

like image 93
dan04 Avatar answered Feb 27 '23 10:02

dan04


Alignment requirements specify what address offsets can be assigned to what types. This is completely implementation-dependent, but is generally based on word size. For instance, some 32-bit architectures require all int variables start on a multiple of four. On some architectures, alignment requirements are absolute. On others (e.g. x86) flouting them only comes with a performance penalty.

malloc is required to return an address suitable for any alignment requirement. In other words, the returned address can be assigned to a pointer of any type. From C99 §7.20.3 (Memory management functions):

The pointer returned if the allocation succeeds is suitably aligned so that it may be assigned to a pointer to any type of object and then used to access such an object or an array of such objects in the space allocated (until the space is explicitly deallocated).

like image 28
Matthew Flaschen Avatar answered Feb 27 '23 10:02

Matthew Flaschen