Embedded Artistry Framework
Embedded Systems C++ Framework
Classes | Macros | Functions
malloc_freelist.c File Reference
#include <linkedlist/ll.h>
#include <malloc.h>
#include <memory.h>
#include <stdint.h>
Include dependency graph for malloc_freelist.c:

Classes

struct  alloc_node_t
 

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 ALLOC_HEADER_SZ   offsetof(alloc_node_t, block)
 We vend a memory address to the user. More...
 
#define MIN_ALLOC_SZ   ALLOC_HEADER_SZ + 32
 

Functions

static void defrag_free_list (void)
 When we free, we can take our node and check to see if any memory blocks can be combined into larger blocks. More...
 
static LIST_INIT (free_list)
 
 __attribute__ ((weak))
 
void * malloc (size_t size)
 Allocates size bytes of uninitialized storage. More...
 
void free (void *ptr)
 Deallocates allocated memory space. More...
 
void malloc_addblock (void *addr, size_t size)
 Assign blocks of memory for use by malloc(). More...
 

Class Documentation

◆ alloc_node_t

struct alloc_node_t
Class Members
char * block
ll_t node
size_t size

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.

◆ ALLOC_HEADER_SZ

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

◆ MIN_ALLOC_SZ

#define MIN_ALLOC_SZ   ALLOC_HEADER_SZ + 32

Function Documentation

◆ __attribute__()

__attribute__ ( (weak)  )

◆ defrag_free_list()

void defrag_free_list ( void  )
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.

References b, alloc_node_t::block, list_del(), list_for_each_entry_safe, NULL, alloc_node_t::size, and t.

Referenced by free().

◆ free()

void free ( void *  ptr)

Deallocates allocated memory space.

Deallocates the space previously allocated by

See also
malloc,
calloc,
realloc. If ptr is a null pointer, the function does nothing.

The behavior is undefined if the value of ptr does not equal a value returned earlier by

See also
malloc,
calloc,
realloc.

The behavior is undefined if the memory area referred to by ptr has already been deallocated, that is, free() or realloc has already been called with ptr as the argument and no calls to malloc, calloc or realloc resulted in a pointer equal to ptr afterwards.

The behavior is undefined if after free returns, an access is made through the pointer ptr (unless another allocation function happened to result in a pointer value equal to ptr)

Parameters
ptrpointer to the memory to deallocate

References container_of, defrag_free_list(), list_add_tail(), list_for_each_entry, list_insert(), alloc_node_t::node, and ll_head::prev.

Referenced by malloc_test().

Here is the caller graph for this function:

◆ LIST_INIT()

static LIST_INIT ( free_list  )
static

◆ malloc()

void* malloc ( size_t  size)

Allocates size bytes of uninitialized storage.

If size is zero, the behavior is implementation defined (null pointer may be returned, or some non-null pointer may be returned that may not be used to access storage, but has to be passed to

See also
free). malloc is thread-safe: it behaves as though only accessing the memory locations visible through its argument, and not any static storage.
Parameters
sizenumber of bytes to allocate
Returns
On success, returns the pointer to the beginning of newly allocated memory. The returned pointer must be deallocated with
See also
free() or
realloc(). On failure, returns a null pointer.

References align_up, ALLOC_HEADER_SZ, alloc_node_t::block, list_del(), list_for_each_entry, list_insert(), MIN_ALLOC_SZ, ll_head::next, alloc_node_t::node, NULL, and alloc_node_t::size.

Referenced by malloc_test().

Here is the caller graph for this function:

◆ malloc_addblock()

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.

Parameters
addrPointer to the memory block address that you are providing to malloc()
sizeSize of the memory block that you are providing to malloc()

References align_up, ALLOC_HEADER_SZ, list_add(), alloc_node_t::node, and alloc_node_t::size.