/** * \file * * * \defgroup heap Embedded optimized memory allocator * \ingroup core * \{ * * \brief Heap subsystem (public interface). * * \todo Heap memory could be defined as an array of MemChunk, and used * in this form also within the implementation. This would probably remove * memory alignment problems, and also some aliasing issues. * * \author Bernie Innocenti * * $WIZ$ module_name = "heap" * $WIZ$ module_configuration = "bertos/cfg/cfg_heap.h" */ #ifndef STRUCT_HEAP_H #define STRUCT_HEAP_H #include "cfg/cfg_heap.h" #include #include // IS_POW2() /* NOTE: struct size must be a 2's power! */ typedef struct _MemChunk { struct _MemChunk *next; size_t size; } MemChunk; STATIC_ASSERT(IS_POW2(sizeof(MemChunk))); typedef MemChunk heap_buf_t; /// A heap typedef struct Heap { struct _MemChunk *FreeList; ///< Head of the free list } Heap; /** * Utility macro to allocate a heap of size \a size. * * \param name Variable name for the heap. * \param size Heap size in bytes. */ #define HEAP_DEFINE_BUF(name, size) \ heap_buf_t name[((size) + sizeof(heap_buf_t) - 1) / sizeof(heap_buf_t)] /// Initialize \a heap within the buffer pointed by \a memory which is of \a size bytes void heap_init(struct Heap* heap, void* memory, size_t size); /// Allocate a chunk of memory of \a size bytes from the heap void *heap_allocmem(struct Heap* heap, size_t size); /// Free a chunk of memory of \a size bytes from the heap void heap_freemem(struct Heap* heap, void *mem, size_t size); size_t heap_freeSpace(struct Heap *h); #define HNEW(heap, type) \ (type*)heap_allocmem(heap, sizeof(type)) #define HNEWVEC(heap, type, nelem) \ (type*)heap_allocmem(heap, sizeof(type) * (nelem)) #define HDELETE(heap, type, mem) \ heap_freemem(heap, mem, sizeof(type)) #define HDELETEVEC(heap, type, nelem, mem) \ heap_freemem(heap, mem, sizeof(type) * (nelem)) #if CONFIG_HEAP_MALLOC /** * \name Compatibility interface with C standard library * \{ */ void *heap_malloc(struct Heap* heap, size_t size); void *heap_calloc(struct Heap* heap, size_t size); void heap_free(struct Heap* heap, void * mem); /** \} */ #endif /** \} */ //defgroup heap int heap_testSetup(void); int heap_testRun(void); int heap_testTearDown(void); #endif /* STRUCT_HEAP_H */