void my_free(void *ptr) { struct block *current = head; while(current != NULL) { if(ptr == current->memPtr) { current->isFree = true; coalesceBlocks(); } current = current->next; } }
/** * @brief Insert a block back into the free list, which is ordered by address. * @param pool The pool to add the block to. * @param addr The address to add into the free list. */ static void insertIntoFreeList(lpx_mempool_variable_t *pool, void *addr) { long *listHead = (long *)pool->freeList; long *node = (long *)pool->freeList; long *prev = NULL; long *current = (long *)addr; long *next = NULL; int insertAfterInstead = 0; if (listHead == NULL) { // Simple case, this block is the new head. current[VPMD_NEXT_OFFSET] = 0; listHead = current; } else { // Block needs to be inserted into the list. listHead = (listHead > current) ? current : listHead; // First, find the node that it needs to be inserted before. node = listHead; while (node < current) { // Make sure we dont run off the end of the list. if (node[VPMD_NEXT_OFFSET] == 0) { insertAfterInstead = 1; break; } // Keep traversing otherwise. node = (long *)(node[VPMD_NEXT_OFFSET]); } // Now actually insert the node before or after the node. if (insertAfterInstead) { insertAfter(node, current); } else { insertBefore(node, current); } } // If the head of the list has changed, update the free list pointer. if (pool->freeList != listHead) { pool->freeList = (void *)listHead; listHead[VPMD_PREV_OFFSET] = 0; } // Finally, call coalesce to minimize fragmentation. prev = (long *)current[VPMD_PREV_OFFSET]; next = (long *)current[VPMD_NEXT_OFFSET]; coalesceBlocks(prev, current, next); return; }