#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((((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;
#include "cfg/cfg_heap.h"
#include <cfg/compiler.h>
+#include <cfg/macros.h> // 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);