-
- // next chunk
- chunk = (u32*)((uptr)chunk + csize + 4);
- }
- // if neccessary free memory on end of heap
- if (colflag == 1)
- *newchunk = SWAP32(dsize | 1);
-
- chunk = heap_addr;
- csize = ((u32)*chunk) & 0xfffffffc;
- cstat = ((u32)*chunk) & 1;
- dsize = (a0 + 3) & 0xfffffffc;
-
- // exit on uninitialized heap
- if (chunk == NULL) {
- printf("malloc %x,%x: Uninitialized Heap!\n", v0, a0);
- v0 = 0;
- pc0 = ra;
- return;
- }
-
- // search an unused chunk that is big enough until the end of the heap
- while ((dsize > csize || cstat==0) && chunk < heap_end ) {
- chunk = (u32*)((uptr)chunk + csize + 4);
-
- // catch out of memory
- if(chunk >= heap_end) {
- printf("malloc %x,%x: Out of memory error!\n",
- v0, a0);
- v0 = 0; pc0 = ra;
- return;
- }
-
- csize = ((u32)*chunk) & 0xfffffffc;
- cstat = ((u32)*chunk) & 1;