diff options
author | Baitinq <manuelpalenzuelamerino@gmail.com> | 2025-01-04 16:24:29 +0100 |
---|---|---|
committer | Baitinq <manuelpalenzuelamerino@gmail.com> | 2025-01-04 16:24:29 +0100 |
commit | 75d42cc0e3d9feccf3a367f77e299acc4abfabd4 (patch) | |
tree | 9a7a5cbbbba018058aaa86711f0c14843a1ff1a9 | |
parent | Start implementing referencing hashmap impl in fuzzer (diff) | |
download | c-hashtable-75d42cc0e3d9feccf3a367f77e299acc4abfabd4.tar.gz c-hashtable-75d42cc0e3d9feccf3a367f77e299acc4abfabd4.tar.bz2 c-hashtable-75d42cc0e3d9feccf3a367f77e299acc4abfabd4.zip |
Copy memory in hashmap
-rw-r--r-- | lib/hashtable.c | 12 | ||||
-rw-r--r-- | lib/hashtable.h | 2 | ||||
-rw-r--r-- | src/main.zig | 8 |
3 files changed, 14 insertions, 8 deletions
diff --git a/lib/hashtable.c b/lib/hashtable.c index e7799ad..f9f9873 100644 --- a/lib/hashtable.c +++ b/lib/hashtable.c @@ -44,6 +44,11 @@ int hashtable_deinit(HashTable* ht) { for (int i = 0; i < ht_impl->capacity; ++i) { HashTableBucket bucket = ht_impl->buckets[i]; + for (int j = 0; j < bucket.length; ++j) { + HashTableData data = bucket.data[j]; + free(data.key); + free(data.data); + } free(bucket.data); } @@ -68,7 +73,7 @@ void* hashtable_get(HashTable ht, char* key) { return NULL; } -int hashtable_put(HashTable ht, char* key, void* val) { +int hashtable_put(HashTable ht, char* key, void* val, size_t val_size) { HashTableImpl* ht_impl = (HashTableImpl*) ht; int index = hash(key, ht_impl->capacity); @@ -91,10 +96,11 @@ int hashtable_put(HashTable ht, char* key, void* val) { bucket->data = realloc(bucket->data, sizeof(HashTableData) * bucket->length); HashTableData newData = { - .key = key, - .data = val, + .key = strdup(key), + .data = malloc(val_size), .deleted = 0 }; + memcpy(newData.data, val, val_size); bucket->data[bucket->length - 1] = newData; return 0; diff --git a/lib/hashtable.h b/lib/hashtable.h index 3f4be0e..a4ccd73 100644 --- a/lib/hashtable.h +++ b/lib/hashtable.h @@ -7,7 +7,7 @@ HashTable hashtable_init(size_t); int hashtable_deinit(HashTable*); -int hashtable_put(HashTable, char*, void*); +int hashtable_put(HashTable, char*, void*, size_t); int hashtable_remove(HashTable, char*); diff --git a/src/main.zig b/src/main.zig index c443859..a1a049f 100644 --- a/src/main.zig +++ b/src/main.zig @@ -18,7 +18,7 @@ pub fn main() !void { .data = 7, }; - _ = hashtable.hashtable_put(ht, @constCast("key"), @constCast(&data)); + _ = hashtable.hashtable_put(ht, @constCast("key"), @constCast(&data), @sizeOf(Example)); const res: *Example = @ptrCast(hashtable.hashtable_get(ht, @constCast("key"))); std.debug.print("Result: {d}\n", .{res.*.data}); } @@ -27,7 +27,7 @@ test "simple test" { var ht = hashtable.hashtable_init(8); defer _ = hashtable.hashtable_deinit(&ht); const data: i32 = 4; - _ = hashtable.hashtable_put(ht, @constCast("key"), @constCast(&data)); + _ = hashtable.hashtable_put(ht, @constCast("key"), @constCast(&data), @sizeOf(i32)); const res: *align(1) i32 = @ptrCast(hashtable.hashtable_get(ht, @constCast("key"))); try std.testing.expectEqual(@as(i32, 4), res.*); } @@ -36,7 +36,7 @@ test "removing element" { var ht = hashtable.hashtable_init(8); defer _ = hashtable.hashtable_deinit(&ht); const data: i32 = 4; - _ = hashtable.hashtable_put(ht, @constCast("key"), @constCast(&data)); + _ = hashtable.hashtable_put(ht, @constCast("key"), @constCast(&data), @sizeOf(i32)); _ = hashtable.hashtable_remove(ht, @constCast("key")); const res = hashtable.hashtable_get(ht, @constCast("key")); try std.testing.expectEqual(null, res); @@ -79,7 +79,7 @@ test "fuzzing" { }, 1 => { std.debug.print("Putting key {any} - {d}\n", .{ key, key.* }); - _ = hashtable.hashtable_put(ht, key, @constCast(&value)); + _ = hashtable.hashtable_put(ht, key, @constCast(&value), @sizeOf(u8)); try reference_hashmap.put(key, @constCast(&value)); }, 2 => { |