Mercurial
comparison dowa/d_memory.c @ 21:09def63429b9
[Dowa] Updated the naming scheme and tests.
| author | June Park <parkjune1995@gmail.com> |
|---|---|
| date | Mon, 06 Oct 2025 10:57:30 -0700 |
| parents | 0a9e67c7039a |
| children | 947b81010aba |
comparison
equal
deleted
inserted
replaced
| 20:0a9e67c7039a | 21:09def63429b9 |
|---|---|
| 24 void *currnet_ptr = p_arena->buffer + p_arena->offset; | 24 void *currnet_ptr = p_arena->buffer + p_arena->offset; |
| 25 p_arena->offset += size; | 25 p_arena->offset += size; |
| 26 return currnet_ptr; | 26 return currnet_ptr; |
| 27 } | 27 } |
| 28 | 28 |
| 29 void Dowa_Arena_Destory(Dowa_PArena p_arena) | 29 void Dowa_Arena_Destroy(Dowa_PArena p_arena) |
| 30 { | 30 { |
| 31 if (p_arena) | 31 if (p_arena) |
| 32 { | 32 { |
| 33 if (p_arena->buffer) | 33 if (p_arena->buffer) |
| 34 free(p_arena->buffer); | 34 free(p_arena->buffer); |
| 54 p_hash_map->capacity = capacity; | 54 p_hash_map->capacity = capacity; |
| 55 p_hash_map->current_capacity = 0; | 55 p_hash_map->current_capacity = 0; |
| 56 return p_hash_map; | 56 return p_hash_map; |
| 57 } | 57 } |
| 58 | 58 |
| 59 Dowa_PHashMap Dowa_HashMap_Create(size_t capacity, Dowa_PArena p_arena) | 59 Dowa_PHashMap Dowa_HashMap_Create_With_Arena(size_t capacity, Dowa_PArena p_arena) |
| 60 { | 60 { |
| 61 if (p_arena == NULL) | 61 if (p_arena == NULL) |
| 62 printf("Arena is NULL"); | 62 printf("Arena is NULL\n"); |
| 63 return Dowa_HashMap_Create(capacity); | 63 return Dowa_HashMap_Create(capacity); |
| 64 | 64 |
| 65 Dowa_PHashMap p_hash_map; | 65 Dowa_PHashMap p_hash_map; |
| 66 p_hash_map = Dowa_Arena_Allocate(p_arena, sizeof(Dowa_HashMap)); | 66 p_hash_map = Dowa_Arena_Allocate(p_arena, sizeof(Dowa_HashMap)); |
| 67 if (p_hash_map == NULL) | 67 if (p_hash_map == NULL) |
| 78 p_hash_map->current_capacity = 0; | 78 p_hash_map->current_capacity = 0; |
| 79 p_hash_map->p_arena = p_arena; | 79 p_hash_map->p_arena = p_arena; |
| 80 return p_hash_map; | 80 return p_hash_map; |
| 81 } | 81 } |
| 82 | 82 |
| 83 void Dowa_HashMap_Destory(Dowa_PHashMap p_hash_map) | 83 void Dowa_HashMap_Destroy(Dowa_PHashMap p_hash_map) |
| 84 { | 84 { |
| 85 if (!p_hash_map) return; | 85 if (!p_hash_map) return; |
| 86 | 86 |
| 87 if (p_hash_map->p_arena) | 87 if (p_hash_map->p_arena) |
| 88 Dowa_Arena_Destory(p_hash_map->p_arena); | 88 Dowa_Arena_Destroy(p_hash_map->p_arena); |
| 89 else | 89 else |
| 90 { | 90 { |
| 91 Dowa_PHashEntry entry; | 91 Dowa_PHashEntry entry; |
| 92 if (p_hash_map->entries) | 92 if (p_hash_map->entries) |
| 93 { | 93 { |
| 105 free(p_hash_map->entries); | 105 free(p_hash_map->entries); |
| 106 } | 106 } |
| 107 free(p_hash_map); | 107 free(p_hash_map); |
| 108 } | 108 } |
| 109 | 109 |
| 110 int32 Dowa_HashMap_Get_Position(Dowa_PHashMap p_hash_map, char *key) | 110 int32 Dowa_HashMap_Get_Position(Dowa_PHashMap p_hash_map, const char *key) |
| 111 { | 111 { |
| 112 int32 hash_val = HASH_KEY_NUMBER; | 112 int32 hash_val = HASH_KEY_NUMBER; |
| 113 int32 c; | 113 int32 c; |
| 114 while ((c = *key++)) | 114 while ((c = *key++)) |
| 115 { | 115 { |
| 116 hash_val = (hash_val << 5) + hash_val + c; | 116 hash_val = (hash_val << 5) + hash_val + c; |
| 117 } | 117 } |
| 118 return hash_val % p_hash_map->capacity; | 118 return hash_val % p_hash_map->capacity; |
| 119 } | 119 } |
| 120 | 120 |
| 121 void *Dowa_HashMap_Get(Dowa_PHashMap p_hash_map, char *key) | 121 void *Dowa_HashMap_Get(Dowa_PHashMap p_hash_map, const char *key) |
| 122 { | 122 { |
| 123 int idx_foo = Dowa_HashMap_Get_Position(p_hash_map, key); | 123 int idx_foo = Dowa_HashMap_Get_Position(p_hash_map, key); |
| 124 void *value = p_hash_map->entries[idx_foo]; | 124 void *value = p_hash_map->entries[idx_foo]; |
| 125 if (value == NULL || strcmp(((Dowa_PHashEntry) value)->key, key) != 0) | 125 if (value == NULL || strcmp(((Dowa_PHashEntry) value)->key, key) != 0) |
| 126 { | 126 { |
| 127 return NULL; | 127 return NULL; |
| 128 } | 128 } |
| 129 return ((Dowa_PHashEntry) value)->buffer; | 129 return ((Dowa_PHashEntry) value)->buffer; |
| 130 } | 130 } |
| 131 | 131 |
| 132 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) | 132 int32 Dowa_HashMap_Push_Value_With_Type_NoCopy(Dowa_PHashMap p_hash_map, const char *key, |
| 133 { | 133 void *value, size_t value_size, |
| 134 int idx = Dowa_HashMap_GetPosition(p_hash_map, key); | 134 Dowa_HashMap_ValueType type) |
| 135 { | |
| 136 int idx = Dowa_HashMap_Get_Position(p_hash_map, key); | |
| 135 Dowa_PHashEntry entry = p_hash_map->entries[idx]; | 137 Dowa_PHashEntry entry = p_hash_map->entries[idx]; |
| 136 if (entry) | 138 if (entry) |
| 137 free(entry->buffer); | 139 free(entry->buffer); |
| 138 else | 140 else |
| 139 { | 141 { |
| 150 entry->capacity = value_size; | 152 entry->capacity = value_size; |
| 151 entry->type = type; | 153 entry->type = type; |
| 152 return 0; | 154 return 0; |
| 153 } | 155 } |
| 154 | 156 |
| 155 int32 Dowa_HashMap_Push_Value_With_Type(Dowa_PHashMap p_hash_map, char *key, void *value, size_t value_size, Dowa_HashMap_ValueType type) | 157 int32 Dowa_HashMap_Push_Value_With_Type(Dowa_PHashMap p_hash_map, const char *key, |
| 156 { | 158 void *value, size_t value_size, |
| 157 int idx = Dowa_HashMap_GetPosition(p_hash_map, key); | 159 Dowa_HashMap_ValueType type) |
| 160 { | |
| 161 int idx = Dowa_HashMap_Get_Position(p_hash_map, key); | |
| 158 Dowa_PHashEntry entry = p_hash_map->entries[idx]; | 162 Dowa_PHashEntry entry = p_hash_map->entries[idx]; |
| 159 if (entry) | 163 if (entry) |
| 160 free(entry->buffer); | 164 free(entry->buffer); |
| 161 else | 165 else |
| 162 { | 166 { |
| 178 entry->type = type; | 182 entry->type = type; |
| 179 memcpy(entry->buffer, value, value_size); | 183 memcpy(entry->buffer, value, value_size); |
| 180 return 0; | 184 return 0; |
| 181 } | 185 } |
| 182 | 186 |
| 183 void Dowa_HashMap_Push_Value(Dowa_PHashMap p_hash_map, char *key, void *value, size_t value_size) | 187 void Dowa_HashMap_Push_Value(Dowa_PHashMap p_hash_map, const char *key, void *value, size_t value_size) |
| 184 { | 188 { |
| 185 Dowa_HashMap_Push_Value_With_Type(p_hash_map, key, value, value_size, DOWA_HASH_MAP_TYPE_BUFFER); | 189 Dowa_HashMap_Push_Value_With_Type(p_hash_map, key, value, value_size, DOWA_HASH_MAP_TYPE_BUFFER); |
| 186 } | 190 } |
| 187 | 191 |
| 188 void Dowa_HashMap_Pop_Key(Dowa_PHashMap p_hash_map, char *key) | 192 void Dowa_HashMap_Pop_Key(Dowa_PHashMap p_hash_map, const char *key) |
| 189 { | 193 { |
| 190 int idx = Dowa_HashMap_GetPosition(p_hash_map, key); | 194 int idx = Dowa_HashMap_Get_Position(p_hash_map, key); |
| 191 Dowa_PHashEntry entry = p_hash_map->entries[idx]; | 195 Dowa_PHashEntry entry = p_hash_map->entries[idx]; |
| 192 if (entry) | 196 if (entry) |
| 193 { | 197 { |
| 194 free(entry->key); | 198 free(entry->key); |
| 195 free(entry->buffer); | 199 free(entry->buffer); |
| 271 size_t size = (size_t)st.st_size; | 275 size_t size = (size_t)st.st_size; |
| 272 FILE *f = fopen(fullpath, "rb"); | 276 FILE *f = fopen(fullpath, "rb"); |
| 273 if (!f) { perror("fopen"); continue; } | 277 if (!f) { perror("fopen"); continue; } |
| 274 | 278 |
| 275 void *buf = p_hash_map->p_arena ? | 279 void *buf = p_hash_map->p_arena ? |
| 276 Dowa_Arena_Allocate(p_hash_map->p_arena, value_size) : | 280 Dowa_Arena_Allocate(p_hash_map->p_arena, size) : |
| 277 malloc(value_size); | 281 malloc(size); |
| 278 if (!buf) { perror("malloc"); fclose(f); closedir(dir); return -1; } | 282 if (!buf) { perror("malloc"); fclose(f); closedir(dir); return -1; } |
| 279 | 283 |
| 280 if (fread(buf, 1, size, f) != size) | 284 if (fread(buf, 1, size, f) != size) |
| 281 { | 285 { |
| 282 perror("fread"); | 286 perror("fread"); |
| 284 fclose(f); | 288 fclose(f); |
| 285 continue; | 289 continue; |
| 286 } | 290 } |
| 287 fclose(f); | 291 fclose(f); |
| 288 | 292 |
| 289 Dowa_HashMap_PushValueWithType(p_hash_map, entry->d_name, buf, size, DOWA_HASH_MAP_TYPE_STRING); | 293 Dowa_HashMap_Push_Value_With_Type(p_hash_map, entry->d_name, buf, size, DOWA_HASH_MAP_TYPE_STRING); |
| 290 free(buf); // Dowa_HashMap_PushValue made its own copy | 294 free(buf); // Dowa_HashMap_PushValue made its own copy |
| 291 } | 295 } |
| 292 else if (S_ISDIR(st.st_mode)) | 296 else if (S_ISDIR(st.st_mode)) |
| 293 { | 297 { |
| 294 Dowa_PHashMap p_child_map = Dowa_HashMap_Create(100, p_hash_map->p_arena); | 298 Dowa_PHashMap p_child_map = Dowa_HashMap_Create_With_Arena(100, p_hash_map->p_arena); |
| 295 if (!p_child_map) | 299 if (!p_child_map) |
| 296 { | 300 { |
| 297 perror("Dowa_HashMap_Create"); | 301 perror("Dowa_HashMap_Create"); |
| 298 return -1; | 302 return -1; |
| 299 } | 303 } |
| 305 | 309 |
| 306 // Should not copy as we malloced already. | 310 // Should not copy as we malloced already. |
| 307 if (Dowa_HashMap_Push_Value_With_Type_NoCopy(p_hash_map, entry->d_name, p_child_map, | 311 if (Dowa_HashMap_Push_Value_With_Type_NoCopy(p_hash_map, entry->d_name, p_child_map, |
| 308 sizeof(p_child_map), DOWA_HASH_MAP_TYPE_HASHMAP) == -1) | 312 sizeof(p_child_map), DOWA_HASH_MAP_TYPE_HASHMAP) == -1) |
| 309 { | 313 { |
| 310 Dowa_HashMap_Destory(p_child_map); | 314 Dowa_HashMap_Destroy(p_child_map); |
| 311 return -1; | 315 return -1; |
| 312 } | 316 } |
| 313 } | 317 } |
| 314 } | 318 } |
| 315 closedir(dir); | 319 closedir(dir); |