diff --git a/docs/metadesk_reference.mdesk b/docs/metadesk_reference.mdesk index 3e925b1..5fc4927 100644 --- a/docs/metadesk_reference.mdesk +++ b/docs/metadesk_reference.mdesk @@ -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, diff --git a/samples/static_site_generator/static_site_generator.c b/samples/static_site_generator/static_site_generator.c index 56c10b7..6f37bf3 100644 --- a/samples/static_site_generator/static_site_generator.c +++ b/samples/static_site_generator/static_site_generator.c @@ -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; } diff --git a/samples/toy_language/toy_language.c b/samples/toy_language/toy_language.c index 3f6854b..85c2f46 100644 --- a/samples/toy_language/toy_language.c +++ b/samples/toy_language/toy_language.c @@ -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); diff --git a/source/md.c b/source/md.c index 20cd227..5e6ebb1 100644 --- a/source/md.c +++ b/source/md.c @@ -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; diff --git a/source/md.h b/source/md.h index 756ac2c..efd7e3c 100644 --- a/source/md.h +++ b/source/md.h @@ -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 diff --git a/tests/sanity_tests.c b/tests/sanity_tests.c index 3c8615a..3a552a0 100644 --- a/tests/sanity_tests.c +++ b/tests/sanity_tests.c @@ -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]);