Embedded SDK
Embedded SDK
Loading...
Searching...
No Matches
mem_alloc.c File Reference

Dynamic memory allocation implementation. More...

#include <stdio.h>
#include <string.h>
#include "exceptions.h"
#include "errors.h"
#include "os_helpers.h"
#include "os_math.h"
#include "os_print.h"
#include "os_utils.h"
#include "mem_alloc.h"
Include dependency graph for mem_alloc.c:

Go to the source code of this file.

Classes

struct  header_s
struct  heap_t

Macros

#define PAYLOAD_ALIGNEMENT   4
#define PAYLOAD_DATA_ALIGNEMENT   8
#define ALLOC_CHUNK_HEADER_SIZE   4
#define FREE_CHUNK_HEADER_SIZE   8
#define MAX_BLOCK_SIZE   0x7FFF
#define HEAP_HEADER_SIZE    ((sizeof(heap_t) + PAYLOAD_DATA_ALIGNEMENT - 1) & ~(PAYLOAD_DATA_ALIGNEMENT - 1))
#define NB_LINEAR_SEGMENTS   5
#define NB_MAX_SEGMENTS   10
#define NB_SUB_SEGMENTS   4
#define GET_PTR(_heap, index)
#define GET_IDX(_heap, _ptr)
#define GET_SEGMENT(_size)
#define MEM_POISON(_addr, _size)
#define MEM_UNPOISON(_addr, _size)
#define MEM_REDZONE_SIZE   0
#define LINKS_ADDR(_ptr)
#define LINKS_SIZE   (FREE_CHUNK_HEADER_SIZE - ALLOC_CHUNK_HEADER_SIZE)
#define LINKS_UNPOISON(_ptr)
#define LINKS_POISON(_ptr)

Typedefs

typedef struct header_s header_t

Functions

static size_t align_alloc_size (size_t size)
static int seglist_index (heap_t *heap, size_t size)
static int seglist_index_up (heap_t *heap, size_t size)
static void set_fprev (header_t *header, uint16_t fprev)
static void set_fnext (header_t *header, uint16_t fnext)
static void list_remove (heap_t *heap, uint16_t *first_free, uint16_t elem)
static void list_push (heap_t *heap, header_t *header)
static void ensure_chunk_valid (heap_t *heap, header_t *header)
static header_t * coalesce (heap_t *heap, header_t *header, header_t *neighbour)
static bool parse_callback (void *data, uint8_t *addr, bool allocated, size_t size)
mem_ctx_t mem_init (void *heap_start, size_t heap_size)
 initializes the heap for this allocator
void * mem_alloc (mem_ctx_t ctx, size_t nb_bytes)
 allocates a buffer of the given size, if possible
void * mem_realloc (mem_ctx_t ctx, void *ptr, size_t size)
 Reallocates a buffer to a new size.
void mem_free (mem_ctx_t ctx, void *ptr)
 frees the given buffer
void mem_parse (mem_ctx_t ctx, mem_parse_callback_t callback, void *data)
 parse the heap
void mem_stat (mem_ctx_t ctx, mem_stat_t *stat)
 function used to get statistics (nb chunks, nb allocated chunks, total size) about the heap

Detailed Description

Dynamic memory allocation implementation.

Definition in file mem_alloc.c.

Macro Definition Documentation

◆ ALLOC_CHUNK_HEADER_SIZE

#define ALLOC_CHUNK_HEADER_SIZE   4

Definition at line 27 of file mem_alloc.c.

◆ FREE_CHUNK_HEADER_SIZE

#define FREE_CHUNK_HEADER_SIZE   8

Definition at line 29 of file mem_alloc.c.

◆ GET_IDX

#define GET_IDX ( _heap,
_ptr )
Value:
((((uint8_t *) (_ptr)) - ((uint8_t *) _heap)) >> 3)

Definition at line 56 of file mem_alloc.c.

◆ GET_PTR

#define GET_PTR ( _heap,
index )
Value:
((header_t *) (((uint8_t *) _heap) + ((index) << 3)))
struct header_s header_t

Definition at line 55 of file mem_alloc.c.

◆ GET_SEGMENT

#define GET_SEGMENT ( _size)
Value:
MAX(NB_LINEAR_SEGMENTS, (31 - __builtin_clz(size)))
#define NB_LINEAR_SEGMENTS
Definition mem_alloc.c:37
#define MAX(x, y)
Definition nbgl_types.h:134

Definition at line 58 of file mem_alloc.c.

◆ HEAP_HEADER_SIZE

#define HEAP_HEADER_SIZE    ((sizeof(heap_t) + PAYLOAD_DATA_ALIGNEMENT - 1) & ~(PAYLOAD_DATA_ALIGNEMENT - 1))

Definition at line 33 of file mem_alloc.c.

◆ LINKS_ADDR

#define LINKS_ADDR ( _ptr)
Value:
(((uint8_t *) (_ptr)) + ALLOC_CHUNK_HEADER_SIZE)
#define ALLOC_CHUNK_HEADER_SIZE
Definition mem_alloc.c:27

Definition at line 90 of file mem_alloc.c.

◆ LINKS_POISON

#define LINKS_POISON ( _ptr)
Value:
#define LINKS_SIZE
Definition mem_alloc.c:91
#define MEM_POISON(_addr, _size)
Definition mem_alloc.c:77
#define LINKS_ADDR(_ptr)
Definition mem_alloc.c:90

Definition at line 93 of file mem_alloc.c.

◆ LINKS_SIZE

#define LINKS_SIZE   (FREE_CHUNK_HEADER_SIZE - ALLOC_CHUNK_HEADER_SIZE)

Definition at line 91 of file mem_alloc.c.

◆ LINKS_UNPOISON

#define LINKS_UNPOISON ( _ptr)
Value:
#define MEM_UNPOISON(_addr, _size)
Definition mem_alloc.c:78

Definition at line 92 of file mem_alloc.c.

◆ MAX_BLOCK_SIZE

#define MAX_BLOCK_SIZE   0x7FFF

Definition at line 31 of file mem_alloc.c.

◆ MEM_POISON

#define MEM_POISON ( _addr,
_size )
Value:
((void) (_addr), (void) (_size))

Definition at line 77 of file mem_alloc.c.

◆ MEM_REDZONE_SIZE

#define MEM_REDZONE_SIZE   0

Definition at line 86 of file mem_alloc.c.

◆ MEM_UNPOISON

#define MEM_UNPOISON ( _addr,
_size )
Value:
((void) (_addr), (void) (_size))

Definition at line 78 of file mem_alloc.c.

◆ NB_LINEAR_SEGMENTS

#define NB_LINEAR_SEGMENTS   5

Definition at line 37 of file mem_alloc.c.

◆ NB_MAX_SEGMENTS

#define NB_MAX_SEGMENTS   10

Definition at line 50 of file mem_alloc.c.

◆ NB_SUB_SEGMENTS

#define NB_SUB_SEGMENTS   4

Definition at line 52 of file mem_alloc.c.

◆ PAYLOAD_ALIGNEMENT

#define PAYLOAD_ALIGNEMENT   4

Definition at line 23 of file mem_alloc.c.

◆ PAYLOAD_DATA_ALIGNEMENT

#define PAYLOAD_DATA_ALIGNEMENT   8

Definition at line 25 of file mem_alloc.c.

Typedef Documentation

◆ header_t

typedef struct header_s header_t

Function Documentation

◆ align_alloc_size()

size_t align_alloc_size ( size_t size)
inlinestatic

Definition at line 125 of file mem_alloc.c.

◆ coalesce()

header_t * coalesce ( heap_t * heap,
header_t * header,
header_t * neighbour )
static

Definition at line 306 of file mem_alloc.c.

◆ ensure_chunk_valid()

void ensure_chunk_valid ( heap_t * heap,
header_t * header )
inlinestatic

Definition at line 280 of file mem_alloc.c.

◆ list_push()

void list_push ( heap_t * heap,
header_t * header )
inlinestatic

Definition at line 254 of file mem_alloc.c.

◆ list_remove()

void list_remove ( heap_t * heap,
uint16_t * first_free,
uint16_t elem )
inlinestatic

Definition at line 227 of file mem_alloc.c.

◆ mem_alloc()

void * mem_alloc ( mem_ctx_t ctx,
size_t nb_bytes )

allocates a buffer of the given size, if possible

Note
this buffer should be free later with mem_free
Parameters
ctxallocator context (returned by mem_init())
nb_bytessize in bytes of the buffer to allocate (must be > 0)
Returns
either a valid address if successful, or NULL if failed (no more memory or invalid nb_bytes)

Definition at line 427 of file mem_alloc.c.

◆ mem_free()

void mem_free ( mem_ctx_t ctx,
void * ptr )

frees the given buffer

Parameters
ctxallocator context (returned by mem_init())
ptrbuffer previously allocated with mem_alloc

Definition at line 627 of file mem_alloc.c.

◆ mem_init()

mem_ctx_t mem_init ( void * heap_start,
size_t heap_size )

initializes the heap for this allocator

Parameters
heap_startaddress of the heap to use
heap_sizesize in bytes of the heap to use. It must be a multiple of 8, and at least 200 bytes but less than 32kBytes (max is exactly 32856 bytes)
Returns
the context to use for further calls, or NULL if failling

Definition at line 380 of file mem_alloc.c.

◆ mem_parse()

void mem_parse ( mem_ctx_t ctx,
mem_parse_callback_t callback,
void * data )

parse the heap

Iterates over blocks managed by mem_alloc and mem_free.

Parameters
ctxallocator context (returned by mem_init())
callbackcallback called for each block while parsing
datacontext data, passed to given callback

Definition at line 666 of file mem_alloc.c.

◆ mem_realloc()

void * mem_realloc ( mem_ctx_t ctx,
void * ptr,
size_t size )

Reallocates a buffer to a new size.

Note
The new size can be either smaller or bigger than the original one. The content is preserved up to the minimum of the old and new sizes. If ptr is NULL, this function behaves like mem_alloc. If size is 0, this function behaves like mem_free and returns NULL.
Parameters
ctxallocator context (returned by mem_init())
ptrbuffer previously allocated with mem_alloc
sizenew requested size in bytes
Returns
address of the reallocated buffer (may be the same or different), or NULL on failure

Definition at line 525 of file mem_alloc.c.

◆ mem_stat()

void mem_stat ( mem_ctx_t ctx,
mem_stat_t * stat )

function used to get statistics (nb chunks, nb allocated chunks, total size) about the heap

Statistics are computed from the heap used by mem_alloc and mem_free.

Parameters
ctxallocator context (returned by mem_init())
statoutput statistics structure

Definition at line 706 of file mem_alloc.c.

◆ parse_callback()

bool parse_callback ( void * data,
uint8_t * addr,
bool allocated,
size_t size )
static

Definition at line 346 of file mem_alloc.c.

◆ seglist_index()

int seglist_index ( heap_t * heap,
size_t size )
inlinestatic

Definition at line 142 of file mem_alloc.c.

◆ seglist_index_up()

int seglist_index_up ( heap_t * heap,
size_t size )
inlinestatic

Definition at line 169 of file mem_alloc.c.

◆ set_fnext()

void set_fnext ( header_t * header,
uint16_t fnext )
inlinestatic

Definition at line 219 of file mem_alloc.c.

◆ set_fprev()

void set_fprev ( header_t * header,
uint16_t fprev )
inlinestatic

Definition at line 212 of file mem_alloc.c.