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.7 2005/04/11 19:10:28 bernie
19 *#* Include top-level headers from cfg/ subdir.
21 *#* Revision 1.6 2004/10/26 09:02:13 bernie
22 *#* heap_free(): Handle NULL pointers like free(), write documentation.
24 *#* Revision 1.5 2004/10/03 20:43:22 bernie
25 *#* Import changes from sc/firmware.
27 *#* Revision 1.1 2004/07/31 16:33:58 rasky
28 *#* Spostato lo heap da kern/ a mware/
30 *#* Revision 1.2 2004/06/03 11:27:09 bernie
31 *#* Add dual-license information.
33 *#* Revision 1.1 2004/05/23 17:27:00 bernie
34 *#* Import kern/ subdirectory.
39 #include <string.h> // memset()
40 #include <cfg/macros.h> // IS_POW2()
41 #include <cfg/debug.h> // ASSERT()
43 /* NOTE: struct size must be a 2's power! */
44 typedef struct _MemChunk
46 struct _MemChunk *next;
50 STATIC_ASSERT(IS_POW2(sizeof(MemChunk)));
52 #define FREE_FILL_CODE 0xDEAD
53 #define ALLOC_FILL_CODE 0xBEEF
55 void heap_init(struct Heap* h, void* memory, size_t size)
58 memset(memory, FREE_FILL_CODE, size);
61 /* Initialize heap with a single big chunk */
62 h->FreeList = (MemChunk *)memory;
63 h->FreeList->next = NULL;
64 h->FreeList->size = size;
68 void *heap_allocmem(struct Heap* h, size_t size)
70 MemChunk *chunk, *prev;
72 /* Round size up to the allocation granularity */
73 size = ROUND2(size, sizeof(MemChunk));
75 /* Handle allocations of 0 bytes */
77 size = sizeof(MemChunk);
79 /* Walk on the free list looking for any chunk big enough to
80 * fit the requested block size.
82 for (prev = (MemChunk *)&h->FreeList, chunk = h->FreeList;
84 prev = chunk, chunk = chunk->next)
86 if (chunk->size >= size)
88 if (chunk->size == size)
90 /* Just remove this chunk from the free list */
91 prev->next = chunk->next;
93 memset(chunk, ALLOC_FILL_CODE, size);
99 /* Allocate from the END of an existing chunk */
102 memset((uint8_t *)chunk + chunk->size, ALLOC_FILL_CODE, size);
104 return (void *)((uint8_t *)chunk + chunk->size);
109 return NULL; /* fail */
113 void heap_freemem(struct Heap* h, void *mem, size_t size)
120 memset(mem, FREE_FILL_CODE, size);
123 /* Round size up to the allocation granularity */
124 size = ROUND2(size, sizeof(MemChunk));
126 /* Handle allocations of 0 bytes */
128 size = sizeof(MemChunk);
130 /* Special case: first chunk in the free list */
131 ASSERT((uint8_t*)mem != (uint8_t*)h->FreeList);
132 if (((uint8_t *)mem) < ((uint8_t *)h->FreeList))
134 /* Insert memory block before the current free list head */
135 prev = (MemChunk *)mem;
136 prev->next = h->FreeList;
140 else /* Normal case: not the first chunk in the free list */
143 * Walk on the free list. Stop at the insertion point (when mem
144 * is between prev and prev->next)
147 while (prev->next < (MemChunk *)mem && prev->next)
150 /* Make sure mem is not *within* prev */
151 ASSERT((uint8_t*)mem >= (uint8_t*)prev + prev->size);
153 /* Should it be merged with previous block? */
154 if (((uint8_t *)prev) + prev->size == ((uint8_t *)mem))
159 else /* not merged with previous chunk */
161 MemChunk *curr = (MemChunk*)mem;
163 /* insert it after the previous node
164 * and move the 'prev' pointer forward
165 * for the following operations
167 curr->next = prev->next;
171 /* Adjust for the following test */
176 /* Also merge with next chunk? */
177 if (((uint8_t *)prev) + prev->size == ((uint8_t *)prev->next))
179 prev->size += prev->next->size;
180 prev->next = prev->next->next;
182 /* There should be only one merge opportunity, becuase we always merge on free */
183 ASSERT((uint8_t*)prev + prev->size != (uint8_t*)prev->next);
187 #if CONFIG_HEAP_MALLOC
189 void *heap_malloc(struct Heap* h, size_t size)
193 size += sizeof(size_t);
194 if ((mem = (size_t*)heap_allocmem(h, size)))
200 void *heap_calloc(struct Heap* h, size_t size)
204 if ((mem = heap_malloc(h, size)))
205 memset(mem, 0, size);
211 * Free a block of memory, determining its size automatically.
213 * \param h Heap from which the block was allocated.
214 * \param mem Pointer to a block of memory previously allocated with
215 * either heap_malloc() or heap_calloc().
217 * \note If \a mem is a NULL pointer, no operation is performed.
219 * \note Freeing the same memory block twice has undefined behavior.
221 * \note This function works like the ANSI C free().
223 void heap_free(struct Heap *h, void *mem)
225 size_t *_mem = (size_t *)mem;
230 heap_freemem(h, _mem, *_mem);
234 #endif /* CONFIG_HEAP_MALLOC */