Mercurial
comparison 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 |
comparison
equal
deleted
inserted
replaced
| 19:875bb6e10db7 | 20:0a9e67c7039a |
|---|---|
| 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_Free(Dowa_PArena p_arena) | 29 void Dowa_Arena_Destory(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 void Dowa_HashMap_Free(Dowa_PHashMap p_hash_map) | 59 Dowa_PHashMap Dowa_HashMap_Create(size_t capacity, Dowa_PArena p_arena) |
| 60 { | 60 { |
| 61 if (p_hash_map) | 61 if (p_arena == NULL) |
| 62 printf("Arena is NULL"); | |
| 63 return Dowa_HashMap_Create(capacity); | |
| 64 | |
| 65 Dowa_PHashMap p_hash_map; | |
| 66 p_hash_map = Dowa_Arena_Allocate(p_arena, sizeof(Dowa_HashMap)); | |
| 67 if (p_hash_map == NULL) | |
| 68 { | |
| 69 return NULL; | |
| 70 } | |
| 71 p_hash_map->entries = Dowa_Arena_Allocate(p_arena, sizeof(*p_hash_map->entries) * capacity); | |
| 72 if (p_hash_map->entries == NULL) | |
| 73 { | |
| 74 free(p_hash_map); | |
| 75 return NULL; | |
| 76 } | |
| 77 p_hash_map->capacity = capacity; | |
| 78 p_hash_map->current_capacity = 0; | |
| 79 p_hash_map->p_arena = p_arena; | |
| 80 return p_hash_map; | |
| 81 } | |
| 82 | |
| 83 void Dowa_HashMap_Destory(Dowa_PHashMap p_hash_map) | |
| 84 { | |
| 85 if (!p_hash_map) return; | |
| 86 | |
| 87 if (p_hash_map->p_arena) | |
| 88 Dowa_Arena_Destory(p_hash_map->p_arena); | |
| 89 else | |
| 62 { | 90 { |
| 63 Dowa_PHashEntry entry; | 91 Dowa_PHashEntry entry; |
| 64 if (p_hash_map->entries) | 92 if (p_hash_map->entries) |
| 65 { | 93 { |
| 66 for (int idx=0; idx<p_hash_map->capacity; idx++) | 94 for (int idx=0; idx<p_hash_map->capacity; idx++) |
| 71 free(entry->key); | 99 free(entry->key); |
| 72 free(entry->buffer); | 100 free(entry->buffer); |
| 73 free(entry); | 101 free(entry); |
| 74 } | 102 } |
| 75 } | 103 } |
| 76 free(p_hash_map->entries); | |
| 77 } | 104 } |
| 105 free(p_hash_map->entries); | |
| 78 } | 106 } |
| 79 free(p_hash_map); | 107 free(p_hash_map); |
| 80 } | 108 } |
| 81 | 109 |
| 82 int32 Dowa_HashMap_GetPosition(Dowa_PHashMap p_hash_map, char *key) | 110 int32 Dowa_HashMap_Get_Position(Dowa_PHashMap p_hash_map, char *key) |
| 83 { | 111 { |
| 84 int32 hash_val = HASH_KEY_NUMBER; | 112 int32 hash_val = HASH_KEY_NUMBER; |
| 85 int32 c; | 113 int32 c; |
| 86 while ((c = *key++)) | 114 while ((c = *key++)) |
| 87 { | 115 { |
| 90 return hash_val % p_hash_map->capacity; | 118 return hash_val % p_hash_map->capacity; |
| 91 } | 119 } |
| 92 | 120 |
| 93 void *Dowa_HashMap_Get(Dowa_PHashMap p_hash_map, char *key) | 121 void *Dowa_HashMap_Get(Dowa_PHashMap p_hash_map, char *key) |
| 94 { | 122 { |
| 95 int idx_foo = Dowa_HashMap_GetPosition(p_hash_map, key); | 123 int idx_foo = Dowa_HashMap_Get_Position(p_hash_map, key); |
| 96 void *value = p_hash_map->entries[idx_foo]; | 124 void *value = p_hash_map->entries[idx_foo]; |
| 97 if (value == NULL || strcmp(((Dowa_PHashEntry) value)->key, key) != 0) | 125 if (value == NULL || strcmp(((Dowa_PHashEntry) value)->key, key) != 0) |
| 98 { | 126 { |
| 99 return NULL; | 127 return NULL; |
| 100 } | 128 } |
| 101 return ((Dowa_PHashEntry) value)->buffer; | 129 return ((Dowa_PHashEntry) value)->buffer; |
| 102 } | 130 } |
| 103 | 131 |
| 104 int32 Dowa_HashMap_PushValueWithTypeNoCopy(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, char *key, void *value, size_t value_size, Dowa_HashMap_ValueType type) |
| 105 { | 133 { |
| 106 int idx = Dowa_HashMap_GetPosition(p_hash_map, key); | 134 int idx = Dowa_HashMap_GetPosition(p_hash_map, key); |
| 107 Dowa_PHashEntry entry = p_hash_map->entries[idx]; | 135 Dowa_PHashEntry entry = p_hash_map->entries[idx]; |
| 108 if (entry) | 136 if (entry) |
| 109 free(entry->buffer); | 137 free(entry->buffer); |
| 110 else | 138 else |
| 111 { | 139 { |
| 112 entry = malloc(sizeof(Dowa_HashEntry)); | 140 entry = p_hash_map->p_arena ? |
| 113 if (entry == NULL) { perror("malloc"); return -1; } | 141 Dowa_Arena_Allocate(p_hash_map->p_arena, sizeof(Dowa_HashEntry)) : |
| 142 malloc(sizeof(Dowa_HashEntry)); | |
| 143 if (entry == NULL) { perror("malloc or arena alloc"); return -1; } | |
| 114 | 144 |
| 115 p_hash_map->entries[idx] = entry; | 145 p_hash_map->entries[idx] = entry; |
| 116 p_hash_map->current_capacity++; | 146 p_hash_map->current_capacity++; |
| 117 entry->key = strdup(key); | 147 entry->key = strdup(key); |
| 118 } | 148 } |
| 120 entry->capacity = value_size; | 150 entry->capacity = value_size; |
| 121 entry->type = type; | 151 entry->type = type; |
| 122 return 0; | 152 return 0; |
| 123 } | 153 } |
| 124 | 154 |
| 125 int32 Dowa_HashMap_PushValueWithType(Dowa_PHashMap p_hash_map, char *key, void *value, size_t value_size, Dowa_HashMap_ValueType type) | 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) |
| 126 { | 156 { |
| 127 int idx = Dowa_HashMap_GetPosition(p_hash_map, key); | 157 int idx = Dowa_HashMap_GetPosition(p_hash_map, key); |
| 128 Dowa_PHashEntry entry = p_hash_map->entries[idx]; | 158 Dowa_PHashEntry entry = p_hash_map->entries[idx]; |
| 129 if (entry) | 159 if (entry) |
| 130 free(entry->buffer); | 160 free(entry->buffer); |
| 131 else | 161 else |
| 132 { | 162 { |
| 133 entry = malloc(sizeof(Dowa_HashEntry)); | 163 entry = p_hash_map->p_arena ? |
| 134 if (entry == NULL) { perror("malloc"); return -1; } | 164 Dowa_Arena_Allocate(p_hash_map->p_arena, sizeof(Dowa_HashEntry)) : |
| 165 malloc(sizeof(Dowa_HashEntry)); | |
| 166 if (entry == NULL) { perror("malloc or arena alloc"); return -1; } | |
| 135 | 167 |
| 136 p_hash_map->entries[idx] = entry; | 168 p_hash_map->entries[idx] = entry; |
| 137 p_hash_map->current_capacity++; | 169 p_hash_map->current_capacity++; |
| 138 entry->key = strdup(key); | 170 entry->key = strdup(key); |
| 139 } | 171 } |
| 140 entry->buffer = malloc(value_size); | 172 entry->buffer = p_hash_map->p_arena ? |
| 141 if (entry->buffer == NULL) { perror("malloc"); return -1; } | 173 Dowa_Arena_Allocate(p_hash_map->p_arena, value_size) : |
| 174 malloc(value_size); | |
| 175 | |
| 176 if (entry->buffer == NULL) { perror("malloc or arena alloc"); return -1; } | |
| 142 entry->capacity = value_size; | 177 entry->capacity = value_size; |
| 143 entry->type = type; | 178 entry->type = type; |
| 144 memcpy(entry->buffer, value, value_size); | 179 memcpy(entry->buffer, value, value_size); |
| 145 return 0; | 180 return 0; |
| 146 } | 181 } |
| 147 | 182 |
| 148 void Dowa_HashMap_PushValue(Dowa_PHashMap p_hash_map, char *key, void *value, size_t value_size) | 183 void Dowa_HashMap_Push_Value(Dowa_PHashMap p_hash_map, char *key, void *value, size_t value_size) |
| 149 { | 184 { |
| 150 Dowa_HashMap_PushValueWithType(p_hash_map, key, value, value_size, DOWA_HASH_MAP_TYPE_BUFFER); | 185 Dowa_HashMap_Push_Value_With_Type(p_hash_map, key, value, value_size, DOWA_HASH_MAP_TYPE_BUFFER); |
| 151 } | 186 } |
| 152 | 187 |
| 153 void Dowa_HashMap_PopKey(Dowa_PHashMap p_hash_map, char *key) | 188 void Dowa_HashMap_Pop_Key(Dowa_PHashMap p_hash_map, char *key) |
| 154 { | 189 { |
| 155 int idx = Dowa_HashMap_GetPosition(p_hash_map, key); | 190 int idx = Dowa_HashMap_GetPosition(p_hash_map, key); |
| 156 Dowa_PHashEntry entry = p_hash_map->entries[idx]; | 191 Dowa_PHashEntry entry = p_hash_map->entries[idx]; |
| 157 if (entry) | 192 if (entry) |
| 158 { | 193 { |
| 209 } | 244 } |
| 210 } | 245 } |
| 211 printf("-----------\n"); | 246 printf("-----------\n"); |
| 212 } | 247 } |
| 213 | 248 |
| 214 int Dowa_HashMap_Cache_Folder(Dowa_PHashMap map, const char *folder_path) | 249 int Dowa_HashMap_Cache_Folder(Dowa_PHashMap p_hash_map, const char *folder_path) |
| 215 { | 250 { |
| 216 DIR *dir = opendir(folder_path); | 251 DIR *dir = opendir(folder_path); |
| 217 if (!dir) { perror("opendir"); return -1; } | 252 if (!dir) { perror("opendir"); return -1; } |
| 218 | 253 |
| 219 struct dirent *entry; | 254 struct dirent *entry; |
| 235 { | 270 { |
| 236 size_t size = (size_t)st.st_size; | 271 size_t size = (size_t)st.st_size; |
| 237 FILE *f = fopen(fullpath, "rb"); | 272 FILE *f = fopen(fullpath, "rb"); |
| 238 if (!f) { perror("fopen"); continue; } | 273 if (!f) { perror("fopen"); continue; } |
| 239 | 274 |
| 240 void *buf = malloc(size); | 275 void *buf = p_hash_map->p_arena ? |
| 276 Dowa_Arena_Allocate(p_hash_map->p_arena, value_size) : | |
| 277 malloc(value_size); | |
| 241 if (!buf) { perror("malloc"); fclose(f); closedir(dir); return -1; } | 278 if (!buf) { perror("malloc"); fclose(f); closedir(dir); return -1; } |
| 242 | 279 |
| 243 if (fread(buf, 1, size, f) != size) | 280 if (fread(buf, 1, size, f) != size) |
| 244 { | 281 { |
| 245 perror("fread"); | 282 perror("fread"); |
| 247 fclose(f); | 284 fclose(f); |
| 248 continue; | 285 continue; |
| 249 } | 286 } |
| 250 fclose(f); | 287 fclose(f); |
| 251 | 288 |
| 252 Dowa_HashMap_PushValueWithType(map, entry->d_name, buf, size, DOWA_HASH_MAP_TYPE_STRING); | 289 Dowa_HashMap_PushValueWithType(p_hash_map, entry->d_name, buf, size, DOWA_HASH_MAP_TYPE_STRING); |
| 253 free(buf); // Dowa_HashMap_PushValue made its own copy | 290 free(buf); // Dowa_HashMap_PushValue made its own copy |
| 254 } | 291 } |
| 255 else if (S_ISDIR(st.st_mode)) | 292 else if (S_ISDIR(st.st_mode)) |
| 256 { | 293 { |
| 257 Dowa_PHashMap p_child_map = Dowa_HashMap_Create(100); | 294 Dowa_PHashMap p_child_map = Dowa_HashMap_Create(100, p_hash_map->p_arena); |
| 258 if (!p_child_map) | 295 if (!p_child_map) |
| 259 { | 296 { |
| 260 perror("Dowa_HashMap_Create"); | 297 perror("Dowa_HashMap_Create"); |
| 261 return -1; | 298 return -1; |
| 262 } | 299 } |
| 265 perror("Dowa_HashMap_Cache_Folder"); | 302 perror("Dowa_HashMap_Cache_Folder"); |
| 266 return -1; | 303 return -1; |
| 267 } | 304 } |
| 268 | 305 |
| 269 // Should not copy as we malloced already. | 306 // Should not copy as we malloced already. |
| 270 if (Dowa_HashMap_PushValueWithTypeNoCopy(map, entry->d_name, p_child_map, | 307 if (Dowa_HashMap_Push_Value_With_Type_NoCopy(p_hash_map, entry->d_name, p_child_map, |
| 271 sizeof(p_child_map), DOWA_HASH_MAP_TYPE_HASHMAP) == -1) | 308 sizeof(p_child_map), DOWA_HASH_MAP_TYPE_HASHMAP) == -1) |
| 272 { | 309 { |
| 273 Dowa_HashMap_Free(p_child_map); | 310 Dowa_HashMap_Destory(p_child_map); |
| 274 return -1; | 311 return -1; |
| 275 } | 312 } |
| 276 } | 313 } |
| 277 } | 314 } |
| 278 closedir(dir); | 315 closedir(dir); |