adding arena (p6)

This commit is contained in:
Allen Webster
2021-07-20 22:17:03 -07:00
parent 2953d97c53
commit a54f8b893f
6 changed files with 29 additions and 26 deletions
+3
View File
@@ -1125,12 +1125,14 @@ main:
@send(Map)
MD_MapMakeBucketCount: {
arena: *MD_Arena,
bucket_count: MD_u64,
return: MD_Map,
};
@send(Map)
MD_MapMake: {
arena: *MD_Arena,
return: MD_Map,
};
@@ -1162,6 +1164,7 @@ MD_MapScan: {
@send(Map)
MD_MapInsert: {
arena: *MD_Arena,
map: *MD_Map,
key: MD_MapKey,
val: *void,
@@ -100,7 +100,7 @@ int main(int argument_count, char **arguments)
{
for(MD_EachNode(index_string, node->first_child))
{
MD_MapInsert(&index_table, MD_MapKeyStr(index_string->string), root);
MD_MapInsert(arena, &index_table, MD_MapKeyStr(index_string->string), root);
}
goto end_index_build;
}
+4 -2
View File
@@ -26,6 +26,8 @@ struct Value
MD_Node *node;
};
static MD_Arena *arena = 0;
static Value
MakeValue_Number(MD_f64 v)
{
@@ -49,7 +51,7 @@ InsertValueToNamespace(NamespaceNode *ns, MD_String8 string, Value v)
{
Value *v_store = malloc(sizeof(*v_store));
*v_store = v;
MD_MapInsert(&ns->symbol_map, MD_MapKeyStr(string), v_store);
MD_MapInsert(arena, &ns->symbol_map, MD_MapKeyStr(string), v_store);
}
static Value
@@ -180,7 +182,7 @@ EvaluateScope(NamespaceNode *ns, MD_Node *code)
int main(int argument_count, char **arguments)
{
MD_Arena *arena = MD_ArenaNew(1ull << 40);
arena = MD_ArenaNew(1ull << 40);
//- rjf: parse command line
MD_String8List arg_list = MD_StringListFromArgCV(arena, argument_count, arguments);
+12 -15
View File
@@ -1296,7 +1296,7 @@ MD_F64FromString(MD_String8 string)
MD_FUNCTION_IMPL MD_String8
MD_CStyleHexStringFromU64(MD_u64 x, MD_b32 caps)
MD_CStyleHexStringFromU64(MD_Arena *arena, MD_u64 x, MD_b32 caps)
{
static char md_int_value_to_char[] = "0123456789abcdef";
MD_u8 buffer[10];
@@ -1328,7 +1328,7 @@ MD_CStyleHexStringFromU64(MD_u64 x, MD_b32 caps)
MD_String8 result = MD_ZERO_STRUCT;
result.size = (MD_u64)(ptr - buffer);
result.str = MD_PushArray(MD_u8, result.size);
result.str = MD_PushArrayAr(arena, MD_u8, result.size);
MD_MemoryCopy(result.str, buffer, result.size);
return(result);
}
@@ -1423,18 +1423,17 @@ MD_HashPtr(void *p)
}
MD_FUNCTION_IMPL MD_Map
MD_MapMakeBucketCount(MD_u64 bucket_count){
// TODO(allen): permanent arena? scratch arena? -- would really
// make most sense with a parameter
MD_MapMakeBucketCount(MD_Arena *arena, MD_u64 bucket_count){
// TODO(allen): super arena?
MD_Map result = {0};
result.bucket_count = bucket_count;
result.buckets = MD_PushArray(MD_MapBucket, bucket_count); //zero
result.buckets = MD_PushArrayZeroAr(arena, MD_MapBucket, bucket_count);
return(result);
}
MD_FUNCTION_IMPL MD_Map
MD_MapMake(void){
MD_Map result = MD_MapMakeBucketCount(4093);
MD_MapMake(MD_Arena *arena){
MD_Map result = MD_MapMakeBucketCount(arena, 4093);
return(result);
}
@@ -1502,13 +1501,11 @@ MD_MapScan(MD_MapSlot *first_slot, MD_MapKey key){
}
MD_FUNCTION_IMPL MD_MapSlot*
MD_MapInsert(MD_Map *map, MD_MapKey key, void *val){
MD_MapInsert(MD_Arena *arena, MD_Map *map, MD_MapKey key, void *val){
MD_MapSlot *result = 0;
if (map->bucket_count > 0){
MD_u64 index = key.hash%map->bucket_count;
// TODO(allen): again, memory? permanent arena? scratch arena?
// should definitely match the table's memory "object"
MD_MapSlot *slot = MD_PushArray(MD_MapSlot, 1);
MD_MapSlot *slot = MD_PushArrayAr(arena, MD_MapSlot, 1);
MD_MapBucket *bucket = &map->buckets[index];
MD_QueuePush(bucket->first, bucket->last, slot);
slot->key = key;
@@ -1519,13 +1516,13 @@ MD_MapInsert(MD_Map *map, MD_MapKey key, void *val){
}
MD_FUNCTION_IMPL MD_MapSlot*
MD_MapOverwrite(MD_Map *map, MD_MapKey key, void *val){
MD_MapOverwrite(MD_Arena *arena, MD_Map *map, MD_MapKey key, void *val){
MD_MapSlot *result = MD_MapLookup(map, key);
if (result != 0){
result->val = val;
}
else{
result = MD_MapInsert(map, key, val);
result = MD_MapInsert(arena, map, key, val);
}
return(result);
}
@@ -2308,7 +2305,7 @@ MD_ParseOneNode(MD_Arena *arena, MD_String8 string, MD_u64 offset)
for(int i_byte = 0; i_byte < bad_token.raw_string.size; ++i_byte)
{
MD_u8 b = bad_token.raw_string.str[i_byte];
MD_S8ListPush(arena, &bytes, MD_CStyleHexStringFromU64(b, 1));
MD_S8ListPush(arena, &bytes, MD_CStyleHexStringFromU64(arena, b, 1));
}
MD_StringJoin join = MD_ZERO_STRUCT;
+6 -5
View File
@@ -803,7 +803,7 @@ MD_FUNCTION MD_u64 MD_U64FromString(MD_String8 string, MD_u32 radix);
MD_FUNCTION MD_i64 MD_CStyleIntFromString(MD_String8 string);
MD_FUNCTION MD_f64 MD_F64FromString(MD_String8 string);
MD_FUNCTION MD_String8 MD_CStyleHexStringFromU64(MD_u64 x, MD_b32 caps);
MD_FUNCTION MD_String8 MD_CStyleHexStringFromU64(MD_Arena *arena, MD_u64 x, MD_b32 caps);
//~ Enum/Flag Strings
@@ -815,14 +815,15 @@ MD_FUNCTION MD_String8List MD_StringListFromNodeFlags(MD_Arena *arena, MD_NodeFl
MD_FUNCTION MD_u64 MD_HashStr(MD_String8 string);
MD_FUNCTION MD_u64 MD_HashPtr(void *p);
MD_FUNCTION MD_Map MD_MapMakeBucketCount(MD_u64 bucket_count);
MD_FUNCTION MD_Map MD_MapMake(void);
MD_FUNCTION MD_Map MD_MapMakeBucketCount(MD_Arena *arena, MD_u64 bucket_count);
MD_FUNCTION MD_Map MD_MapMake(MD_Arena *arena);
MD_FUNCTION MD_MapKey MD_MapKeyStr(MD_String8 string);
MD_FUNCTION MD_MapKey MD_MapKeyPtr(void *ptr);
MD_FUNCTION MD_MapSlot* MD_MapLookup(MD_Map *map, MD_MapKey key);
MD_FUNCTION MD_MapSlot* MD_MapScan(MD_MapSlot *first_slot, MD_MapKey key);
MD_FUNCTION MD_MapSlot* MD_MapInsert(MD_Map *map, MD_MapKey key, void *val);
MD_FUNCTION MD_MapSlot* MD_MapOverwrite(MD_Map *map, MD_MapKey key, void *val);
MD_FUNCTION MD_MapSlot* MD_MapInsert(MD_Arena *arena, MD_Map *map, MD_MapKey key, void *val);
MD_FUNCTION MD_MapSlot* MD_MapOverwrite(MD_Arena *arena, MD_Map *map, MD_MapKey key,
void *val);
//~ Parsing
+3 -3
View File
@@ -571,16 +571,16 @@ int main(void)
}
{
MD_Map map = MD_MapMake();
MD_Map map = MD_MapMake(arena);
for (MD_u64 i = 0; i < MD_ArrayCount(keys); i += 1){
MD_MapInsert(&map, keys[i], (void *)i);
MD_MapInsert(arena, &map, keys[i], (void *)i);
}
for (MD_u64 i = 0; i < MD_ArrayCount(keys); i += 1){
MD_MapSlot *slot = MD_MapLookup(&map, keys[i]);
TestResult(slot && slot->val == (void *)i);
}
for (MD_u64 i = 0; i < MD_ArrayCount(keys); i += 1){
MD_MapOverwrite(&map, keys[i], (void *)(i + 10));
MD_MapOverwrite(arena, &map, keys[i], (void *)(i + 10));
}
for (MD_u64 i = 0; i < MD_ArrayCount(keys); i += 1){
MD_MapSlot *slot = MD_MapLookup(&map, keys[i]);