X-Git-Url: https://codewiz.org/gitweb?a=blobdiff_plain;f=bertos%2Fstruct%2Fheap.h;h=efd02c284ad5a7ffd9544cc8315bf37b521399c4;hb=f884c67ed85598875ef683987323fd6085e01e14;hp=6819d6793a2ac4dffb481aaef5f00203cfe0d49b;hpb=e5174304054e26cd8f3cd1f9980871c20c07fc46;p=bertos.git diff --git a/bertos/struct/heap.h b/bertos/struct/heap.h index 6819d679..efd02c28 100644 --- a/bertos/struct/heap.h +++ b/bertos/struct/heap.h @@ -36,24 +36,45 @@ * in this form also within the implementation. This would probably remove * memory alignment problems, and also some aliasing issues. * - * \version $Id: heap.h 1532 2008-08-04 07:21:26Z bernie $ + * \version $Id$ * \author Bernie Innocenti + * + * $WIZ$ module_name = "heap" + * $WIZ$ module_configuration = "bertos/cfg/cfg_heap.h" */ -#ifndef MWARE_HEAP_H -#define MWARE_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))); -struct _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); @@ -64,6 +85,7 @@ 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)) @@ -86,4 +108,8 @@ void heap_free(struct Heap* heap, void * mem); #endif -#endif /* MWARE_HEAP_H */ +int heap_testSetup(void); +int heap_testRun(void); +int heap_testTearDown(void); + +#endif /* STRUCT_HEAP_H */