Fixed bug when calling allocators for realloc/resize

This commit is contained in:
Musa Mahmood 2025-11-21 09:59:12 -05:00
parent 680f735b72
commit 998b4213fb
8 changed files with 44 additions and 21 deletions

View File

@ -1,14 +1,18 @@
[#TODO]
[ ] Context should include links to other living thread data (+ save dead threads in debug mode)
[ ] GetUnixTimestamp for timing and rdtsc for timing.
[ ] Thread primitives (Mutex, Semaphore, etc.)
[ ] Thread Creation / Deletion
[ ] Base layer and OS abstraction for Windows. (Ryan's Win32 platform layer is ~2k lines of code.)
- just implement the stuff we need
[ ] push_scratch() macro. (basically an auto_release_temp)
[ ] CreateWindow
[ ] ... Mouse / Keyboard inputs
[*] This is the order I want to follow, because I need them in thread context.
- Entry point(s) `Entry_Point_Main.cpp`
-> We can have a lib_init() instead of a clear entry point for now.
-> Switch from library to application once I add an entry point
-> See how rjf abstracts his entry points for each platform with TCTX.
-> We can have a lib_init() instead of a clear entry point for now.
-> Switch from library to application once I add an entry point
-> See how rjf abstracts his entry points for each platform with TCTX.
[ ] 1. setup thread-local storage via thread_static (see raddbg, base_core.h,
C_LINKAGE thread_static TCTX *tctx_thread_local;
>> Must be assigned at entry point (arena_alloc())
@ -24,8 +28,9 @@
[ ] Maybe we just shouldn't do this?
[ ] *our* program memory usage can be calculated by Stack Size + allocations from GPAllocator + allocations from arena.
[TODO - Low priority]
[ ] Fix implementation so all source is just in .cpp files (including templates), and auto-generate forward declares like Vjekoslav does.
[ ] Implement Secure Arenas (with VirtualLock, wiping memory with RtlSecureZeroMemory)
[ ]
[ ] Way to serialize and deserialize settings in Text (I think using binary is a mistake here?).
[Project Ideas]
[ ](Graphics): How do I create a basic overlay that shows the time, date, cpu/gpu temps, frequency, memory usage all the time and responds to global hotkey to show more info
[Documentation Notes]

View File

@ -9,7 +9,7 @@ void* internal_alloc (s64 size) {
void* internal_realloc (void* memory, s64 size, s64 old_size) {
Allocator allocator = get_context_allocator();
void* result = allocator.proc(Allocator_Mode::RESIZE, size, old_size, nullptr, allocator.data);
void* result = allocator.proc(Allocator_Mode::RESIZE, size, old_size, memory, allocator.data);
return result;
}

View File

@ -76,10 +76,26 @@ template <typename T> T* NewArray (s64 count, bool initialize=true) {
return memory;
}
// #TODO: allocator versions of resize and delete
template <typename T> T* Resize (Allocator allocator, void* memory, s64 size, s64 old_size, bool initialize=true) {
void* result = allocator.proc(Allocator_Mode::RESIZE, size, old_size, memory, allocator.data);
return result;
}
// template <typename T> void Delete (Allocator allocator, void* memory) {
// We use internal functions when we assume the user just wants to use the
// current allocator on the context.
// For Resizes and Deletes, use internal_realloc and internal_free.
// template typename<T> void reset_struct(T* src) { (*src) = T(); }
// template typename<T> void zero_struct(T* src) { memset(src, 0, sizeof(T)); }
// template typename<T> T* copy_struct(T* src) {
// T* dst = New<T>(false);
// memcpy(dst, src, sizeof(T));
// }
template <typename T> void reset_struct (T* src) {
(*src) = T();
}
template <typename T> void zero_struct(T* src) {
memset(src, 0, sizeof(T));
}
template <typename T> T* copy_struct(T* src) {
T* dst = New<T>(false);
memcpy(dst, src, sizeof(T));
}

View File

@ -19,6 +19,7 @@ enum class Arena_Reserve: u8 {
Size_64G = 4,
Size_2T = 5
};
enum class Arena_Flags: u8 {
None = 0,
Chained = 0x01,

View File

@ -144,7 +144,7 @@ void array_reserve(Array<T>& src, s64 desired_items) {
Assert(src.allocator.proc != nullptr);
src.data = (T*)src.allocator.proc(Allocator_Mode::RESIZE, desired_items * sizeof(T), src.allocated * sizeof(T), nullptr, src.allocator.data);
src.data = (T*)src.allocator.proc(Allocator_Mode::RESIZE, desired_items * sizeof(T), src.allocated * sizeof(T), src.data, src.allocator.data);
Assert(src.data != nullptr);

View File

@ -8,16 +8,19 @@ struct Arena; // #TEMP - Base_Thread_Context and Arena rely on each other, so I
// See Context_Base in jai, and TCTX in raddebugger:
struct Thread_Context {
Arena* temp; // Used for temporary allocations.
Arena* temp; // Used for temporary allocations, scratch space.
Arena* arena; // general purpose local arena
Allocator allocator;
s32 thread_idx;
u16 _padding0;
u16 GPAllocator_alignment = 16;
// Logger logger;
// Stack_Trace* stack_trace;
// #TODO: other debug information
s64 thread_idx;
// #TODO:
// Array<Thread*> threads_created; // maybe should be linked-list?
// Thread* thread_that_created_me = nullptr; // so we can remove from above array
string thread_name;
};

View File

@ -1,5 +1,3 @@
// Move into src/Win32.cpp
struct OS_System_Info {
s32 logical_processor_count;
s32 physical_core_count;

View File

@ -1,9 +1,9 @@
#include "unity_build_lib.cpp"
#include "Base_Entry_Point.cpp"
// #include "imgui-docking.cpp"
#include "src/OS_Win32.cpp"
// #include "src/OS_Linux.cpp"
// #include "src/OS_MacOS.cpp"
#include "src/OS_Win32.cpp" // should be in lib/
// #include "src/OS_Linux.cpp" // #TODO: Future.
// #include "src/OS_MacOS.cpp" // #TODO: Future.
#if OS_WINDOWS