Gromacs  2025-dev-20241003-bd59e46
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Functions
gmx::anonymous_namespace{alignedallocator.cpp} Namespace Reference

Functions

gmx_unused void * alignedMallocGeneric (std::size_t bytes, std::size_t alignment)
 Allocate aligned memory in a fully portable way. More...
 
gmx_unused void alignedFreeGeneric (void *p)
 Free aligned memory. More...
 
void * mallocImpl (std::size_t bytes, std::size_t alignment)
 Implement malloc of bytes of memory, aligned to alignment.
 
void freeImpl (void *p)
 Free aligned memory allocated with mallocImpl().
 

Function Documentation

gmx_unused void gmx::anonymous_namespace{alignedallocator.cpp}::alignedFreeGeneric ( void *  p)

Free aligned memory.

Parameters
pMemory pointer previously returned from gmx::internal::alignedFreePortable().

Since this routine relies on the original pointer being stored just before the memory area p points to, bad things will happen if you call this routine with a pointer obtained any other way, or if you call the system free() with a pointer obtained from std::alignedMalloc().

Note
This is an internal routine that should only be called from gmx::alignedFree().
gmx_unused void* gmx::anonymous_namespace{alignedallocator.cpp}::alignedMallocGeneric ( std::size_t  bytes,
std::size_t  alignment 
)

Allocate aligned memory in a fully portable way.

Parameters
bytesAmount of memory (bytes) to allocate. The routine will return nullptr if the allocation fails. However, note that asking for zero bytes will return a pointer that is non-null and properly aligned (but obviously you cannot use it, since you promised not to access data beyond the 0 bytes you asked for).
alignmentAlignment specification in bytes, must be a power of 2.
Returns
Nonzero pointer if the allocation worked, otherwise nullptr. This routine should only be called from alignedMalloc(), which also does the checking for valid values. This particular function is used for platforms where we have no control of the alignment of memory returned by the system. Instead, we increase the amount of memory requested internally such that we both can create a pointer inside this memory that fulfills the memory alignment requested, and that we have room to store the original pointer just before this area.
Note
This is an internal routine that should only be called from gmx::alignedMalloc(). Just like system-provided routines, it provides memory that is aligned - but not padded.
This functionality is provided by C++17 std::aligned_alloc, and it would be preferable to use that instead, however it is not yet widely enough available to depend on. See #3968.