Mercurial
diff dowa/d_memory.c @ 20:0a9e67c7039a
[Seobeo] Chaning Function naming to be easily readable.
| author | June Park <parkjune1995@gmail.com> |
|---|---|
| date | Mon, 06 Oct 2025 10:13:41 -0700 |
| parents | fa2b8af609d9 |
| children | 09def63429b9 |
line wrap: on
line diff
--- a/dowa/d_memory.c Mon Oct 06 09:55:34 2025 -0700 +++ b/dowa/d_memory.c Mon Oct 06 10:13:41 2025 -0700 @@ -26,7 +26,7 @@ return currnet_ptr; } -void Dowa_Arena_Free(Dowa_PArena p_arena) +void Dowa_Arena_Destory(Dowa_PArena p_arena) { if (p_arena) { @@ -56,9 +56,37 @@ return p_hash_map; } -void Dowa_HashMap_Free(Dowa_PHashMap p_hash_map) +Dowa_PHashMap Dowa_HashMap_Create(size_t capacity, Dowa_PArena p_arena) { - if (p_hash_map) + if (p_arena == NULL) + printf("Arena is NULL"); + return Dowa_HashMap_Create(capacity); + + Dowa_PHashMap p_hash_map; + p_hash_map = Dowa_Arena_Allocate(p_arena, sizeof(Dowa_HashMap)); + if (p_hash_map == NULL) + { + return NULL; + } + p_hash_map->entries = Dowa_Arena_Allocate(p_arena, sizeof(*p_hash_map->entries) * capacity); + if (p_hash_map->entries == NULL) + { + free(p_hash_map); + return NULL; + } + p_hash_map->capacity = capacity; + p_hash_map->current_capacity = 0; + p_hash_map->p_arena = p_arena; + return p_hash_map; +} + +void Dowa_HashMap_Destory(Dowa_PHashMap p_hash_map) +{ + if (!p_hash_map) return; + + if (p_hash_map->p_arena) + Dowa_Arena_Destory(p_hash_map->p_arena); + else { Dowa_PHashEntry entry; if (p_hash_map->entries) @@ -73,13 +101,13 @@ free(entry); } } - free(p_hash_map->entries); } + free(p_hash_map->entries); } free(p_hash_map); } -int32 Dowa_HashMap_GetPosition(Dowa_PHashMap p_hash_map, char *key) +int32 Dowa_HashMap_Get_Position(Dowa_PHashMap p_hash_map, char *key) { int32 hash_val = HASH_KEY_NUMBER; int32 c; @@ -92,7 +120,7 @@ void *Dowa_HashMap_Get(Dowa_PHashMap p_hash_map, char *key) { - int idx_foo = Dowa_HashMap_GetPosition(p_hash_map, key); + int idx_foo = Dowa_HashMap_Get_Position(p_hash_map, key); void *value = p_hash_map->entries[idx_foo]; if (value == NULL || strcmp(((Dowa_PHashEntry) value)->key, key) != 0) { @@ -101,7 +129,7 @@ return ((Dowa_PHashEntry) value)->buffer; } -int32 Dowa_HashMap_PushValueWithTypeNoCopy(Dowa_PHashMap p_hash_map, char *key, void *value, size_t value_size, Dowa_HashMap_ValueType type) +int32 Dowa_HashMap_Push_Value_With_Type_NoCopy(Dowa_PHashMap p_hash_map, char *key, void *value, size_t value_size, Dowa_HashMap_ValueType type) { int idx = Dowa_HashMap_GetPosition(p_hash_map, key); Dowa_PHashEntry entry = p_hash_map->entries[idx]; @@ -109,8 +137,10 @@ free(entry->buffer); else { - entry = malloc(sizeof(Dowa_HashEntry)); - if (entry == NULL) { perror("malloc"); return -1; } + entry = p_hash_map->p_arena ? + Dowa_Arena_Allocate(p_hash_map->p_arena, sizeof(Dowa_HashEntry)) : + malloc(sizeof(Dowa_HashEntry)); + if (entry == NULL) { perror("malloc or arena alloc"); return -1; } p_hash_map->entries[idx] = entry; p_hash_map->current_capacity++; @@ -122,7 +152,7 @@ return 0; } -int32 Dowa_HashMap_PushValueWithType(Dowa_PHashMap p_hash_map, char *key, void *value, size_t value_size, Dowa_HashMap_ValueType type) +int32 Dowa_HashMap_Push_Value_With_Type(Dowa_PHashMap p_hash_map, char *key, void *value, size_t value_size, Dowa_HashMap_ValueType type) { int idx = Dowa_HashMap_GetPosition(p_hash_map, key); Dowa_PHashEntry entry = p_hash_map->entries[idx]; @@ -130,27 +160,32 @@ free(entry->buffer); else { - entry = malloc(sizeof(Dowa_HashEntry)); - if (entry == NULL) { perror("malloc"); return -1; } + entry = p_hash_map->p_arena ? + Dowa_Arena_Allocate(p_hash_map->p_arena, sizeof(Dowa_HashEntry)) : + malloc(sizeof(Dowa_HashEntry)); + if (entry == NULL) { perror("malloc or arena alloc"); return -1; } p_hash_map->entries[idx] = entry; p_hash_map->current_capacity++; entry->key = strdup(key); } - entry->buffer = malloc(value_size); - if (entry->buffer == NULL) { perror("malloc"); return -1; } + entry->buffer = p_hash_map->p_arena ? + Dowa_Arena_Allocate(p_hash_map->p_arena, value_size) : + malloc(value_size); + + if (entry->buffer == NULL) { perror("malloc or arena alloc"); return -1; } entry->capacity = value_size; entry->type = type; memcpy(entry->buffer, value, value_size); return 0; } -void Dowa_HashMap_PushValue(Dowa_PHashMap p_hash_map, char *key, void *value, size_t value_size) +void Dowa_HashMap_Push_Value(Dowa_PHashMap p_hash_map, char *key, void *value, size_t value_size) { - Dowa_HashMap_PushValueWithType(p_hash_map, key, value, value_size, DOWA_HASH_MAP_TYPE_BUFFER); + Dowa_HashMap_Push_Value_With_Type(p_hash_map, key, value, value_size, DOWA_HASH_MAP_TYPE_BUFFER); } -void Dowa_HashMap_PopKey(Dowa_PHashMap p_hash_map, char *key) +void Dowa_HashMap_Pop_Key(Dowa_PHashMap p_hash_map, char *key) { int idx = Dowa_HashMap_GetPosition(p_hash_map, key); Dowa_PHashEntry entry = p_hash_map->entries[idx]; @@ -211,7 +246,7 @@ printf("-----------\n"); } -int Dowa_HashMap_Cache_Folder(Dowa_PHashMap map, const char *folder_path) +int Dowa_HashMap_Cache_Folder(Dowa_PHashMap p_hash_map, const char *folder_path) { DIR *dir = opendir(folder_path); if (!dir) { perror("opendir"); return -1; } @@ -237,7 +272,9 @@ FILE *f = fopen(fullpath, "rb"); if (!f) { perror("fopen"); continue; } - void *buf = malloc(size); + void *buf = p_hash_map->p_arena ? + Dowa_Arena_Allocate(p_hash_map->p_arena, value_size) : + malloc(value_size); if (!buf) { perror("malloc"); fclose(f); closedir(dir); return -1; } if (fread(buf, 1, size, f) != size) @@ -249,12 +286,12 @@ } fclose(f); - Dowa_HashMap_PushValueWithType(map, entry->d_name, buf, size, DOWA_HASH_MAP_TYPE_STRING); + Dowa_HashMap_PushValueWithType(p_hash_map, entry->d_name, buf, size, DOWA_HASH_MAP_TYPE_STRING); free(buf); // Dowa_HashMap_PushValue made its own copy } else if (S_ISDIR(st.st_mode)) { - Dowa_PHashMap p_child_map = Dowa_HashMap_Create(100); + Dowa_PHashMap p_child_map = Dowa_HashMap_Create(100, p_hash_map->p_arena); if (!p_child_map) { perror("Dowa_HashMap_Create"); @@ -267,10 +304,10 @@ } // Should not copy as we malloced already. - if (Dowa_HashMap_PushValueWithTypeNoCopy(map, entry->d_name, p_child_map, + if (Dowa_HashMap_Push_Value_With_Type_NoCopy(p_hash_map, entry->d_name, p_child_map, sizeof(p_child_map), DOWA_HASH_MAP_TYPE_HASHMAP) == -1) { - Dowa_HashMap_Free(p_child_map); + Dowa_HashMap_Destory(p_child_map); return -1; } }