3 heap.c - Heap Manager
\r
5 #include <acess/sys.h>
\r
11 # define DEBUGS(s...) _SysDebug(s)
\r
13 # define DEBUGS(s...) do{}while(0)
\r
16 // === Constants ===
\r
17 #define MAGIC 0xACE55051 //AcessOS1
\r
18 #define MAGIC_FREE (~MAGIC)
\r
19 #define BLOCK_SIZE 16 //Minimum
\r
20 #define HEAP_INIT_SIZE 0x10000
\r
22 typedef unsigned int Uint;
\r
35 // === LOCAL VARIABLES ===
\r
36 static void *_heap_start = NULL;
\r
37 static void *_heap_end = NULL;
\r
39 // === PROTOTYPES ===
\r
40 EXPORT void *malloc(size_t bytes);
\r
41 EXPORT void *calloc(size_t bytes, size_t count);
\r
42 EXPORT void free(void *mem);
\r
43 EXPORT void *realloc(void *mem, size_t bytes);
\r
44 EXPORT void *sbrk(int increment);
\r
45 LOCAL void *extendHeap(int bytes);
\r
46 static void *FindHeapBase();
\r
47 LOCAL uint brk(uintptr_t newpos);
\r
52 \fn EXPORT void *malloc(size_t bytes)
\r
53 \brief Allocates memory from the heap space
\r
54 \param bytes Integer - Size of buffer to return
\r
55 \return Pointer to buffer
\r
57 EXPORT void *malloc(size_t bytes)
\r
60 size_t closestMatch = 0;
\r
61 void *bestMatchAddr = 0;
\r
62 heap_head *curBlock;
\r
64 // _SysDebug("&_heap_start = %p, _heap_start = %p", &_heap_start, _heap_start);
\r
66 if(_heap_start == NULL)
\r
68 _heap_start = sbrk(0);
\r
69 _heap_end = _heap_start;
\r
70 extendHeap(HEAP_INIT_SIZE);
\r
73 curBlock = _heap_start;
\r
74 // _SysDebug("_heap_start = %p", _heap_start);
\r
76 bestSize = bytes + sizeof(heap_head) + sizeof(heap_foot) + BLOCK_SIZE - 1;
\r
77 bestSize = (bestSize/BLOCK_SIZE)*BLOCK_SIZE; //Round up to block size
\r
79 while( (uintptr_t)curBlock < (uintptr_t)_heap_end)
\r
81 //_SysDebug(" malloc: curBlock = 0x%x, curBlock->magic = 0x%x\n", curBlock, curBlock->magic);
\r
82 if(curBlock->magic == MAGIC_FREE)
\r
84 if(curBlock->size == bestSize)
\r
86 if(bestSize < curBlock->size && (curBlock->size < closestMatch || closestMatch == 0)) {
\r
87 closestMatch = curBlock->size;
\r
88 bestMatchAddr = curBlock;
\r
91 else if(curBlock->magic != MAGIC)
\r
94 _SysDebug("malloc: Corrupt Heap\n");
\r
97 curBlock = (heap_head*)((uintptr_t)curBlock + curBlock->size);
\r
100 if((uintptr_t)curBlock < (uintptr_t)_heap_start) {
\r
101 _SysDebug("malloc: Heap underrun for some reason\n");
\r
105 //Found a perfect match
\r
106 if((uintptr_t)curBlock < (uintptr_t)_heap_end) {
\r
107 curBlock->magic = MAGIC;
\r
108 return (void*)((uintptr_t)curBlock + sizeof(heap_head));
\r
111 //Out of Heap Space
\r
112 if(!closestMatch) {
\r
113 curBlock = extendHeap(bestSize); //Allocate more
\r
114 if(curBlock == NULL) {
\r
115 _SysDebug("malloc: Out of Heap Space\n");
\r
118 curBlock->magic = MAGIC;
\r
119 DEBUGS("malloc(0x%x) = %p (extend) 0x%x", bytes, curBlock->data, bestSize);
\r
120 return curBlock->data;
\r
123 heap_head *besthead = (void*)bestMatchAddr;
\r
126 if(closestMatch - bestSize > BLOCK_SIZE) {
\r
128 curBlock = (heap_head*)bestMatchAddr;
\r
129 curBlock->magic = MAGIC;
\r
130 curBlock->size = bestSize;
\r
131 foot = (heap_foot*)(bestMatchAddr + bestSize - sizeof(heap_foot));
\r
132 foot->header = curBlock;
\r
133 foot->magic = MAGIC;
\r
135 curBlock = (heap_head*)(bestMatchAddr + bestSize);
\r
136 curBlock->magic = MAGIC_FREE;
\r
137 curBlock->size = closestMatch - bestSize;
\r
139 foot = (heap_foot*)(bestMatchAddr + closestMatch - sizeof(heap_foot));
\r
140 foot->header = curBlock;
\r
142 besthead->magic = MAGIC; //mark as used
\r
143 DEBUGS("malloc(0x%x) = %p (split) 0x%x", bytes, besthead->data, bestSize);
\r
144 return besthead->data;
\r
147 //Don't Split the block
\r
148 besthead->magic = MAGIC;
\r
149 DEBUGS("malloc(0x%x) = %p (reuse) 0x%x", bytes, besthead->data, besthead->size);
\r
150 return besthead->data;
\r
154 * \fn EXPORT void *calloc(size_t bytes, size_t count)
\r
155 * \brief Allocate and zero a block of memory
\r
156 * \param __nmemb Number of memeber elements
\r
157 * \param __size Size of one element
\r
159 EXPORT void *calloc(size_t __nmemb, size_t __size)
\r
161 void *ret = malloc(__size*__nmemb);
\r
162 if(!ret) return NULL;
\r
163 memset(ret, 0, __size*__nmemb);
\r
168 \fn EXPORT void free(void *mem)
\r
169 \brief Free previously allocated memory
\r
170 \param mem Pointer - Memory to free
\r
172 EXPORT void free(void *mem)
\r
174 heap_head *head = (void*)((intptr_t)mem-sizeof(heap_head));
\r
179 if(head->magic != MAGIC) //Valid Heap Address
\r
182 head->magic = MAGIC_FREE;
\r
183 DEBUGS("free(%p) : 0x%x bytes", mem, head->size);
\r
186 if((intptr_t)head + head->size < (intptr_t)_heap_end)
\r
188 heap_head *nextHead = (heap_head*)((intptr_t)head + head->size);
\r
189 if(nextHead->magic == MAGIC_FREE) { //Is the next block free
\r
190 head->size += nextHead->size; //Amalgamate
\r
191 nextHead->magic = 0; //For Security
\r
195 if((intptr_t)head - sizeof(heap_foot) > (intptr_t)_heap_start)
\r
197 heap_head *prevHead;
\r
198 heap_foot *prevFoot = (heap_foot *)((intptr_t)head - sizeof(heap_foot));
\r
199 if(prevFoot->magic == MAGIC) {
\r
200 prevHead = prevFoot->header;
\r
201 if(prevHead->magic == MAGIC_FREE) {
\r
202 prevHead->size += head->size; //Amalgamate
\r
203 head->magic = 0; //For Security
\r
210 \fn EXPORT void *realloc(void *oldPos, size_t bytes)
\r
211 \brief Reallocate a block of memory
\r
212 \param bytes Integer - Size of new buffer
\r
213 \param oldPos Pointer - Old Buffer
\r
214 \return Pointer to new buffer
\r
216 EXPORT void *realloc(void *oldPos, size_t bytes)
\r
221 if(oldPos == NULL) {
\r
222 return malloc(bytes);
\r
225 //Check for free space after block
\r
226 head = (heap_head*)((uintptr_t)oldPos-sizeof(heap_head));
\r
228 //Hack to used free's amagamating algorithym and malloc's splitting
\r
231 //Allocate new memory
\r
232 ret = malloc(bytes);
\r
237 if(ret != oldPos) {
\r
238 memcpy(ret, oldPos, head->size-sizeof(heap_head)-sizeof(heap_foot));
\r
246 * \fn LOCAL void *extendHeap(int bytes)
\r
247 * \brief Create a new block at the end of the heap area
\r
248 * \param bytes Integer - Size reqired
\r
249 * \return Pointer to last free block
\r
252 LOCAL void *extendHeap(int bytes)
\r
254 heap_head *head = _heap_end;
\r
257 //Expand Memory Space (Use foot as a temp pointer)
\r
258 foot = sbrk(bytes);
\r
259 if(foot == (void*)-1)
\r
264 head->magic = MAGIC_FREE; //Unallocated
\r
265 head->size = bytes;
\r
267 foot = _heap_end + bytes - sizeof(heap_foot);
\r
268 foot->header = head;
\r
269 foot->magic = MAGIC;
\r
271 //Combine with previous block if nessasary
\r
272 if(_heap_end != _heap_start && ((heap_foot*)((uintptr_t)_heap_end-sizeof(heap_foot)))->magic == MAGIC) {
\r
273 heap_head *tmpHead = ((heap_foot*)((uintptr_t)_heap_end-sizeof(heap_foot)))->header;
\r
274 if(tmpHead->magic == MAGIC_FREE) {
\r
275 tmpHead->size += bytes;
\r
276 foot->header = tmpHead;
\r
281 _heap_end = (void*) ((uintptr_t)foot+sizeof(heap_foot));
\r
286 \fn EXPORT void *sbrk(int increment)
\r
287 \brief Increases the program's memory space
\r
288 \param count Integer - Size of heap increase
\r
289 \return Pointer to start of new region
\r
291 EXPORT void *sbrk(int increment)
\r
293 static uintptr_t oldEnd = 0;
\r
294 static uintptr_t curEnd = 0;
\r
296 //_SysDebug("sbrk: (increment=%i)", increment);
\r
299 oldEnd = curEnd = (uintptr_t)FindHeapBase();
\r
300 //_SysAllocate(curEnd); // Allocate the first page
\r
303 //_SysDebug(" sbrk: oldEnd = 0x%x", oldEnd);
\r
304 if (increment == 0) return (void *) curEnd;
\r
309 if( (curEnd & 0xFFF) && (curEnd & 0xFFF) + increment < 0x1000 )
\r
311 //if( curEnd & 0xFFF == 0 )
\r
313 // if( !_SysAllocate(curEnd) )
\r
315 // _SysDebug("sbrk - Error allocating memory");
\r
316 // return (void*)-1;
\r
319 curEnd += increment;
\r
320 //_SysDebug("sbrk: RETURN %p (single page, no alloc)", (void *) oldEnd);
\r
321 return (void *)oldEnd;
\r
324 increment -= curEnd & 0xFFF;
\r
325 curEnd += 0xFFF; curEnd &= ~0xFFF;
\r
326 while( increment > 0 )
\r
328 if( !_SysAllocate(curEnd) )
\r
331 _SysDebug("sbrk - Error allocating memory");
\r
334 increment -= 0x1000;
\r
338 //_SysDebug("sbrk: RETURN %p", (void *) oldEnd);
\r
339 return (void *) oldEnd;
\r
343 * \fn EXPORT int IsHeap(void *ptr)
\r
345 EXPORT int IsHeap(void *ptr)
\r
351 if( (uintptr_t)ptr < (uintptr_t)_heap_start ) return 0;
\r
352 if( (uintptr_t)ptr > (uintptr_t)_heap_end ) return 0;
\r
355 head = (void*)((Uint)ptr - 4);
\r
356 if( head->magic != MAGIC ) return 0;
\r
357 foot = (void*)( (Uint)ptr + head->size - sizeof(heap_foot) );
\r
358 if( foot->magic != MAGIC ) return 0;
\r
363 // === STATIC FUNCTIONS ===
\r
365 * Does the job of brk(0)
\r
367 static void *FindHeapBase()
\r
370 #define MAX 0xC0000000 // Address
\r
371 #define THRESHOLD 512 // Pages
\r
376 // Scan address space
\r
382 tmp = _SysGetPhys(addr);
\r
387 if(stretch > THRESHOLD)
\r
389 return (void*)( addr - stretch*0x1000 );
\r
392 //__asm__ __volatile__ (
\r
393 // "push %%ebx;mov %%edx,%%ebx;int $0xAC;pop %%ebx"
\r
394 // ::"a"(256),"d"("%x"),"c"(addr));
\r
399 return (void*)0x00900000;
\r
403 LOCAL uint brk(uintptr_t newpos)
\r
405 static uintptr_t curpos;
\r
410 _SysDebug("brk: (newpos=0x%x)", newpos);
\r
412 // Find initial position
\r
413 if(curpos == 0) curpos = (uintptr_t)FindHeapBase();
\r
415 // Get Current Position
\r
416 if(newpos == 0) return curpos;
\r
418 if(newpos < curpos) return newpos;
\r
420 delta = newpos - curpos;
\r
421 _SysDebug(" brk: delta = 0x%x", delta);
\r
423 // Do we need to add pages
\r
424 if(curpos & 0xFFF && (curpos & 0xFFF) + delta < 0x1000)
\r
425 return curpos += delta;
\r
427 // Page align current position
\r
428 if(curpos & 0xFFF) delta -= 0x1000 - (curpos & 0xFFF);
\r
429 curpos = (curpos + 0xFFF) & ~0xFFF;
\r
432 pages = (delta + 0xFFF) >> 12;
\r
435 _SysAllocate(curpos);
\r
440 // Bring the current position to exactly what we want
\r
441 curpos -= ((delta + 0xFFF) & ~0xFFF) - delta;
\r
443 return ret; // Return old curpos
\r