Embedded Artistry libmemory
Memory library for embedded systems (malloc and friends)
|
Go to the source code of this file.
Classes | |
struct | alloc_node_t |
Macros | |
#define | align_up(num, align) (((num) + ((align)-1)) & ~((align)-1)) |
#define | ALLOC_HEADER_SZ offsetof(alloc_node_t, block) |
#define | MIN_ALLOC_SZ ALLOC_HEADER_SZ + 32 |
Functions | |
static void | defrag_free_list (void) |
static | LIST_INIT (free_list) |
__attribute__ ((weak)) | |
void * | malloc (size_t size) |
void | free (void *ptr) |
void | malloc_addblock (void *addr, size_t size) |
Assign blocks of memory for use by malloc(). More... | |
struct alloc_node_t |
Definition at line 26 of file malloc_freelist.c.
Class Members | ||
---|---|---|
char * | block | |
ll_t | node | |
size_t | size |
#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
Definition at line 18 of file malloc_freelist.c.
#define ALLOC_HEADER_SZ offsetof(alloc_node_t, block) |
We vend a memory address to the user. This lets us translate back and forth between the vended pointer and the container we use for managing the data.
Definition at line 37 of file malloc_freelist.c.
#define MIN_ALLOC_SZ ALLOC_HEADER_SZ + 32 |
Definition at line 40 of file malloc_freelist.c.
__attribute__ | ( | (weak) | ) |
Definition at line 81 of file malloc_freelist.c.
|
static |
When we free, we can take our node and check to see if any memory blocks can be combined into larger blocks. This will help us fight against memory fragmentation in a simple way.
Definition at line 58 of file malloc_freelist.c.
References alloc_node_t::block, and alloc_node_t::size.
Referenced by free().
void free | ( | void * | ptr | ) |
Definition at line 128 of file malloc_freelist.c.
References defrag_free_list(), and alloc_node_t::node.
|
static |
void* malloc | ( | size_t | size | ) |
Definition at line 87 of file malloc_freelist.c.
References align_up, ALLOC_HEADER_SZ, alloc_node_t::block, MIN_ALLOC_SZ, alloc_node_t::node, and alloc_node_t::size.
void malloc_addblock | ( | void * | addr, |
size_t | size | ||
) |
Assign blocks of memory for use by malloc().
Initializes the malloc() backend with a memory address and memory pool size. This memory is assumed to be owned by malloc() and is vended out when memory is requested. Multiple blocks can be added.
NOTE: This API must be called before malloc() can be used. If you call malloc() before allocating memory, malloc() will return NULL because there is no available memory to provide to the user.
addr | Pointer to the memory block address that you are providing to malloc() |
size | Size of the memory block that you are providing to malloc() |
Definition at line 156 of file malloc_freelist.c.
References align_up, ALLOC_HEADER_SZ, alloc_node_t::node, and alloc_node_t::size.