mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-05 07:08:48 +00:00
Make permanent_allocator() thread local
This commit is contained in:
+9
-15
@@ -50,17 +50,16 @@ void virtual_memory_init(void) {
|
|||||||
|
|
||||||
|
|
||||||
struct MemoryBlock {
|
struct MemoryBlock {
|
||||||
|
MemoryBlock *prev;
|
||||||
u8 * base;
|
u8 * base;
|
||||||
isize size;
|
isize size;
|
||||||
isize used;
|
isize used;
|
||||||
MemoryBlock *prev;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Arena {
|
struct Arena {
|
||||||
MemoryBlock *curr_block;
|
MemoryBlock *curr_block;
|
||||||
isize minimum_block_size;
|
isize minimum_block_size;
|
||||||
bool use_local_mutex;
|
bool ignore_mutex;
|
||||||
BlockingMutex local_mutex;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
enum { DEFAULT_MINIMUM_BLOCK_SIZE = 8ll*1024ll*1024ll };
|
enum { DEFAULT_MINIMUM_BLOCK_SIZE = 8ll*1024ll*1024ll };
|
||||||
@@ -72,10 +71,6 @@ void virtual_memory_dealloc(MemoryBlock *block);
|
|||||||
void *arena_alloc(Arena *arena, isize min_size, isize alignment);
|
void *arena_alloc(Arena *arena, isize min_size, isize alignment);
|
||||||
void arena_free_all(Arena *arena);
|
void arena_free_all(Arena *arena);
|
||||||
|
|
||||||
void arena_init_local_mutex(Arena *arena) {
|
|
||||||
mutex_init(&arena->local_mutex);
|
|
||||||
arena->use_local_mutex = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
isize arena_align_forward_offset(Arena *arena, isize alignment) {
|
isize arena_align_forward_offset(Arena *arena, isize alignment) {
|
||||||
isize alignment_offset = 0;
|
isize alignment_offset = 0;
|
||||||
@@ -91,11 +86,9 @@ void *arena_alloc(Arena *arena, isize min_size, isize alignment) {
|
|||||||
GB_ASSERT(gb_is_power_of_two(alignment));
|
GB_ASSERT(gb_is_power_of_two(alignment));
|
||||||
|
|
||||||
BlockingMutex *mutex = &global_memory_allocator_mutex;
|
BlockingMutex *mutex = &global_memory_allocator_mutex;
|
||||||
if (arena->use_local_mutex) {
|
if (!arena->ignore_mutex) {
|
||||||
mutex = &arena->local_mutex;
|
mutex_lock(mutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
mutex_lock(mutex);
|
|
||||||
|
|
||||||
isize size = 0;
|
isize size = 0;
|
||||||
if (arena->curr_block != nullptr) {
|
if (arena->curr_block != nullptr) {
|
||||||
@@ -122,7 +115,9 @@ void *arena_alloc(Arena *arena, isize min_size, isize alignment) {
|
|||||||
curr_block->used += size;
|
curr_block->used += size;
|
||||||
GB_ASSERT(curr_block->used <= curr_block->size);
|
GB_ASSERT(curr_block->used <= curr_block->size);
|
||||||
|
|
||||||
mutex_unlock(mutex);
|
if (!arena->ignore_mutex) {
|
||||||
|
mutex_unlock(mutex);
|
||||||
|
}
|
||||||
|
|
||||||
// NOTE(bill): memory will be zeroed by default due to virtual memory
|
// NOTE(bill): memory will be zeroed by default due to virtual memory
|
||||||
return ptr;
|
return ptr;
|
||||||
@@ -296,14 +291,13 @@ GB_ALLOCATOR_PROC(arena_allocator_proc) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
gb_global Arena permanent_arena = {};
|
gb_global gb_thread_local Arena permanent_arena = {nullptr, DEFAULT_MINIMUM_BLOCK_SIZE, true};
|
||||||
gbAllocator permanent_allocator() {
|
gbAllocator permanent_allocator() {
|
||||||
return arena_allocator(&permanent_arena);
|
return arena_allocator(&permanent_arena);
|
||||||
}
|
}
|
||||||
|
|
||||||
gb_global Arena temporary_arena = {};
|
|
||||||
gbAllocator temporary_allocator() {
|
gbAllocator temporary_allocator() {
|
||||||
return arena_allocator(&temporary_arena);
|
return permanent_allocator();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -2489,7 +2489,6 @@ int main(int arg_count, char const **arg_ptr) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
remove_temp_files(gen);
|
remove_temp_files(gen);
|
||||||
arena_free_all(&temporary_arena);
|
|
||||||
|
|
||||||
if (run_output) {
|
if (run_output) {
|
||||||
#if defined(GB_SYSTEM_WINDOWS)
|
#if defined(GB_SYSTEM_WINDOWS)
|
||||||
|
|||||||
@@ -4710,9 +4710,6 @@ ParseFileError init_ast_file(AstFile *f, String fullpath, TokenPos *err_pos) {
|
|||||||
block_size = ((block_size + page_size-1)/page_size) * page_size;
|
block_size = ((block_size + page_size-1)/page_size) * page_size;
|
||||||
block_size = gb_clamp(block_size, page_size, DEFAULT_MINIMUM_BLOCK_SIZE);
|
block_size = gb_clamp(block_size, page_size, DEFAULT_MINIMUM_BLOCK_SIZE);
|
||||||
f->arena.minimum_block_size = block_size;
|
f->arena.minimum_block_size = block_size;
|
||||||
#if 0
|
|
||||||
arena_init_local_mutex(&f->arena);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
array_init(&f->comments, heap_allocator(), 0, 0);
|
array_init(&f->comments, heap_allocator(), 0, 0);
|
||||||
array_init(&f->imports, heap_allocator(), 0, 0);
|
array_init(&f->imports, heap_allocator(), 0, 0);
|
||||||
|
|||||||
Reference in New Issue
Block a user