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.5 2004/10/03 20:43:22 bernie
19 *#* Import changes from sc/firmware.
21 *#* Revision 1.1 2004/07/31 16:33:58 rasky
22 *#* Spostato lo heap da kern/ a mware/
24 *#* Revision 1.2 2004/06/03 11:27:09 bernie
25 *#* Add dual-license information.
27 *#* Revision 1.1 2004/05/23 17:27:00 bernie
28 *#* Import kern/ subdirectory.
33 #include <string.h> // memset()
34 #include <macros.h> // IS_POW2()
35 #include <debug.h> // ASSERT()
37 /* NOTE: struct size must be a 2's power! */
38 typedef struct _MemChunk
40 struct _MemChunk *next;
44 STATIC_ASSERT(IS_POW2(sizeof(MemChunk)));
46 #define FREE_FILL_CODE 0xDEAD
47 #define ALLOC_FILL_CODE 0xBEEF
49 void heap_init(struct Heap* h, void* memory, size_t size)
52 memset(memory, FREE_FILL_CODE, size);
55 /* Initialize heap with a single big chunk */
56 h->FreeList = (MemChunk *)memory;
57 h->FreeList->next = NULL;
58 h->FreeList->size = size;
62 void *heap_allocmem(struct Heap* h, size_t size)
64 MemChunk *chunk, *prev;
66 /* Round size up to the allocation granularity */
67 size = ROUND2(size, sizeof(MemChunk));
69 /* Handle allocations of 0 bytes */
71 size = sizeof(MemChunk);
73 /* Walk on the free list looking for any chunk big enough to
74 * fit the requested block size.
76 for (prev = (MemChunk *)&h->FreeList, chunk = h->FreeList;
78 prev = chunk, chunk = chunk->next)
80 if (chunk->size >= size)
82 if (chunk->size == size)
84 /* Just remove this chunk from the free list */
85 prev->next = chunk->next;
87 memset(chunk, ALLOC_FILL_CODE, size);
93 /* Allocate from the END of an existing chunk */
96 memset((uint8_t *)chunk + chunk->size, ALLOC_FILL_CODE, size);
98 return (void *)((uint8_t *)chunk + chunk->size);
103 return NULL; /* fail */
107 void heap_freemem(struct Heap* h, void *mem, size_t size)
114 memset(mem, FREE_FILL_CODE, size);
117 /* Round size up to the allocation granularity */
118 size = ROUND2(size, sizeof(MemChunk));
120 /* Handle allocations of 0 bytes */
122 size = sizeof(MemChunk);
124 /* Special case: first chunk in the free list */
125 ASSERT((uint8_t*)mem != (uint8_t*)h->FreeList);
126 if (((uint8_t *)mem) < ((uint8_t *)h->FreeList))
128 /* Insert memory block before the current free list head */
129 prev = (MemChunk *)mem;
130 prev->next = h->FreeList;
134 else /* Normal case: not the first chunk in the free list */
137 * Walk on the free list. Stop at the insertion point (when mem
138 * is between prev and prev->next)
141 while (prev->next < (MemChunk *)mem && prev->next)
144 /* Make sure mem is not *within* prev */
145 ASSERT((uint8_t*)mem >= (uint8_t*)prev + prev->size);
147 /* Should it be merged with previous block? */
148 if (((uint8_t *)prev) + prev->size == ((uint8_t *)mem))
153 else /* not merged with previous chunk */
155 MemChunk *curr = (MemChunk*)mem;
157 /* insert it after the previous node
158 * and move the 'prev' pointer forward
159 * for the following operations
161 curr->next = prev->next;
165 /* Adjust for the following test */
170 /* Also merge with next chunk? */
171 if (((uint8_t *)prev) + prev->size == ((uint8_t *)prev->next))
173 prev->size += prev->next->size;
174 prev->next = prev->next->next;
176 /* There should be only one merge opportunity, becuase we always merge on free */
177 ASSERT((uint8_t*)prev + prev->size != (uint8_t*)prev->next);
181 #if CONFIG_HEAP_MALLOC
183 void *heap_malloc(struct Heap* h, size_t size)
187 size += sizeof(size_t);
188 if ((mem = (size_t*)heap_allocmem(h, size)))
194 void *heap_calloc(struct Heap* h, size_t size)
198 if ((mem = heap_malloc(h, size)))
199 memset(mem, 0, size);
204 void heap_free(struct Heap* h, void *mem_)
206 size_t* mem = (size_t*)mem_;
208 heap_freemem(h, mem, *mem);
211 #endif /* CONFIG_HEAP_MALLOC */