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); 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 * 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; size_t generous_size = size + a->alignment;
if(a->len+generous_size>a->memory.commit){ if(a->len+generous_size>a->memory.commit){
if(a->memory.reserve == 0){ if(a->memory.reserve == 0){
@@ -570,6 +572,11 @@ arena_push_size(Arena *a, size_t size){
assert(a->memory.reserve > a->len + size); assert(a->memory.reserve > a->len + size);
void *result = (U8*)a->memory.data + a->len; void *result = (U8*)a->memory.data + a->len;
a->len += size; a->len += size;
if(flags & AF_ZeroMemory){
memory_zero(result, size);
}
return result; return result;
} }
@@ -911,7 +918,7 @@ struct Map_Key_Value{
}; };
struct Map{ struct Map{
Allocator *allocator; Arena *allocator;
Map_Key_Value *data; Map_Key_Value *data;
S64 len; S64 len;
S64 cap; S64 cap;
@@ -923,10 +930,10 @@ map_grow(Map *map, S64 new_size){
new_size = max((S64)16, new_size); new_size = max((S64)16, new_size);
assert(new_size > map->cap); assert(new_size > map->cap);
assert(is_pow2(new_size)); assert(is_pow2(new_size));
if(map->cap == 0 && map->allocator == 0) map->allocator = imp_get(); assert(map->allocator);
Map new_map = {}; 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.cap = new_size;
new_map.allocator = map->allocator; 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); 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; *map = new_map;
} }
function Map function Map
map_make(Allocator *a, S64 size){ map_make(Arena *a, S64 size){
Map result = {a}; Map result = {a};
map_grow(&result, size); map_grow(&result, size);
return result; return result;
@@ -1057,14 +1064,14 @@ map_test(){
// String intern // String intern
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
struct Intern_Table{ struct Intern_Table{
Allocator *string_allocator; Arena *string_allocator;
Map map; Map map;
U8 *first_keyword; U8 *first_keyword;
U8 *last_keyword; U8 *last_keyword;
}; };
function Intern_Table 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 = {}; Intern_Table result = {};
result.map = map_make(map_allocator, initial_size); result.map = map_make(map_allocator, initial_size);
result.string_allocator = string_allocator; result.string_allocator = string_allocator;
@@ -1341,7 +1348,7 @@ T pop(List<T> *list){
template<class T> template<class T>
T *merge(Arena *arena, List<T> *list){ T *merge(Arena *arena, List<T> *list){
int len = length(list); int len = length(list);
T *result = exp_alloc_array(arena, T, len); T *result = arena_push_array(arena, T, len);
int i = 0; int i = 0;
For_Linked_List(list->first){ For_Linked_List(list->first){

View File

@@ -76,8 +76,8 @@ operator==(String a, String b){
} }
function String function String
string_copy(Allocator *a, String string){ string_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};
@@ -368,7 +368,7 @@ string_trim_end(String string) {
} }
function String function String
string_to_lower_case(Allocator *arena, String s) { string_to_lower_case(Arena *arena, String s) {
String copy = string_copy(arena, s); String copy = string_copy(arena, s);
for (U64 i = 0; i < copy.len; i++) { for (U64 i = 0; i < copy.len; i++) {
copy.str[i] = to_lower_case(copy.str[i]); copy.str[i] = to_lower_case(copy.str[i]);
@@ -377,7 +377,7 @@ string_to_lower_case(Allocator *arena, String s) {
} }
function String function String
string_to_upper_case(Allocator *arena, String s) { string_to_upper_case(Arena *arena, String s) {
String copy = string_copy(arena, s); String copy = string_copy(arena, s);
for (U64 i = 0; i < copy.len; i++) { for (U64 i = 0; i < copy.len; i++) {
copy.str[i] = to_upper_case(copy.str[i]); copy.str[i] = to_upper_case(copy.str[i]);

View File

@@ -293,7 +293,7 @@ struct Ast_Decl: Ast{
// AST Constructors beginning with expressions // AST Constructors beginning with expressions
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
#define AST_NEW(T,ikind,ipos,iflags) \ #define AST_NEW(T,ikind,ipos,iflags) \
Ast_##T *result = exp_alloc_type(pctx->perm, Ast_##T, AF_ZeroMemory);\ Ast_##T *result = arena_push_type(pctx->perm, Ast_##T, AF_ZeroMemory);\
result->flags = iflags; \ result->flags = iflags; \
result->kind = AST_##ikind; \ result->kind = AST_##ikind; \
result->parent_scope = pctx->currently_parsed_scope; \ result->parent_scope = pctx->currently_parsed_scope; \
@@ -303,7 +303,7 @@ struct Ast_Decl: Ast{
#define ast_new(T,kind,pos,flags) (T *)_ast_new(sizeof(T), kind, pos, flags) #define ast_new(T,kind,pos,flags) (T *)_ast_new(sizeof(T), kind, pos, flags)
function Ast * function Ast *
_ast_new(size_t size, Ast_Kind kind, Token *pos, Ast_Flag flags = 0){ _ast_new(size_t size, Ast_Kind kind, Token *pos, Ast_Flag flags = 0){
Ast *result = (Ast *)exp_alloc(pctx->perm, size, AF_ZeroMemory); Ast *result = (Ast *)arena_push_size(pctx->perm, size, AF_ZeroMemory);
result->flags = flags; result->flags = flags;
result->kind = kind; result->kind = kind;
result->parent_scope = pctx->currently_parsed_scope; result->parent_scope = pctx->currently_parsed_scope;
@@ -487,7 +487,7 @@ finalize_decl_scope(Ast_Scope *scope){
} }
function Ast_Scope * function Ast_Scope *
begin_stmt_scope(Allocator *scratch, Token *pos){ begin_stmt_scope(Arena *scratch, Token *pos){
AST_NEW(Scope, SCOPE, pos, AST_STMT); AST_NEW(Scope, SCOPE, pos, AST_STMT);
result->stmts = {scratch}; result->stmts = {scratch};
result->file = pctx->currently_parsed_file; result->file = pctx->currently_parsed_file;

View File

@@ -34,7 +34,7 @@ gen_last_line(){
} }
function String function String
string_scope_name(Allocator *a, Ast_Scope *scope){ string_scope_name(Arena *a, Ast_Scope *scope){
String string = {}; String string = {};
if(scope->parent_scope) string = string_scope_name(a, scope->parent_scope); if(scope->parent_scope) string = string_scope_name(a, scope->parent_scope);
assert_message(scope->scope_id != 0, "Scope id is equal to 0 which is invalid, scope didn't initialize id"); assert_message(scope->scope_id != 0, "Scope id is equal to 0 which is invalid, scope didn't initialize id");
@@ -50,8 +50,8 @@ gen_scope_name(Ast_Scope *scope){
} }
function String function String
unique_name(Allocator *allocator, Ast *ast){ unique_name(Arena *allocator, Ast *ast){
Scratch scratch; Scratch scratch(allocator);
String result = string_scope_name(scratch, ast->parent_scope); String result = string_scope_name(scratch, ast->parent_scope);
assert(result.len); assert(result.len);
result = string_fmt(allocator, "%Q%d", result, ast->pos->line); result = string_fmt(allocator, "%Q%d", result, ast->pos->line);
@@ -85,7 +85,7 @@ get_ctype_name_for_type(Ast_Type *type){
} }
function String function String
string_simple_decl_prefix(Allocator *a, Ast_Type *ast){ string_simple_decl_prefix(Arena *a, Ast_Type *ast){
switch(ast->kind){ switch(ast->kind){
case TYPE_POINTER:{ case TYPE_POINTER:{
String string = string_simple_decl_prefix(a, ast->base); String string = string_simple_decl_prefix(a, ast->base);
@@ -122,7 +122,7 @@ string_simple_decl_prefix(Allocator *a, Ast_Type *ast){
} }
function String function String
string_simple_decl_postfix(Allocator *a, Ast_Type *ast){ string_simple_decl_postfix(Arena *a, Ast_Type *ast){
switch(ast->kind){ switch(ast->kind){
case TYPE_POINTER: case TYPE_POINTER:
return string_simple_decl_postfix(a, ast->base); return string_simple_decl_postfix(a, ast->base);
@@ -141,7 +141,7 @@ string_simple_decl_postfix(Allocator *a, Ast_Type *ast){
} }
function String function String
string_simple_decl(Allocator *a, Ast_Type *ast, Intern_String name = {}){ string_simple_decl(Arena *a, Ast_Type *ast, Intern_String name = {}){
if(ast->kind == TYPE_LAMBDA) { if(ast->kind == TYPE_LAMBDA) {
String prefix = string_simple_decl_prefix(a, ast->func.ret); String prefix = string_simple_decl_prefix(a, ast->func.ret);
String string = string_fmt(a, "%Q(*%Q)(", prefix, name); String string = string_fmt(a, "%Q(*%Q)(", prefix, name);
@@ -173,7 +173,7 @@ gen_simple_decl(Ast_Type *ast, Intern_String name = {}){
} }
function String function String
gen_string_simple_decl(Allocator *a, Ast_Type *ast, String name){ gen_string_simple_decl(Arena *a, Ast_Type *ast, String name){
Scratch scratch; Scratch scratch;
String string = string_simple_decl(scratch, ast, pctx->intern(name)); String string = string_simple_decl(scratch, ast, pctx->intern(name));
String result = string_copy(a, string); String result = string_copy(a, string);

View File

@@ -1,6 +1,6 @@
function void function void
lex_init(Allocator *token_string_arena, Allocator *map_allocator, Lexer *l){ lex_init(Arena *token_string_arena, Arena *map_allocator, Lexer *l){
l->arena = token_string_arena; l->arena = token_string_arena;
l->tokens = array_make<Token>(token_string_arena, 4096*4); l->tokens = array_make<Token>(token_string_arena, 4096*4);
l->interns= intern_table_make(token_string_arena, map_allocator, 2048); l->interns= intern_table_make(token_string_arena, map_allocator, 2048);
@@ -106,8 +106,8 @@ begin_compilation(){
function void function void
destroy_compiler(){ destroy_compiler(){
exp_free_all(pctx->perm); arena_clear(pctx->perm);
exp_free_all(&pctx->stage_arena); arena_clear(&pctx->stage_arena);
} }
function void function void

View File

@@ -187,7 +187,7 @@ struct Parse_Ctx:Lexer{
}; };
function void init_type(); function void init_type();
function void lex_init(Allocator *token_string_arena, Allocator *map_allocator, Lexer *l); function void lex_init(Arena *token_string_arena, Arena *map_allocator, Lexer *l);
function String compile_to_c_code(); function String compile_to_c_code();
function Ast_Module *ast_module(Token *pos, Intern_String filename); function Ast_Module *ast_module(Token *pos, Intern_String filename);
function void insert_builtin_types_into_scope(Ast_Scope *p); function void insert_builtin_types_into_scope(Ast_Scope *p);

View File

@@ -586,7 +586,7 @@ lex__stream(Lexer *lexer){
} }
function Lexer function Lexer
lex_make(Allocator *token_string_arena, Allocator *map_allocator){ lex_make(Arena *token_string_arena, Arena *map_allocator){
Lexer result = {}; Lexer result = {};
lex_init(token_string_arena, map_allocator, &result); lex_init(token_string_arena, map_allocator, &result);
return result; return result;
@@ -606,7 +606,7 @@ lex_restream(Lexer *lexer, String istream, String file){
} }
function Lexer function Lexer
lex_stream(Allocator *token_string_arena, Allocator *map_allocator, String istream, String file){ lex_stream(Arena *token_string_arena, Arena *map_allocator, String istream, String file){
Lexer result = lex_make(token_string_arena, map_allocator); Lexer result = lex_make(token_string_arena, map_allocator);
lex_restream(&result, istream, file); lex_restream(&result, istream, file);
return result; return result;

View File

@@ -20,7 +20,7 @@ operand(Ast_Decl *decl){
return result; return result;
} }
function Operand static Operand
operand_type(Ast_Type *type) { operand_type(Ast_Type *type) {
Operand result = {}; Operand result = {};
result.type = type_type; result.type = type_type;

View File

@@ -182,7 +182,7 @@ os_does_file_exist(String path){
} }
function Array<OS_File_Info> function Array<OS_File_Info>
os_list_dir(Allocator *a, String dir, U32 flags = LIST_NO_FLAGS){ os_list_dir(Arena *a, String dir, U32 flags = LIST_NO_FLAGS){
Scratch scratch(a); Scratch scratch(a);
Array<String> dirs_to_read = {scratch}; Array<String> dirs_to_read = {scratch};
dirs_to_read.add(dir); dirs_to_read.add(dir);