Embedded Artistry Framework
Embedded Systems C++ Framework
Macros | Functions
aligned_malloc.h File Reference
#include <stddef.h>
Include dependency graph for aligned_malloc.h:
This graph shows which files directly or indirectly include this file:

Macros

#define memalign(align, size)   aligned_malloc(align, size)
 A convenience macro for the Linux API. More...
 

Functions

void * aligned_malloc (size_t align, size_t size)
 Allocated aligned memory. More...
 
int posix_memalign (void **memptr, size_t alignment, size_t size)
 Posix Memory Alignment Extension. More...
 
void aligned_free (void *ptr)
 Free aligned memory. More...
 

Macro Definition Documentation

◆ memalign

#define memalign (   align,
  size 
)    aligned_malloc(align, size)

A convenience macro for the Linux API.

memalign() is simply a convenience definition which provides the commonly-used Linux API memalign. This forwards the arguments to aligned_malloc() and behaves in the same manner.

Parameters
alignAlignment of the memory block. Alignment refers to the starting address of the memory block. If 32-byte alignment is requested, the start address of the returned pointer will be 32-byte aligned. Note: Alignment must be a power of two. (1, 2, 4, 8, etc.)
sizeSize of the memory allocation
Returns
Pointer to allocated memory. If memory was not successfully allocated, the function will return NULL. This can happen due to invalid input (alignment is 0, size is 0, alignment is not a power of 2) or due to insufficient memory left for the requested allocation.

Function Documentation

◆ aligned_free()

void aligned_free ( void *  ptr)

Free aligned memory.

Free memory that was allocated using aligned_malloc(). This function must not be called on memory which was not allocated with aligned_malloc().

Parameters
ptrPointer to the aligned_memory() block that will be freed.

Free aligned memory.

References assert, free(), and p.

Referenced by aligned_malloc_test(), embutil::aligned_sptr(), embutil::aligned_uptr(), and posix_memalign_test().

Here is the caller graph for this function:

◆ aligned_malloc()

void* aligned_malloc ( size_t  align,
size_t  size 
)

Allocated aligned memory.

Allocate memory with at least alignment align and size size Memory which has been allocated with aligned_malloc() must be freed by calling aligned_free(). Calling free() will result in a panic or other negative effects.

Parameters
alignAlignment of the memory block. Alignment refers to the starting address of the memory block. If 32-byte alignment is requested, the start address of the returned pointer will be 32-byte aligned. Note: Alignment must be a power of two. (1, 2, 4, 8, etc.)
sizeSize of the memory allocation
Returns
Pointer to allocated memory. If memory was not successfully allocated, the function will return NULL. This can happen due to invalid input (alignment is 0, size is 0, alignment is not a power of 2) or due to insufficient memory left for the requested allocation.

Allocated aligned memory.

References align_up, assert, malloc(), NULL, p, and PTR_OFFSET_SZ.

Referenced by aligned_malloc_test(), embutil::aligned_sptr(), embutil::aligned_uptr(), and posix_memalign().

Here is the caller graph for this function:

◆ posix_memalign()

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

Posix Memory Alignment Extension.

Generated aligned memory. This function forwards the request to aligned malloc. Allocated memory must be freed with aligned_free().

Parameters
memptrA pointer to the pointer which will store the aligned memory. The memory must be freed with aligned_free(). memptr must not be NULL.
alignmentThe target alignment for the memory. Must be a power of 2.
sizeThe size of the allocation. Must be > 0.
Precondition
memptr is not NULL. alignment is power-of-2. size > 0.
Returns
ENOMEM if memory could not be allocated, EINVAL if alignment is not a power-of-2, and 0 on successful allocation.

References aligned_malloc(), assert, EINVAL, ENOMEM, IS_POWER_2, NULL, and r.

Referenced by posix_memalign_test().

Here is the caller graph for this function: