Remove Allocator stuff

This commit is contained in:
Krzosa Karol
2022-10-10 10:22:04 +02:00
parent 2f153a7cd3
commit 7aa0ba56b6
14 changed files with 69 additions and 192 deletions

162
base.cpp
View File

@@ -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_Named(list, it) for(auto it = iterate(list); should_we_continue(&it); advance(&it))
#define Iter(list) Iter_Named(list, 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{ enum Alloc_Flag{
AF_None, AF_None,
AF_ZeroMemory 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 // 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_reserve_size = gib(4);
global const size_t default_alignment = 8; global const size_t default_alignment = 8;
global const size_t additional_commit_size = mib(1); global const size_t additional_commit_size = mib(1);
struct Arena:Allocator{ struct Arena{
OS_Memory memory; OS_Memory memory;
size_t alignment; size_t alignment;
size_t len; size_t len;
String debug_string;
// 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;
}; };
function void arena_init(Arena *arena, String debug_name); 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; 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 function void
arena_init(Arena *a, String debug_name){ arena_init(Arena *a, String debug_name){
a->memory = os_reserve(default_reserve_size); a->memory = os_reserve(default_reserve_size);
a->alignment = default_alignment; a->alignment = default_alignment;
a->debug_name = debug_name; a->debug_string = debug_name;
a->kind = Allocator_Arena;
if(!a->proc) a->proc = arena_allocator_proc;
} }
enum Log_Kind{Log_Kind_Normal, Log_Kind_Error, Log_Kind_Trace}; 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{ struct Thread_Ctx{
Arena scratch[2]; Arena scratch[2];
Log_Proc *log_proc; Log_Proc *log_proc;
Allocator *implicit_alloc;
int thread_index; int thread_index;
int line; int line;
@@ -640,7 +593,7 @@ struct Scratch{
size_t saved_pos; size_t saved_pos;
Arena *arena; Arena *arena;
Scratch(Allocator *conflict = 0){ Scratch(Arena *conflict = 0){
if(conflict == thread_ctx.scratch){ if(conflict == thread_ctx.scratch){
arena = thread_ctx.scratch + 1; arena = thread_ctx.scratch + 1;
} }
@@ -653,7 +606,6 @@ struct Scratch{
arena_pop_pos(arena, saved_pos); arena_pop_pos(arena, saved_pos);
} }
force_inline operator Arena*(){ return arena; } force_inline operator Arena*(){ return arena; }
force_inline operator Allocator*(){ return arena; }
// @Note: Disable copy constructors, cause it caused lots of confusing errors // @Note: Disable copy constructors, cause it caused lots of confusing errors
// Where it passed scratch instead of the arena into the constructor // Where it passed scratch instead of the arena into the constructor
@@ -663,78 +615,6 @@ struct Scratch{
Scratch(Scratch &arena, Scratch &a2); 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 function void
thread_ctx_init(){ thread_ctx_init(){
arena_init(thread_ctx.scratch, "Scratch1"_s); arena_init(thread_ctx.scratch, "Scratch1"_s);
@@ -742,13 +622,12 @@ thread_ctx_init(){
arena_init(&pernament_arena, "Pernament Arena"_s); arena_init(&pernament_arena, "Pernament Arena"_s);
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// Array // Array
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template<class T> template<class T>
struct Array{ struct Array{
Allocator *allocator; Arena *allocator;
T *data; T *data;
S64 cap; S64 cap;
S64 len; S64 len;
@@ -768,14 +647,15 @@ struct Array{
void grow(S64 required_size){ void grow(S64 required_size){
if(cap == 0){ if(cap == 0){
if(!allocator) allocator = imp_get();
S64 new_cap = max(required_size*2, (S64)16); 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; cap = new_cap;
} }
else if(len + required_size > cap){ else if(len + required_size > cap){
U64 new_cap = max(cap * 2, len+required_size+1); 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; cap = new_cap;
} }
} }
@@ -810,28 +690,28 @@ struct Array{
*item = data[--len]; *item = data[--len];
} }
void init(Allocator *a, S64 size = 16){ void init(Arena *a, S64 size = 16){
allocator = a; allocator = a;
data = exp_alloc_array(a, T, size); data = arena_push_array(a, T, size);
cap = size; cap = size;
} }
Array<T> copy(Allocator *a){ Array<T> copy(Arena *a){
Array<T> result = {}; Array<T> result = {};
result.len = len; result.len = len;
result.cap = len*2; result.cap = len*2;
result.allocator = a; 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); memory_copy(result.data, data, sizeof(T)*result.len);
return result; return result;
} }
Array<T> tight_copy(Allocator *a){ Array<T> tight_copy(Arena *a){
Array<T> result = {}; Array<T> result = {};
result.len = len; result.len = len;
result.cap = len; result.cap = len;
result.allocator = 0; 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); memory_copy(result.data, data, sizeof(T)*len);
return result; return result;
} }
@@ -845,7 +725,6 @@ struct Array{
force_inline T *end () { return data + len; } force_inline T *end () { return data + len; }
force_inline T &operator[](S64 i){ assert(i >= 0 && i < cap); return data[i]; } force_inline T &operator[](S64 i){ assert(i >= 0 && i < cap); return data[i]; }
struct Array_Iter{ struct Array_Iter{
Array<T> *array; Array<T> *array;
S64 i; S64 i;
@@ -863,7 +742,7 @@ struct Array{
template<class T> template<class T>
function Array<T> function Array<T>
array_make(Allocator *a, S64 size = 16){ array_make(Arena *a, S64 size = 16){
Array<T> result = {}; Array<T> result = {};
result.init(a, size); result.init(a, size);
return result; return result;
@@ -1088,7 +967,7 @@ intern_string(Intern_Table *t, String string){
return result; 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; *len_address = string.len;
U8 *string_address = (U8 *)(len_address + 1); U8 *string_address = (U8 *)(len_address + 1);
@@ -1113,16 +992,13 @@ test_intern_table(){
} }
function Arena function Arena
arena_sub(Allocator *base, size_t size, String debug_name) { arena_sub(Arena *base, size_t size, String debug_name) {
Arena result = {}; 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.commit = size;
result.memory.reserve = size; result.memory.reserve = size;
result.alignment = default_alignment; result.alignment = default_alignment;
result.len = 0; result.len = 0;
result.debug_name = debug_name;
result.kind = Allocator_Arena;
if(!result.proc) result.proc = arena_allocator_proc;
return result; return result;
} }

View File

@@ -84,13 +84,13 @@ string_copy(Arena *a, String string){
} }
function String function String
string_fmtv(Allocator *a, const char *str, va_list args1) { string_fmtv(Arena *a, const char *str, va_list args1) {
va_list args2; va_list args2;
va_copy(args2, args1); va_copy(args2, args1);
S64 len = stbsp_vsnprintf(0, 0, str, args2); S64 len = stbsp_vsnprintf(0, 0, str, args2);
va_end(args2); va_end(args2);
char *result = exp_alloc_array(a, char, len + 1); char *result = arena_push_array(a, char, len + 1);
stbsp_vsnprintf(result, len + 1, str, args1); stbsp_vsnprintf(result, len + 1, str, args1);
String res = {(U8 *)result, len}; String res = {(U8 *)result, len};
@@ -104,7 +104,7 @@ String result = string_fmtv(alloc, str, args1); \
va_end(args1) va_end(args1)
function String function String
string_fmt(Allocator *a, const char *str, ...) { string_fmt(Arena *a, const char *str, ...) {
STRING_FMT(a, str, result); STRING_FMT(a, str, result);
return result; return result;
} }
@@ -120,7 +120,7 @@ struct String_Builder_Block{
}; };
struct String_Builder{ struct String_Builder{
Allocator *allocator; Arena *allocator;
String_Builder_Block *first_free; String_Builder_Block *first_free;
String_Builder_Block *first; String_Builder_Block *first;
String_Builder_Block *last; String_Builder_Block *last;
@@ -147,7 +147,7 @@ struct String_Builder{
block = first_free; block = first_free;
first_free = first_free->next; first_free = first_free->next;
} else{ } else{
block = (String_Builder_Block *)exp_alloc(allocator, sizeof(String_Builder_Block) + size); block = (String_Builder_Block *)arena_push_size(allocator, sizeof(String_Builder_Block) + size);
} }
memory_zero(block, sizeof(String_Builder_Block)+1); // Also clear first byte of character data memory_zero(block, sizeof(String_Builder_Block)+1); // Also clear first byte of character data
block->cap = size; block->cap = size;
@@ -203,7 +203,7 @@ struct String_Builder{
}; };
function String_Builder function String_Builder
string_builder_make(Allocator *a, S64 first_block_size = 4096){ string_builder_make(Arena *a, S64 first_block_size = 4096){
String_Builder sb = {a}; String_Builder sb = {a};
sb.init(first_block_size); sb.init(first_block_size);
return sb; return sb;
@@ -215,7 +215,7 @@ enum String_Builder_Flag{
}; };
function String function String
string_flatten(Allocator *a, String_Builder *b, String_Builder_Flag flags = String_Builder_Flag_None){ string_flatten(Arena *a, String_Builder *b, String_Builder_Flag flags = String_Builder_Flag_None){
// @Note(Krzosa): Compute size to allocate // @Note(Krzosa): Compute size to allocate
S64 size = 1; S64 size = 1;
if(is_flag_set(flags, String_Builder_Flag_AddSize)) size += sizeof(size_t); if(is_flag_set(flags, String_Builder_Flag_AddSize)) size += sizeof(size_t);
@@ -224,7 +224,7 @@ string_flatten(Allocator *a, String_Builder *b, String_Builder_Flag flags = Stri
} }
String result = {}; String result = {};
result.str = (U8 *)exp_alloc(a, size); result.str = (U8 *)arena_push_size(a, size);
if(is_flag_set(flags, String_Builder_Flag_AddSize)) { if(is_flag_set(flags, String_Builder_Flag_AddSize)) {
memory_copy(result.str + result.len, &size, sizeof(S64)); memory_copy(result.str + result.len, &size, sizeof(S64));
result.len += sizeof(S64); result.len += sizeof(S64);

View File

@@ -159,8 +159,8 @@ utf16_to_utf32(U16 *c, S32 max_advance) {
} }
function String32 function String32
string16_to_string32(Allocator *allocator, String16 string){ string16_to_string32(Arena *allocator, String16 string){
String32 result = {exp_alloc_array(allocator, U32, string.len+1)}; String32 result = {arena_push_array(allocator, U32, string.len+1)};
for(S64 i = 0; i < string.len;){ for(S64 i = 0; i < string.len;){
UTF32_Result decode = utf16_to_utf32(string.str + i, string.len - i); UTF32_Result decode = utf16_to_utf32(string.str + i, string.len - i);
if(!decode.error){ if(!decode.error){
@@ -175,8 +175,8 @@ string16_to_string32(Allocator *allocator, String16 string){
} }
function String32 function String32
string8_to_string32(Allocator *allocator, String string){ string8_to_string32(Arena *allocator, String string){
String32 result = {exp_alloc_array(allocator, U32, string.len+1)}; String32 result = {arena_push_array(allocator, U32, string.len+1)};
for(S64 i = 0; i < string.len;){ for(S64 i = 0; i < string.len;){
UTF32_Result decode = utf8_to_utf32(string.str + i, string.len - i); UTF32_Result decode = utf8_to_utf32(string.str + i, string.len - i);
if(!decode.error){ if(!decode.error){
@@ -190,8 +190,8 @@ string8_to_string32(Allocator *allocator, String string){
} }
function String16 function String16
string8_to_string16(Allocator *allocator, String in){ string8_to_string16(Arena *allocator, String in){
String16 result = {exp_alloc_array(allocator, U16, (in.len*2)+1)}; // @Note(Krzosa): Should be more then enough space String16 result = {arena_push_array(allocator, U16, (in.len*2)+1)}; // @Note(Krzosa): Should be more then enough space
for(S64 i = 0; i < in.len;){ for(S64 i = 0; i < in.len;){
UTF32_Result decode = utf8_to_utf32(in.str + i, in.len - i); UTF32_Result decode = utf8_to_utf32(in.str + i, in.len - i);
if(!decode.error){ if(!decode.error){
@@ -212,8 +212,8 @@ string8_to_string16(Allocator *allocator, String in){
} }
function String function String
string16_to_string8(Allocator *allocator, String16 in){ string16_to_string8(Arena *allocator, String16 in){
String result = {exp_alloc_array(allocator, U8, in.len*4+1)}; String result = {arena_push_array(allocator, U8, in.len*4+1)};
for(S64 i = 0; i < in.len;){ for(S64 i = 0; i < in.len;){
UTF32_Result decode = utf16_to_utf32(in.str + i, in.len - i); UTF32_Result decode = utf16_to_utf32(in.str + i, in.len - i);
if(!decode.error){ if(!decode.error){
@@ -266,8 +266,8 @@ string16_from_widechar(wchar_t *string){
} }
function String function String
string16_copy(Allocator *a, String string){ string16_copy(Arena *a, String string){
U8 *copy = exp_alloc_array(a, U8, string.len+1); U8 *copy = arena_push_array(a, U8, string.len+1);
memory_copy(copy, string.str, string.len); memory_copy(copy, string.str, string.len);
copy[string.len] = 0; copy[string.len] = 0;
return String{copy, string.len}; return String{copy, string.len};

View File

@@ -3,11 +3,11 @@
// a copy of which can be found in the LICENSE file. // a copy of which can be found in the LICENSE file.
#include <string.h> #include <string.h>
#define Set_BigInt_Allocator(x) BigInt_Allocator bigint_allocator(x) #define Set_BigInt_Arena(x) BigInt_Arena bigint_allocator(x)
struct BigInt_Allocator{ struct BigInt_Arena{
Allocator *old; Arena *old;
BigInt_Allocator(Allocator *allocator){old = bigint_allocator; bigint_allocator = allocator;} BigInt_Arena(Arena *allocator){old = bigint_allocator; bigint_allocator = allocator;}
~BigInt_Allocator(){bigint_allocator = old;} ~BigInt_Arena(){bigint_allocator = old;}
}; };
function BigInt function BigInt
@@ -39,7 +39,7 @@ bigint_add(const BigInt *a, const BigInt *b){
} }
function BigInt function BigInt
bigint_copy(Allocator *allocator, BigInt *src){ bigint_copy(Arena *allocator, BigInt *src){
BigInt dest = {}; BigInt dest = {};
if (src->digit_count == 0){ if (src->digit_count == 0){
bigint_init_unsigned(&dest, 0); bigint_init_unsigned(&dest, 0);
@@ -55,7 +55,7 @@ bigint_copy(Allocator *allocator, BigInt *src){
dest.digit_count = src->digit_count; dest.digit_count = src->digit_count;
count_bigint_alloc(); count_bigint_alloc();
dest.digits = exp_alloc_array(allocator, uint64_t, dest.digit_count, AF_ZeroMemory); dest.digits = arena_push_array(allocator, uint64_t, dest.digit_count, AF_ZeroMemory);
memcpy(dest.digits, src->digits, sizeof(uint64_t) * dest.digit_count); memcpy(dest.digits, src->digits, sizeof(uint64_t) * dest.digit_count);
return dest; return dest;
} }
@@ -1925,9 +1925,9 @@ void bigint_print(BigInt *bigint, uint64_t base)
} }
} }
const char *bigint_to_error_string(Allocator *allocator, const BigInt *bigint, uint64_t base) const char *bigint_to_error_string(Arena *allocator, const BigInt *bigint, uint64_t base)
{ {
Set_BigInt_Allocator(allocator); Set_BigInt_Arena(allocator);
if (bigint->digit_count == 0) if (bigint->digit_count == 0)
{ {
return "0"; return "0";
@@ -1948,7 +1948,7 @@ const char *bigint_to_error_string(Allocator *allocator, const BigInt *bigint, u
return res; return res;
} }
size_t len = bigint->digit_count * 64; size_t len = bigint->digit_count * 64;
char *start = (char *)exp_alloc(allocator, len); char *start = (char *)arena_push_size(allocator, len);
char *buf = start; char *buf = start;
BigInt digit_bi = { 0 }; BigInt digit_bi = { 0 };
@@ -1980,7 +1980,7 @@ const char *bigint_to_error_string(Allocator *allocator, const BigInt *bigint, u
} }
// reverse // reverse
char *out = (char *)exp_alloc(allocator, buf - start + 2); char *out = (char *)arena_push_size(allocator, buf - start + 2);
char *current = out; char *current = out;
if (bigint->is_negative) if (bigint->is_negative)
{ {

View File

@@ -18,12 +18,12 @@ enum CmpRes
}; };
#define count_bigint_alloc() (bigint_allocator != thread_ctx.scratch ? bigint_allocation_count++ : 0) #define count_bigint_alloc() (bigint_allocator != thread_ctx.scratch ? bigint_allocation_count++ : 0)
#define malloc_arena(x) (count_bigint_alloc(), exp_alloc(bigint_allocator, x, AF_ZeroMemory)) #define malloc_arena(x) (count_bigint_alloc(), arena_push_size(bigint_allocator, x, AF_ZeroMemory))
#define ALLOC_DIGITS(_digits) (uint64_t *)((_digits) ? malloc_arena(sizeof(uint64_t) * (_digits)) : NULL) #define ALLOC_DIGITS(_digits) (uint64_t *)((_digits) ? malloc_arena(sizeof(uint64_t) * (_digits)) : NULL)
#define FATAL_ERROR(x) compiler_error(0, x) #define FATAL_ERROR(x) compiler_error(0, x)
function void compiler_error(Token *token, const char *str, ...); function void compiler_error(Token *token, const char *str, ...);
const char *bigint_to_error_string(Allocator *allocator, const BigInt *bigint, uint64_t base); const char *bigint_to_error_string(Arena *allocator, const BigInt *bigint, uint64_t base);
void bigint_init_unsigned(BigInt *big_int, uint64_t value); void bigint_init_unsigned(BigInt *big_int, uint64_t value);
void bigint_init_signed(BigInt *big_int, int64_t value); void bigint_init_signed(BigInt *big_int, int64_t value);
void bigint_init_bigint(BigInt *dest, const BigInt *src); void bigint_init_bigint(BigInt *dest, const BigInt *src);

View File

@@ -470,7 +470,7 @@ ast_array(Token *pos, Ast_Expr *expr){
} }
function Ast_Scope * function Ast_Scope *
begin_decl_scope(Allocator *scratch, Token *pos){ begin_decl_scope(Arena *scratch, Token *pos){
AST_NEW(Scope, SCOPE, pos, AST_DECL); AST_NEW(Scope, SCOPE, pos, AST_DECL);
result->file = pctx->currently_parsed_file; result->file = pctx->currently_parsed_file;
result->module = pctx->currently_parsed_file->module; result->module = pctx->currently_parsed_file->module;
@@ -555,7 +555,7 @@ ast_type(Token *pos, Intern_String name, Ast_Type *type){
} }
function Ast_Scope * function Ast_Scope *
ast_decl_scope(Token *pos, Allocator *allocator, Ast_File *file){ ast_decl_scope(Token *pos, Arena *allocator, Ast_File *file){
AST_NEW(Scope, SCOPE, pos, AST_DECL); AST_NEW(Scope, SCOPE, pos, AST_DECL);
result->file = file; result->file = file;

View File

@@ -99,7 +99,7 @@ parse_init(Parse_Ctx *ctx, Arena *perm_allocator){
function void function void
begin_compilation(){ begin_compilation(){
init_ctx_time_begin = os_time(); init_ctx_time_begin = os_time();
Parse_Ctx *ctx = exp_alloc_type(&pernament_arena, Parse_Ctx, AF_ZeroMemory); Parse_Ctx *ctx = arena_push_type(&pernament_arena, Parse_Ctx, AF_ZeroMemory);
parse_init(ctx, &pernament_arena); parse_init(ctx, &pernament_arena);
init_ctx_time_end = os_time(); init_ctx_time_end = os_time();
} }

View File

@@ -134,7 +134,7 @@ struct Lex_Stream{
}; };
struct Lexer{ struct Lexer{
Allocator *arena; Arena *arena;
Lex_Stream stream; Lex_Stream stream;
Array<Token> tokens; Array<Token> tokens;
Intern_Table interns; Intern_Table interns;

View File

@@ -6,7 +6,7 @@ global String single_header_library_name = ""_s;
thread_local Parse_Ctx *pctx; thread_local Parse_Ctx *pctx;
Allocator *bigint_allocator; Arena *bigint_allocator;
global S64 bigint_allocation_count; global S64 bigint_allocation_count;
global Token token_null = {SAME_SCOPE}; global Token token_null = {SAME_SCOPE};

View File

@@ -81,7 +81,7 @@ token_error(Token *t, String error_val){
function void function void
lex_parse_u64(Lexer *lexer, Token *t, S64 base){ lex_parse_u64(Lexer *lexer, Token *t, S64 base){
Scratch scratch; Scratch scratch;
Set_BigInt_Allocator(scratch); Set_BigInt_Arena(scratch);
t->kind = TK_Integer; t->kind = TK_Integer;
BigInt m = bigint_u64(1); BigInt m = bigint_u64(1);

View File

@@ -14,6 +14,7 @@ Current:
Memory: Memory:
- [ ] Redesign Type map to use List and reduce wasting space - [ ] Redesign Type map to use List and reduce wasting space
- [ ] Redesign lexing to minimize memory usage, we got rid of heap but in a naive way! - [ ] Redesign lexing to minimize memory usage, we got rid of heap but in a naive way!
- [ ] Probably need to move all the global data into the context if we want to use this as library
In the future In the future

View File

@@ -43,6 +43,6 @@ enum{
function Operand resolve_expr(Ast_Expr *ast, Resolve_Flag flags, Ast_Type *compound_context, Ast_Scope *field_access_scope); function Operand resolve_expr(Ast_Expr *ast, Resolve_Flag flags, Ast_Type *compound_context, Ast_Scope *field_access_scope);
function void resolve_decl(Ast_Decl *ast); function void resolve_decl(Ast_Decl *ast);
function Ast_Decl *resolve_name(Ast_Scope *parent_scope, Token *pos, Intern_String name, Search_Flag search_flags = 0); function Ast_Decl *resolve_name(Ast_Scope *parent_scope, Token *pos, Intern_String name, Search_Flag search_flags = 0);
function String gen_string_simple_decl(Allocator *a, Ast_Type *ast, String name = {}, Ast_Scope *scope = 0, bool scope_names = true); function String gen_string_simple_decl(Arena *a, Ast_Type *ast, String name = {}, Ast_Scope *scope = 0, bool scope_names = true);
#define CASE(kind,type) case AST_##kind: { Ast_##type *node = (Ast_##type *)ast; #define CASE(kind,type) case AST_##kind: { Ast_##type *node = (Ast_##type *)ast;
#define BREAK() } break #define BREAK() } break

View File

@@ -58,8 +58,8 @@ force_inline B32 is_numeric(Ast_Type *type){
// Hash consed types // Hash consed types
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
function Ast_Type * function Ast_Type *
type_new(Allocator *allocator, Ast_Type_Kind kind, size_t size, size_t align){ type_new(Arena *allocator, Ast_Type_Kind kind, size_t size, size_t align){
Ast_Type *result = exp_alloc_type(allocator, Ast_Type, AF_ZeroMemory); Ast_Type *result = arena_push_type(allocator, Ast_Type, AF_ZeroMemory);
result->kind = kind; result->kind = kind;
result->size = size; result->size = size;
result->align = align; result->align = align;
@@ -69,9 +69,9 @@ type_new(Allocator *allocator, Ast_Type_Kind kind, size_t size, size_t align){
} }
function Ast_Type * function Ast_Type *
type_copy(Allocator *a, Ast_Type *type){ type_copy(Arena *a, Ast_Type *type){
// @warning: This changes type id !!!! // @warning: This changes type id !!!!
Ast_Type *result = exp_alloc_type(a, Ast_Type); Ast_Type *result = arena_push_type(a, Ast_Type);
memory_copy(result, type, sizeof(Ast_Type)); memory_copy(result, type, sizeof(Ast_Type));
result->type_id = pctx->type_ids++; result->type_id = pctx->type_ids++;
add(pctx->perm, &pctx->all_types, result); add(pctx->perm, &pctx->all_types, result);
@@ -322,7 +322,7 @@ typename_base(String_Builder *sb, Ast_Type *type){
} }
function String function String
get_typename(Allocator *a, Ast_Type *type){ get_typename(Arena *a, Ast_Type *type){
pctx->helper_builder.addf("["); pctx->helper_builder.addf("[");
typename_base(&pctx->helper_builder, type); typename_base(&pctx->helper_builder, type);
pctx->helper_builder.addf("]"); pctx->helper_builder.addf("]");

View File

@@ -115,14 +115,14 @@ os_write_file(String filename, String filecontent){
} }
function String function String
os_read_file(Allocator *a, String name){ os_read_file(Arena *a, String name){
String result = {0}; String result = {0};
FILE *f = fopen((char *)name.str, "rb"); FILE *f = fopen((char *)name.str, "rb");
if(f){ if(f){
fseek(f, 0, SEEK_END); fseek(f, 0, SEEK_END);
result.len = ftell(f); result.len = ftell(f);
fseek(f, 0, SEEK_SET); fseek(f, 0, SEEK_SET);
result.str = (U8 *)exp_alloc(a, result.len + 1); result.str = (U8 *)arena_push_size(a, result.len + 1);
fread(result.str, result.len, 1, f); fread(result.str, result.len, 1, f);
fclose(f); fclose(f);
result.str[result.len] = 0; result.str[result.len] = 0;
@@ -132,7 +132,7 @@ os_read_file(Allocator *a, String name){
} }
function String function String
os_get_working_dir(Allocator *a){ os_get_working_dir(Arena *a){
wchar_t buffer[2048]; wchar_t buffer[2048];
DWORD written = GetCurrentDirectoryW(2048, buffer); DWORD written = GetCurrentDirectoryW(2048, buffer);
assert(written != 0); assert(written != 0);
@@ -143,7 +143,7 @@ os_get_working_dir(Allocator *a){
} }
function String function String
os_get_exe_dir(Allocator *a){ os_get_exe_dir(Arena *a){
wchar_t buffer[2048]; wchar_t buffer[2048];
DWORD written = GetModuleFileNameW(0, buffer, 2048); DWORD written = GetModuleFileNameW(0, buffer, 2048);
assert(written != 0); assert(written != 0);
@@ -158,11 +158,11 @@ os_get_exe_dir(Allocator *a){
} }
function String function String
os_get_absolute_path(Allocator *a, String path){ os_get_absolute_path(Arena *a, String path){
Scratch scratch(a); Scratch scratch(a);
String16 path16 = string8_to_string16(scratch, path); String16 path16 = string8_to_string16(scratch, path);
wchar_t *buffer = exp_alloc_array(scratch, wchar_t, 2048); wchar_t *buffer = arena_push_array(scratch, wchar_t, 2048);
DWORD written = GetFullPathNameW((wchar_t *)path16.str, 2048, buffer, 0); DWORD written = GetFullPathNameW((wchar_t *)path16.str, 2048, buffer, 0);
if(written == 0) return {}; if(written == 0) return {};