/** * @file free.c * @brief free implementation. */ #include "malloc.h" #include "malloc_internal.h" #include static t_chunk * _s_find_chunk (t_zone *zone, void *ptr) { t_chunk *chunk; while (zone) { chunk = (t_chunk *)((char *)zone + ALIGN (sizeof (t_zone))); while (chunk) { if ((char *)chunk + ALIGN (sizeof (t_chunk)) == ptr) return (chunk); chunk = chunk->next; } zone = zone->next; } return (NULL); } static void _s_unlink_zone (t_zone **zone_list, t_zone *zone) { t_zone *prev; if (*zone_list == zone) { *zone_list = zone->next; return; } prev = *zone_list; while (prev && prev->next != zone) prev = prev->next; if (prev) prev->next = zone->next; } void free (void *ptr) { t_chunk *chunk; t_zone *zone; if (!ptr) return; chunk = _s_find_chunk (g_heap.large, ptr); if (chunk) { zone = (t_zone *)((char *)chunk - ALIGN (sizeof (t_zone))); _s_unlink_zone (&g_heap.large, zone); munmap (zone, zone->size); return; } chunk = _s_find_chunk (g_heap.tiny, ptr); if (!chunk) chunk = _s_find_chunk (g_heap.small, ptr); if (chunk) chunk->is_free = 1; }