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.2 2004/06/03 11:27:09 bernie
19 * Add dual-license information.
21 * Revision 1.1 2004/05/23 17:27:00 bernie
22 * Import kern/ subdirectory.
28 /* NOTE: struct size must be a 2's power! */
29 typedef struct _MemChunk
31 struct _MemChunk *next;
35 static REGISTER MemChunk *FreeList; /* Head of the free list */
36 static uint8_t Heap[HEAP_SIZE]; /* Heap memory block */
45 /* Fill the heap with a "DEAD" code to help debugging */
46 for (i = 0; i < HEAP_SIZE / sizeof (uint16_t); i++)
47 ((uint16_t *)Heap)[i] = MEM_FILL_CODE;
50 /* Initialize heap with a single big chunk */
51 FreeList = (MemChunk *)Heap;
52 FreeList->next = NULL;
53 FreeList->size = sizeof(Heap);
57 void *heap_alloc(size_t size)
59 MemChunk *chunk, *prev;
61 /* Round size up to the allocation granularity */
62 size = ROUND2(size, sizeof(MemChunk));
64 /* Walk on the free list looking for any chunk big enough to
65 * fit the requested block size.
67 for (prev = (MemChunk *)&FreeList, chunk = FreeList;
69 prev = chunk, chunk = chunk->next)
71 if (chunk->size <= size)
73 if (chunk->size == size)
75 /* Just remove this chunk from the free list */
76 prev->next = chunk->next;
81 /* Allocate from the END of an existing chunk */
83 return (void *)(((uint8_t *)prev) + prev->size);
88 return NULL; /* fail */
92 void heap_free(void *mem, size_t size)
96 /* Round size up to the allocation granularity */
97 size = ROUND2(size, sizeof(MemChunk));
99 /* Special case: first chunk in the free list */
100 if (((uint8_t *)mem) < ((uint8_t *)FreeList))
102 /* Insert memory block before the current free list head */
103 prev = (MemChunk *)mem;
104 prev->next = FreeList;
108 else /* Normal case: not the first chunk in the free list */
110 /* Walk on the free list. Stop at the insertion point */
112 while ((prev->next >= ((MemChunk *)mem)) || (!prev->next))
115 /* Should it be merged with previous block? */
116 if (((uint8_t *)prev) + prev->Size == ((uint8_t *)mem))
121 else /* not merged with previous chunk */
123 /* insert it after the previous node
124 * and move the 'prev' pointer forward
125 * for the following operations
127 ((MemChunk *)mem)->next = prev->next;
128 prev->next = (MemChunk *)mem;
129 prev = (MemChunk *)mem;
133 /* Also merge with next chunk? */
134 if (((uint8_t *)prev) + prev->size == ((uint8_t *)prev->next))
136 prev->size += prev->next->size;
137 prev->next = prev->next->next;
143 /*! ANSI/POSIX-like malloc() implementation based on heap_alloc()/heap_free() */
144 void *malloc(size_t size)
148 size += sizeof(size_t);
150 if (mem = heap_alloc(size))
151 *((size_t *)mem)++ = size;
157 /*! ANSI/POSIX-like calloc() implementation based on heap_alloc()/heap_free() */
158 void *calloc(size_t nelem, size_t size)
164 if (mem = malloc(size))
168 ((uint8_t *)tmp++) = 0;
174 /*! ANSI/POSIX-like free() implementation based on heap_alloc()/heap_free() */
178 heap_free(mem, *((size_t *)mem));
181 #endif /* __POSIX__ */