What is aligned memory allocation?

后端 未结 4 1538
臣服心动
臣服心动 2020-12-01 09:29

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

相关标签:
4条回答
  • 2020-12-01 10:01

    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.

    0 讨论(0)
  • 2020-12-01 10:01

    The malloc() documentation says:

    [...] the allocated memory that is suitably aligned for any kind of variable.
    

    Which is true for most everything you do in C/C++. However, as pointed out by others, many special cases exist and require a specific alignment. For example, Intel processors support a 256 bit type: __m256, which is most certainly not taken in account by malloc().

    Similarly, if you want to allocate a memory buffer for data that is to be paged (similar to addresses returned by mmap(), etc.) then you need a possibly very large alignment which would waste a lot of memory if malloc() was to return buffers always aligned to such boundaries.

    Under Linux or other Unix systems, I suggest you use the posix_memalign() function:

    int posix_memalign(void **memptr, size_t alignment, size_t size);
    

    This is the most current function that one wants to use for such needs.

    0 讨论(0)
  • 2020-12-01 10:02

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

    0 讨论(0)
  • 2020-12-01 10:23

    If you have particular memory alignemnt needs (for particular hardware or libraries), you can check out non-portable memory allocators such as _aligned_malloc() and memalign(). These can easily be abstracted behind a "portable" interface, but are unfortunately non-standard.

    0 讨论(0)
提交回复
热议问题