Remove Allocator stuff
This commit is contained in:
162
base.cpp
162
base.cpp
@@ -475,33 +475,11 @@ operator!=(Intern_String a, Intern_String b){
|
||||
#define Iter_Named(list, it) for(auto it = iterate(list); should_we_continue(&it); advance(&it))
|
||||
#define Iter(list) Iter_Named(list, it)
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Base Allocator stuff
|
||||
//-----------------------------------------------------------------------------
|
||||
enum Allocation_Kind{
|
||||
Allocation_Alloc,
|
||||
Allocation_Resize,
|
||||
Allocation_FreeAll,
|
||||
Allocation_Free,
|
||||
Allocation_Destroy
|
||||
};
|
||||
|
||||
enum Allocator_Kind{
|
||||
Allocator_None,
|
||||
Allocator_Arena,
|
||||
Allocator_PersonalArena,
|
||||
Allocator_OSHeap,
|
||||
};
|
||||
|
||||
enum Alloc_Flag{
|
||||
AF_None,
|
||||
AF_ZeroMemory
|
||||
};
|
||||
|
||||
struct Allocator;
|
||||
typedef void *Allocator_Proc(Allocator*, Allocation_Kind, void *, size_t);
|
||||
struct Allocator{Allocator_Kind kind; Allocator_Proc *proc; String debug_name;};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Memory OS
|
||||
//-----------------------------------------------------------------------------
|
||||
@@ -520,15 +498,11 @@ function B32 os_decommit_pos(OS_Memory *m, size_t pos);
|
||||
global const size_t default_reserve_size = gib(4);
|
||||
global const size_t default_alignment = 8;
|
||||
global const size_t additional_commit_size = mib(1);
|
||||
struct Arena:Allocator{
|
||||
struct Arena{
|
||||
OS_Memory memory;
|
||||
size_t alignment;
|
||||
size_t len;
|
||||
|
||||
// Personal arena memes so we can compute correct size when resizing
|
||||
// Also a pointer so that we can make sure it didn't change
|
||||
size_t old_size;
|
||||
void *debug_prev_pointer;
|
||||
String debug_string;
|
||||
};
|
||||
function void arena_init(Arena *arena, String debug_name);
|
||||
|
||||
@@ -580,31 +554,11 @@ arena_push_size(Arena *a, size_t size, Alloc_Flag flags = AF_None){
|
||||
return result;
|
||||
}
|
||||
|
||||
force_inline void *
|
||||
arena_allocator_proc(Allocator *a, Allocation_Kind kind, void *old_pointer, size_t size){
|
||||
Arena *arena = (Arena *)a;
|
||||
switch(kind){
|
||||
case Allocation_Alloc: return arena_push_size(arena, size);
|
||||
case Allocation_Resize:{
|
||||
void *result = arena_push_size(arena, size);
|
||||
memory_copy(result, old_pointer, size);
|
||||
return result;
|
||||
}
|
||||
case Allocation_Free : return 0;
|
||||
case Allocation_FreeAll: arena_clear(arena); return 0;
|
||||
case Allocation_Destroy: arena_release(arena); return 0;
|
||||
}
|
||||
invalid_codepath;
|
||||
return 0;
|
||||
}
|
||||
|
||||
function void
|
||||
arena_init(Arena *a, String debug_name){
|
||||
a->memory = os_reserve(default_reserve_size);
|
||||
a->alignment = default_alignment;
|
||||
a->debug_name = debug_name;
|
||||
a->kind = Allocator_Arena;
|
||||
if(!a->proc) a->proc = arena_allocator_proc;
|
||||
a->debug_string = debug_name;
|
||||
}
|
||||
|
||||
enum Log_Kind{Log_Kind_Normal, Log_Kind_Error, Log_Kind_Trace};
|
||||
@@ -615,7 +569,6 @@ typedef void Log_Proc(Log_Kind kind, String string, char *file, int line);
|
||||
struct Thread_Ctx{
|
||||
Arena scratch[2];
|
||||
Log_Proc *log_proc;
|
||||
Allocator *implicit_alloc;
|
||||
int thread_index;
|
||||
|
||||
int line;
|
||||
@@ -640,7 +593,7 @@ struct Scratch{
|
||||
size_t saved_pos;
|
||||
Arena *arena;
|
||||
|
||||
Scratch(Allocator *conflict = 0){
|
||||
Scratch(Arena *conflict = 0){
|
||||
if(conflict == thread_ctx.scratch){
|
||||
arena = thread_ctx.scratch + 1;
|
||||
}
|
||||
@@ -653,7 +606,6 @@ struct Scratch{
|
||||
arena_pop_pos(arena, saved_pos);
|
||||
}
|
||||
force_inline operator Arena*(){ return arena; }
|
||||
force_inline operator Allocator*(){ return arena; }
|
||||
|
||||
// @Note: Disable copy constructors, cause it caused lots of confusing errors
|
||||
// Where it passed scratch instead of the arena into the constructor
|
||||
@@ -663,78 +615,6 @@ struct Scratch{
|
||||
Scratch(Scratch &arena, Scratch &a2);
|
||||
};
|
||||
|
||||
#define Set_Allocator(a) Scoped_Allocator JOIN(scoped_alloc, __LINE__)(a)
|
||||
struct Scoped_Allocator{
|
||||
Allocator *prev_allocator;
|
||||
Scoped_Allocator(Allocator *a){
|
||||
prev_allocator = thread_ctx.implicit_alloc;
|
||||
thread_ctx.implicit_alloc = a;
|
||||
}
|
||||
~Scoped_Allocator(){
|
||||
thread_ctx.implicit_alloc = prev_allocator;
|
||||
}
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Explicit allocator
|
||||
//-----------------------------------------------------------------------------
|
||||
#define exp_alloc_array(a, T, size,...) (T *)exp_alloc(a, sizeof(T)*(size),##__VA_ARGS__)
|
||||
#define exp_alloc_type(a, T, ...) exp_alloc_array(a, T, 1,##__VA_ARGS__)
|
||||
#define exp_alloc(a, size, ...) (report_file_and_line(), exp__alloc(a, size,##__VA_ARGS__))
|
||||
#define exp_resize(a,p,size) (report_file_and_line(), exp__resize(a, p, size))
|
||||
#define exp_resize_array(a, p, T, size) (report_file_and_line(), (T *)exp_resize(a, p, sizeof(T)*(size)))
|
||||
#define exp_free(a, p) (report_file_and_line(), exp__free(a, p))
|
||||
#define exp_free_all(a) (report_file_and_line(), exp__free_all(a))
|
||||
#define exp_destroy(a) (report_file_and_line(), exp__destroy(a))
|
||||
|
||||
force_inline void *
|
||||
exp__alloc(Allocator *a, size_t size, Alloc_Flag flag = AF_None){
|
||||
#if REPORT_ALLOCATIONS
|
||||
printf("Alloc(%s) %s:%d %u\n", a->debug_name.str, thread_ctx.file, thread_ctx.line, (U32)size);
|
||||
#endif
|
||||
|
||||
void *result = a->proc(a, Allocation_Alloc, 0, size);
|
||||
if(flag & AF_ZeroMemory) memory_zero(result, size);
|
||||
return result;
|
||||
}
|
||||
force_inline void *
|
||||
exp__resize(Allocator *a, void *pointer, size_t size){
|
||||
#if REPORT_ALLOCATIONS
|
||||
printf("Resize(%s) %s:%d %u\n", a->debug_name.str, thread_ctx.file, thread_ctx.line, (U32)size);
|
||||
#endif
|
||||
|
||||
return a->proc(a, Allocation_Resize, pointer, size);
|
||||
}
|
||||
force_inline void
|
||||
exp__free(Allocator *a, void *pointer){
|
||||
#if REPORT_ALLOCATIONS
|
||||
printf("Free(%s) %s:%d\n", a->debug_name.str, thread_ctx.file, thread_ctx.line);
|
||||
#endif
|
||||
|
||||
a->proc(a, Allocation_Free, pointer, 0);
|
||||
}
|
||||
force_inline void
|
||||
exp__free_all(Allocator *a){
|
||||
#if REPORT_ALLOCATIONS
|
||||
printf("FreeAll(%s) %s:%d\n", a->debug_name.str, thread_ctx.file, thread_ctx.line);
|
||||
#endif
|
||||
|
||||
a->proc(a, Allocation_FreeAll, 0, 0);
|
||||
}
|
||||
force_inline void
|
||||
exp__destroy(Allocator *a){
|
||||
#if REPORT_ALLOCATIONS
|
||||
printf("Destroy(%s) %s:%d\n", a->debug_name.str, thread_ctx.file, thread_ctx.line);
|
||||
#endif
|
||||
|
||||
a->proc(a, Allocation_Destroy, 0, 0);
|
||||
}
|
||||
force_inline Allocator *
|
||||
imp_get(){
|
||||
assert(thread_ctx.implicit_alloc);
|
||||
return thread_ctx.implicit_alloc;
|
||||
}
|
||||
|
||||
function void
|
||||
thread_ctx_init(){
|
||||
arena_init(thread_ctx.scratch, "Scratch1"_s);
|
||||
@@ -742,13 +622,12 @@ thread_ctx_init(){
|
||||
arena_init(&pernament_arena, "Pernament Arena"_s);
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Array
|
||||
//-----------------------------------------------------------------------------
|
||||
template<class T>
|
||||
struct Array{
|
||||
Allocator *allocator;
|
||||
Arena *allocator;
|
||||
T *data;
|
||||
S64 cap;
|
||||
S64 len;
|
||||
@@ -768,14 +647,15 @@ struct Array{
|
||||
|
||||
void grow(S64 required_size){
|
||||
if(cap == 0){
|
||||
if(!allocator) allocator = imp_get();
|
||||
S64 new_cap = max(required_size*2, (S64)16);
|
||||
data = exp_alloc_array(allocator, T, new_cap);
|
||||
data = arena_push_array(allocator, T, new_cap);
|
||||
cap = new_cap;
|
||||
}
|
||||
else if(len + required_size > cap){
|
||||
U64 new_cap = max(cap * 2, len+required_size+1);
|
||||
data = exp_resize_array(allocator, data, T, new_cap);
|
||||
T *new_data = arena_push_array(allocator, T, new_cap);
|
||||
memory_copy(new_data, data, cap*sizeof(T));
|
||||
data = new_data;
|
||||
cap = new_cap;
|
||||
}
|
||||
}
|
||||
@@ -810,28 +690,28 @@ struct Array{
|
||||
*item = data[--len];
|
||||
}
|
||||
|
||||
void init(Allocator *a, S64 size = 16){
|
||||
void init(Arena *a, S64 size = 16){
|
||||
allocator = a;
|
||||
data = exp_alloc_array(a, T, size);
|
||||
data = arena_push_array(a, T, size);
|
||||
cap = size;
|
||||
}
|
||||
|
||||
Array<T> copy(Allocator *a){
|
||||
Array<T> copy(Arena *a){
|
||||
Array<T> result = {};
|
||||
result.len = len;
|
||||
result.cap = len*2;
|
||||
result.allocator = a;
|
||||
result.data = exp_alloc_array(a, T, result.cap);
|
||||
result.data = arena_push_array(a, T, result.cap);
|
||||
memory_copy(result.data, data, sizeof(T)*result.len);
|
||||
return result;
|
||||
}
|
||||
|
||||
Array<T> tight_copy(Allocator *a){
|
||||
Array<T> tight_copy(Arena *a){
|
||||
Array<T> result = {};
|
||||
result.len = len;
|
||||
result.cap = len;
|
||||
result.allocator = 0;
|
||||
result.data = exp_alloc_array(a, T, len);
|
||||
result.data = arena_push_array(a, T, len);
|
||||
memory_copy(result.data, data, sizeof(T)*len);
|
||||
return result;
|
||||
}
|
||||
@@ -845,7 +725,6 @@ struct Array{
|
||||
force_inline T *end () { return data + len; }
|
||||
force_inline T &operator[](S64 i){ assert(i >= 0 && i < cap); return data[i]; }
|
||||
|
||||
|
||||
struct Array_Iter{
|
||||
Array<T> *array;
|
||||
S64 i;
|
||||
@@ -863,7 +742,7 @@ struct Array{
|
||||
|
||||
template<class T>
|
||||
function Array<T>
|
||||
array_make(Allocator *a, S64 size = 16){
|
||||
array_make(Arena *a, S64 size = 16){
|
||||
Array<T> result = {};
|
||||
result.init(a, size);
|
||||
return result;
|
||||
@@ -1088,7 +967,7 @@ intern_string(Intern_Table *t, String string){
|
||||
return result;
|
||||
}
|
||||
|
||||
S64 *len_address = (S64 *)exp_alloc(t->string_allocator, string.len+1+sizeof(S64));
|
||||
S64 *len_address = (S64 *)arena_push_size(t->string_allocator, string.len+1+sizeof(S64));
|
||||
*len_address = string.len;
|
||||
|
||||
U8 *string_address = (U8 *)(len_address + 1);
|
||||
@@ -1113,16 +992,13 @@ test_intern_table(){
|
||||
}
|
||||
|
||||
function Arena
|
||||
arena_sub(Allocator *base, size_t size, String debug_name) {
|
||||
arena_sub(Arena *base, size_t size, String debug_name) {
|
||||
Arena result = {};
|
||||
result.memory.data = (U8 *)exp_alloc(base, size);
|
||||
result.memory.data = (U8 *)arena_push_size(base, size);
|
||||
result.memory.commit = size;
|
||||
result.memory.reserve = size;
|
||||
result.alignment = default_alignment;
|
||||
result.len = 0;
|
||||
result.debug_name = debug_name;
|
||||
result.kind = Allocator_Arena;
|
||||
if(!result.proc) result.proc = arena_allocator_proc;
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user