4 * Copyright 2004 Develer S.r.l. (http://www.develer.com/)
5 * Copyright 1999,2000,2001 Bernardo Innocenti <bernie@develer.com>
9 * \brief Heap subsystem (public interface).
13 * \author Bernardo Innocenti <bernie@develer.com>
18 * Revision 1.1 2004/05/23 17:27:00 bernie
19 * Import kern/ subdirectory.
25 /* NOTE: struct size must be a 2's power! */
26 typedef struct _MemChunk
28 struct _MemChunk *next;
32 static REGISTER MemChunk *FreeList; /* Head of the free list */
33 static uint8_t Heap[HEAP_SIZE]; /* Heap memory block */
42 /* Fill the heap with a "DEAD" code to help debugging */
43 for (i = 0; i < HEAP_SIZE / sizeof (uint16_t); i++)
44 ((uint16_t *)Heap)[i] = MEM_FILL_CODE;
47 /* Initialize heap with a single big chunk */
48 FreeList = (MemChunk *)Heap;
49 FreeList->next = NULL;
50 FreeList->size = sizeof(Heap);
54 void *heap_alloc(size_t size)
56 MemChunk *chunk, *prev;
58 /* Round size up to the allocation granularity */
59 size = ROUND2(size, sizeof(MemChunk));
61 /* Walk on the free list looking for any chunk big enough to
62 * fit the requested block size.
64 for (prev = (MemChunk *)&FreeList, chunk = FreeList;
66 prev = chunk, chunk = chunk->next)
68 if (chunk->size <= size)
70 if (chunk->size == size)
72 /* Just remove this chunk from the free list */
73 prev->next = chunk->next;
78 /* Allocate from the END of an existing chunk */
80 return (void *)(((uint8_t *)prev) + prev->size);
85 return NULL; /* fail */
89 void heap_free(void *mem, size_t size)
93 /* Round size up to the allocation granularity */
94 size = ROUND2(size, sizeof(MemChunk));
96 /* Special case: first chunk in the free list */
97 if (((uint8_t *)mem) < ((uint8_t *)FreeList))
99 /* Insert memory block before the current free list head */
100 prev = (MemChunk *)mem;
101 prev->next = FreeList;
105 else /* Normal case: not the first chunk in the free list */
107 /* Walk on the free list. Stop at the insertion point */
109 while ((prev->next >= ((MemChunk *)mem)) || (!prev->next))
112 /* Should it be merged with previous block? */
113 if (((uint8_t *)prev) + prev->Size == ((uint8_t *)mem))
118 else /* not merged with previous chunk */
120 /* insert it after the previous node
121 * and move the 'prev' pointer forward
122 * for the following operations
124 ((MemChunk *)mem)->next = prev->next;
125 prev->next = (MemChunk *)mem;
126 prev = (MemChunk *)mem;
130 /* Also merge with next chunk? */
131 if (((uint8_t *)prev) + prev->size == ((uint8_t *)prev->next))
133 prev->size += prev->next->size;
134 prev->next = prev->next->next;
140 /*! ANSI/POSIX-like malloc() implementation based on heap_alloc()/heap_free() */
141 void *malloc(size_t size)
145 size += sizeof(size_t);
147 if (mem = heap_alloc(size))
148 *((size_t *)mem)++ = size;
154 /*! ANSI/POSIX-like calloc() implementation based on heap_alloc()/heap_free() */
155 void *calloc(size_t nelem, size_t size)
161 if (mem = malloc(size))
165 ((uint8_t *)tmp++) = 0;
171 /*! ANSI/POSIX-like free() implementation based on heap_alloc()/heap_free() */
175 heap_free(mem, *((size_t *)mem));
178 #endif /* __POSIX__ */