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

Dynamic memory allocation API. More...

#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
Include dependency graph for mem_alloc.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  mem_stat_t
 this structure, filled by mem_stat More...

Typedefs

typedef void * mem_ctx_t
 type shared externally
typedef bool(* mem_parse_callback_t) (void *data, uint8_t *addr, bool allocated, size_t size)
 function called for each chunk found in the heap.

Functions

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 *dat)
 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 API.

Definition in file mem_alloc.h.

Typedef Documentation

◆ mem_ctx_t

typedef void* mem_ctx_t

type shared externally

Definition at line 27 of file mem_alloc.h.

◆ mem_parse_callback_t

typedef bool(* mem_parse_callback_t) (void *data, uint8_t *addr, bool allocated, size_t size)

function called for each chunk found in the heap.

Parameters
datacontext data, passed as argument of mem_parse
addraddress of the chunk (as returned by mem_alloc) if allocated
allocatedif true, means the chunk is allocated
sizesize of the chunk, in bytes (including header & alignment bytes)
Returns
shall return true to break parsing

Definition at line 40 of file mem_alloc.h.

Function Documentation

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