1 #ifndef STRUCT_HEAP_H 2 #define STRUCT_HEAP_H 3 4 //#include <stdio.h> 5 #include <stdint.h> 6 #include <osdep_service.h> 7 8 /* NOTE: struct size must be a 2's power! */ 9 typedef struct _MemChunk 10 { 11 struct _MemChunk *next; 12 int size; 13 } MemChunk; 14 15 typedef MemChunk heap_buf_t; 16 17 /// A heap 18 typedef struct Heap 19 { 20 struct _MemChunk *FreeList; ///< Head of the free list 21 } Heap; 22 23 /** 24 * Utility macro to allocate a heap of size \a size. 25 * 26 * \param name Variable name for the heap. 27 * \param size Heap size in bytes. 28 */ 29 #define HEAP_DEFINE_BUF(name, size) \ 30 heap_buf_t name[((size) + sizeof(heap_buf_t) - 1) / sizeof(heap_buf_t)] 31 32 /// Initialize \a heap within the buffer pointed by \a memory which is of \a size bytes 33 void tcm_heap_init(void); 34 35 /// Allocate a chunk of memory of \a size bytes from the heap 36 void *tcm_heap_allocmem(int size); 37 38 /// Free a chunk of memory of \a size bytes from the heap 39 void tcm_heap_freemem(void *mem, int size); 40 41 int tcm_heap_freeSpace(void); 42 43 #define HNEW(heap, type) \ 44 (type*)tcm_heap_allocmem(heap, sizeof(type)) 45 46 #define HNEWVEC(heap, type, nelem) \ 47 (type*)tcm_heap_allocmem(heap, sizeof(type) * (nelem)) 48 49 #define HDELETE(heap, type, mem) \ 50 tcm_heap_freemem(heap, mem, sizeof(type)) 51 52 #define HDELETEVEC(heap, type, nelem, mem) \ 53 tcm_heap_freemem(heap, mem, sizeof(type) * (nelem)) 54 55 56 /** 57 * \name Compatibility interface with C standard library 58 * \{ 59 */ 60 void *tcm_heap_malloc(int size); 61 void *tcm_heap_calloc(int size); 62 void tcm_heap_free(void * mem); 63 /** \} */ 64 65 66 #endif /* STRUCT_HEAP_H */ 67