Embedded SDK
Embedded SDK
Loading...
Searching...
No Matches
Classes | Macros | Typedefs | Functions
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 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)   ((header_t *) (((uint8_t *) _heap) + ((index) << 3)))
 
#define GET_IDX(_heap, _ptr)   ((((uint8_t *) (_ptr)) - ((uint8_t *) _heap)) >> 3)
 
#define GET_PREV(_heap, _ptr)   ((header_t *) (((uint8_t *) _heap) + ((_ptr)->fprev << 3)))
 
#define GET_NEXT(_heap, _ptr)   ((header_t *) (((uint8_t *) _heap) + ((_ptr)->fnext << 3)))
 

Typedefs

typedef struct header_s header_t
 

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_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 
)    ((((uint8_t *) (_ptr)) - ((uint8_t *) _heap)) >> 3)

Definition at line 54 of file mem_alloc.c.

◆ GET_NEXT

#define GET_NEXT (   _heap,
  _ptr 
)    ((header_t *) (((uint8_t *) _heap) + ((_ptr)->fnext << 3)))

Definition at line 56 of file mem_alloc.c.

◆ GET_PREV

#define GET_PREV (   _heap,
  _ptr 
)    ((header_t *) (((uint8_t *) _heap) + ((_ptr)->fprev << 3)))

Definition at line 55 of file mem_alloc.c.

◆ GET_PTR

#define GET_PTR (   _heap,
  index 
)    ((header_t *) (((uint8_t *) _heap) + ((index) << 3)))

Definition at line 53 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 31 of file mem_alloc.c.

◆ NB_LINEAR_SEGMENTS

#define NB_LINEAR_SEGMENTS   5

Definition at line 35 of file mem_alloc.c.

◆ NB_MAX_SEGMENTS

#define NB_MAX_SEGMENTS   10

Definition at line 48 of file mem_alloc.c.

◆ NB_SUB_SEGMENTS

#define NB_SUB_SEGMENTS   4

Definition at line 50 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

◆ 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 320 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 406 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 283 of file mem_alloc.c.

◆ mem_parse()

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

parse the heap

Parameters
ctxallocator context (returned by mem_init())
ptrbuffer previously allocated with mem_alloc
datacontext data, passed to given callback

Definition at line 439 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

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

Definition at line 477 of file mem_alloc.c.