Browse Source

Use local mutex for each `AstFile.arena`

gingerBill 4 years ago
parent
commit
da79124e5d
3 changed files with 19 additions and 7 deletions
  1. 16 4
      src/common_memory.cpp
  2. 2 1
      src/parser.cpp
  3. 1 2
      src/tokenizer.cpp

+ 16 - 4
src/common_memory.cpp

@@ -59,6 +59,8 @@ struct MemoryBlock {
 struct Arena {
 struct Arena {
 	MemoryBlock * curr_block;
 	MemoryBlock * curr_block;
 	isize minimum_block_size;
 	isize minimum_block_size;
+	bool use_local_mutex;
+	BlockingMutex local_mutex;
 };
 };
 
 
 enum { DEFAULT_MINIMUM_BLOCK_SIZE = 8ll*1024ll*1024ll };
 enum { DEFAULT_MINIMUM_BLOCK_SIZE = 8ll*1024ll*1024ll };
@@ -79,15 +81,25 @@ isize arena_align_forward_offset(Arena *arena, isize alignment) {
 	return alignment_offset;
 	return alignment_offset;
 }
 }
 
 
+void arena_init_local_mutex(Arena *arena) {
+	mutex_init(&arena->local_mutex);
+	arena->use_local_mutex = true;
+}
+
+
 
 
 void *arena_alloc(Arena *arena, isize min_size, isize alignment) {
 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));
 	
 	
-	isize size = 0;
+	
+	BlockingMutex *mutex = &global_memory_allocator_mutex;
+	if (arena->use_local_mutex) {
+		mutex = &arena->local_mutex;
+	}
 		
 		
-	// TODO(bill): make it so that this can be done lock free (if possible)
-	mutex_lock(&global_memory_allocator_mutex);
+	mutex_lock(mutex);
 	
 	
+	isize size = 0;
 	if (arena->curr_block != nullptr) {
 	if (arena->curr_block != nullptr) {
 		size = min_size + arena_align_forward_offset(arena, alignment);
 		size = min_size + arena_align_forward_offset(arena, alignment);
 	}
 	}
@@ -112,7 +124,7 @@ 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(&global_memory_allocator_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;	

+ 2 - 1
src/parser.cpp

@@ -4786,6 +4786,7 @@ 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;
+	arena_init_local_mutex(&f->arena);
 
 
 
 
 	array_init(&f->comments, heap_allocator(), 0, 0);
 	array_init(&f->comments, heap_allocator(), 0, 0);
@@ -5539,7 +5540,7 @@ ParseFileError process_imported_file(Parser *p, ImportedFile imported_file) {
 	FileInfo    fi  = imported_file.fi;
 	FileInfo    fi  = imported_file.fi;
 	TokenPos    pos = imported_file.pos;
 	TokenPos    pos = imported_file.pos;
 
 
-	AstFile *file = gb_alloc_item(heap_allocator(), AstFile);
+	AstFile *file = gb_alloc_item(permanent_allocator(), AstFile);
 	file->pkg = pkg;
 	file->pkg = pkg;
 	file->id = cast(i32)(imported_file.index+1);
 	file->id = cast(i32)(imported_file.index+1);
 	TokenPos err_pos = {0};
 	TokenPos err_pos = {0};

+ 1 - 2
src/tokenizer.cpp

@@ -805,8 +805,7 @@ void init_tokenizer_with_data(Tokenizer *t, String const &fullpath, void *data,
 TokenizerInitError init_tokenizer_from_fullpath(Tokenizer *t, String const &fullpath, TokenizerFlags flags = TokenizerFlag_None) {
 TokenizerInitError init_tokenizer_from_fullpath(Tokenizer *t, String const &fullpath, TokenizerFlags flags = TokenizerFlag_None) {
 	TokenizerInitError err = TokenizerInit_None;
 	TokenizerInitError err = TokenizerInit_None;
 
 
-	char *c_str = alloc_cstring(heap_allocator(), fullpath);
-	defer (gb_free(heap_allocator(), c_str));
+	char *c_str = alloc_cstring(temporary_allocator(), fullpath);
 
 
 	// TODO(bill): Memory map rather than copy contents
 	// TODO(bill): Memory map rather than copy contents
 	gbFileContents fc = gb_file_read_contents(heap_allocator(), true, c_str);
 	gbFileContents fc = gb_file_read_contents(heap_allocator(), true, c_str);