Mercurial
comparison dowa/d_memory.c @ 36:84672efec192
[Zenbu] WIP fixing issues regarding to using edge only. I think there is a problem where socket closes before sending back the info.
| author | MrJuneJune <me@mrjunejune.com> |
|---|---|
| date | Sun, 09 Nov 2025 06:25:16 -0800 |
| parents | 08a465eec50b |
| children | 636eab07809d |
comparison
equal
deleted
inserted
replaced
| 35:33b1caf051cd | 36:84672efec192 |
|---|---|
| 29 void Dowa_Arena_Destroy(Dowa_PArena p_arena) | 29 void Dowa_Arena_Destroy(Dowa_PArena p_arena) |
| 30 { | 30 { |
| 31 if (!p_arena) return; | 31 if (!p_arena) return; |
| 32 | 32 |
| 33 if (p_arena->buffer) | 33 if (p_arena->buffer) |
| 34 free(p_arena->buffer); | 34 Dowa_Free(p_arena->buffer); |
| 35 free(p_arena); | 35 Dowa_Free(p_arena); |
| 36 } | 36 } |
| 37 | 37 |
| 38 void *Dowa_Arena_Copy(Dowa_PArena p_arena, const void *src, size_t size) | 38 void *Dowa_Arena_Copy(Dowa_PArena p_arena, const void *src, size_t size) |
| 39 { | 39 { |
| 40 if (p_arena == NULL || src == NULL || size == 0) | 40 if (p_arena == NULL || src == NULL || size == 0) |
| 58 return NULL; | 58 return NULL; |
| 59 } | 59 } |
| 60 p_hash_map->entries = calloc(capacity, sizeof(*p_hash_map->entries)); | 60 p_hash_map->entries = calloc(capacity, sizeof(*p_hash_map->entries)); |
| 61 if (p_hash_map->entries == NULL) | 61 if (p_hash_map->entries == NULL) |
| 62 { | 62 { |
| 63 free(p_hash_map); | 63 Dowa_Free(p_hash_map); |
| 64 return NULL; | 64 return NULL; |
| 65 } | 65 } |
| 66 p_hash_map->capacity = capacity; | 66 p_hash_map->capacity = capacity; |
| 67 p_hash_map->current_capacity = 0; | 67 p_hash_map->current_capacity = 0; |
| 68 p_hash_map->p_arena = NULL; | 68 p_hash_map->p_arena = NULL; |
| 85 } | 85 } |
| 86 p_hash_map->entries = Dowa_Arena_Allocate(p_arena, sizeof(*p_hash_map->entries) * capacity); | 86 p_hash_map->entries = Dowa_Arena_Allocate(p_arena, sizeof(*p_hash_map->entries) * capacity); |
| 87 memset(p_hash_map->entries, 0, capacity * sizeof *p_hash_map->entries); | 87 memset(p_hash_map->entries, 0, capacity * sizeof *p_hash_map->entries); |
| 88 if (p_hash_map->entries == NULL) | 88 if (p_hash_map->entries == NULL) |
| 89 { | 89 { |
| 90 free(p_hash_map); | 90 Dowa_Free(p_hash_map); |
| 91 return NULL; | 91 return NULL; |
| 92 } | 92 } |
| 93 p_hash_map->capacity = capacity; | 93 p_hash_map->capacity = capacity; |
| 94 p_hash_map->current_capacity = 0; | 94 p_hash_map->current_capacity = 0; |
| 95 p_hash_map->p_arena = p_arena; | 95 p_hash_map->p_arena = p_arena; |
| 114 for (int idx=0; idx<p_hash_map->capacity; idx++) | 114 for (int idx=0; idx<p_hash_map->capacity; idx++) |
| 115 { | 115 { |
| 116 entry = p_hash_map->entries[idx]; | 116 entry = p_hash_map->entries[idx]; |
| 117 if (entry) | 117 if (entry) |
| 118 { | 118 { |
| 119 free(entry->key); | 119 Dowa_Free(entry->key); |
| 120 free(entry->buffer); | 120 Dowa_Free(entry->buffer); |
| 121 free(entry); | 121 Dowa_Free(entry); |
| 122 } | 122 } |
| 123 } | 123 } |
| 124 } | 124 } |
| 125 free(p_hash_map->entries); | 125 Dowa_Free(p_hash_map->entries); |
| 126 } | 126 } |
| 127 free(p_hash_map); | 127 Dowa_Free(p_hash_map); |
| 128 } | 128 } |
| 129 | 129 |
| 130 int32 Dowa_HashMap_Get_Position(Dowa_PHashMap p_hash_map, const char *key) | 130 int32 Dowa_HashMap_Get_Position(Dowa_PHashMap p_hash_map, const char *key) |
| 131 { | 131 { |
| 132 int32 hash_val = HASH_KEY_NUMBER; | 132 int32 hash_val = HASH_KEY_NUMBER; |
| 167 // Old key | 167 // Old key |
| 168 while (entry) | 168 while (entry) |
| 169 { | 169 { |
| 170 if (strcmp(entry->key, key) == 0) | 170 if (strcmp(entry->key, key) == 0) |
| 171 { | 171 { |
| 172 if (!p_hash_map->p_arena && entry->buffer) | 172 // Fails if it the key exists... |
| 173 free(entry->buffer); | 173 return -1; |
| 174 entry->buffer = value; | 174 } |
| 175 entry->capacity = value_size; | 175 } |
| 176 entry->type = type; | 176 |
| 177 return 0; | 177 // Overriding doesn't really make sense? when copying over |
| 178 } | 178 // as we need to free it. |
| 179 prev = entry; | 179 // |
| 180 entry = entry->next; | 180 //while (entry) |
| 181 } | 181 //{ |
| 182 // if (strcmp(entry->key, key) == 0) | |
| 183 // { | |
| 184 // if (!p_hash_map->p_arena && entry->buffer) | |
| 185 // Dowa_Free(entry->buffer); | |
| 186 // entry->buffer = value; | |
| 187 // entry->capacity = value_size; | |
| 188 // entry->type = type; | |
| 189 // return 0; | |
| 190 // } | |
| 191 // prev = entry; | |
| 192 // entry = entry->next; | |
| 193 //} | |
| 182 | 194 |
| 183 // New Key | 195 // New Key |
| 184 entry = p_hash_map->p_arena ? | 196 entry = p_hash_map->p_arena ? |
| 185 Dowa_Arena_Allocate(p_hash_map->p_arena, sizeof(Dowa_HashEntry)) : | 197 Dowa_Arena_Allocate(p_hash_map->p_arena, sizeof(Dowa_HashEntry)) : |
| 186 malloc(sizeof(Dowa_HashEntry)); | 198 malloc(sizeof(Dowa_HashEntry)); |
| 215 while (entry) | 227 while (entry) |
| 216 { | 228 { |
| 217 if (strcmp(entry->key, key) == 0) | 229 if (strcmp(entry->key, key) == 0) |
| 218 { | 230 { |
| 219 if (!p_hash_map->p_arena && entry->buffer) | 231 if (!p_hash_map->p_arena && entry->buffer) |
| 220 free(entry->buffer); | 232 Dowa_Free(entry->buffer); |
| 221 | 233 |
| 222 entry->buffer = p_hash_map->p_arena ? | 234 entry->buffer = p_hash_map->p_arena ? |
| 223 Dowa_Arena_Allocate(p_hash_map->p_arena, value_size) : | 235 Dowa_Arena_Allocate(p_hash_map->p_arena, value_size) : |
| 224 malloc(value_size); | 236 malloc(value_size); |
| 225 if (!entry->buffer) { perror("malloc or arena alloc"); return -1; } | 237 if (!entry->buffer) { perror("malloc or arena alloc"); return -1; } |
| 272 { | 284 { |
| 273 int idx = Dowa_HashMap_Get_Position(p_hash_map, key); | 285 int idx = Dowa_HashMap_Get_Position(p_hash_map, key); |
| 274 Dowa_PHashEntry entry = p_hash_map->entries[idx]; | 286 Dowa_PHashEntry entry = p_hash_map->entries[idx]; |
| 275 if (entry && !(p_hash_map->p_arena)) | 287 if (entry && !(p_hash_map->p_arena)) |
| 276 { | 288 { |
| 277 free(entry->key); | 289 Dowa_Free(entry->key); |
| 278 free(entry->buffer); | 290 Dowa_Free(entry->buffer); |
| 279 free(entry); | 291 Dowa_Free(entry); |
| 280 } | 292 } |
| 281 p_hash_map->entries[idx] = NULL; | 293 p_hash_map->entries[idx] = NULL; |
| 282 if (p_hash_map->current_capacity > 0) | 294 if (p_hash_map->current_capacity > 0) |
| 283 { | 295 { |
| 284 p_hash_map->current_capacity--; | 296 p_hash_map->current_capacity--; |
| 366 | 378 |
| 367 | 379 |
| 368 if (fread(buf, 1, size, f) != size) | 380 if (fread(buf, 1, size, f) != size) |
| 369 { | 381 { |
| 370 perror("fread"); | 382 perror("fread"); |
| 371 if (!p_hash_map->p_arena) free(buf); | 383 if (!p_hash_map->p_arena) Dowa_Free(buf); |
| 372 fclose(f); | 384 fclose(f); |
| 373 continue; | 385 continue; |
| 374 } | 386 } |
| 375 fclose(f); | 387 fclose(f); |
| 376 | 388 |
| 377 Dowa_HashMap_Push_Value_With_Type(p_hash_map, entry->d_name, buf, size, DOWA_HASH_MAP_TYPE_STRING); | 389 Dowa_HashMap_Push_Value_With_Type(p_hash_map, entry->d_name, buf, size, DOWA_HASH_MAP_TYPE_STRING); |
| 378 free(buf); // Dowa_HashMap_PushValue made its own copy | 390 Dowa_Free(buf); // Dowa_HashMap_PushValue made its own copy |
| 379 } | 391 } |
| 380 else if (S_ISDIR(st.st_mode)) | 392 else if (S_ISDIR(st.st_mode)) |
| 381 { | 393 { |
| 382 // TODO: Adjust the sizes of the recursive map? | 394 // TODO: Adjust the sizes of the recursive map? |
| 383 Dowa_PHashMap p_child_map = Dowa_HashMap_Create_With_Arena(100, p_hash_map->p_arena); | 395 Dowa_PHashMap p_child_map = Dowa_HashMap_Create_With_Arena(100, p_hash_map->p_arena); |