Skip to content

Commit

Permalink
[CN-Exec] Add zalloc for hashtable
Browse files Browse the repository at this point in the history
It expects zeroed memory
  • Loading branch information
ZippeyKeys12 committed Sep 26, 2024
1 parent ffd0e1e commit 45d51b4
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
5 changes: 5 additions & 0 deletions runtime/libcn/include/cn-executable/alloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 6 additions & 0 deletions runtime/libcn/src/alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions runtime/libcn/src/hash_table.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 45d51b4

Please sign in to comment.