X-Git-Url: https://codewiz.org/gitweb?a=blobdiff_plain;ds=sidebyside;f=bertos%2Fstruct%2Fheap.h;fp=bertos%2Fstruct%2Fheap.h;h=7bddcba20ec01b75a6f0aabb4b0a9938c5c50748;hb=c81265e6459403444c859758db53a9675c3a4ec7;hp=7044f33134cf3a40078ed8173bea9bf4c950be08;hpb=a9d1483df4bd7d32fac7ff6207d9d938012d2989;p=bertos.git diff --git a/bertos/struct/heap.h b/bertos/struct/heap.h index 7044f331..7bddcba2 100644 --- a/bertos/struct/heap.h +++ b/bertos/struct/heap.h @@ -45,15 +45,33 @@ #include "cfg/cfg_heap.h" #include +#include // IS_POW2() -struct _MemChunk; +/* 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 -struct 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);