4 * Copyright 2004 Develer S.r.l. (http://www.develer.com/)
5 * Copyright 1999,2000,2001 Bernardo Innocenti <bernie@develer.com>
6 * This file is part of DevLib - See devlib/README for information.
9 * \brief Heap subsystem (public interface).
13 * \author Bernardo Innocenti <bernie@develer.com>
18 *#* Revision 1.6 2004/10/26 09:02:13 bernie
19 *#* heap_free(): Handle NULL pointers like free(), write documentation.
21 *#* Revision 1.5 2004/10/03 20:43:22 bernie
22 *#* Import changes from sc/firmware.
24 *#* Revision 1.1 2004/07/31 16:33:58 rasky
25 *#* Spostato lo heap da kern/ a mware/
27 *#* Revision 1.2 2004/06/03 11:27:09 bernie
28 *#* Add dual-license information.
30 *#* Revision 1.1 2004/05/23 17:27:00 bernie
31 *#* Import kern/ subdirectory.
36 #include <string.h> // memset()
37 #include <macros.h> // IS_POW2()
38 #include <debug.h> // ASSERT()
40 /* NOTE: struct size must be a 2's power! */
41 typedef struct _MemChunk
43 struct _MemChunk *next;
47 STATIC_ASSERT(IS_POW2(sizeof(MemChunk)));
49 #define FREE_FILL_CODE 0xDEAD
50 #define ALLOC_FILL_CODE 0xBEEF
52 void heap_init(struct Heap* h, void* memory, size_t size)
55 memset(memory, FREE_FILL_CODE, size);
58 /* Initialize heap with a single big chunk */
59 h->FreeList = (MemChunk *)memory;
60 h->FreeList->next = NULL;
61 h->FreeList->size = size;
65 void *heap_allocmem(struct Heap* h, size_t size)
67 MemChunk *chunk, *prev;
69 /* Round size up to the allocation granularity */
70 size = ROUND2(size, sizeof(MemChunk));
72 /* Handle allocations of 0 bytes */
74 size = sizeof(MemChunk);
76 /* Walk on the free list looking for any chunk big enough to
77 * fit the requested block size.
79 for (prev = (MemChunk *)&h->FreeList, chunk = h->FreeList;
81 prev = chunk, chunk = chunk->next)
83 if (chunk->size >= size)
85 if (chunk->size == size)
87 /* Just remove this chunk from the free list */
88 prev->next = chunk->next;
90 memset(chunk, ALLOC_FILL_CODE, size);
96 /* Allocate from the END of an existing chunk */
99 memset((uint8_t *)chunk + chunk->size, ALLOC_FILL_CODE, size);
101 return (void *)((uint8_t *)chunk + chunk->size);
106 return NULL; /* fail */
110 void heap_freemem(struct Heap* h, void *mem, size_t size)
117 memset(mem, FREE_FILL_CODE, size);
120 /* Round size up to the allocation granularity */
121 size = ROUND2(size, sizeof(MemChunk));
123 /* Handle allocations of 0 bytes */
125 size = sizeof(MemChunk);
127 /* Special case: first chunk in the free list */
128 ASSERT((uint8_t*)mem != (uint8_t*)h->FreeList);
129 if (((uint8_t *)mem) < ((uint8_t *)h->FreeList))
131 /* Insert memory block before the current free list head */
132 prev = (MemChunk *)mem;
133 prev->next = h->FreeList;
137 else /* Normal case: not the first chunk in the free list */
140 * Walk on the free list. Stop at the insertion point (when mem
141 * is between prev and prev->next)
144 while (prev->next < (MemChunk *)mem && prev->next)
147 /* Make sure mem is not *within* prev */
148 ASSERT((uint8_t*)mem >= (uint8_t*)prev + prev->size);
150 /* Should it be merged with previous block? */
151 if (((uint8_t *)prev) + prev->size == ((uint8_t *)mem))
156 else /* not merged with previous chunk */
158 MemChunk *curr = (MemChunk*)mem;
160 /* insert it after the previous node
161 * and move the 'prev' pointer forward
162 * for the following operations
164 curr->next = prev->next;
168 /* Adjust for the following test */
173 /* Also merge with next chunk? */
174 if (((uint8_t *)prev) + prev->size == ((uint8_t *)prev->next))
176 prev->size += prev->next->size;
177 prev->next = prev->next->next;
179 /* There should be only one merge opportunity, becuase we always merge on free */
180 ASSERT((uint8_t*)prev + prev->size != (uint8_t*)prev->next);
184 #if CONFIG_HEAP_MALLOC
186 void *heap_malloc(struct Heap* h, size_t size)
190 size += sizeof(size_t);
191 if ((mem = (size_t*)heap_allocmem(h, size)))
197 void *heap_calloc(struct Heap* h, size_t size)
201 if ((mem = heap_malloc(h, size)))
202 memset(mem, 0, size);
208 * Free a block of memory, determining its size automatically.
210 * \param h Heap from which the block was allocated.
211 * \param mem Pointer to a block of memory previously allocated with
212 * either heap_malloc() or heap_calloc().
214 * \note If \a mem is a NULL pointer, no operation is performed.
216 * \note Freeing the same memory block twice has undefined behavior.
218 * \note This function works like the ANSI C free().
220 void heap_free(struct Heap *h, void *mem)
222 size_t *_mem = (size_t *)mem;
227 heap_freemem(h, _mem, *_mem);
231 #endif /* CONFIG_HEAP_MALLOC */