Embedded Artistry libmemory
Memory library for embedded systems (malloc and friends)
posix_memalign.c
Go to the documentation of this file.
1 #include <aligned_malloc.h>
2 #include <assert.h>
3 #include <errno.h>
4 #include <stddef.h>
5 
6 #define IS_POWER_2(x) (!((x) & ((x)-1)))
7 
8 int posix_memalign(void** __memptr, size_t __alignment, size_t __size)
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 }
int posix_memalign(void **__memptr, size_t __alignment, size_t __size)
Definition: posix_memalign.c:8
void * aligned_malloc(size_t align, size_t size)
Allocated aligned memory.
#define IS_POWER_2(x)
Definition: posix_memalign.c:6