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; 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 // Array
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@@ -716,27 +656,6 @@ array_make(Allocator *a, S64 size = 16){
} }
#include "base_string.cpp" #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 // Defer
@@ -896,20 +815,6 @@ map_insert(Map *map, Intern_String key, void *value){
map_insert(map, hash_string(key.s), 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 // String intern
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@@ -952,17 +857,6 @@ intern_string(Intern_Table *t, String string){
return result; 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 // Array List
// @todo(krzosa): If even one item got removed from block // @todo(krzosa): If even one item got removed from block

View File

@@ -76,7 +76,7 @@ operator==(String a, String b){
} }
CORE_Static String CORE_Static String
string_copy(Arena *a, String string){ string_copy(Allocator *a, String string){
U8 *copy = allocate_array(a, U8, string.len+1); U8 *copy = allocate_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;
@@ -84,7 +84,7 @@ string_copy(Arena *a, String string){
} }
CORE_Static String CORE_Static String
string_fmtv(Arena *a, const char *str, va_list args1) { string_fmtv(Allocator *a, const char *str, va_list args1) {
va_list args2; va_list args2;
va_copy(args2, args1); va_copy(args2, args1);
S64 len = stbsp_vsnprintf(0, 0, str, args2); S64 len = stbsp_vsnprintf(0, 0, str, args2);
@@ -104,7 +104,7 @@ String result = string_fmtv(alloc, str, args1); \
va_end(args1) va_end(args1)
CORE_Static String CORE_Static String
string_fmt(Arena *a, const char *str, ...) { string_fmt(Allocator *a, const char *str, ...) {
STRING_FMT(a, str, result); STRING_FMT(a, str, result);
return result; return result;
} }
@@ -120,7 +120,7 @@ struct String_Builder_Block{
}; };
struct String_Builder{ struct String_Builder{
Arena *allocator; Allocator *allocator;
String_Builder_Block *first_free; String_Builder_Block *first_free;
String_Builder_Block *first; String_Builder_Block *first;
String_Builder_Block *last; String_Builder_Block *last;
@@ -147,7 +147,7 @@ struct String_Builder{
block = first_free; block = first_free;
first_free = first_free->next; first_free = first_free->next;
} else{ } else{
block = (String_Builder_Block *)arena_push_size(allocator, sizeof(String_Builder_Block) + size); block = (String_Builder_Block *)allocate_size(allocator, sizeof(String_Builder_Block) + size, false);
} }
memory_zero(block, sizeof(String_Builder_Block)+1); // Also clear first byte of character data memory_zero(block, sizeof(String_Builder_Block)+1); // Also clear first byte of character data
block->cap = size; block->cap = size;
@@ -203,7 +203,7 @@ struct String_Builder{
}; };
CORE_Static String_Builder CORE_Static String_Builder
string_builder_make(Arena *a, S64 first_block_size = 4096){ string_builder_make(Allocator *a, S64 first_block_size = 4096){
String_Builder sb = {a}; String_Builder sb = {a};
sb.init(first_block_size); sb.init(first_block_size);
return sb; return sb;
@@ -215,7 +215,7 @@ enum String_Builder_Flag{
}; };
CORE_Static String CORE_Static String
string_flatten(Arena *a, String_Builder *b, String_Builder_Flag flags = String_Builder_Flag_None){ string_flatten(Allocator *a, String_Builder *b, String_Builder_Flag flags = String_Builder_Flag_None){
// @Note(Krzosa): Compute size to allocate // @Note(Krzosa): Compute size to allocate
S64 size = 1; S64 size = 1;
if(is_flag_set(flags, String_Builder_Flag_AddSize)) size += sizeof(size_t); if(is_flag_set(flags, String_Builder_Flag_AddSize)) size += sizeof(size_t);
@@ -224,7 +224,7 @@ string_flatten(Arena *a, String_Builder *b, String_Builder_Flag flags = String_B
} }
String result = {}; String result = {};
result.str = (U8 *)arena_push_size(a, size); result.str = (U8 *)allocate_size(a, size, false);
if(is_flag_set(flags, String_Builder_Flag_AddSize)) { if(is_flag_set(flags, String_Builder_Flag_AddSize)) {
memory_copy(result.str + result.len, &size, sizeof(S64)); memory_copy(result.str + result.len, &size, sizeof(S64));
result.len += sizeof(S64); result.len += sizeof(S64);
@@ -240,18 +240,6 @@ string_flatten(Arena *a, String_Builder *b, String_Builder_Flag flags = String_B
return result; return result;
} }
CORE_Static void
test_string_builder(){
Scratch scratch;
String_Builder sb = string_builder_make(scratch, 4);
sb.addf("Thing, %d", 242252);
String f = string_flatten(scratch, &sb);
assert(string_compare(f, "Thing, 242252"_s));
sb.addf("-%f %f %f", 23.0, 42.29, 2925.2);
f = string_flatten(scratch, &sb);
sb.reset();
}
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// String ops // String ops
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@@ -368,7 +356,7 @@ string_trim_end(String string) {
} }
CORE_Static String CORE_Static String
string_to_lower_case(Arena *arena, String s) { string_to_lower_case(Allocator *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 +365,7 @@ string_to_lower_case(Arena *arena, String s) {
} }
CORE_Static String CORE_Static String
string_to_upper_case(Arena *arena, String s) { string_to_upper_case(Allocator *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]);
@@ -471,14 +459,15 @@ string_from_cstring(char *string){
return result; return result;
} }
#include "core_arena.cpp" // @! Move this include
struct String_Replace { struct String_Replace {
String find; String find;
String replace; String replace;
}; };
CORE_Static String CORE_Static String
string_replace(Arena *arena, String string, Array<String_Replace> pairs){ string_replace(Scratch_Arena *scratch, Allocator *allocator, String string, Array<String_Replace> pairs){
Scratch scratch(arena); Scratch_Scope _scope(scratch);
String_Builder builder = {scratch}; String_Builder builder = {scratch};
for(int i = 0; i < string.len; i++){ for(int i = 0; i < string.len; i++){
For(pairs){ For(pairs){
@@ -493,5 +482,5 @@ string_replace(Arena *arena, String string, Array<String_Replace> pairs){
builder.append_data(string.str + i, 1); builder.append_data(string.str + i, 1);
} }
return string_flatten(arena, &builder); return string_flatten(allocator, &builder);
} }

View File

@@ -272,108 +272,3 @@ string16_copy(Allocator *a, String string){
copy[string.len] = 0; copy[string.len] = 0;
return String{copy, string.len}; return String{copy, string.len};
} }
CORE_Static void
test_unicode(){
assert(utf8_to_utf32((U8 *)"A", 1).out_str == 'A');
assert(utf8_to_utf32((U8 *)"ć", 2).out_str == 0x107);
assert(utf8_to_utf32((U8 *)"ó", 2).out_str == 0x000000f3);
Scratch scratch;
String32 result = string8_to_string32(scratch, "Thing"_s);
assert(result.len == 5);
assert(result.str[0] == 'T');
assert(result.str[4] == 'g');
result = string8_to_string32(scratch, "óźćłŁQ42-=?"_s);
{
U16 u16_code[] = {0x0054, 0x0068, 0x0069, 0x006e, 0x0067, 0x0020, 0x0069, 0x0073};
String16 thing_is16 = {u16_code, buff_cap(u16_code)};
String string = "Thing is"_s;
String16 thing_is_convert = string8_to_string16(scratch, string);
assert(string_compare(thing_is16, thing_is_convert));
String32 a = string16_to_string32(scratch, thing_is16);
String32 b = string16_to_string32(scratch, thing_is_convert);
String32 c = string8_to_string32 (scratch, string);
assert(string_compare(a, b) && string_compare(b,c));
}
{
U16 code[] = {0x015a, 0x0104, 0x00f3, 0x015b, 0x017a, 0x0107, 0x0144, 0x0143, 0x0119, 0x00f3};
String16 code_str = {code, buff_cap(code)};
String string = "ŚĄóśźćńŃęó"_s;
String16 convert = string8_to_string16(scratch, string);
assert(string_compare(convert, code_str));
String32 a = string16_to_string32(scratch, code_str);
String32 b = string16_to_string32(scratch, convert);
String32 c = string8_to_string32 (scratch, string);
assert(string_compare(a, b) && string_compare(b,c));
}
{
String str = " ഃ അ ആ ഇ ഈ ഉ ഊ ഋ ഌ എ ഏ ഐ ഒ ഓ ഔ ക ഖ ഗ ഘ ങ ച ഛ ജ ഝ ഞ ട ഡ ഢ ണ ത ഥ ദ ധ ന പ ഫ ബ ഭ മ യ ര റ ല ള ഴ വ ശ ഷ സ ഹ ാ ി ീ ു ൂ ൃ െ േ ൈ ൊ ോ ൌ ് ൗ ൠ ൡ ൧ ൨ ൩ ൪ ൫ ൬ ൮ ൯ "_s;
U16 arr[] = {0x0020, 0x0020, 0x0020, 0x0020, 0x0d02, 0x0020, 0x0d03, 0x0020, 0x0d05, 0x0020, 0x0d06, 0x0020, 0x0d07, 0x0020, 0x0d08, 0x0020, 0x0d09, 0x0020, 0x0d0a, 0x0020, 0x0d0b, 0x0020, 0x0d0c, 0x0020, 0x0d0e, 0x0020, 0x0d0f, 0x0020, 0x0d10, 0x0020, 0x0d12, 0x0020, 0x0d13, 0x0020, 0x0d14, 0x0020, 0x0d15, 0x0020, 0x0d16, 0x0020, 0x0d17, 0x0020, 0x0d18, 0x0020, 0x0d19, 0x0020, 0x0d1a, 0x0020, 0x0d1b, 0x0020, 0x0d1c, 0x0020, 0x0d1d, 0x0020, 0x0d1e, 0x0020, 0x0d1f, 0x0020, 0x0d20, 0x0020, 0x0d21, 0x0020, 0x0d22, 0x0020, 0x0d23, 0x0020, 0x0d24, 0x0020, 0x0d25, 0x0020, 0x0d26, 0x0020, 0x0d27, 0x0020, 0x0d28, 0x0020, 0x0d2a, 0x0020, 0x0d2b, 0x0020, 0x0d2c, 0x0020, 0x0d2d, 0x0020, 0x0d2e, 0x0020, 0x0d2f, 0x0020, 0x0d30, 0x0020, 0x0d31, 0x0020, 0x0d32, 0x0020, 0x0d33, 0x0020, 0x0d34, 0x0020, 0x0d35, 0x0020, 0x0d36, 0x0020, 0x0d37, 0x0020, 0x0d38, 0x0020, 0x0d39, 0x0020, 0x0d3e, 0x0020, 0x0d3f, 0x0020, 0x0d40, 0x0020, 0x0d41, 0x0020, 0x0d42, 0x0020, 0x0d43, 0x0020, 0x0d46, 0x0020, 0x0d47, 0x0020, 0x0d48, 0x0020, 0x0d4a, 0x0020, 0x0d4b, 0x0020, 0x0d4c, 0x0020, 0x0d4d, 0x0020, 0x0d57, 0x0020, 0x0d60, 0x0020, 0x0d61, 0x0020, 0x0d66, 0x0020, 0x0d67, 0x0020, 0x0d68, 0x0020, 0x0d69, 0x0020, 0x0d6a, 0x0020, 0x0d6b, 0x0020, 0x0d6c, 0x0020, 0x0d6d, 0x0020, 0x0d6e, 0x0020, 0x0d6f, 0x0020};
String16 a = {arr, buff_cap(arr)};
String16 b = string8_to_string16(scratch, str);
assert(string_compare(a,b));
String32 c = string16_to_string32(scratch, a);
String32 d = string16_to_string32(scratch, b);
assert(string_compare(c,d));
String e = string16_to_string8(scratch, a);
String f = string16_to_string8(scratch, b);
assert(string_compare(e,f));
}
{
String str = " 豈 更 車 賈 滑 串 句 龜 龜 契 金 喇 奈 懶 癩 羅 蘿 螺 裸 邏 樂 洛 烙 珞 落 酪 駱 亂 卵 欄 爛 蘭 鸞 嵐 濫 藍 襤 拉 臘 蠟 廊 朗 浪 狼 郎 來 冷 勞 擄 櫓 爐 盧 老 蘆 虜 路 露 魯 鷺 碌 祿 綠 菉 錄 鹿 論 壟 弄 籠 聾 牢 磊 賂 雷 壘 屢 樓 淚 漏 累 縷 陋 勒 肋 凜 凌 稜 綾 菱 陵 讀 拏 樂 諾 丹 寧 怒 率 異 北 磻 便 復 不 泌 數 索 參 塞 省 葉 說 殺 辰 沈 拾 若 掠 略 亮 兩 凉 梁 糧 良 諒 量 勵 ..."_s;
U16 arr[] = {0x0020, 0x0020, 0x0020, 0x0020, 0xf900, 0x0020, 0xf901, 0x0020, 0xf902, 0x0020, 0xf903, 0x0020, 0xf904, 0x0020, 0xf905, 0x0020, 0xf906, 0x0020, 0xf907, 0x0020, 0xf908, 0x0020, 0xf909, 0x0020, 0xf90a, 0x0020, 0xf90b, 0x0020, 0xf90c, 0x0020, 0xf90d, 0x0020, 0xf90e, 0x0020, 0xf90f, 0x0020, 0xf910, 0x0020, 0xf911, 0x0020, 0xf912, 0x0020, 0xf913, 0x0020, 0xf914, 0x0020, 0xf915, 0x0020, 0xf916, 0x0020, 0xf917, 0x0020, 0xf918, 0x0020, 0xf919, 0x0020, 0xf91a, 0x0020, 0xf91b, 0x0020, 0xf91c, 0x0020, 0xf91d, 0x0020, 0xf91e, 0x0020, 0xf91f, 0x0020, 0xf920, 0x0020, 0xf921, 0x0020, 0xf922, 0x0020, 0xf923, 0x0020, 0xf924, 0x0020, 0xf925, 0x0020, 0xf926, 0x0020, 0xf927, 0x0020, 0xf928, 0x0020, 0xf929, 0x0020, 0xf92a, 0x0020, 0xf92b, 0x0020, 0xf92c, 0x0020, 0xf92d, 0x0020, 0xf92e, 0x0020, 0xf92f, 0x0020, 0xf930, 0x0020, 0xf931, 0x0020, 0xf932, 0x0020, 0xf933, 0x0020, 0xf934, 0x0020, 0xf935, 0x0020, 0xf936, 0x0020, 0xf937, 0x0020, 0xf938, 0x0020, 0xf939, 0x0020, 0xf93a, 0x0020, 0xf93b, 0x0020, 0xf93c, 0x0020, 0xf93d, 0x0020, 0xf93e, 0x0020, 0xf93f, 0x0020, 0xf940, 0x0020, 0xf941, 0x0020, 0xf942, 0x0020, 0xf943, 0x0020, 0xf944, 0x0020, 0xf945, 0x0020, 0xf946, 0x0020, 0xf947, 0x0020, 0xf948, 0x0020, 0xf949, 0x0020, 0xf94a, 0x0020, 0xf94b, 0x0020, 0xf94c, 0x0020, 0xf94d, 0x0020, 0xf94e, 0x0020, 0xf94f, 0x0020, 0xf950, 0x0020, 0xf951, 0x0020, 0xf952, 0x0020, 0xf953, 0x0020, 0xf954, 0x0020, 0xf955, 0x0020, 0xf956, 0x0020, 0xf957, 0x0020, 0xf958, 0x0020, 0xf959, 0x0020, 0xf95a, 0x0020, 0xf95b, 0x0020, 0xf95c, 0x0020, 0xf95d, 0x0020, 0xf95e, 0x0020, 0xf95f, 0x0020, 0xf960, 0x0020, 0xf961, 0x0020, 0xf962, 0x0020, 0xf963, 0x0020, 0xf964, 0x0020, 0xf965, 0x0020, 0xf966, 0x0020, 0xf967, 0x0020, 0xf968, 0x0020, 0xf969, 0x0020, 0xf96a, 0x0020, 0xf96b, 0x0020, 0xf96c, 0x0020, 0xf96d, 0x0020, 0xf96e, 0x0020, 0xf96f, 0x0020, 0xf970, 0x0020, 0xf971, 0x0020, 0xf972, 0x0020, 0xf973, 0x0020, 0xf974, 0x0020, 0xf975, 0x0020, 0xf976, 0x0020, 0xf977, 0x0020, 0xf978, 0x0020, 0xf979, 0x0020, 0xf97a, 0x0020, 0xf97b, 0x0020, 0xf97c, 0x0020, 0xf97d, 0x0020, 0xf97e, 0x0020, 0xf97f, 0x0020, 0x002e, 0x002e, 0x002e};
String16 a = {arr, buff_cap(arr)};
String16 b = string8_to_string16(scratch, str);
assert(string_compare(a,b));
String32 c = string16_to_string32(scratch, a);
String32 d = string16_to_string32(scratch, b);
assert(string_compare(c,d));
String e = string16_to_string8(scratch, a);
String f = string16_to_string8(scratch, b);
assert(string_compare(e,f));
}
{
String str = " _ 。 「 」 、 ・ ヲ ァ ィ ゥ ェ ォ ャ ュ ョ ッ ー ア イ ウ エ オ カ キ ク ケ コ サ シ ス セ ソ タ チ ツ ... "_s;
U16 arr[] = {0x0020, 0x0020, 0x0020, 0x0020, 0xff01, 0x0020, 0xff02, 0x0020, 0xff03, 0x0020, 0xff04, 0x0020, 0xff05, 0x0020, 0xff06, 0x0020, 0xff07, 0x0020, 0xff08, 0x0020, 0xff09, 0x0020, 0xff0a, 0x0020, 0xff0b, 0x0020, 0xff0c, 0x0020, 0xff0d, 0x0020, 0xff0e, 0x0020, 0xff0f, 0x0020, 0xff10, 0x0020, 0xff11, 0x0020, 0xff12, 0x0020, 0xff13, 0x0020, 0xff14, 0x0020, 0xff15, 0x0020, 0xff16, 0x0020, 0xff17, 0x0020, 0xff18, 0x0020, 0xff19, 0x0020, 0xff1a, 0x0020, 0xff1b, 0x0020, 0xff1c, 0x0020, 0xff1d, 0x0020, 0xff1e, 0x0020, 0xff1f, 0x0020, 0xff20, 0x0020, 0xff21, 0x0020, 0xff22, 0x0020, 0xff23, 0x0020, 0xff24, 0x0020, 0xff25, 0x0020, 0xff26, 0x0020, 0xff27, 0x0020, 0xff28, 0x0020, 0xff29, 0x0020, 0xff2a, 0x0020, 0xff2b, 0x0020, 0xff2c, 0x0020, 0xff2d, 0x0020, 0xff2e, 0x0020, 0xff2f, 0x0020, 0xff30, 0x0020, 0xff31, 0x0020, 0xff32, 0x0020, 0xff33, 0x0020, 0xff34, 0x0020, 0xff35, 0x0020, 0xff36, 0x0020, 0xff37, 0x0020, 0xff38, 0x0020, 0xff39, 0x0020, 0xff3a, 0x0020, 0xff3b, 0x0020, 0xff3c, 0x0020, 0xff3d, 0x0020, 0xff3e, 0x0020, 0xff3f, 0x0020, 0xff40, 0x0020, 0xff41, 0x0020, 0xff42, 0x0020, 0xff43, 0x0020, 0xff44, 0x0020, 0xff45, 0x0020, 0xff46, 0x0020, 0xff47, 0x0020, 0xff48, 0x0020, 0xff49, 0x0020, 0xff4a, 0x0020, 0xff4b, 0x0020, 0xff4c, 0x0020, 0xff4d, 0x0020, 0xff4e, 0x0020, 0xff4f, 0x0020, 0xff50, 0x0020, 0xff51, 0x0020, 0xff52, 0x0020, 0xff53, 0x0020, 0xff54, 0x0020, 0xff55, 0x0020, 0xff56, 0x0020, 0xff57, 0x0020, 0xff58, 0x0020, 0xff59, 0x0020, 0xff5a, 0x0020, 0xff5b, 0x0020, 0xff5c, 0x0020, 0xff5d, 0x0020, 0xff5e, 0x0020, 0xff61, 0x0020, 0xff62, 0x0020, 0xff63, 0x0020, 0xff64, 0x0020, 0xff65, 0x0020, 0xff66, 0x0020, 0xff67, 0x0020, 0xff68, 0x0020, 0xff69, 0x0020, 0xff6a, 0x0020, 0xff6b, 0x0020, 0xff6c, 0x0020, 0xff6d, 0x0020, 0xff6e, 0x0020, 0xff6f, 0x0020, 0xff70, 0x0020, 0xff71, 0x0020, 0xff72, 0x0020, 0xff73, 0x0020, 0xff74, 0x0020, 0xff75, 0x0020, 0xff76, 0x0020, 0xff77, 0x0020, 0xff78, 0x0020, 0xff79, 0x0020, 0xff7a, 0x0020, 0xff7b, 0x0020, 0xff7c, 0x0020, 0xff7d, 0x0020, 0xff7e, 0x0020, 0xff7f, 0x0020, 0xff80, 0x0020, 0xff81, 0x0020, 0xff82, 0x0020, 0x002e, 0x002e, 0x002e, 0x0020};
String16 a = {arr, buff_cap(arr)};
String16 b = string8_to_string16(scratch, str);
assert(string_compare(a,b));
String32 c = string16_to_string32(scratch, a);
String32 d = string16_to_string32(scratch, b);
assert(string_compare(c,d));
String e = string16_to_string8(scratch, a);
String f = string16_to_string8(scratch, b);
assert(string_compare(e,f));
}
{ // With surrogate pairs
U8 arr8[] = {0xf0, 0x90, 0x90, 0x90, 0xf0, 0x90, 0x90, 0x91};
String str = {arr8, buff_cap(arr8)};
U16 arr[] = {0xd801, 0xdc10, 0xd801, 0xdc11};
String16 a = {arr, buff_cap(arr)};
String16 b = string8_to_string16(scratch, str);
assert(string_compare(a,b));
String32 c = string16_to_string32(scratch, a);
String32 d = string16_to_string32(scratch, b);
assert(string_compare(c,d));
String e = string16_to_string8(scratch, a);
String f = string16_to_string8(scratch, b);
assert(string_compare(e,f));
}
}

View File

@@ -5,8 +5,8 @@
#define Set_BigInt_Arena(x) BigInt_Arena bigint_allocator(x) #define Set_BigInt_Arena(x) BigInt_Arena bigint_allocator(x)
struct BigInt_Arena{ struct BigInt_Arena{
Arena *old; Allocator *old;
BigInt_Arena(Arena *allocator){old = bigint_allocator; bigint_allocator = allocator;} BigInt_Arena(Allocator *allocator){old = bigint_allocator; bigint_allocator = allocator;}
~BigInt_Arena(){bigint_allocator = old;} ~BigInt_Arena(){bigint_allocator = old;}
}; };
@@ -39,7 +39,7 @@ bigint_add(const BigInt *a, const BigInt *b){
} }
CORE_Static BigInt CORE_Static BigInt
bigint_copy(Arena *allocator, BigInt *src){ bigint_copy(Allocator *allocator, BigInt *src){
BigInt dest = {}; BigInt dest = {};
if (src->digit_count == 0){ if (src->digit_count == 0){
bigint_init_unsigned(&dest, 0); bigint_init_unsigned(&dest, 0);
@@ -54,7 +54,6 @@ bigint_copy(Arena *allocator, BigInt *src){
dest.is_negative = src->is_negative; dest.is_negative = src->is_negative;
dest.digit_count = src->digit_count; dest.digit_count = src->digit_count;
count_bigint_alloc();
dest.digits = allocate_array(allocator, uint64_t, dest.digit_count); dest.digits = allocate_array(allocator, uint64_t, dest.digit_count);
memcpy(dest.digits, src->digits, sizeof(uint64_t) * dest.digit_count); memcpy(dest.digits, src->digits, sizeof(uint64_t) * dest.digit_count);
return dest; return dest;
@@ -1925,7 +1924,7 @@ void bigint_print(BigInt *bigint, uint64_t base)
} }
} }
const char *bigint_to_error_string(Arena *allocator, const BigInt *bigint, uint64_t base) const char *bigint_to_error_string(Allocator *allocator, const BigInt *bigint, uint64_t base)
{ {
Set_BigInt_Arena(allocator); Set_BigInt_Arena(allocator);
if (bigint->digit_count == 0) if (bigint->digit_count == 0)
@@ -1948,7 +1947,7 @@ const char *bigint_to_error_string(Arena *allocator, const BigInt *bigint, uint6
return res; return res;
} }
size_t len = bigint->digit_count * 64; size_t len = bigint->digit_count * 64;
char *start = (char *)arena_push_size(allocator, len); char *start = (char *)allocate_size(allocator, len);
char *buf = start; char *buf = start;
BigInt digit_bi = { 0 }; BigInt digit_bi = { 0 };
@@ -1980,7 +1979,7 @@ const char *bigint_to_error_string(Arena *allocator, const BigInt *bigint, uint6
} }
// reverse // reverse
char *out = (char *)arena_push_size(allocator, buf - start + 2); char *out = (char *)allocate_size(allocator, buf - start + 2);
char *current = out; char *current = out;
if (bigint->is_negative) if (bigint->is_negative)
{ {

View File

@@ -8,8 +8,7 @@ enum CmpRes
CMP_EQ, CMP_EQ,
}; };
#define count_bigint_alloc() (bigint_allocator != thread_ctx.scratch ? bigint_allocation_count++ : 0) #define malloc_arena(x) allocate_size(bigint_allocator, x)
#define malloc_arena(x) (count_bigint_alloc(), allocate_size(bigint_allocator, x))
#define ALLOC_DIGITS(_digits) (uint64_t *)((_digits) ? malloc_arena(sizeof(uint64_t) * (_digits)) : NULL) #define ALLOC_DIGITS(_digits) (uint64_t *)((_digits) ? malloc_arena(sizeof(uint64_t) * (_digits)) : NULL)
#define FATAL_ERROR(x) compiler_error(0, x) #define FATAL_ERROR(x) compiler_error(0, x)

View File

@@ -31,6 +31,15 @@ static void *scratch_arena_push_size(Scratch_Arena *arena, size_t size) {
return (void *)result; return (void *)result;
} }
static Scratch_Arena *make_scratch_arena(void *buff, size_t size) {
Scratch_Arena *scratch = (Scratch_Arena *)buff;
scratch->len = 0;
scratch->cap = size - sizeof(Scratch_Arena);
scratch->allocate = (Allocator::Allocate *)scratch_arena_push_size;
scratch->deallocate = deallocate_stub;
return scratch;
}
static Scratch_Arena *allocate_scratch_arena(Allocator *allocator, size_t size_without_members) { static Scratch_Arena *allocate_scratch_arena(Allocator *allocator, size_t size_without_members) {
Scratch_Arena *scratch = (Scratch_Arena *)allocate_size(allocator, size_without_members + sizeof(Scratch_Arena), false); Scratch_Arena *scratch = (Scratch_Arena *)allocate_size(allocator, size_without_members + sizeof(Scratch_Arena), false);
scratch->cap = size_without_members; scratch->cap = size_without_members;

View File

@@ -178,7 +178,7 @@ ast_array(Token *pos, Ast_Expr *expr){
} }
CORE_Static Ast_Scope * CORE_Static Ast_Scope *
begin_decl_scope(Arena *scratch, Token *pos){ begin_decl_scope(Allocator *scratch, Token *pos){
AST_NEW(Scope, SCOPE, pos, AST_DECL); AST_NEW(Scope, SCOPE, pos, AST_DECL);
result->file = pctx->currently_parsed_file; result->file = pctx->currently_parsed_file;
result->module = pctx->currently_parsed_file->module; result->module = pctx->currently_parsed_file->module;
@@ -195,7 +195,7 @@ finalize_decl_scope(Ast_Scope *scope){
} }
CORE_Static Ast_Scope * CORE_Static Ast_Scope *
begin_stmt_scope(Arena *scratch, Token *pos){ begin_stmt_scope(Allocator *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;
@@ -263,7 +263,7 @@ ast_type(Token *pos, Intern_String name, Ast_Type *type){
} }
CORE_Static Ast_Scope * CORE_Static Ast_Scope *
ast_decl_scope(Token *pos, Arena *allocator, Ast_File *file){ ast_decl_scope(Token *pos, Allocator *allocator, Ast_File *file){
AST_NEW(Scope, SCOPE, pos, AST_DECL); AST_NEW(Scope, SCOPE, pos, AST_DECL);
result->file = file; result->file = file;

View File

@@ -34,7 +34,7 @@ gen_last_line(){
} }
CORE_Static String CORE_Static String
string_scope_name(Arena *a, Ast_Scope *scope){ string_scope_name(Allocator *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");
@@ -44,17 +44,17 @@ string_scope_name(Arena *a, Ast_Scope *scope){
CORE_Static void CORE_Static void
gen_scope_name(Ast_Scope *scope){ gen_scope_name(Ast_Scope *scope){
Scratch scratch; Scratch_Arena *scratch = pctx->scratch;
Scratch_Scope _scope(scratch);
String string = string_scope_name(scratch, scope); String string = string_scope_name(scratch, scope);
gen("%.*s", (int)string.len, string.str); gen("%.*s", (int)string.len, string.str);
} }
CORE_Static String CORE_Static String
unique_name(Arena *allocator, Ast *ast){ unique_name_scratch(Allocator *scratch, Ast *ast){
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(scratch, "%Q%d", result, ast->pos->line);
return result; return result;
} }
@@ -87,7 +87,7 @@ get_ctype_name_for_type(Ast_Type *type){
} }
CORE_Static String CORE_Static String
string_simple_decl_prefix(Arena *a, Ast_Type *ast){ string_simple_decl_prefix(Allocator *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);
@@ -124,7 +124,7 @@ string_simple_decl_prefix(Arena *a, Ast_Type *ast){
} }
CORE_Static String CORE_Static String
string_simple_decl_postfix(Arena *a, Ast_Type *ast){ string_simple_decl_postfix(Allocator *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);
@@ -143,7 +143,7 @@ string_simple_decl_postfix(Arena *a, Ast_Type *ast){
} }
CORE_Static String CORE_Static String
string_simple_decl(Arena *a, Ast_Type *ast, Intern_String name = {}){ string_simple_decl(Allocator *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);
@@ -169,19 +169,13 @@ string_simple_decl(Arena *a, Ast_Type *ast, Intern_String name = {}){
CORE_Static void CORE_Static void
gen_simple_decl(Ast_Type *ast, Intern_String name = {}){ gen_simple_decl(Ast_Type *ast, Intern_String name = {}){
Scratch scratch; Scratch_Arena *scratch = pctx->scratch;
Scratch_Scope _scope(scratch);
String string = string_simple_decl(scratch, ast, name); String string = string_simple_decl(scratch, ast, name);
gen("%.*s", (int)string.len, string.str); gen("%.*s", (int)string.len, string.str);
} }
CORE_Static String
gen_string_simple_decl(Arena *a, Ast_Type *ast, String name){
Scratch scratch;
String string = string_simple_decl(scratch, ast, pctx->intern(name));
String result = string_copy(a, string);
return result;
}
CORE_Static String CORE_Static String
get_type_postfix(Ast_Type *type){ get_type_postfix(Ast_Type *type){
switch(type->kind) { switch(type->kind) {
@@ -211,7 +205,9 @@ gen_value(Token *pos, Value a){
switch(type->kind){ switch(type->kind){
CASE_INT: { CASE_INT: {
Scratch scratch; Scratch_Arena *scratch = pctx->scratch;
Scratch_Scope _scope(scratch);
String postfix = get_type_postfix(type); String postfix = get_type_postfix(type);
const char *string = bigint_to_error_string(scratch, &a.big_int_val, 10); const char *string = bigint_to_error_string(scratch, &a.big_int_val, 10);
gen("%s%Q", string, postfix); gen("%s%Q", string, postfix);
@@ -535,8 +531,10 @@ gen_ast(Ast *ast){
CASE(RETURN, Return){ CASE(RETURN, Return){
if(is_tuple(node->resolved_type)) { if(is_tuple(node->resolved_type)) {
Scratch scratch; Scratch_Arena *scratch = pctx->scratch;
Intern_String var_name = pctx->intern(unique_name(scratch, node)); Scratch_Scope _scope(scratch);
Intern_String var_name = pctx->intern(unique_name_scratch(scratch, node));
gen_simple_decl(node->resolved_type, var_name); gen_simple_decl(node->resolved_type, var_name);
gen(";"); gen(";");
@@ -744,8 +742,10 @@ gen_ast(Ast *ast){
For(node->vars) For(node->vars)
gen_ast(it); gen_ast(it);
Scratch scratch; Scratch_Arena *scratch = pctx->scratch;
Intern_String var_name = pctx->intern(unique_name(scratch, node)); Scratch_Scope _scope(scratch);
Intern_String var_name = pctx->intern(unique_name_scratch(scratch, node));
gen_simple_decl(node->resolved_type, var_name); gen_simple_decl(node->resolved_type, var_name);
gen(" = "); gen(" = ");
gen_expr(node->expr); gen_expr(node->expr);
@@ -845,7 +845,9 @@ typedef struct String{
// Generate slice and tuple types // Generate slice and tuple types
Iter(&pctx->all_types){ Iter(&pctx->all_types){
Scratch scratch; Scratch_Arena *scratch = pctx->scratch;
Scratch_Scope _scope(scratch);
Ast_Type *type = it.item[0]; Ast_Type *type = it.item[0];
if(type->kind == TYPE_SLICE){ if(type->kind == TYPE_SLICE){

View File

@@ -138,7 +138,8 @@ parse_all_modules() {
CORE_Static Ast_Module * CORE_Static Ast_Module *
add_module(Token *pos, Intern_String filename, B32 command_line_module) { add_module(Token *pos, Intern_String filename, B32 command_line_module) {
Scratch scratch; Scratch_Arena *scratch = pctx->scratch;
Scratch_Scope _scope(scratch);
String absolute_file_path = {}; String absolute_file_path = {};
String absolute_base_folder = {}; String absolute_base_folder = {};
@@ -292,14 +293,17 @@ const U32 COMPILE_TESTING = 0x8;
CORE_Static void CORE_Static void
compile_file(Arena *arena, String filename, U32 compile_flags = COMPILE_NULL) { compile_file(Arena *arena, String filename, U32 compile_flags = COMPILE_NULL) {
String result = compile_file_to_string(arena, filename);
if (is_flag_set(compile_flags, COMPILE_AND_RUN)) { if (is_flag_set(compile_flags, COMPILE_AND_RUN)) {
log_info_no_nl("%Q - ", filename); log_info_no_nl("%Q - ", filename);
} }
String result = compile_file_to_string(arena, filename); B32 r = os_write_file("program.c"_s, result);
assert(os_write_file("program.c"_s, result)); assert(r);
Scratch_Arena *scratch = pctx->scratch;
Scratch_Scope _scope(scratch);
Scratch scratch;
F64 begin = os_time(); F64 begin = os_time();
String_Builder builder = {scratch}; String_Builder builder = {scratch};
builder.addf("clang program.c -Wall -Wno-unused-function -Wno-parentheses-equality -g -o a" OS_EXE " "); builder.addf("clang program.c -Wall -Wno-unused-function -Wno-parentheses-equality -g -o a" OS_EXE " ");
@@ -321,12 +325,13 @@ compile_file(Arena *arena, String filename, U32 compile_flags = COMPILE_NULL) {
} }
if (is_flag_set(compile_flags, COMPILE_PRINT_ALLOCATOR_STATS_BEFORE_DESTROY)) { if (is_flag_set(compile_flags, COMPILE_PRINT_ALLOCATOR_STATS_BEFORE_DESTROY)) {
Arena *p = (Arena *)pctx->perm; // @! allocator stats
Arena *stage = (Arena *)&pctx->stage_arena; //Arena *p = (Arena *)pctx->perm;
log_info("Pernament arena len: %llu commit: %llu", p->len, p->memory.commit); //Arena *stage = (Arena *)&pctx->stage_arena;
log_info("Stage arena len: %llu commit: %llu", stage->len, stage->memory.commit); //log_info("Pernament arena len: %llu commit: %llu", pct, p->memory.commit);
log_info("Scratch1 len: %llu commit: %llu", thread_ctx.scratch[0].len, thread_ctx.scratch[0].memory.commit); //log_info("Stage arena len: %llu commit: %llu", stage->len, stage->memory.commit);
log_info("Scratch2 len: %llu commit: %llu", thread_ctx.scratch[1].len, thread_ctx.scratch[1].memory.commit); //log_info("Scratch1 len: %llu commit: %llu", thread_ctx.scratch[0].len, thread_ctx.scratch[0].memory.commit);
//log_info("Scratch2 len: %llu commit: %llu", thread_ctx.scratch[1].len, thread_ctx.scratch[1].memory.commit);
} }
if (is_flag_set(compile_flags, COMPILE_AND_RUN)) { if (is_flag_set(compile_flags, COMPILE_AND_RUN)) {

View File

@@ -7,8 +7,7 @@ global bool color_codes_enabled;
thread_local Core_Ctx *pctx; thread_local Core_Ctx *pctx;
Allocator *bigint_allocator;
Arena *bigint_allocator;
global S64 bigint_allocation_count; global S64 bigint_allocation_count;
global Token token_null = {SAME_SCOPE}; global Token token_null = {SAME_SCOPE};

View File

@@ -80,8 +80,8 @@ token_error(Token *t, String error_val){
CORE_Static void CORE_Static void
lex_parse_u64(Core_Ctx *lexer, Token *t, S64 base){ lex_parse_u64(Core_Ctx *lexer, Token *t, S64 base){
Scratch scratch; Scratch_Scope _scope(lexer->scratch);
Set_BigInt_Arena(scratch); Set_BigInt_Arena(lexer->scratch);
t->kind = TK_Integer; t->kind = TK_Integer;
BigInt m = bigint_u64(1); BigInt m = bigint_u64(1);
@@ -592,8 +592,8 @@ lex_restream(Core_Ctx *lexer, String istream, String file){
lexer->stream.line_begin = istream.str; lexer->stream.line_begin = istream.str;
lexer->stream.file = lexer->intern(file); lexer->stream.file = lexer->intern(file);
Scratch scratch; Scratch_Scope _scope(lexer->scratch);
lexer->stream.indent_stack.allocator = scratch; lexer->stream.indent_stack.allocator = lexer->scratch;
lexer->stream.indent_stack.add(&token_null); lexer->stream.indent_stack.add(&token_null);
lex__stream(lexer); lex__stream(lexer);
} }

View File

@@ -259,7 +259,6 @@ For modules it's a bit different cause they should be distributed as valid.
#include "base.cpp" #include "base.cpp"
#include "base_unicode.cpp" #include "base_unicode.cpp"
#include "core_arena.cpp"
#include "os.h" #include "os.h"
#if OS_WINDOWS #if OS_WINDOWS
#include "os_windows.cpp" #include "os_windows.cpp"
@@ -287,13 +286,11 @@ For modules it's a bit different cause they should be distributed as valid.
int main(int argument_count, char **arguments){ int main(int argument_count, char **arguments){
thread_ctx_init();
Arena arena = {}; Arena arena = {};
arena_init(&arena, "Pernament arena"_s); arena_init(&arena, "Pernament arena"_s);
Scratch_Arena *scratch_arena = allocate_scratch_arena(&arena, mib(1));
Scratch scratch; Array<String> args = {scratch_arena};
Array<String> args = {scratch};
for(int i = 1; i < argument_count; i+=1){ for(int i = 1; i < argument_count; i+=1){
String arg = string_from_cstring(arguments[i]); String arg = string_from_cstring(arguments[i]);
args.add(arg); args.add(arg);
@@ -332,20 +329,14 @@ int main(int argument_count, char **arguments){
test_os_memory(); test_os_memory();
#endif #endif
test_unicode();
map_test();
test_string_builder();
test_intern_table();
// emit_line_directives = false; // emit_line_directives = false;
// emit_type_info = false; // emit_type_info = false;
For(args){ For(args){
if(it == "-testing"_s){ // @copy_paste if(it == "-testing"_s){ // @copy_paste
Scratch scratch; Scratch_Scope _scope(scratch_arena);
Array<OS_File_Info> examples = os_list_dir(scratch, "examples"_s); Array<OS_File_Info> examples = os_list_dir(scratch_arena, scratch_arena, "examples"_s);
Array<OS_File_Info> tests = os_list_dir(scratch, "tests"_s); Array<OS_File_Info> tests = os_list_dir(scratch_arena, scratch_arena, "tests"_s);
For(examples){ For(examples){
if(it.is_directory) continue; if(it.is_directory) continue;
compile_file(&arena, it.absolute_path, COMPILE_AND_RUN | COMPILE_TESTING); compile_file(&arena, it.absolute_path, COMPILE_AND_RUN | COMPILE_TESTING);

View File

@@ -1,5 +1,25 @@
CORE_Static Ast_Decl *parse_decl(B32 is_global); CORE_Static Ast_Decl *parse_decl(B32 is_global);
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);
#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_Arena *scratch = pctx->scratch;
Scratch_Scope _scope(scratch);
STRING_FMT(scratch, str, message);
printf("%s", message.str);
if(kind != Log_Kind_Normal_No_NewLine){
printf("\n");
}
}
CORE_Static void CORE_Static void
print_token_line(Token *token){ print_token_line(Token *token){
if(!token) return; if(!token) return;
@@ -37,8 +57,7 @@ print_token_context(Token *token){
CORE_Static void CORE_Static void
compiler_error(Token *token1, Token *token2, const char *str, ...){ compiler_error(Token *token1, Token *token2, const char *str, ...){
Scratch scratch; STRING_FMT(pctx->perm, str, string);
STRING_FMT(scratch, str, string);
log_info_no_nl("\n%s", string.str); log_info_no_nl("\n%s", string.str);
if(token1){ if(token1){
@@ -64,8 +83,7 @@ compiler_error(Token *token1, Token *token2, const char *str, ...){
CORE_Static void CORE_Static void
compiler_error(Token *token, const char *str, ...){ compiler_error(Token *token, const char *str, ...){
Scratch scratch; STRING_FMT(pctx->perm, str, string);
STRING_FMT(scratch, str, string);
if(token) log_info_no_nl("\n%s:%d %Q", token->file.str, (S32)token->line + 1, string); if(token) log_info_no_nl("\n%s:%d %Q", token->file.str, (S32)token->line + 1, string);
else log_info_no_nl("\n%s", string.str); else log_info_no_nl("\n%s", string.str);
@@ -205,7 +223,8 @@ parse_init_stmt(Ast_Expr *expr){
CORE_Static Ast_Call * CORE_Static Ast_Call *
parse_expr_call(Ast_Expr *left, Token_Kind close_kind){ parse_expr_call(Ast_Expr *left, Token_Kind close_kind){
Scratch scratch; Scratch_Arena *scratch = pctx->scratch;
Scratch_Scope __scope(scratch);
Token *pos = token_get(); Token *pos = token_get();
Array<Ast_Call_Item *> exprs = {scratch}; Array<Ast_Call_Item *> exprs = {scratch};
@@ -257,7 +276,8 @@ parse_stmt_scope(Ast_Scope *scope_defined_outside = 0){
if(token_expect(OPEN_SCOPE)){ // @todo: Fix error message here, it doesn't show proper token context if(token_expect(OPEN_SCOPE)){ // @todo: Fix error message here, it doesn't show proper token context
Token *token_block = token_get(); Token *token_block = token_get();
Scratch scratch; Scratch_Arena *scratch = pctx->scratch;
Scratch_Scope __scope(scratch);
if(!scope_defined_outside) scope = begin_stmt_scope(scratch, token_block); if(!scope_defined_outside) scope = begin_stmt_scope(scratch, token_block);
do{ do{
Token *token = token_get(); Token *token = token_get();
@@ -441,7 +461,8 @@ parse_stmt_scope(Ast_Scope *scope_defined_outside = 0){
CORE_Static Ast_Lambda * CORE_Static Ast_Lambda *
parse_lambda(Token *token){ parse_lambda(Token *token){
Scratch scratch; Scratch_Arena *scratch = pctx->scratch;
Scratch_Scope __scope(scratch);
Array<Ast_Decl *> params = {scratch}; Array<Ast_Decl *> params = {scratch};
if(!token_is(TK_CloseParen)){ if(!token_is(TK_CloseParen)){
@@ -668,7 +689,8 @@ parse_assign_expr(){
CORE_Static Ast_Decl * CORE_Static Ast_Decl *
parse_struct(Token *pos){ parse_struct(Token *pos){
Scratch scratch; Scratch_Arena *scratch = pctx->scratch;
Scratch_Scope __scope(scratch);
token_match(OPEN_SCOPE); token_match(OPEN_SCOPE);
Ast_Scope *scope = begin_decl_scope(scratch, token_get()); Ast_Scope *scope = begin_decl_scope(scratch, token_get());
@@ -692,7 +714,8 @@ parse_struct(Token *pos){
CORE_Static Ast_Decl * CORE_Static Ast_Decl *
parse_enum(Token *pos){ parse_enum(Token *pos){
Scratch scratch; Scratch_Arena *scratch = pctx->scratch;
Scratch_Scope __scope(scratch);
Ast_Expr *typespec = parse_optional_type(); Ast_Expr *typespec = parse_optional_type();
Token *flag = token_match_pound(intern_flag); Token *flag = token_match_pound(intern_flag);
@@ -772,12 +795,12 @@ register_ast_file(Token *pos, String absolute_file_path, Ast_Module *module, B32
CORE_Static Intern_String CORE_Static Intern_String
preprocess_filename(Token *token_filename){ preprocess_filename(Token *token_filename){
Scratch scratch; Scratch_Scope _scope(pctx->scratch);
String filename = token_filename->intern_val.s; String filename = token_filename->intern_val.s;
Array<String_Replace> replace = {scratch}; Array<String_Replace> replace = {pctx->scratch};
replace.add({"$OS"_s, OS_NAME}); replace.add({"$OS"_s, OS_NAME});
replace.add({"$os"_s, OS_NAME_LOWER}); replace.add({"$os"_s, OS_NAME_LOWER});
String result0 = string_replace(scratch, filename, replace); String result0 = string_replace(pctx->scratch, pctx->scratch, filename, replace);
Intern_String result = pctx->intern(result0); Intern_String result = pctx->intern(result0);
return result; return result;
} }
@@ -918,7 +941,8 @@ CORE_Static void
parse_file(Ast_File *file){ parse_file(Ast_File *file){
assert(file); assert(file);
Scratch scratch; Scratch_Arena *scratch = pctx->scratch;
Scratch_Scope __scope(scratch);
file->filecontent = os_read_file(pctx->perm, file->absolute_file_path); file->filecontent = os_read_file(pctx->perm, file->absolute_file_path);
if(file->filecontent.len == 0){ if(file->filecontent.len == 0){
compiler_error(file->pos, "Failed to open file \"%Q\"", file->absolute_file_path); compiler_error(file->pos, "Failed to open file \"%Q\"", file->absolute_file_path);

View File

@@ -114,7 +114,8 @@ CORE_Static void
check_value_bounds(Token *pos, Value *a){ check_value_bounds(Token *pos, Value *a){
if(!is_int(a->type)) return; if(!is_int(a->type)) return;
Scratch scratch; Scratch_Arena *scratch = pctx->scratch;
Scratch_Scope __scope(scratch);
if(!bigint_fits_in_bits(&a->big_int_val, a->type->size*8, is_signed_int(a->type))){ if(!bigint_fits_in_bits(&a->big_int_val, a->type->size*8, is_signed_int(a->type))){
const char *string = bigint_to_error_string(scratch, &a->big_int_val, 10); const char *string = bigint_to_error_string(scratch, &a->big_int_val, 10);
compiler_error(pos, "Value %s doesn't fit in type %Q", string, typestring(a->type)); compiler_error(pos, "Value %s doesn't fit in type %Q", string, typestring(a->type));
@@ -489,7 +490,7 @@ scope_search(Scope_Search *search){
} }
CORE_Static Scope_Search CORE_Static Scope_Search
make_scope_search(Arena *arena, Ast_Scope *scope, Intern_String name){ make_scope_search(Allocator *arena, Ast_Scope *scope, Intern_String name){
Scope_Search result = {}; Scope_Search result = {};
result.results.allocator = arena; result.results.allocator = arena;
result.name = name; result.name = name;
@@ -502,7 +503,9 @@ make_scope_search(Arena *arena, Ast_Scope *scope, Intern_String name){
CORE_Static Ast_Decl * CORE_Static Ast_Decl *
search_for_single_decl(Ast_Scope *scope, Intern_String name){ search_for_single_decl(Ast_Scope *scope, Intern_String name){
Scratch scratch; Scratch_Arena *scratch = pctx->scratch;
Scratch_Scope _scope(scratch);
Scope_Search search = make_scope_search(scratch, scope, name); Scope_Search search = make_scope_search(scratch, scope, name);
scope_search(&search); scope_search(&search);
@@ -518,7 +521,8 @@ search_for_single_decl(Ast_Scope *scope, Intern_String name){
CORE_Static Ast_Decl * CORE_Static Ast_Decl *
resolve_name(Ast_Scope *scope, Token *pos, Intern_String name, Search_Flag search_flags){ resolve_name(Ast_Scope *scope, Token *pos, Intern_String name, Search_Flag search_flags){
Scratch scratch; Scratch_Arena *scratch = pctx->scratch;
Scratch_Scope _scope(scratch);
Scope_Search search = make_scope_search(scratch, scope, name); Scope_Search search = make_scope_search(scratch, scope, name);
search.search_only_current_scope = search_flags & SEARCH_ONLY_CURRENT_SCOPE; search.search_only_current_scope = search_flags & SEARCH_ONLY_CURRENT_SCOPE;
scope_search(&search); scope_search(&search);
@@ -551,7 +555,8 @@ resolve_operator_overload(Ast_Scope *scope, Ast_Type *left, Ast_Type *right, Tok
// Search for all possible candidates in three scopes // Search for all possible candidates in three scopes
// The current module, left type definition module, right type definition module // The current module, left type definition module, right type definition module
Scratch scratch; Scratch_Arena *scratch = pctx->scratch;
Scratch_Scope _scope(scratch);
Scope_Search search = make_scope_search(scratch, scope, op_info->op); Scope_Search search = make_scope_search(scratch, scope, op_info->op);
if( left->ast && left->ast->parent_scope) search.scopes.add(left->ast->parent_scope); if( left->ast && left->ast->parent_scope) search.scopes.add(left->ast->parent_scope);
if(right && right->ast && right->ast->parent_scope) search.scopes.add(right->ast->parent_scope); if(right && right->ast && right->ast->parent_scope) search.scopes.add(right->ast->parent_scope);
@@ -585,7 +590,8 @@ insert_into_scope(Ast_Scope *scope, Ast_Decl *decl){
// //
// It's also called when scanning top level declarations of a module // It's also called when scanning top level declarations of a module
// as such we probably don't want to call any resolve stuff here // as such we probably don't want to call any resolve stuff here
Scratch scratch; Scratch_Arena *scratch = pctx->scratch;
Scratch_Scope _scope(scratch);
Scope_Search search = make_scope_search(scratch, scope, decl->name); Scope_Search search = make_scope_search(scratch, scope, decl->name);
search.search_only_current_scope = true; search.search_only_current_scope = true;
scope_search(&search); scope_search(&search);
@@ -670,7 +676,8 @@ resolve_typespec(Ast_Expr *ast, Resolve_Flag flags){
if(!ast && is_flag_set(flags, AST_CAN_BE_NULL)) if(!ast && is_flag_set(flags, AST_CAN_BE_NULL))
return 0; return 0;
Scratch scratch; Scratch_Arena *scratch = pctx->scratch;
Scratch_Scope _scope(scratch);
Operand resolved = resolve_expr(ast, flags | RESOLVE_TYPESPEC, 0, 0); Operand resolved = resolve_expr(ast, flags | RESOLVE_TYPESPEC, 0, 0);
if(is_flag_set(flags, RESOLVE_TYPESPEC_COMPLETE)) if(is_flag_set(flags, RESOLVE_TYPESPEC_COMPLETE))
type_complete(resolved.type_val); type_complete(resolved.type_val);
@@ -687,7 +694,8 @@ resolve_and_require_bool(const char *error, Ast_Expr *expr, Resolve_Flag flags){
else compiler_error(0, "Compiler error: Null expression"); else compiler_error(0, "Compiler error: Null expression");
} }
Scratch scratch; Scratch_Arena *scratch = pctx->scratch;
Scratch_Scope _scope(scratch);
Operand op = resolve_expr(expr, flags, 0, 0); Operand op = resolve_expr(expr, flags, 0, 0);
if(!is_bool(op.type)){ if(!is_bool(op.type)){
compiler_error(expr->pos, "Expected type [Bool] got instead type %Q :: %s", typestring(op.type), error); compiler_error(expr->pos, "Expected type [Bool] got instead type %Q :: %s", typestring(op.type), error);
@@ -723,7 +731,8 @@ resolve_stmt(Ast *ast, Ast_Type *ret){
switch(ast->kind){ switch(ast->kind){
CASE(RETURN, Return){ // @todo: need to check if all paths return a value CASE(RETURN, Return){ // @todo: need to check if all paths return a value
Scratch scratch; Scratch_Arena *scratch = pctx->scratch;
Scratch_Scope _scope(scratch);
Array<Ast_Type *> types = {scratch}; Array<Ast_Type *> types = {scratch};
For_It(node->expr){ For_It(node->expr){
@@ -885,7 +894,8 @@ resolve_stmt(Ast *ast, Ast_Type *ret){
CORE_Static Ast_Type * CORE_Static Ast_Type *
resolve_lambda_type(Ast_Lambda *lambda){ resolve_lambda_type(Ast_Lambda *lambda){
Scratch scratch; Scratch_Arena *scratch = pctx->scratch;
Scratch_Scope _scope(scratch);
Array<Ast_Type *> args = {scratch}; Array<Ast_Type *> args = {scratch};
Array<Ast_Type *> ret = {scratch}; Array<Ast_Type *> ret = {scratch};
For(lambda->ret) { For(lambda->ret) {
@@ -1508,7 +1518,8 @@ resolve_expr(Ast_Expr *ast, Resolve_Flag flags, Ast_Type *compound_and_const_str
} }
node->resolved_decl = name.resolved_decl; node->resolved_decl = name.resolved_decl;
Scratch scratch; Scratch_Arena *scratch = pctx->scratch;
Scratch_Scope _scope(scratch);
Array<Ast_Call_Item *> items = {scratch}; Array<Ast_Call_Item *> items = {scratch};
S64 default_iter = 0; S64 default_iter = 0;
@@ -1631,7 +1642,9 @@ resolve_decl(Ast_Decl *ast){
try_resolving_lambda_scope(&result, lambda, node->type); try_resolving_lambda_scope(&result, lambda, node->type);
node->value = result.value; node->value = result.value;
Scratch scratch; Scratch_Arena *scratch = pctx->scratch;
Scratch_Scope _scope(scratch);
node->unique_name = node->name; node->unique_name = node->name;
if(!is_flag_set(node->expr->flags, AST_FOREIGN)){ if(!is_flag_set(node->expr->flags, AST_FOREIGN)){
node->unique_name = pctx->intern(string_fmt(scratch, "%Q%Q%d", symbol_prefix, node->name, pctx->lambda_ids++)); node->unique_name = pctx->intern(string_fmt(scratch, "%Q%Q%d", symbol_prefix, node->name, pctx->lambda_ids++));

View File

@@ -60,6 +60,5 @@ enum{
CORE_Static Operand resolve_expr(Ast_Expr *ast, Resolve_Flag flags, Ast_Type *compound_context, Ast_Scope *field_access_scope); CORE_Static Operand resolve_expr(Ast_Expr *ast, Resolve_Flag flags, Ast_Type *compound_context, Ast_Scope *field_access_scope);
CORE_Static void resolve_decl(Ast_Decl *ast); CORE_Static void resolve_decl(Ast_Decl *ast);
CORE_Static Ast_Decl *resolve_name(Ast_Scope *parent_scope, Token *pos, Intern_String name, Search_Flag search_flags = 0); CORE_Static Ast_Decl *resolve_name(Ast_Scope *parent_scope, Token *pos, Intern_String name, Search_Flag search_flags = 0);
CORE_Static String gen_string_simple_decl(Arena *a, Ast_Type *ast, String name = {}, Ast_Scope *scope = 0, bool scope_names = true);
#define CASE(kind,type) case AST_##kind: { Ast_##type *node = (Ast_##type *)ast; #define CASE(kind,type) case AST_##kind: { Ast_##type *node = (Ast_##type *)ast;
#define BREAK() } break #define BREAK() } break

View File

@@ -226,7 +226,9 @@ type_struct_complete(Ast_Type *type, Ast_Decl *node){
assert(node->kind == AST_STRUCT); assert(node->kind == AST_STRUCT);
// @todo: compute size, alignement, offset !!! // @todo: compute size, alignement, offset !!!
// @note: resolve all the struct members first // @note: resolve all the struct members first
Scratch scratch; Scratch_Arena *scratch = pctx->scratch;
Scratch_Scope __scope(scratch);
Array<Ast_Resolved_Member> members = {scratch}; Array<Ast_Resolved_Member> members = {scratch};
type->kind = TYPE_COMPLETING; type->kind = TYPE_COMPLETING;
size_t members_size = 0; size_t members_size = 0;

View File

@@ -21,14 +21,14 @@ os_write_file(String filename, String filecontent){
} }
CORE_Static String CORE_Static String
os_read_file(Arena *a, String name){ os_read_file(Alloator *a, String name){
String result = {0}; String result = {0};
FILE *f = fopen((char *)name.str, "rb"); FILE *f = fopen((char *)name.str, "rb");
if(f){ if(f){
fseek(f, 0, SEEK_END); fseek(f, 0, SEEK_END);
result.len = ftell(f); result.len = ftell(f);
fseek(f, 0, SEEK_SET); fseek(f, 0, SEEK_SET);
result.str = (U8 *)arena_push_size(a, result.len + 1); result.str = (U8 *)allocate_size(a, result.len + 1, false);
fread(result.str, result.len, 1, f); fread(result.str, result.len, 1, f);
fclose(f); fclose(f);
result.str[result.len] = 0; result.str[result.len] = 0;
@@ -38,7 +38,7 @@ os_read_file(Arena *a, String name){
} }
CORE_Static String CORE_Static String
os_get_exe_dir(Arena *a){ os_get_exe_dir(Allocator *a){
char buffer[PATH_MAX] = {}; char buffer[PATH_MAX] = {};
if (readlink("/proc/self/exe", buffer, PATH_MAX) == -1) { if (readlink("/proc/self/exe", buffer, PATH_MAX) == -1) {
log_info("Failed to retrieve the path of the executable, the method used is fetching /proc/self/exe, very likely you are using an OS that doesn't follow this convention and as such it is currently not supported"); log_info("Failed to retrieve the path of the executable, the method used is fetching /proc/self/exe, very likely you are using an OS that doesn't follow this convention and as such it is currently not supported");
@@ -52,7 +52,7 @@ os_get_exe_dir(Arena *a){
} }
CORE_Static String CORE_Static String
os_get_absolute_path(Arena *a, String path){ os_get_absolute_path(Allocator *a, String path){
assert(path.str[path.len] == 0); assert(path.str[path.len] == 0);
char buffer[PATH_MAX] = {}; char buffer[PATH_MAX] = {};
realpath((char *)path.str, buffer); realpath((char *)path.str, buffer);
@@ -73,15 +73,15 @@ os_does_file_exist(String path){
} }
CORE_Static String CORE_Static String
os_get_working_dir(Arena *allocator){ os_get_working_dir(Allocator *allocator){
char *buffer = arena_push_array(allocator, char, PATH_MAX); char *buffer = allocate_array(allocator, char, PATH_MAX, false);
char *result = getcwd(buffer, PATH_MAX); char *result = getcwd(buffer, PATH_MAX);
return string_from_cstring(result); return string_from_cstring(result);
} }
CORE_Static Array<OS_File_Info> CORE_Static Array<OS_File_Info>
os_list_dir(Arena *a, String dir, U32 flags = LIST_RECURSE_INTO_DIRS){ os_list_dir(Scratch_Arena *scratch, Allocator *a, String dir, U32 flags = LIST_RECURSE_INTO_DIRS){
Scratch scratch(a); Scratch_Scope _scope(scratch);
Array<String> dirs_to_read = {scratch}; Array<String> dirs_to_read = {scratch};
dirs_to_read.add(dir); dirs_to_read.add(dir);

View File

@@ -115,14 +115,14 @@ os_write_file(String filename, String filecontent){
} }
CORE_Static String CORE_Static String
os_read_file(Arena *a, String name){ os_read_file(Allocator *a, String name){
String result = {0}; String result = {0};
FILE *f = fopen((char *)name.str, "rb"); FILE *f = fopen((char *)name.str, "rb");
if(f){ if(f){
fseek(f, 0, SEEK_END); fseek(f, 0, SEEK_END);
result.len = ftell(f); result.len = ftell(f);
fseek(f, 0, SEEK_SET); fseek(f, 0, SEEK_SET);
result.str = (U8 *)arena_push_size(a, result.len + 1); result.str = (U8 *)allocate_size(a, result.len + 1, false);
fread(result.str, result.len, 1, f); fread(result.str, result.len, 1, f);
fclose(f); fclose(f);
result.str[result.len] = 0; result.str[result.len] = 0;
@@ -132,7 +132,7 @@ os_read_file(Arena *a, String name){
} }
CORE_Static String CORE_Static String
os_get_working_dir(Arena *a){ os_get_working_dir(Allocator *a){
wchar_t buffer[2048]; wchar_t buffer[2048];
DWORD written = GetCurrentDirectoryW(2048, buffer); DWORD written = GetCurrentDirectoryW(2048, buffer);
assert(written != 0); assert(written != 0);
@@ -143,7 +143,7 @@ os_get_working_dir(Arena *a){
} }
CORE_Static String CORE_Static String
os_get_exe_dir(Arena *a){ os_get_exe_dir(Allocator *a){
wchar_t buffer[2048]; wchar_t buffer[2048];
DWORD written = GetModuleFileNameW(0, buffer, 2048); DWORD written = GetModuleFileNameW(0, buffer, 2048);
assert(written != 0); assert(written != 0);
@@ -158,12 +158,13 @@ os_get_exe_dir(Arena *a){
} }
CORE_Static String CORE_Static String
os_get_absolute_path(Arena *a, String path){ os_get_absolute_path(Allocator *a, String path){
Scratch scratch(a); char buff[2048];
Scratch_Arena *scratch = make_scratch_arena(buff, 2048);
String16 path16 = string8_to_string16(scratch, path); String16 path16 = string8_to_string16(scratch, path);
wchar_t *buffer = allocate_array(scratch, wchar_t, 2048); wchar_t *buffer = allocate_array(scratch, wchar_t, 512);
DWORD written = GetFullPathNameW((wchar_t *)path16.str, 2048, buffer, 0); DWORD written = GetFullPathNameW((wchar_t *)path16.str, 512, buffer, 0);
if(written == 0) return {}; if(written == 0) return {};
String16 absolute16 = string16_from_widechar(buffer); String16 absolute16 = string16_from_widechar(buffer);
@@ -174,7 +175,8 @@ os_get_absolute_path(Arena *a, String path){
CORE_Static B32 CORE_Static B32
os_does_file_exist(String path){ os_does_file_exist(String path){
Scratch scratch; char buff[2048];
Scratch_Arena *scratch = make_scratch_arena(buff, buff_cap(buff));
String16 path16 = string8_to_string16(scratch, path); String16 path16 = string8_to_string16(scratch, path);
DWORD attribs = GetFileAttributesW((wchar_t *)path16.str); DWORD attribs = GetFileAttributesW((wchar_t *)path16.str);
B32 result = attribs == INVALID_FILE_ATTRIBUTES ? false : true; B32 result = attribs == INVALID_FILE_ATTRIBUTES ? false : true;
@@ -182,8 +184,8 @@ os_does_file_exist(String path){
} }
CORE_Static Array<OS_File_Info> CORE_Static Array<OS_File_Info>
os_list_dir(Arena *a, String dir, U32 flags = LIST_NO_FLAGS){ os_list_dir(Scratch_Arena *scratch, Allocator *a, String dir, U32 flags = LIST_NO_FLAGS){
Scratch scratch(a); Scratch_Scope _scope(scratch);
Array<String> dirs_to_read = {scratch}; Array<String> dirs_to_read = {scratch};
dirs_to_read.add(dir); dirs_to_read.add(dir);