Embedded Artistry libmemory
Memory library for embedded systems (malloc and friends)
posix_memalign.c File Reference
#include <aligned_malloc.h>
#include <assert.h>
#include <errno.h>
#include <stddef.h>
Include dependency graph for posix_memalign.c:

Go to the source code of this file.

Macros

#define IS_POWER_2(x)   (!((x) & ((x)-1)))
 

Functions

int posix_memalign (void **__memptr, size_t __alignment, size_t __size)
 

Macro Definition Documentation

◆ IS_POWER_2

#define IS_POWER_2 (   x)    (!((x) & ((x)-1)))

Definition at line 6 of file posix_memalign.c.

Function Documentation

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

Definition at line 8 of file posix_memalign.c.

9 {
10  int r = ENOMEM;
11 
12  assert(__memptr);
13  assert(__size > 0);
14 
15  // TODO: Do we need to check if __alignment is a multiple of sizeof(void *)?
16  if(!IS_POWER_2(__alignment))
17  {
18  r = EINVAL;
19  }
20  else
21  {
22  *__memptr = aligned_malloc(__alignment, __size);
23 
24  if(*__memptr != NULL)
25  {
26  r = 0;
27  }
28  }
29 
30  return r;
31 }
void * aligned_malloc(size_t align, size_t size)
Allocated aligned memory.
#define IS_POWER_2(x)
Definition: posix_memalign.c:6

References aligned_malloc(), and IS_POWER_2.