Embedded Artistry Framework
Embedded Systems C++ Framework
Macros | Typedefs | Functions
aligned_malloc.c File Reference
#include <assert.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include "aligned_malloc.h"
#include "malloc.h"
Include dependency graph for aligned_malloc.c:

Macros

#define align_up(num, align)   (((num) + ((align)-1)) & ~((align)-1))
 Simple macro for making sure memory addresses are aligned to the nearest power of two. More...
 
#define PTR_OFFSET_SZ   sizeof(offset_t)
 Macro for accessing the size of our current pointer offset. More...
 

Typedefs

typedef uint16_t offset_t
 Number of bytes we're using for storing the aligned pointer offset. More...
 

Functions

void * aligned_malloc (size_t align, size_t size)
 We will call malloc with extra bytes for our header and the offset required to guarantee the desired alignment. More...
 
void aligned_free (void *ptr)
 aligned_free works like free(), but we work backwards from the returned pointer to find the correct offset and pointer location to return to free() Note that it is VERY BAD to call free() on an aligned_malloc() pointer. More...
 

Macro Definition Documentation

◆ align_up

#define align_up (   num,
  align 
)    (((num) + ((align)-1)) & ~((align)-1))

Simple macro for making sure memory addresses are aligned to the nearest power of two.

◆ PTR_OFFSET_SZ

#define PTR_OFFSET_SZ   sizeof(offset_t)

Macro for accessing the size of our current pointer offset.

Typedef Documentation

◆ offset_t

typedef uint16_t offset_t

Number of bytes we're using for storing the aligned pointer offset.

Function Documentation

◆ aligned_free()

void aligned_free ( void *  ptr)

aligned_free works like free(), but we work backwards from the returned pointer to find the correct offset and pointer location to return to free() Note that it is VERY BAD to call free() on an aligned_malloc() pointer.

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 
)

We will call malloc with extra bytes for our header and the offset required to guarantee the desired alignment.

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: