Getting rid of heap stuff completely but also naively
This commit is contained in:
63
base.cpp
63
base.cpp
@@ -588,30 +588,6 @@ arena_allocator_proc(Allocator *a, Allocation_Kind kind, void *old_pointer, size
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
force_inline void *
|
|
||||||
personal_arena_allocator_proc(Allocator *a, Allocation_Kind kind, void *old_pointer, size_t size){
|
|
||||||
Arena *arena = (Arena *)a;
|
|
||||||
arena->alignment = 1;
|
|
||||||
|
|
||||||
void *result = 0;
|
|
||||||
switch(kind){
|
|
||||||
case Allocation_Resize: {
|
|
||||||
assert(arena->old_size);
|
|
||||||
assert(arena->old_size < size);
|
|
||||||
assert(arena->debug_prev_pointer == old_pointer);
|
|
||||||
result = arena_push_size(arena, size - arena->old_size);
|
|
||||||
result = old_pointer;
|
|
||||||
} break;
|
|
||||||
default: {
|
|
||||||
result = arena_allocator_proc(a, kind, old_pointer, size);
|
|
||||||
arena->debug_prev_pointer = result;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
arena->old_size = size;
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
||||||
@@ -621,15 +597,6 @@ arena_init(Arena *a, String debug_name){
|
|||||||
if(!a->proc) a->proc = arena_allocator_proc;
|
if(!a->proc) a->proc = arena_allocator_proc;
|
||||||
}
|
}
|
||||||
|
|
||||||
function Arena
|
|
||||||
arena_make_personal(String debug_name){
|
|
||||||
Arena arena = {};
|
|
||||||
arena.proc = personal_arena_allocator_proc;
|
|
||||||
arena_init(&arena, debug_name);
|
|
||||||
arena.kind = Allocator_PersonalArena;
|
|
||||||
return arena;
|
|
||||||
}
|
|
||||||
|
|
||||||
enum Log_Kind{Log_Kind_Normal, Log_Kind_Error, Log_Kind_Trace};
|
enum Log_Kind{Log_Kind_Normal, Log_Kind_Error, Log_Kind_Trace};
|
||||||
typedef void Log_Proc(Log_Kind kind, String string, char *file, int line);
|
typedef void Log_Proc(Log_Kind kind, String string, char *file, int line);
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
@@ -892,35 +859,6 @@ array_make(Allocator *a, S64 size = 16){
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
function void
|
|
||||||
test_array(){
|
|
||||||
Scratch scratch;
|
|
||||||
Array<int> array = {scratch};
|
|
||||||
|
|
||||||
int size = 1000;
|
|
||||||
for(int i = 0; i < size; i++){
|
|
||||||
array.add(i);
|
|
||||||
}
|
|
||||||
S32 i = 0;
|
|
||||||
For(array){
|
|
||||||
assert(it == i++);
|
|
||||||
}
|
|
||||||
|
|
||||||
Arena arena = arena_make_personal("Test personal arena"_s);
|
|
||||||
Array<int> array2 = {&arena};
|
|
||||||
for(int i = 0; i < size; i++){
|
|
||||||
array2.add(i);
|
|
||||||
}
|
|
||||||
i=0;
|
|
||||||
For(array2){
|
|
||||||
assert(it == i++);
|
|
||||||
}
|
|
||||||
exp_destroy(&arena);
|
|
||||||
assert(arena.memory.data == 0);
|
|
||||||
assert(thread_ctx.scratch->memory.data != 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
#include "base_string.cpp"
|
#include "base_string.cpp"
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
// Logging
|
// Logging
|
||||||
@@ -1042,7 +980,6 @@ function Map_Key_Value *
|
|||||||
map_base_get(Map *map, U64 key){
|
map_base_get(Map *map, U64 key){
|
||||||
if(map->len == 0) return 0;
|
if(map->len == 0) return 0;
|
||||||
assert(key);
|
assert(key);
|
||||||
// if(key == 0) key+=1;
|
|
||||||
|
|
||||||
U64 hash = hash_u64(key);
|
U64 hash = hash_u64(key);
|
||||||
U64 index = wrap_around_pow2(hash, map->cap);
|
U64 index = wrap_around_pow2(hash, map->cap);
|
||||||
|
|||||||
@@ -2,8 +2,8 @@
|
|||||||
function void
|
function void
|
||||||
lex_init(Allocator *token_string_arena, Allocator *map_allocator, Lexer *l){
|
lex_init(Allocator *token_string_arena, Allocator *map_allocator, Lexer *l){
|
||||||
l->arena = token_string_arena;
|
l->arena = token_string_arena;
|
||||||
l->tokens = array_make<Token>(token_string_arena, 1024*2);
|
l->tokens = array_make<Token>(token_string_arena, 4096*4);
|
||||||
l->interns= intern_table_make(token_string_arena, map_allocator, 1024);
|
l->interns= intern_table_make(token_string_arena, map_allocator, 2048);
|
||||||
|
|
||||||
/*#import meta
|
/*#import meta
|
||||||
for i in meta.keywords:
|
for i in meta.keywords:
|
||||||
@@ -71,18 +71,17 @@ op_info_table[19].op = l->intern("!"_s);
|
|||||||
}
|
}
|
||||||
|
|
||||||
function void
|
function void
|
||||||
parse_init(Parse_Ctx *ctx, Arena *perm_allocator, Allocator *heap_allocator){
|
parse_init(Parse_Ctx *ctx, Arena *perm_allocator){
|
||||||
pctx = ctx;
|
pctx = ctx;
|
||||||
ctx->perm = perm_allocator;
|
ctx->perm = perm_allocator;
|
||||||
ctx->heap = heap_allocator;
|
ctx->type_map = map_make(ctx->perm, 2048);
|
||||||
ctx->type_map = {ctx->heap};
|
|
||||||
ctx->gen = {ctx->perm};
|
ctx->gen = {ctx->perm};
|
||||||
ctx->helper_builder= {ctx->perm};
|
ctx->helper_builder= {ctx->perm};
|
||||||
ctx->scope_ids = 1;
|
ctx->scope_ids = 1;
|
||||||
bigint_allocator = ctx->perm;
|
bigint_allocator = ctx->perm;
|
||||||
arena_init(&ctx->stage_arena, "Compiler stage arena"_s);
|
arena_init(&ctx->stage_arena, "Compiler stage arena"_s);
|
||||||
|
|
||||||
lex_init(ctx->perm, ctx->heap, ctx);
|
lex_init(ctx->perm, ctx->perm, ctx);
|
||||||
init_type();
|
init_type();
|
||||||
|
|
||||||
// Init paths
|
// Init paths
|
||||||
@@ -97,18 +96,15 @@ parse_init(Parse_Ctx *ctx, Arena *perm_allocator, Allocator *heap_allocator){
|
|||||||
function void
|
function void
|
||||||
begin_compilation(){
|
begin_compilation(){
|
||||||
init_ctx_time_begin = os_time();
|
init_ctx_time_begin = os_time();
|
||||||
OS_Heap *heap = exp_alloc_type(&pernament_arena, OS_Heap, AF_ZeroMemory);
|
|
||||||
*heap = win32_os_heap_create(false, mib(4), 0, "Compiler heap"_s);
|
|
||||||
Parse_Ctx *ctx = exp_alloc_type(&pernament_arena, Parse_Ctx, AF_ZeroMemory);
|
Parse_Ctx *ctx = exp_alloc_type(&pernament_arena, Parse_Ctx, AF_ZeroMemory);
|
||||||
parse_init(ctx, &pernament_arena, heap);
|
parse_init(ctx, &pernament_arena);
|
||||||
init_ctx_time_end = os_time();
|
init_ctx_time_end = os_time();
|
||||||
}
|
}
|
||||||
|
|
||||||
function void
|
function void
|
||||||
destroy_compiler(){
|
destroy_compiler(){
|
||||||
exp_destroy(pctx->heap);
|
|
||||||
exp_free_all(pctx->perm);
|
exp_free_all(pctx->perm);
|
||||||
exp_destroy(&pctx->stage_arena);
|
exp_free_all(&pctx->stage_arena);
|
||||||
}
|
}
|
||||||
|
|
||||||
function void
|
function void
|
||||||
|
|||||||
@@ -155,7 +155,6 @@ struct Lexer{
|
|||||||
|
|
||||||
struct Parse_Ctx:Lexer{
|
struct Parse_Ctx:Lexer{
|
||||||
Arena *perm; // Stores: AST, tokens, interns
|
Arena *perm; // Stores: AST, tokens, interns
|
||||||
Allocator *heap;
|
|
||||||
Arena stage_arena;
|
Arena stage_arena;
|
||||||
|
|
||||||
List<Ast_Type *> all_types;
|
List<Ast_Type *> all_types;
|
||||||
|
|||||||
@@ -10,6 +10,11 @@ Current:
|
|||||||
- [ ] Imports are leaking names ! Multimedia leaks windows stuff
|
- [ ] Imports are leaking names ! Multimedia leaks windows stuff
|
||||||
- [ ] Test and bulletproof any, slices
|
- [ ] Test and bulletproof any, slices
|
||||||
|
|
||||||
|
|
||||||
|
Memory:
|
||||||
|
- [ ] 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!
|
||||||
|
|
||||||
In the future
|
In the future
|
||||||
|
|
||||||
- [ ] Cleanup
|
- [ ] Cleanup
|
||||||
@@ -270,7 +275,6 @@ int main(int argument_count, char **arguments){
|
|||||||
test_unicode();
|
test_unicode();
|
||||||
|
|
||||||
map_test();
|
map_test();
|
||||||
test_array();
|
|
||||||
test_string_builder();
|
test_string_builder();
|
||||||
test_intern_table();
|
test_intern_table();
|
||||||
|
|
||||||
|
|||||||
@@ -248,56 +248,3 @@ os_list_dir(Allocator *a, String dir, U32 flags = LIST_NO_FLAGS){
|
|||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
// OS Heap allocator
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
struct OS_Heap:Allocator{
|
|
||||||
void *handle;
|
|
||||||
};
|
|
||||||
|
|
||||||
function void *
|
|
||||||
os_heap_allocator_proc(Allocator *a, Allocation_Kind kind, void *old_pointer, size_t size){
|
|
||||||
OS_Heap *heap = (OS_Heap *)a;
|
|
||||||
switch(kind){
|
|
||||||
case Allocation_FreeAll:{
|
|
||||||
invalid_codepath;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
case Allocation_Destroy:{
|
|
||||||
BOOL result = HeapDestroy(heap->handle);
|
|
||||||
assert(result != 0);
|
|
||||||
heap->handle = 0;
|
|
||||||
heap->proc = 0;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
case Allocation_Free:{
|
|
||||||
BOOL result = HeapFree(heap->handle, 0, old_pointer);
|
|
||||||
assert(result != 0);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
case Allocation_Alloc:{
|
|
||||||
void *result = HeapAlloc(heap->handle, 0, size);
|
|
||||||
assert(result);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
case Allocation_Resize:{
|
|
||||||
void *result = HeapReAlloc(heap->handle, 0, old_pointer, size);
|
|
||||||
assert(result);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
default: invalid_codepath;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
function OS_Heap // max_size == 0 == growing heap
|
|
||||||
win32_os_heap_create(B32 multithreaded, size_t initial_size, size_t max_size, String debug_name){
|
|
||||||
OS_Heap result = {};
|
|
||||||
result.debug_name = debug_name;
|
|
||||||
result.proc = os_heap_allocator_proc;
|
|
||||||
result.kind = Allocator_OSHeap;
|
|
||||||
result.handle = HeapCreate(multithreaded ? 0 : HEAP_NO_SERIALIZE, initial_size, max_size);
|
|
||||||
assert(result.handle);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user