Fix really stupid bug in Array's array_reserve

This commit is contained in:
Musa Mahmood 2025-12-07 10:24:29 -05:00
parent 5c18e6f4da
commit 046d86423a
11 changed files with 51 additions and 12 deletions

View File

@ -16,3 +16,9 @@ void internal_free (void* memory) {
allocator.proc(Allocator_Mode::DEALLOCATE, 0, 0, memory, allocator.data); 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);
}

View File

@ -15,7 +15,7 @@ enum class Allocator_Mode: s32 {
RESIZE = 1, RESIZE = 1,
DEALLOCATE = 2, DEALLOCATE = 2,
// IS_THIS_YOURS = 3, // 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); typedef void* (*Allocator_Proc)(Allocator_Mode mode, s64 requested_size, s64 old_size, void* old_memory, void* allocator_data);
@ -119,3 +119,7 @@ template <typename T> T* copy_struct (T* src) {
T* dst = New<T>(false); T* dst = New<T>(false);
memcpy(dst, src, sizeof(T)); 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 ();

View File

@ -22,9 +22,15 @@ void* arena_allocator_proc (Allocator_Mode mode, s64 requested_size, s64 old_siz
} }
return result; return result;
} break; } break;
case Allocator_Mode::DEALLOCATE: case Allocator_Mode::DEALLOCATE: {
return nullptr; // unused 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; return nullptr;

View File

@ -122,7 +122,6 @@ template <typename T>
void array_reserve (Array<T>& src, s64 desired_items) { void array_reserve (Array<T>& src, s64 desired_items) {
if (desired_items <= src.allocated) return; if (desired_items <= src.allocated) return;
src.data = nullptr;
if (src.allocator.proc == nullptr) { if (src.allocator.proc == nullptr) {
src.allocator = context_allocator(); src.allocator = context_allocator();
} }
@ -133,7 +132,7 @@ void array_reserve (Array<T>& src, s64 desired_items) {
Assert(src.data != nullptr); Assert(src.data != nullptr);
src.allocated = desired_items; src.allocated = desired_items;
} }
template <typename T> template <typename T>

View File

@ -51,3 +51,4 @@ struct Push_Allocator {
context->allocator = old_allocator; context->allocator = old_allocator;
} }
}; };

View File

@ -213,7 +213,7 @@ void push_errors_to_parent_thread (Thread_Context* tctx) {
} }
ArrayView<Error*> get_all_errors (Thread_Context* tctx) { ArrayView<Error*> get_all_errors (Thread_Context* tctx) {
Array<Error*> error_array; Array<Error*> error_array = {};
// call with temp() recommended. // call with temp() recommended.
Error* current_error = tctx->first_error; Error* current_error = tctx->first_error;
while (current_error) { while (current_error) {

View File

@ -28,9 +28,21 @@ void* expandable_arena_allocator_proc (Allocator_Mode mode, s64 requested_size,
memcpy(new_memory, old_memory, old_size); memcpy(new_memory, old_memory, old_size);
return new_memory; return new_memory;
} break; } break;
case Allocator_Mode::DEALLOCATE: case Allocator_Mode::DEALLOCATE: {
return nullptr; 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; return nullptr;

View File

@ -158,9 +158,13 @@ void* GPAllocator_Proc (Allocator_Mode mode, s64 requested_size, s64 old_size, v
} }
return result; return result;
} break; } break;
case Allocator_Mode::DEALLOCATE: case Allocator_Mode::DEALLOCATE: {
GPAllocator_Delete(old_memory); // unused GPAllocator_Delete(old_memory); // unused
break; } break;
case Allocator_Mode::DETAILS: {
Assert(allocator_data == nullptr);
return "GPAllocator";
} break;
} }
return nullptr; return nullptr;

View File

@ -165,7 +165,7 @@ File_System Win32_filesystem_from_string (string s) {
Assert(false); Assert(false);
return File_System::Unknown; return File_System::Unknown;
} }
struct Dense_FS; // #Temp forward declare! struct Dense_FS; // #hack forward declare!
struct Win32_Drive { struct Win32_Drive {
string label; string label;
string volume_name; string volume_name;

View File

@ -205,6 +205,10 @@ Error* NTFS_MFT_read_raw (OS_Drive* drive) {
Assert(data_attribute != nullptr); Assert(data_attribute != nullptr);
// #dense_fs_alloc
Dense_FS* dfs = New<Dense_FS>();
initialize(dfs, drive);
NTFS_RunHeader* dataRun = (NTFS_RunHeader*)((u8*)data_attribute + data_attribute->dataRunsOffset); NTFS_RunHeader* dataRun = (NTFS_RunHeader*)((u8*)data_attribute + data_attribute->dataRunsOffset);
u64 cluster_number = 0, records_processed = 0; 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 // We need to get size from the data attribute
// #TODO: #continue from here! // #TODO: #continue from here!
// See Dense_FS drive->data
mft->file_count += 1; mft->file_count += 1;
} }
} }

View File

@ -182,10 +182,11 @@ void Ex1_Control_Panel () { using namespace ImGui;
for_each(t, ex1_ntfs.threads_in_flight) { for_each(t, ex1_ntfs.threads_in_flight) {
if (thread_is_done(ex1_ntfs.threads_in_flight[t])) { if (thread_is_done(ex1_ntfs.threads_in_flight[t])) {
push_allocator(GPAllocator()); push_allocator(GPAllocator());
Thread* thread = ex1_ntfs.threads_in_flight[t]; Thread* thread = ex1_ntfs.threads_in_flight[t];
auto task = thread_task(NTFS_Enumeration_Task); auto task = thread_task(NTFS_Enumeration_Task);
array_free(task->drives); array_free(task->drives);
// internal_free(
// make sure to retreive any data you need to from here! // make sure to retreive any data you need to from here!
release_arena(task->pool); release_arena(task->pool);
@ -200,6 +201,7 @@ void Ex1_Control_Panel () { using namespace ImGui;
void ImGui_Debug_Panel () { using namespace ImGui; void ImGui_Debug_Panel () { using namespace ImGui;
push_allocator(temp()); push_allocator(temp());
Begin("Debug Panel"); Begin("Debug Panel");
SeparatorText("ex1_ntfs"); SeparatorText("ex1_ntfs");
Text("Threads in flight count: %d", ex1_ntfs.threads_in_flight.count); Text("Threads in flight count: %d", ex1_ntfs.threads_in_flight.count);