From 45d51b491354a68270e2c6740ff0e538c2f9e9a3 Mon Sep 17 00:00:00 2001 From: Zain K Aamer Date: Thu, 26 Sep 2024 15:07:39 -0400 Subject: [PATCH] [CN-Exec] Add `zalloc` for hashtable It expects zeroed memory --- runtime/libcn/include/cn-executable/alloc.h | 5 +++++ runtime/libcn/src/alloc.c | 6 ++++++ runtime/libcn/src/hash_table.c | 4 ++-- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/runtime/libcn/include/cn-executable/alloc.h b/runtime/libcn/include/cn-executable/alloc.h index 04b25c7a6..9f38bbabf 100644 --- a/runtime/libcn/include/cn-executable/alloc.h +++ b/runtime/libcn/include/cn-executable/alloc.h @@ -21,6 +21,11 @@ void *alloc_(long nbytes, const char *, int); #define alloc(x)\ alloc_(x, __FILE__, __LINE__) +void *zalloc_(long nbytes, const char *, int); + +#define zalloc(x)\ + zalloc_(x, __FILE__, __LINE__) + void free_all(void); // void *alloc_zeros(long nbytes); diff --git a/runtime/libcn/src/alloc.c b/runtime/libcn/src/alloc.c index 674e6d450..d56216fe8 100644 --- a/runtime/libcn/src/alloc.c +++ b/runtime/libcn/src/alloc.c @@ -53,6 +53,12 @@ void *alloc_(long nbytes, const char *str, int line) { //return malloc(nbytes); } +void *zalloc_(long nbytes, const char *str, int line) { + void *p = alloc_(nbytes, str, line); + memset(p, 0, nbytes); + return p; +} + void free_all(void) { curr = buf; } diff --git a/runtime/libcn/src/hash_table.c b/runtime/libcn/src/hash_table.c index ed792da03..e96b74cf5 100644 --- a/runtime/libcn/src/hash_table.c +++ b/runtime/libcn/src/hash_table.c @@ -43,7 +43,7 @@ hash_table* ht_create(void) { table->capacity = INITIAL_CAPACITY; // Allocate (zero'd) space for entry buckets. - table->entries = alloc(table->capacity * sizeof(ht_entry)); + table->entries = zalloc(table->capacity * sizeof(ht_entry)); if (table->entries == NULL) { // free(table); // error, free table before we return! return NULL; @@ -148,7 +148,7 @@ static _Bool ht_expand(hash_table* table) { if (new_capacity < table->capacity) { return 0; // overflow (capacity would be too big) } - ht_entry* new_entries = alloc(new_capacity * sizeof(ht_entry)); + ht_entry* new_entries = zalloc(new_capacity * sizeof(ht_entry)); if (new_entries == NULL) { return 0; }