Skip to content

Commit 95a11e3

Browse files
authored
C: Migrate hb_narray to use new allocator system (#1327)
This MR migrates the `hb_narray` implementation to use the `hb_allocator` interface for allocating memory. Related #1323 and #1326
1 parent 8469af5 commit 95a11e3

File tree

3 files changed

+44
-15
lines changed

3 files changed

+44
-15
lines changed

src/include/util/hb_narray.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,18 @@
55
#include <stdlib.h>
66
#include <stdbool.h>
77

8+
#include "hb_allocator.h"
9+
810
typedef struct HB_NARRAY_STRUCT {
11+
hb_allocator_T *allocator;
912
uint8_t* items;
1013
size_t item_size;
1114
size_t size;
1215
size_t capacity;
1316
} hb_narray_T;
1417

15-
bool hb_narray_init(hb_narray_T* array, size_t item_size, size_t initial_capacity);
16-
#define hb_narray_pointer_init(array, initial_capacity) (hb_narray_init(array, sizeof(void*), initial_capacity))
18+
bool hb_narray_init(hb_narray_T* array, size_t item_size, size_t initial_capacity, hb_allocator_T* allocator);
19+
#define hb_narray_pointer_init(array, initial_capacity, allocator) (hb_narray_init(array, sizeof(void*), initial_capacity, allocator))
1720

1821
void* hb_narray_get(const hb_narray_T* array, size_t index);
1922
void* hb_narray_first(hb_narray_T* array);

src/util/hb_narray.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44
#include <stdbool.h>
55
#include <string.h>
66

7-
bool hb_narray_init(hb_narray_T* array, size_t item_size, size_t initial_capacity) {
7+
bool hb_narray_init(hb_narray_T* array, size_t item_size, size_t initial_capacity, hb_allocator_T* allocator) {
88
assert(initial_capacity != 0);
99

10+
array->allocator = allocator;
1011
array->item_size = item_size;
1112
array->capacity = initial_capacity;
1213
array->size = 0;
13-
array->items = malloc(array->capacity * array->item_size);
14+
array->items = hb_allocator_alloc(array->allocator, array->capacity * array->item_size);
1415

1516
if (!array->items) { return false; }
1617

@@ -20,7 +21,12 @@ bool hb_narray_init(hb_narray_T* array, size_t item_size, size_t initial_capacit
2021
bool hb_narray_append(hb_narray_T* array, void* item) {
2122
if (array->size + 1 > array->capacity) {
2223
size_t new_capacity = array->capacity * 2;
23-
void* new_buffer = realloc(array->items, new_capacity * array->item_size);
24+
void* new_buffer = hb_allocator_realloc(
25+
array->allocator,
26+
array->items,
27+
array->capacity * array->item_size,
28+
new_capacity * array->item_size
29+
);
2430

2531
if (!new_buffer) { return false; }
2632

@@ -80,7 +86,7 @@ void hb_narray_deinit(hb_narray_T* array) {
8086
array->item_size = 0;
8187
array->capacity = 0;
8288
array->size = 0;
83-
free(array->items);
89+
hb_allocator_dealloc(array->allocator, array->items);
8490
}
8591

8692
size_t hb_narray_size(const hb_narray_T* array) {

test/c/test_hb_narray.c

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33

44
TEST(test_hb_narray_init)
55
hb_narray_T array;
6+
hb_allocator_T allocator;
7+
hb_allocator_init(&allocator, HB_ALLOCATOR_MALLOC);
68

7-
ck_assert(hb_narray_init(&array, sizeof(uint64_t), 1024));
9+
ck_assert(hb_narray_init(&array, sizeof(uint64_t), 1024, &allocator));
810

911
ck_assert_int_eq(array.item_size, sizeof(uint64_t));
1012
ck_assert_int_eq(array.capacity, 1024);
@@ -17,7 +19,10 @@ END
1719
TEST(test_hb_narray_pointer_init)
1820
hb_narray_T array;
1921

20-
ck_assert(hb_narray_pointer_init(&array, 1024));
22+
hb_allocator_T allocator;
23+
hb_allocator_init(&allocator, HB_ALLOCATOR_MALLOC);
24+
25+
ck_assert(hb_narray_pointer_init(&array, 1024, &allocator));
2126

2227
ck_assert_int_eq(array.item_size, sizeof(void *));
2328
ck_assert_int_eq(array.capacity, 1024);
@@ -30,7 +35,10 @@ END
3035
TEST(test_hb_narray_append)
3136
hb_narray_T array;
3237

33-
ck_assert(hb_narray_init(&array, sizeof(uint64_t), 2));
38+
hb_allocator_T allocator;
39+
hb_allocator_init(&allocator, HB_ALLOCATOR_MALLOC);
40+
41+
ck_assert(hb_narray_init(&array, sizeof(uint64_t), 2, &allocator));
3442

3543
uint64_t number = 1;
3644
ck_assert(hb_narray_append(&array, &number));
@@ -55,8 +63,10 @@ END
5563

5664
TEST(test_hb_narray_first_last)
5765
hb_narray_T array;
66+
hb_allocator_T allocator;
67+
hb_allocator_init(&allocator, HB_ALLOCATOR_MALLOC);
5868

59-
hb_narray_init(&array, sizeof(uint64_t), 2);
69+
hb_narray_init(&array, sizeof(uint64_t), 2, &allocator);
6070

6171
ck_assert_ptr_null(hb_narray_first(&array));
6272
ck_assert_ptr_null(hb_narray_last(&array));
@@ -78,8 +88,10 @@ END
7888

7989
TEST(test_hb_narray_stack_behavior)
8090
hb_narray_T array;
91+
hb_allocator_T allocator;
92+
hb_allocator_init(&allocator, HB_ALLOCATOR_MALLOC);
8193

82-
hb_narray_init(&array, sizeof(uint64_t), 2);
94+
hb_narray_init(&array, sizeof(uint64_t), 2, &allocator);
8395

8496
for(uint64_t i = 0; i < 4; i++) {
8597
hb_narray_push(&array, &i);
@@ -110,8 +122,10 @@ END
110122

111123
TEST(test_hb_narray_remove)
112124
hb_narray_T array;
125+
hb_allocator_T allocator;
126+
hb_allocator_init(&allocator, HB_ALLOCATOR_MALLOC);
113127

114-
hb_narray_init(&array, sizeof(uint64_t), 2);
128+
hb_narray_init(&array, sizeof(uint64_t), 2, &allocator);
115129

116130
for(uint64_t i = 0; i < 4; i++) {
117131
hb_narray_push(&array, &i);
@@ -141,9 +155,11 @@ END
141155
// Test hb_narray_size with NULL safety
142156
TEST(test_hb_narray_size)
143157
ck_assert_int_eq(hb_narray_size(NULL), 0);
158+
hb_allocator_T allocator;
159+
hb_allocator_init(&allocator, HB_ALLOCATOR_MALLOC);
144160

145161
hb_narray_T array;
146-
hb_narray_init(&array, sizeof(uint64_t), 5);
162+
hb_narray_init(&array, sizeof(uint64_t), 5, &allocator);
147163
ck_assert_int_eq(hb_narray_size(&array), 0);
148164

149165
uint64_t item1 = 42, item2 = 99;
@@ -158,8 +174,10 @@ END
158174

159175
TEST(test_hb_narray_init_returns_bool)
160176
hb_narray_T array;
177+
hb_allocator_T allocator;
178+
hb_allocator_init(&allocator, HB_ALLOCATOR_MALLOC);
161179

162-
ck_assert(hb_narray_init(&array, sizeof(uint64_t), 4));
180+
ck_assert(hb_narray_init(&array, sizeof(uint64_t), 4, &allocator));
163181
ck_assert_ptr_nonnull(array.items);
164182
ck_assert_int_eq(array.size, 0);
165183
ck_assert_int_eq(array.capacity, 4);
@@ -169,8 +187,10 @@ END
169187

170188
TEST(test_hb_narray_append_returns_bool)
171189
hb_narray_T array;
190+
hb_allocator_T allocator;
191+
hb_allocator_init(&allocator, HB_ALLOCATOR_MALLOC);
172192

173-
ck_assert(hb_narray_init(&array, sizeof(uint64_t), 2));
193+
ck_assert(hb_narray_init(&array, sizeof(uint64_t), 2, &allocator));
174194

175195
uint64_t number = 42;
176196
ck_assert(hb_narray_append(&array, &number));

0 commit comments

Comments
 (0)