- /* Get the first piece of memory on the "free_atoms" list.
- * We can go ahead and destroy the list node we used to keep
- * track of it with and to update the "free_atoms" list to
- * point to its next element.
- */
- mem = rmem_chunk->free_atoms;
- rmem_chunk->free_atoms = rmem_chunk->free_atoms->next;
-
- /* Determine which area this piece of memory is allocated from */
- temp_area = g_tree_search (rmem_chunk->mem_tree,
- (GSearchFunc) g_mem_chunk_area_search,
- mem);
-
- /* If the area has been marked, then it is being destroyed.
- * (ie marked to be destroyed).
- * We check to see if all of the segments on the free list that
- * reference this area have been removed. This occurs when
- * the ammount of free memory is less than the allocatable size.
- * If the chunk should be freed, then we place it in the "free_mem_area".
- * This is so we make sure not to free the mem area here and then
- * allocate it again a few lines down.
- * If we don't allocate a chunk a few lines down then the "free_mem_area"
- * will be freed.
- * If there is already a "free_mem_area" then we'll just free this mem area.
- */
- if (temp_area->mark)
- {
- /* Update the "free" memory available in that area */
- temp_area->free += rmem_chunk->atom_size;
-
- if (temp_area->free == rmem_chunk->area_size)
- {
- if (temp_area == rmem_chunk->mem_area)
- rmem_chunk->mem_area = NULL;
-
- if (rmem_chunk->free_mem_area)
- {
- rmem_chunk->num_mem_areas -= 1;
-
- if (temp_area->next)
- temp_area->next->prev = temp_area->prev;
- if (temp_area->prev)
- temp_area->prev->next = temp_area->next;
- if (temp_area == rmem_chunk->mem_areas)
- rmem_chunk->mem_areas = rmem_chunk->mem_areas->next;
-
- if (rmem_chunk->type == G_ALLOC_AND_FREE)
- g_tree_remove (rmem_chunk->mem_tree, temp_area);
- g_free (temp_area);
- }
- else
- rmem_chunk->free_mem_area = temp_area;
-
- rmem_chunk->num_marked_areas -= 1;
- }
- }
- else
- {
- /* Update the number of allocated atoms count.
- */
- temp_area->allocated += 1;
-
- /* The area wasn't marked...return the memory
- */
- goto outa_here;
- }