aligned malloc() in GCC?
Asked Answered
R

5

42

Is there any standardized function in GCC or glibc to allocate memory block at aligned pointer? Like _align_malloc() in MSVC?

Route answered 1/10, 2010 at 14:8 Comment(1)
stdlib only version #228397Enjoyment
G
40

Since the question was asked, a new function was standardized by C11:

void *aligned_alloc(size_t alignment, size_t size);

and it is available in glibc (not on windows as far as I know). It takes its arguments in the same order as memalign, the reverse of Microsoft's _aligned_malloc, and uses the same free function as usual for deallocation.

A subtle difference is that aligned_alloc requires size to be a multiple of alignment.

Goodhen answered 31/1, 2013 at 22:1 Comment(0)
B
18

The [posix_memalign()][1] function provides aligned memory allocation and has been available since glibc 2.1.91.

But not necessarily with other compilers: quoting the standard "The posix_memalign() function is part of the Advisory Information option and need not be provided on all implementations."

Berliner answered 1/10, 2010 at 15:43 Comment(0)
M
11

There are _mm_malloc and _mm_free which are supported by most compilers of the x86/x64 world, with at least:

  • gcc
  • MinGW (gcc win32/win64)
  • MSVC
  • clang
  • ICC

AFAIK, these functions are not a standard at all. But it is to my knowledge the most supported ones. Other functions are more compiler specific:

  • _aligned_malloc is MSVC and MinGW only
  • posix memalign functions are not supported by at least MSVC

There are also C11 standard functions but unfortunately they are not in c++11, and including them in c++ require non standard preprocessor defines...

Mcgriff answered 25/1, 2016 at 14:48 Comment(0)
F
5

It depends on what kind of alignment you expect. Do you want a stricter alignment, or a more relaxed alignment?

malloc by definition is guaranteed to return a pointer that is properly aligned for storing any standard type in C program (and, therefore, any type built from standard types). Is it what your are looking for? Or do you need something different?

Fairbanks answered 1/10, 2010 at 15:35 Comment(3)
A common problem is that SIMD types (like SSE vectors) aren't considered "standard types", and so they have stricter alignment requirements than malloc provides.Monkshood
Another reason for wanting to do this is performance. By aligning a data structure so it spans n instead of n+1 L1 cache lines which are typically 4096 bytes or so in size, you get faster memory access on average. For certain applications, say audio processing, where buffers are the size of an L1 cache line, or a small multiple of that size, this can make a big difference.Sacrilegious
@nitro2k01: Cache lines are much smaller e.g. 32 on x86 and 64 on amd64. Pages OTOH are 4k.Adequacy
S
2

Since C11 (and C++17) there is standard library function aligned_alloc() with signature:

void *aligned_alloc( size_t alignment, size_t size );

You must #include <stdlib.h> to use it. The size parameter must be a multiple of alignment. On failure returns null pointer. Allocated pointer is freed using std::free().

Although not all compilers may have implemented this standard function. For example MSVC didn't implement it for next reason (read here):

MSVC doesn't support the aligned_alloc function. C11 specified aligned_alloc() in a way that's incompatible with the Microsoft implementation of free(), namely, that free() must be able to handle highly aligned allocations.

For MSVC _aligned_malloc() and _aligned_free() must be used.

But GCC/G++ has this standard aligned_alloc(), at least I tested this on Windows+Cygwin.

Serpigo answered 1/5, 2021 at 19:26 Comment(2)
The question wasn't about C++. Also, in general, if the question to some stack overflow question has already been satisfactorily answered, another submission, obfuscates unless providing a significant addition, revision, or argument.Lorenz
Microsoft's "reasoning" for not implementing aligned_alloc() is a joke. Per the C11 standard: "The aligned_alloc function allocates space for an object whose alignment is specified by alignment, whose size is specified by size, and whose value is indeterminate. The value of alignment shall be a valid alignment supported by the implementation and the value of size shall be an integral multiple of alignment." Any half-decent undergrad can implement that.Woolcott

© 2022 - 2024 — McMap. All rights reserved.