#pragma once #include "Base.h" #include "error-codes.h" enum class Allocator_Mode: s32 { ALLOCATE = 0, RESIZE = 1, DEALLOCATE = 2, // IS_THIS_YOURS = 3, // DETAILS = 4, }; typedef void* (*Allocator_Proc)(Allocator_Mode mode, s64 requested_size, s64 old_size, void* old_memory, void* allocator_data); struct Allocator { Allocator_Proc proc; void* data; }; // Public Allocator API: // Thread-local allocators: PROTOTYPING_API Allocator get_temp_allocator(); PROTOTYPING_API Allocator get_context_allocator(); // Note that alignment is handled on a per-allocator basis. void* internal_alloc (s64 size); void internal_free (void* memory); void* internal_realloc (void* memory, s64 size, s64 old_size); template void Initialize (T* memory) { (*memory) = T(); } template T* New (Allocator allocator, bool initialize=true) { auto memory = (T*)allocator.proc(Allocator_Mode::ALLOCATE, sizeof(T), 0, nullptr, allocator.data); if (initialize) { (*memory) = T(); } return memory; } template T* New (bool initialize=true) { auto memory = (T*)internal_alloc(sizeof(T)); if (initialize) { (*memory) = T(); } return memory; } void Delete (void* object) { internal_free(object); } // template T* NewArray(s64 count, bool initialize) // should be in Array.jai