LPC2xxx: add timer support.
[bertos.git] / bertos / struct / heap.c
index b4380a4da499a53e4c6cc581e755667df8867279..33c6de2acadca599ee7057a4dc6710df16a4fba8 100644 (file)
  *
  * \brief Heap subsystem (public interface).
  *
- * \version $Id: heap.c 1532 2008-08-04 07:21:26Z bernie $
+ * \version $Id$
  * \author Bernie Innocenti <bernie@codewiz.org>
  */
 
 #include "heap.h"
 
-#include <cfg/macros.h>           // IS_POW2()
-#include <cfg/debug.h>            // ASSERT()
-
-#include <string.h>           // 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 <cfg/debug.h> // ASSERT()
+#include <string.h>    // 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(((size_t)memory % alignof(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;
@@ -73,7 +71,7 @@ void *heap_allocmem(struct Heap* h, size_t size)
        MemChunk *chunk, *prev;
 
        /* Round size up to the allocation granularity */
-       size = ROUND2(size, sizeof(MemChunk));
+       size = ROUND_UP2(size, sizeof(MemChunk));
 
        /* Handle allocations of 0 bytes */
        if (!size)
@@ -123,7 +121,7 @@ void heap_freemem(struct Heap* h, void *mem, size_t size)
 #endif
 
        /* Round size up to the allocation granularity */
-       size = ROUND2(size, sizeof(MemChunk));
+       size = ROUND_UP2(size, sizeof(MemChunk));
 
        /* Handle allocations of 0 bytes */
        if (!size)