From 046d86423ac9de1991af741ee40f01cefd63a7e2 Mon Sep 17 00:00:00 2001 From: Musa Mahmood Date: Sun, 7 Dec 2025 10:24:29 -0500 Subject: [PATCH] Fix really stupid bug in `Array's array_reserve` --- lib/Base/Allocator.cpp | 6 ++++++ lib/Base/Allocator.h | 6 +++++- lib/Base/Arena.cpp | 10 ++++++++-- lib/Base/Array.h | 3 +-- lib/Base/Base_Thread_Context.h | 1 + lib/Base/ErrorType.cpp | 2 +- lib/Base/Expandable_Arena.cpp | 16 ++++++++++++++-- lib/Base/General_Purpose_Allocator.cpp | 8 ++++++-- lib/OS/OS_Win32.h | 2 +- lib/OS/OS_Win32_NTFS.cpp | 5 +++++ src/Ex1.cpp | 4 +++- 11 files changed, 51 insertions(+), 12 deletions(-) diff --git a/lib/Base/Allocator.cpp b/lib/Base/Allocator.cpp index 0f0feee..b986c9a 100644 --- a/lib/Base/Allocator.cpp +++ b/lib/Base/Allocator.cpp @@ -16,3 +16,9 @@ void internal_free (void* memory) { allocator.proc(Allocator_Mode::DEALLOCATE, 0, 0, memory, allocator.data); } +void print_context_allocator () { + Assert(thread_context()->allocator.proc); + + char* result = (char*)thread_context()->allocator.proc(Allocator_Mode::DETAILS, 0, 0, nullptr, thread_context()->allocator.data); + log_none("Current allocator details: %s", result); +} diff --git a/lib/Base/Allocator.h b/lib/Base/Allocator.h index 3873826..bc17bf3 100644 --- a/lib/Base/Allocator.h +++ b/lib/Base/Allocator.h @@ -15,7 +15,7 @@ enum class Allocator_Mode: s32 { RESIZE = 1, DEALLOCATE = 2, // IS_THIS_YOURS = 3, - // DETAILS = 4, + DETAILS = 4, // #who_am_i }; typedef void* (*Allocator_Proc)(Allocator_Mode mode, s64 requested_size, s64 old_size, void* old_memory, void* allocator_data); @@ -119,3 +119,7 @@ template T* copy_struct (T* src) { T* dst = New(false); memcpy(dst, src, sizeof(T)); } + +// #TODO: Turn this into a macro that also provides the +// file, line number to the print. +void print_context_allocator (); \ No newline at end of file diff --git a/lib/Base/Arena.cpp b/lib/Base/Arena.cpp index b5d567e..3c7ed70 100644 --- a/lib/Base/Arena.cpp +++ b/lib/Base/Arena.cpp @@ -22,9 +22,15 @@ void* arena_allocator_proc (Allocator_Mode mode, s64 requested_size, s64 old_siz } return result; } break; - case Allocator_Mode::DEALLOCATE: + case Allocator_Mode::DEALLOCATE: { return nullptr; // unused - break; + } break; + case Allocator_Mode::DETAILS: { + if (allocator_data == nullptr) { + return "arena_allocator_proc: data pointer is null!"; + } + return "arena_allocator_proc"; + } break; } return nullptr; diff --git a/lib/Base/Array.h b/lib/Base/Array.h index 4693b78..0437436 100644 --- a/lib/Base/Array.h +++ b/lib/Base/Array.h @@ -122,7 +122,6 @@ template void array_reserve (Array& src, s64 desired_items) { if (desired_items <= src.allocated) return; - src.data = nullptr; if (src.allocator.proc == nullptr) { src.allocator = context_allocator(); } @@ -133,7 +132,7 @@ void array_reserve (Array& src, s64 desired_items) { Assert(src.data != nullptr); - src.allocated = desired_items; + src.allocated = desired_items; } template diff --git a/lib/Base/Base_Thread_Context.h b/lib/Base/Base_Thread_Context.h index c18f6bd..6a6cf52 100644 --- a/lib/Base/Base_Thread_Context.h +++ b/lib/Base/Base_Thread_Context.h @@ -51,3 +51,4 @@ struct Push_Allocator { context->allocator = old_allocator; } }; + diff --git a/lib/Base/ErrorType.cpp b/lib/Base/ErrorType.cpp index d7a4613..94514bd 100644 --- a/lib/Base/ErrorType.cpp +++ b/lib/Base/ErrorType.cpp @@ -213,7 +213,7 @@ void push_errors_to_parent_thread (Thread_Context* tctx) { } ArrayView get_all_errors (Thread_Context* tctx) { - Array error_array; + Array error_array = {}; // call with temp() recommended. Error* current_error = tctx->first_error; while (current_error) { diff --git a/lib/Base/Expandable_Arena.cpp b/lib/Base/Expandable_Arena.cpp index 3d16c39..dbff1ab 100644 --- a/lib/Base/Expandable_Arena.cpp +++ b/lib/Base/Expandable_Arena.cpp @@ -28,9 +28,21 @@ void* expandable_arena_allocator_proc (Allocator_Mode mode, s64 requested_size, memcpy(new_memory, old_memory, old_size); return new_memory; } break; - case Allocator_Mode::DEALLOCATE: + case Allocator_Mode::DEALLOCATE: { return nullptr; - break; + } break; + case Allocator_Mode::DETAILS: { + if (allocator_data == nullptr) { + return "expandable_arena_allocator_proc: data pointer is null!"; + } + if (thread_context()->temp == allocator_data) { + return "expandable_arena_allocator_proc: temp arena"; + } + if (thread_context()->arena == allocator_data) { + return "expandable_arena_allocator_proc: main arena"; + } + return "expandable_arena_allocator_proc: other arena"; + } break; } return nullptr; diff --git a/lib/Base/General_Purpose_Allocator.cpp b/lib/Base/General_Purpose_Allocator.cpp index f65abff..97bd555 100644 --- a/lib/Base/General_Purpose_Allocator.cpp +++ b/lib/Base/General_Purpose_Allocator.cpp @@ -158,9 +158,13 @@ void* GPAllocator_Proc (Allocator_Mode mode, s64 requested_size, s64 old_size, v } return result; } break; - case Allocator_Mode::DEALLOCATE: + case Allocator_Mode::DEALLOCATE: { GPAllocator_Delete(old_memory); // unused - break; + } break; + case Allocator_Mode::DETAILS: { + Assert(allocator_data == nullptr); + return "GPAllocator"; + } break; } return nullptr; diff --git a/lib/OS/OS_Win32.h b/lib/OS/OS_Win32.h index b15939b..7a02522 100644 --- a/lib/OS/OS_Win32.h +++ b/lib/OS/OS_Win32.h @@ -165,7 +165,7 @@ File_System Win32_filesystem_from_string (string s) { Assert(false); return File_System::Unknown; } -struct Dense_FS; // #Temp forward declare! +struct Dense_FS; // #hack forward declare! struct Win32_Drive { string label; string volume_name; diff --git a/lib/OS/OS_Win32_NTFS.cpp b/lib/OS/OS_Win32_NTFS.cpp index 5f19a3e..08cba71 100644 --- a/lib/OS/OS_Win32_NTFS.cpp +++ b/lib/OS/OS_Win32_NTFS.cpp @@ -205,6 +205,10 @@ Error* NTFS_MFT_read_raw (OS_Drive* drive) { Assert(data_attribute != nullptr); + // #dense_fs_alloc + Dense_FS* dfs = New(); + initialize(dfs, drive); + NTFS_RunHeader* dataRun = (NTFS_RunHeader*)((u8*)data_attribute + data_attribute->dataRunsOffset); u64 cluster_number = 0, records_processed = 0; @@ -273,6 +277,7 @@ Error* NTFS_MFT_read_raw (OS_Drive* drive) { // We need to get size from the data attribute // #TODO: #continue from here! + // See Dense_FS drive->data mft->file_count += 1; } } diff --git a/src/Ex1.cpp b/src/Ex1.cpp index aa389bf..0a52ad0 100644 --- a/src/Ex1.cpp +++ b/src/Ex1.cpp @@ -182,10 +182,11 @@ void Ex1_Control_Panel () { using namespace ImGui; for_each(t, ex1_ntfs.threads_in_flight) { if (thread_is_done(ex1_ntfs.threads_in_flight[t])) { push_allocator(GPAllocator()); + Thread* thread = ex1_ntfs.threads_in_flight[t]; auto task = thread_task(NTFS_Enumeration_Task); array_free(task->drives); - // internal_free( + // make sure to retreive any data you need to from here! release_arena(task->pool); @@ -200,6 +201,7 @@ void Ex1_Control_Panel () { using namespace ImGui; void ImGui_Debug_Panel () { using namespace ImGui; push_allocator(temp()); + Begin("Debug Panel"); SeparatorText("ex1_ntfs"); Text("Threads in flight count: %d", ex1_ntfs.threads_in_flight.count);