Working on simplifying configurable allocation scheme

This commit is contained in:
Krzosa Karol
2023-01-01 12:40:58 +01:00
parent 8c0a8bf72b
commit c5539276ae
18 changed files with 169 additions and 347 deletions

106
base.cpp
View File

@@ -528,66 +528,6 @@ arena_sub(Arena *base, size_t size, String debug_name) {
return result;
}
enum Log_Kind{Log_Kind_Normal_No_NewLine, Log_Kind_Normal, Log_Kind_Error, Log_Kind_Trace};
typedef void Log_Proc(Log_Kind kind, String string, char *file, int line);
//-----------------------------------------------------------------------------
// Thread Context
//-----------------------------------------------------------------------------
struct Thread_Ctx{
Arena scratch[2];
Log_Proc *log_proc;
int thread_index;
int line;
char *file;
};
thread_local Thread_Ctx thread_ctx;
#define REPORT_ALLOCATIONS 0
#define report_file_and_line() report__file_and_line(__FILE__, __LINE__)
force_inline void
report__file_and_line(const char *file, int line){
thread_ctx.file = (char *)file;
thread_ctx.line = line;
}
//-----------------------------------------------------------------------------
// Implicit scratch stack
//-----------------------------------------------------------------------------
struct Scratch{
size_t saved_pos;
Arena *arena;
Scratch(Arena *conflict = 0){
if(conflict == thread_ctx.scratch){
arena = thread_ctx.scratch + 1;
}
else {
arena = thread_ctx.scratch;
}
saved_pos = arena->len;
}
~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
// which is an error
private:
Scratch(Scratch &arena);
Scratch(Scratch &arena, Scratch &a2);
};
CORE_Static void
thread_ctx_init(){
arena_init(thread_ctx.scratch, "Scratch1"_s);
arena_init(thread_ctx.scratch+1, "Scratch2"_s);
}
//-----------------------------------------------------------------------------
// Array
//-----------------------------------------------------------------------------
@@ -716,27 +656,6 @@ array_make(Allocator *a, S64 size = 16){
}
#include "base_string.cpp"
//-----------------------------------------------------------------------------
// Logging
//-----------------------------------------------------------------------------
#define log_info(...) handle_log_message(Log_Kind_Normal, __LINE__, __FILE__,##__VA_ARGS__)
#define log_info_no_nl(...) handle_log_message(Log_Kind_Normal_No_NewLine, __LINE__, __FILE__,##__VA_ARGS__)
#define log_trace(...) handle_log_message(Log_Kind_Trace, __LINE__, __FILE__,##__VA_ARGS__)
#define log_error(...) handle_log_message(Log_Kind_Error, __LINE__, __FILE__,##__VA_ARGS__)
CORE_Static void
handle_log_message(Log_Kind kind, int line, const char *file, const char *str, ...){
if(kind == Log_Kind_Trace) return;
Scratch scratch;
STRING_FMT(scratch, str, message);
if(thread_ctx.log_proc) thread_ctx.log_proc(kind, message, (char *)file, line);
else{
printf("%s", message.str);
if(kind != Log_Kind_Normal_No_NewLine){
printf("\n");
}
}
}
//-----------------------------------------------------------------------------
// Defer
@@ -896,20 +815,6 @@ map_insert(Map *map, Intern_String key, void *value){
map_insert(map, hash_string(key.s), value);
}
CORE_Static void
map_test(){
Scratch scratch;
Map map = {scratch};
const size_t size = 1025;
for(size_t i = 1; i < size; i++){
map_insert(&map, i, (void *)i);
}
for(size_t i = 1; i < size; i++){
size_t val = (size_t)map_get(&map, i);
assert(val == i);
}
}
//-----------------------------------------------------------------------------
// String intern
//-----------------------------------------------------------------------------
@@ -952,17 +857,6 @@ intern_string(Intern_Table *t, String string){
return result;
}
CORE_Static void
test_intern_table(){
Scratch scratch;
Intern_Table table = intern_table_make(scratch, scratch);
Intern_String intern1 = intern_string(&table, "Thing"_s);
Intern_String intern2 = intern_string(&table, "Thing"_s);
Intern_String intern3 = intern_string(&table, "Not Thing"_s);
assert(intern1.str == intern2.str);
assert(intern3.str != intern2.str);
}
//-----------------------------------------------------------------------------
// Array List
// @todo(krzosa): If even one item got removed from block