From: batt Date: Wed, 23 Dec 2009 16:49:42 +0000 (+0000) Subject: Check for alignment of heap memory buffer; add utility macro used to declare a heap. X-Git-Tag: 2.4.0~70 X-Git-Url: https://codewiz.org/gitweb?a=commitdiff_plain;h=c81265e6459403444c859758db53a9675c3a4ec7;p=bertos.git Check for alignment of heap memory buffer; add utility macro used to declare a heap. git-svn-id: https://src.develer.com/svnoss/bertos/trunk@3127 38d2e660-2303-0410-9eaa-f027e97ec537 --- diff --git a/bertos/struct/heap.c b/bertos/struct/heap.c index dc8ceaab..27f80325 100644 --- a/bertos/struct/heap.c +++ b/bertos/struct/heap.c @@ -38,28 +38,26 @@ #include "heap.h" -#include // IS_POW2() -#include // ASSERT() - -#include // memset() - -/* 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))); +#include // ASSERT() +#include // memset() #define FREE_FILL_CODE 0xDEAD #define ALLOC_FILL_CODE 0xBEEF + +/* + * This function prototype is deprecated, will change in: + * void heap_init(struct Heap* h, heap_buf_t* memory, size_t size) + * in the nex BeRTOS release. + */ void heap_init(struct Heap* h, void* memory, size_t size) { -#ifdef _DEBUG + #ifdef _DEBUG memset(memory, FREE_FILL_CODE, size); -#endif + #endif + + ASSERT2((((uintptr_t)memory % sizeof(heap_buf_t)) == 0), + "memory buffer is unaligned, please use the HEAP_DEFINE_BUF() macro to declare heap buffers!\n"); /* Initialize heap with a single big chunk */ h->FreeList = (MemChunk *)memory; 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);