#temp_allocator destroy vs free_all

10 messages · Page 1 of 1 (latest)

frigid cliff
#

what is the difference between
default_temp_allocator_destroy and
free_all(temp_allocator)
I traced the free_all all the way to Windows OS commands and understand that one.
It ends up at `_, err = allocator.procedure(allocator.data, .Free_All, 0, 0, nil, 0, loc).

The destroy calls

arena_destroy :: proc(arena: ^Arena, loc := #caller_location) {
    for arena.curr_block != nil {
        free_block := arena.curr_block
        arena.curr_block = free_block.prev

        arena.total_capacity -= free_block.capacity
        memory_block_dealloc(free_block, loc)
    }
    arena.total_used = 0
    arena.total_capacity = 0
}

which calls

memory_block_dealloc :: proc(block_to_free: ^Memory_Block, loc := #caller_location) {
    if block_to_free != nil {
        allocator := block_to_free.allocator
        mem_free(block_to_free, allocator, loc)
    }
}
#

This calls

mem_free :: #force_inline proc(ptr: rawptr, allocator := context.allocator, loc := #caller_location) -> Allocator_Error {
    if ptr == nil || allocator.procedure == nil {
        return nil
    }
    _, err := allocator.procedure(allocator.data, .Free, 0, 0, ptr, 0, loc)
#

They look the same to me in the end
I am missing something

#

It seems the destroy is focused on a specific block?

obsidian hornet
#

free_all leaves the first block to be reused

frigid cliff
#

of course, thank you

frigid cliff
# obsidian hornet free_all leaves the first block to be reused

I used grep and found this
odin\base\runtime\default_temp_allocator_arena.odin

// `arena_free_all` will free all but the first memory block, and then reset the memory block
arena_free_all :: proc(arena: ^Arena, loc := #caller_location) {
for arena.curr_block != nil && arena.curr_block.prev != nil {
arena_free_last_memory_block(arena, loc)

is it the && arena.curr_block.prev !=nil that prevents the first block from being freed?

#

The first block would not have a prev block I think

obsidian hornet
#

Yeah

frigid cliff
#

thank you