Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

what does alignment means in c/c++ malloc

Status
Not open for further replies.

syedshan

Advanced Member level 1
Advanced Member level 1
Joined
Feb 27, 2012
Messages
463
Helped
27
Reputation
54
Reaction score
26
Trophy points
1,308
Location
Jeonju, South Korea
Activity points
5,134

can any one explain what does alignment means when we are using dynamic memory allocation related functions, like

_alligned_maloc()

for example : while searching for posix_memalign() function I get to know

Alignment refers to the native word size of a processor/system. For example if a system has a native word size of 32-bit or four bytes, ideally a word stored in memory/storage would have a beginning address on a multiple of four, e.g., 0000, 0004, 0008, etc. A word stored at beginning memory/storage address not an even multiple of four, such as 0010, would require significantly more processing to successfully read the misaligned word, i.e., two four byte block reads instead of one byte block read and additional processing to retrieve the four byte word. A misalignment can complicate other system related tasks including caching and DMA.

A memory address a, is said to be n-byte aligned when n is a power of two and a is a multiple of n bytes. In this context a byte is the smallest unit of memory access, i.e. each memory address specifies a different byte. An n-byte aligned address would have log2(n) least-significant zeros when expressed in binary.

The alternate wording b-bit aligned designates a b/8 byte aligned address (ex. 64-bit aligned is 8 bytes aligned).

A memory access is said to be aligned when the datum being accessed is n bytes long and the datum address is n-byte aligned. When a memory access is not aligned, it is said to be misaligned. Note that by definition byte memory accesses are always aligned.

A memory pointer that refers to primitive data that is n bytes long is said to be aligned if it is only allowed to contain addresses that are n-byte aligned, otherwise it is said to be unaligned. A memory pointer that refers to a data aggregate (a data structure or array) is aligned if (and only if) each primitive datum in the aggregate is aligned.

Note that the definitions above assume that each primitive datum is a power of two bytes long. When this is not the case (as with 80-bit floating-point on x86) the context influences the conditions where the datum is considered aligned or not.

Data structures can be stored in memory on the stack with a static size known as bounded or on the heap with a dynamic size known as unbounded.

Typical x86 Alignments:
A char (one byte) will be 1-byte aligned.

A short (two bytes) will be 2-byte aligned.

An int (four bytes) will be 4-byte aligned.

A long (four bytes) will be 4-byte aligned.

A float (four bytes) will be 4-byte aligned.

A double (eight bytes) will be 8-byte aligned on Windows and 4-byte aligned on Linux (8-byte with -malign-double compile time option).

A long double (ten bytes with C++Builder and DMC, eight bytes with Visual C++, twelve bytes with GCC) will be 8-byte aligned with C++Builder, 2-byte aligned with DMC, 8-byte aligned with Visual C++ and 4-byte aligned with GCC.

Any pointer (four bytes) will be 4-byte aligned. (e.g.: char*, int*)


The only notable difference in alignment for a 64-bit system when compared to a 32-bit system is:

A long (eight bytes) will be 8-byte aligned.

A double (eight bytes) will be 8-byte aligned.

A long double (eight bytes with Visual C++, sixteen bytes with GCC) will be 8-byte aligned with Visual C++ and 16-byte aligned with GCC.

Any pointer (eight bytes) will be 8-byte aligned.

The posix_memalign() routine ensures the proper alignment during dynamic allocation by specifying an alignment interval/multiple.
In the following example an array of double with a size of 10 elements is aligned on a beginning memory/address with an even multiple of 64:

Code:
#include <stdlib.h>
double *foo(void) 
{
   double *var;//create array of size 10
   int     ok;
 
   ok = posix_memalign((void**)&var, 64, 10*sizeof(double));
 
   if(ok != 0)
     return NULL;
 
   return var;
}

Does this help in your understanding of alignment and the posix routines which facilitate proper alignment?

BigDog
 

ok....

So I partially understand the concept, frankly :|

Can you explain the meaning of this statement used in a sample program I've been trying to understand

Code:
dma_buffer_in = (char *) _alligned_malloc(_MEMORY_SIZE, 4096);

//where this function is used to allocate memory on a specified memory boundary,
// #define _MEMORY_SIZE 4096
// char * dma_buffer_in;//buffer used for DMA transaction
 

Can you explain the meaning of this statement used in a sample program I've been trying to understand

Code:
dma_buffer_in = (char *) _alligned_malloc(_MEMORY_SIZE, 4096);

//where this function is used to allocate memory on a specified memory boundary,
// #define _MEMORY_SIZE 4096
// char * dma_buffer_in;//buffer used for DMA transaction

Specifics may depend on the the routines implementation, from which library/compiler/system this particular routine originates.

However, as both the size of the allocation and its alignment are the same, an explanation follows:

The _alligned_malloc() routine dynamically allocates a block of memory, dma buffer, with a specified size, 4096 bytes in this case, with a specific alignment of 2^n, also 4096 or 2^12 in this case. The routine return a void pointer (void *), which is cast as a pointer of type char, which facilitates byte by byte access of the buffer.

Usually in these case a larger block of memory is actually allocated from which the small requested block can be properly aligned, therefore call to aligned dynamic allocation routines can consume far more memory resources than originally intended, particularly when the alignment specification is large, a high power of 2.

Does the above explanation help?

BigDog
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top