void __memwalk() { int i = 0; if (__mem_blocks[0]==0) __mem_blocks[0] = __BLOCK0; while (i<__MEM_BLOCKS) { printf("BLOCK %i:\n", i); printf(" free: %i\n", __BLOCK_GET_FREEBLOCK(__mem_blocks[i])); printf(" size: %i\n", __BLOCK_GET_SIZE(__mem_blocks[i])); printf(" prev: %i\n", __BLOCK_GET_PREV(__mem_blocks[i])); i+=__BLOCK_GET_SIZE(__mem_blocks[i]); } }
void vfree( void* ptr ) { if (ptr==0) return; //ptr -= 0x40000000; // @_@: int block = ((unsigned int)ptr - __MEM_START)/__BLOCK_SIZE; if (block<0 || block>__MEM_BLOCKS) { printf("Block is out of range: %i (0x%x)\n", block, (int)ptr); return; } int csize = __BLOCK_GET_SIZE(__mem_blocks[block]); printf("freeing block %i (0x%x), size: %i\n", block, (int)ptr, csize); if (__BLOCK_GET_FREEBLOCK(__mem_blocks[block])!=1 || csize==0) { #ifdef _DEBUG printf("Block was not allocated!\n"); #endif return; } // Mark block as free __BLOCK_SET_FREE(__mem_blocks[block],1); __mem_free += csize; int next = block+csize; // Merge with previous block if possible int prev = __BLOCK_GET_PREV(__mem_blocks[block]); if (prev<block) { if (__BLOCK_GET_FREEBLOCK(__mem_blocks[prev])==3) { __BLOCK_ADD_SIZE(__mem_blocks[prev], csize); __BLOCK_SET_BLOCK(__mem_blocks[block],0); // mark current block as inter block if (next<__MEM_BLOCKS) __BLOCK_SET_PREV(__mem_blocks[next], prev); block = prev; } } // Merge with next block if possible if (next<__MEM_BLOCKS) { if (__BLOCK_GET_FREEBLOCK(__mem_blocks[next])==3) { __BLOCK_ADD_SIZE(__mem_blocks[block], __BLOCK_GET_SIZE(__mem_blocks[next])); __BLOCK_SET_BLOCK(__mem_blocks[next],0); // mark next block as inter block int nextnext = next + __BLOCK_GET_SIZE(__mem_blocks[next]); if (nextnext<__MEM_BLOCKS) __BLOCK_SET_PREV(__mem_blocks[nextnext], block); } } // Update if a new largest block emerged if (__largest_block<__BLOCK_GET_SIZE(__mem_blocks[block])) { __largest_block = __BLOCK_GET_SIZE(__mem_blocks[block]); __largest_update = 0; // No update necessary any more, because update only necessary when largest has shrinked at most } }