From 042dbda47f8a428c1be2b1af2937f0cbff109c11 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Mon, 13 Sep 2021 01:30:30 +0100 Subject: Replace many uses of `heap_allocator()` with `permanent_allocator()` --- src/parser.cpp | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) (limited to 'src/parser.cpp') diff --git a/src/parser.cpp b/src/parser.cpp index e1f21f459..722df0d90 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -1196,7 +1196,7 @@ CommentGroup *consume_comment_group(AstFile *f, isize n, isize *end_line_) { CommentGroup *comments = nullptr; if (list.count > 0) { - comments = gb_alloc_item(heap_allocator(), CommentGroup); + comments = gb_alloc_item(permanent_allocator(), CommentGroup); comments->list = slice_from_array(list); array_add(&f->comments, comments); } @@ -4727,8 +4727,6 @@ void destroy_ast_file(AstFile *f) { array_free(&f->tokens); array_free(&f->comments); array_free(&f->imports); - gb_free(heap_allocator(), f->tokenizer.fullpath.text); - destroy_tokenizer(&f->tokenizer); } bool init_parser(Parser *p) { @@ -4795,7 +4793,7 @@ WORKER_TASK_PROC(parser_worker_proc) { void parser_add_file_to_process(Parser *p, AstPackage *pkg, FileInfo fi, TokenPos pos) { // TODO(bill): Use a better allocator ImportedFile f = {pkg, fi, pos, p->file_to_process_count++}; - auto wd = gb_alloc_item(heap_allocator(), ParserWorkerData); + auto wd = gb_alloc_item(permanent_allocator(), ParserWorkerData); wd->parser = p; wd->imported_file = f; global_thread_pool_add_task(parser_worker_proc, wd); @@ -4833,7 +4831,7 @@ WORKER_TASK_PROC(foreign_file_worker_proc) { void parser_add_foreign_file_to_process(Parser *p, AstPackage *pkg, AstForeignFileKind kind, FileInfo fi, TokenPos pos) { // TODO(bill): Use a better allocator ImportedFile f = {pkg, fi, pos, p->file_to_process_count++}; - auto wd = gb_alloc_item(heap_allocator(), ForeignFileWorkerData); + auto wd = gb_alloc_item(permanent_allocator(), ForeignFileWorkerData); wd->parser = p; wd->imported_file = f; wd->foreign_kind = kind; @@ -4854,7 +4852,7 @@ AstPackage *try_add_import_path(Parser *p, String const &path, String const &rel string_set_add(&p->imported_files, path); - AstPackage *pkg = gb_alloc_item(heap_allocator(), AstPackage); + AstPackage *pkg = gb_alloc_item(permanent_allocator(), AstPackage); pkg->kind = kind; pkg->fullpath = path; array_init(&pkg->files, heap_allocator()); -- cgit v1.2.3 From 15c309b0b84c2ae36feea4220f0ccef28587db63 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Mon, 13 Sep 2021 01:39:41 +0100 Subject: Make `permanent_allocator()` thread local --- src/common_memory.cpp | 24 +++++++++--------------- src/main.cpp | 1 - src/parser.cpp | 3 --- 3 files changed, 9 insertions(+), 19 deletions(-) (limited to 'src/parser.cpp') diff --git a/src/common_memory.cpp b/src/common_memory.cpp index 4d9811b4a..2d7a7a246 100644 --- a/src/common_memory.cpp +++ b/src/common_memory.cpp @@ -50,17 +50,16 @@ void virtual_memory_init(void) { struct MemoryBlock { + MemoryBlock *prev; u8 * base; isize size; isize used; - MemoryBlock *prev; }; struct Arena { MemoryBlock *curr_block; isize minimum_block_size; - bool use_local_mutex; - BlockingMutex local_mutex; + bool ignore_mutex; }; 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_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 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)); BlockingMutex *mutex = &global_memory_allocator_mutex; - if (arena->use_local_mutex) { - mutex = &arena->local_mutex; + if (!arena->ignore_mutex) { + mutex_lock(mutex); } - - mutex_lock(mutex); isize size = 0; if (arena->curr_block != nullptr) { @@ -122,7 +115,9 @@ void *arena_alloc(Arena *arena, isize min_size, isize alignment) { curr_block->used += 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 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() { return arena_allocator(&permanent_arena); } -gb_global Arena temporary_arena = {}; gbAllocator temporary_allocator() { - return arena_allocator(&temporary_arena); + return permanent_allocator(); } diff --git a/src/main.cpp b/src/main.cpp index dd9882408..9e35062f2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2489,7 +2489,6 @@ int main(int arg_count, char const **arg_ptr) { } remove_temp_files(gen); - arena_free_all(&temporary_arena); if (run_output) { #if defined(GB_SYSTEM_WINDOWS) diff --git a/src/parser.cpp b/src/parser.cpp index 722df0d90..e33531fad 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -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 = gb_clamp(block_size, page_size, DEFAULT_MINIMUM_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->imports, heap_allocator(), 0, 0); -- cgit v1.2.3 From 05ac2002e0296c3acccca1d8cffaafb002e43120 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sat, 18 Sep 2021 12:52:43 +0100 Subject: Force file copy on `odin strip-semicolon` --- src/common.cpp | 126 +++++++++++++++++++++++++++--------------------------- src/parser.cpp | 3 +- src/tokenizer.cpp | 10 +++-- 3 files changed, 71 insertions(+), 68 deletions(-) (limited to 'src/parser.cpp') diff --git a/src/common.cpp b/src/common.cpp index 8a220c799..9497aaf18 100644 --- a/src/common.cpp +++ b/src/common.cpp @@ -868,76 +868,77 @@ enum MemoryMappedFileError { MemoryMappedFile_COUNT, }; -MemoryMappedFileError memory_map_file_32(char const *fullpath, MemoryMappedFile *memory_mapped_file) { +MemoryMappedFileError memory_map_file_32(char const *fullpath, MemoryMappedFile *memory_mapped_file, bool copy_file_contents) { MemoryMappedFileError err = MemoryMappedFile_None; -#if defined(GB_SYSTEM_WINDOWS) - isize w_len = 0; - wchar_t *w_str = gb__alloc_utf8_to_ucs2(temporary_allocator(), fullpath, &w_len); - if (w_str == nullptr) { - return MemoryMappedFile_Invalid; - } - i64 file_size = 0; - LARGE_INTEGER li_file_size = {}; - HANDLE handle = nullptr; - HANDLE file_mapping = nullptr; - void *file_data = nullptr; - - handle = CreateFileW(w_str, GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL); - if (handle == INVALID_HANDLE_VALUE) { - handle = nullptr; - goto window_handle_file_error; - } - - li_file_size = {}; - if (!GetFileSizeEx(handle, &li_file_size)) { - goto window_handle_file_error; - } - file_size = cast(i64)li_file_size.QuadPart; - if (file_size > I32_MAX) { - CloseHandle(handle); - return MemoryMappedFile_FileTooLarge; - } - - if (file_size == 0) { + if (!copy_file_contents) { + #if defined(GB_SYSTEM_WINDOWS) + isize w_len = 0; + wchar_t *w_str = gb__alloc_utf8_to_ucs2(temporary_allocator(), fullpath, &w_len); + if (w_str == nullptr) { + return MemoryMappedFile_Invalid; + } + i64 file_size = 0; + LARGE_INTEGER li_file_size = {}; + HANDLE handle = nullptr; + HANDLE file_mapping = nullptr; + void *file_data = nullptr; + + handle = CreateFileW(w_str, GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL); + if (handle == INVALID_HANDLE_VALUE) { + handle = nullptr; + goto window_handle_file_error; + } + + li_file_size = {}; + if (!GetFileSizeEx(handle, &li_file_size)) { + goto window_handle_file_error; + } + file_size = cast(i64)li_file_size.QuadPart; + if (file_size > I32_MAX) { + CloseHandle(handle); + return MemoryMappedFile_FileTooLarge; + } + + if (file_size == 0) { + CloseHandle(handle); + err = MemoryMappedFile_Empty; + memory_mapped_file->handle = nullptr; + memory_mapped_file->data = nullptr; + memory_mapped_file->size = 0; + return err; + } + + file_mapping = CreateFileMappingW(handle, nullptr, PAGE_READONLY, 0, 0, nullptr); CloseHandle(handle); - err = MemoryMappedFile_Empty; - memory_mapped_file->handle = nullptr; - memory_mapped_file->data = nullptr; - memory_mapped_file->size = 0; + + file_data = MapViewOfFileEx(file_mapping, FILE_MAP_READ, 0, 0, 0/*file_size*/, nullptr/*base address*/); + memory_mapped_file->handle = cast(void *)file_mapping; + memory_mapped_file->data = file_data; + memory_mapped_file->size = cast(i32)file_size; return err; - } - - file_mapping = CreateFileMappingW(handle, nullptr, PAGE_READONLY, 0, 0, nullptr); - CloseHandle(handle); - - file_data = MapViewOfFileEx(file_mapping, FILE_MAP_READ, 0, 0, 0/*file_size*/, nullptr/*base address*/); - memory_mapped_file->handle = cast(void *)file_mapping; - memory_mapped_file->data = file_data; - memory_mapped_file->size = cast(i32)file_size; - return err; -window_handle_file_error:; - { - DWORD handle_err = GetLastError(); - CloseHandle(handle); - err = MemoryMappedFile_Invalid; - switch (handle_err) { - case ERROR_FILE_NOT_FOUND: - case ERROR_PATH_NOT_FOUND: - case ERROR_INVALID_DRIVE: - err = MemoryMappedFile_NotExists; - break; - case ERROR_ACCESS_DENIED: - case ERROR_INVALID_ACCESS: - err = MemoryMappedFile_Permission; - break; + window_handle_file_error:; + { + DWORD handle_err = GetLastError(); + CloseHandle(handle); + err = MemoryMappedFile_Invalid; + switch (handle_err) { + case ERROR_FILE_NOT_FOUND: + case ERROR_PATH_NOT_FOUND: + case ERROR_INVALID_DRIVE: + err = MemoryMappedFile_NotExists; + break; + case ERROR_ACCESS_DENIED: + case ERROR_INVALID_ACCESS: + err = MemoryMappedFile_Permission; + break; + } + return err; } - return err; + #endif } -#else - // TODO(bill): Memory map rather than copy contents gbFileContents fc = gb_file_read_contents(heap_allocator(), true, fullpath); if (fc.size > I32_MAX) { @@ -963,7 +964,6 @@ window_handle_file_error:; } } return err; -#endif } diff --git a/src/parser.cpp b/src/parser.cpp index e33531fad..f72d8a73c 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -4645,7 +4645,8 @@ ParseFileError init_ast_file(AstFile *f, String fullpath, TokenPos *err_pos) { zero_item(&f->tokenizer); f->tokenizer.curr_file_id = f->id; - TokenizerInitError err = init_tokenizer_from_fullpath(&f->tokenizer, f->fullpath); + bool copy_file_contents = build_context.command_kind == Command_strip_semicolon; + TokenizerInitError err = init_tokenizer_from_fullpath(&f->tokenizer, f->fullpath, copy_file_contents); if (err != TokenizerInit_None) { switch (err) { case TokenizerInit_Empty: diff --git a/src/tokenizer.cpp b/src/tokenizer.cpp index 2251b4f96..97836bd1b 100644 --- a/src/tokenizer.cpp +++ b/src/tokenizer.cpp @@ -722,6 +722,8 @@ struct Tokenizer { i32 error_count; bool insert_semicolon; + + MemoryMappedFile memory_mapped_file; }; @@ -811,17 +813,17 @@ TokenizerInitError memory_mapped_file_error_map_to_tokenizer[MemoryMappedFile_CO TokenizerInit_Permission, /*MemoryMappedFile_Permission*/ }; -TokenizerInitError init_tokenizer_from_fullpath(Tokenizer *t, String const &fullpath) { - MemoryMappedFile memory_mapped_file = {}; +TokenizerInitError init_tokenizer_from_fullpath(Tokenizer *t, String const &fullpath, bool copy_file_contents) { MemoryMappedFileError mmf_err = memory_map_file_32( alloc_cstring(temporary_allocator(), fullpath), - &memory_mapped_file + &t->memory_mapped_file, + copy_file_contents ); TokenizerInitError err = memory_mapped_file_error_map_to_tokenizer[mmf_err]; switch (mmf_err) { case MemoryMappedFile_None: - init_tokenizer_with_data(t, fullpath, memory_mapped_file.data, cast(isize)memory_mapped_file.size); + init_tokenizer_with_data(t, fullpath, t->memory_mapped_file.data, cast(isize)t->memory_mapped_file.size); break; case MemoryMappedFile_FileTooLarge: case MemoryMappedFile_Empty: -- cgit v1.2.3