Types, Fixed personal arena
This commit is contained in:
55
main.cpp
55
main.cpp
@@ -29,11 +29,10 @@ typedef double F64;
|
||||
#define mib(x) (kib(x)*1024llu)
|
||||
#define gib(x) (mib(x)*1024llu)
|
||||
struct String{U8 *str;S64 len;};
|
||||
union Intern_String{
|
||||
String s;
|
||||
struct{ U8 *str; S64 len; };
|
||||
};
|
||||
union Intern_String{ String s; struct{ U8 *str; S64 len; }; }; // Basically just String
|
||||
|
||||
#define JOIN1(X,Y) X##Y // helper macro
|
||||
#define JOIN(X,Y) JOIN1(X,Y)
|
||||
//-----------------------------------------------------------------------------
|
||||
// Utilities
|
||||
//-----------------------------------------------------------------------------
|
||||
@@ -287,6 +286,11 @@ struct Arena:Allocator{
|
||||
OS_Memory memory;
|
||||
SizeU alignment;
|
||||
SizeU len;
|
||||
|
||||
// Personal arena memes so we can compute correct size when resizing
|
||||
// Also a pointer so that we can make sure it didn't change
|
||||
SizeU old_size;
|
||||
void *debug_prev_pointer;
|
||||
};
|
||||
|
||||
function void arena_init(Arena *arena, String debug_name);
|
||||
@@ -347,7 +351,24 @@ force_inline void *
|
||||
personal_arena_allocator_proc(Allocator *a, Allocation_Kind kind, void *old_pointer, SizeU size){
|
||||
Arena *arena = (Arena *)a;
|
||||
arena->alignment = 1;
|
||||
return arena_allocator_proc(a, kind, old_pointer, size);
|
||||
|
||||
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
|
||||
@@ -447,8 +468,8 @@ report__file_and_line(const char *file, int line){
|
||||
//-----------------------------------------------------------------------------
|
||||
// Implicit scratch stack
|
||||
//-----------------------------------------------------------------------------
|
||||
#define Set_Scratch() Scoped_Scratch scratch_##__LINE__
|
||||
#define Set_Backup_Scratch() Scoped_Scratch scratch_##__LINE__(true)
|
||||
#define Set_Scratch() Scoped_Scratch JOIN(scratch, __LINE__)
|
||||
#define Set_Backup_Scratch() Scoped_Scratch JOIN(scratch, __LINE__)(true)
|
||||
struct Scoped_Scratch{
|
||||
SizeU saved_pos;
|
||||
Allocator *saved_allocator;
|
||||
@@ -470,7 +491,7 @@ struct Scoped_Scratch{
|
||||
//-----------------------------------------------------------------------------
|
||||
// Implicit allocator stack
|
||||
//-----------------------------------------------------------------------------
|
||||
#define Set_Allocator(a) Scoped_Allocator scoped_##__LINE__(a)
|
||||
#define Set_Allocator(a) Scoped_Allocator JOIN(scoped,__LINE__)(a)
|
||||
struct Scoped_Allocator{
|
||||
Allocator *allocator;
|
||||
Scoped_Allocator(Allocator *a){
|
||||
@@ -492,7 +513,7 @@ thread_ctx_get_user_ctx(U64 id){
|
||||
}
|
||||
|
||||
#define Get_Ctx(T) T *ctx = (T *)thread_ctx_get_user_ctx(T##_ID)
|
||||
#define Set_Ctx(ctx, id) Scoped_Ctx scoped_ctx_##__LINE__((void *)ctx, id)
|
||||
#define Set_Ctx(ctx, id) Scoped_Ctx JOIN(scoped_ctx, __LINE__)((void *)ctx, id)
|
||||
struct Scoped_Ctx{
|
||||
void *prev_ctx;
|
||||
U64 prev_id;
|
||||
@@ -699,7 +720,6 @@ test_custom_context(){
|
||||
assert(thread_ctx.ctx == 0);
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Defer
|
||||
// http://www.gingerbill.org/article/2015/08/19/defer-in-cpp/
|
||||
@@ -757,6 +777,16 @@ struct Array{
|
||||
len = 0;
|
||||
}
|
||||
|
||||
Array<T> copy(Allocator *a){
|
||||
Array<T> result = {};
|
||||
result.len = len;
|
||||
result.cap = len;
|
||||
result.allocator = 0;
|
||||
result.data = exp_alloc_array(a, T, len);
|
||||
memory_copy(result.data, data, sizeof(T)*len);
|
||||
return result;
|
||||
}
|
||||
|
||||
T *begin(){ return data; }
|
||||
T *end (){ return data + len; }
|
||||
T &operator[](S64 i){ return data[i]; }
|
||||
@@ -1089,17 +1119,18 @@ test_intern_table(){ Set_Scratch();
|
||||
#include "new_lex.cpp"
|
||||
#include "new_ast.cpp"
|
||||
#include "new_parse.cpp"
|
||||
#include "new_type.cpp"
|
||||
int main(){
|
||||
test_custom_context();
|
||||
test_heap_allocator();
|
||||
test_os_memory();
|
||||
|
||||
thread_ctx_init();
|
||||
map_test();
|
||||
|
||||
test_types();
|
||||
test_parse_decl();
|
||||
test_parse_expr();
|
||||
|
||||
map_test();
|
||||
test_array();
|
||||
test_string_builder();
|
||||
test_intern_table();
|
||||
|
||||
Reference in New Issue
Block a user