Replacing core allocation stuff, still need to rewrite map and token

structures
This commit is contained in:
Krzosa Karol
2022-10-10 00:05:38 +02:00
parent 13f2f20ea6
commit 2f153a7cd3
9 changed files with 43 additions and 36 deletions

View File

@@ -555,8 +555,10 @@ arena_clear(Arena *arena){
arena_pop_pos(arena, 0);
}
#define arena_push_array(a, T, size,...) (T *)arena_push_size(a, sizeof(T)*(size),##__VA_ARGS__)
#define arena_push_type(a, T, ...) arena_push_array(a, T, 1,##__VA_ARGS__)
function void *
arena_push_size(Arena *a, size_t size){
arena_push_size(Arena *a, size_t size, Alloc_Flag flags = AF_None){
size_t generous_size = size + a->alignment;
if(a->len+generous_size>a->memory.commit){
if(a->memory.reserve == 0){
@@ -570,6 +572,11 @@ arena_push_size(Arena *a, size_t size){
assert(a->memory.reserve > a->len + size);
void *result = (U8*)a->memory.data + a->len;
a->len += size;
if(flags & AF_ZeroMemory){
memory_zero(result, size);
}
return result;
}
@@ -911,7 +918,7 @@ struct Map_Key_Value{
};
struct Map{
Allocator *allocator;
Arena *allocator;
Map_Key_Value *data;
S64 len;
S64 cap;
@@ -923,10 +930,10 @@ map_grow(Map *map, S64 new_size){
new_size = max((S64)16, new_size);
assert(new_size > map->cap);
assert(is_pow2(new_size));
if(map->cap == 0 && map->allocator == 0) map->allocator = imp_get();
assert(map->allocator);
Map new_map = {};
new_map.data = exp_alloc_array(map->allocator, Map_Key_Value, new_size, AF_ZeroMemory);
new_map.data = arena_push_array(map->allocator, Map_Key_Value, new_size, AF_ZeroMemory);
new_map.cap = new_size;
new_map.allocator = map->allocator;
@@ -935,12 +942,12 @@ map_grow(Map *map, S64 new_size){
map_insert(&new_map, map->data[i].key, map->data[i].value);
}
}
if(map->data) exp_free(map->allocator, map->data);
// if(map->data) exp_free(map->allocator, map->data);
*map = new_map;
}
function Map
map_make(Allocator *a, S64 size){
map_make(Arena *a, S64 size){
Map result = {a};
map_grow(&result, size);
return result;
@@ -1057,14 +1064,14 @@ map_test(){
// String intern
//-----------------------------------------------------------------------------
struct Intern_Table{
Allocator *string_allocator;
Arena *string_allocator;
Map map;
U8 *first_keyword;
U8 *last_keyword;
};
function Intern_Table
intern_table_make(Allocator *string_allocator, Allocator *map_allocator, S64 initial_size = 32){
intern_table_make(Arena *string_allocator, Arena *map_allocator, S64 initial_size = 32){
Intern_Table result = {};
result.map = map_make(map_allocator, initial_size);
result.string_allocator = string_allocator;
@@ -1341,7 +1348,7 @@ T pop(List<T> *list){
template<class T>
T *merge(Arena *arena, List<T> *list){
int len = length(list);
T *result = exp_alloc_array(arena, T, len);
T *result = arena_push_array(arena, T, len);
int i = 0;
For_Linked_List(list->first){