Skip to content

Commit 6d543f5

Browse files
committed
Migrate hb_narray to use new allocator system
1 parent 2585052 commit 6d543f5

File tree

3 files changed

+45
-15
lines changed

3 files changed

+45
-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: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
#include "include/test.h"
22
#include "../../src/include/util/hb_narray.h"
3+
#include <alloca.h>
34

45
TEST(test_hb_narray_init)
56
hb_narray_T array;
7+
hb_allocator_T allocator;
8+
hb_allocator_init(&allocator, HB_ALLOCATOR_MALLOC);
69

7-
ck_assert(hb_narray_init(&array, sizeof(uint64_t), 1024));
10+
ck_assert(hb_narray_init(&array, sizeof(uint64_t), 1024, &allocator));
811

912
ck_assert_int_eq(array.item_size, sizeof(uint64_t));
1013
ck_assert_int_eq(array.capacity, 1024);
@@ -17,7 +20,10 @@ END
1720
TEST(test_hb_narray_pointer_init)
1821
hb_narray_T array;
1922

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

2228
ck_assert_int_eq(array.item_size, sizeof(void *));
2329
ck_assert_int_eq(array.capacity, 1024);
@@ -30,7 +36,10 @@ END
3036
TEST(test_hb_narray_append)
3137
hb_narray_T array;
3238

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

3544
uint64_t number = 1;
3645
ck_assert(hb_narray_append(&array, &number));
@@ -55,8 +64,10 @@ END
5564

5665
TEST(test_hb_narray_first_last)
5766
hb_narray_T array;
67+
hb_allocator_T allocator;
68+
hb_allocator_init(&allocator, HB_ALLOCATOR_MALLOC);
5869

59-
hb_narray_init(&array, sizeof(uint64_t), 2);
70+
hb_narray_init(&array, sizeof(uint64_t), 2, &allocator);
6071

6172
ck_assert_ptr_null(hb_narray_first(&array));
6273
ck_assert_ptr_null(hb_narray_last(&array));
@@ -78,8 +89,10 @@ END
7889

7990
TEST(test_hb_narray_stack_behavior)
8091
hb_narray_T array;
92+
hb_allocator_T allocator;
93+
hb_allocator_init(&allocator, HB_ALLOCATOR_MALLOC);
8194

82-
hb_narray_init(&array, sizeof(uint64_t), 2);
95+
hb_narray_init(&array, sizeof(uint64_t), 2, &allocator);
8396

8497
for(uint64_t i = 0; i < 4; i++) {
8598
hb_narray_push(&array, &i);
@@ -110,8 +123,10 @@ END
110123

111124
TEST(test_hb_narray_remove)
112125
hb_narray_T array;
126+
hb_allocator_T allocator;
127+
hb_allocator_init(&allocator, HB_ALLOCATOR_MALLOC);
113128

114-
hb_narray_init(&array, sizeof(uint64_t), 2);
129+
hb_narray_init(&array, sizeof(uint64_t), 2, &allocator);
115130

116131
for(uint64_t i = 0; i < 4; i++) {
117132
hb_narray_push(&array, &i);
@@ -141,9 +156,11 @@ END
141156
// Test hb_narray_size with NULL safety
142157
TEST(test_hb_narray_size)
143158
ck_assert_int_eq(hb_narray_size(NULL), 0);
159+
hb_allocator_T allocator;
160+
hb_allocator_init(&allocator, HB_ALLOCATOR_MALLOC);
144161

145162
hb_narray_T array;
146-
hb_narray_init(&array, sizeof(uint64_t), 5);
163+
hb_narray_init(&array, sizeof(uint64_t), 5, &allocator);
147164
ck_assert_int_eq(hb_narray_size(&array), 0);
148165

149166
uint64_t item1 = 42, item2 = 99;
@@ -158,8 +175,10 @@ END
158175

159176
TEST(test_hb_narray_init_returns_bool)
160177
hb_narray_T array;
178+
hb_allocator_T allocator;
179+
hb_allocator_init(&allocator, HB_ALLOCATOR_MALLOC);
161180

162-
ck_assert(hb_narray_init(&array, sizeof(uint64_t), 4));
181+
ck_assert(hb_narray_init(&array, sizeof(uint64_t), 4, &allocator));
163182
ck_assert_ptr_nonnull(array.items);
164183
ck_assert_int_eq(array.size, 0);
165184
ck_assert_int_eq(array.capacity, 4);
@@ -169,8 +188,10 @@ END
169188

170189
TEST(test_hb_narray_append_returns_bool)
171190
hb_narray_T array;
191+
hb_allocator_T allocator;
192+
hb_allocator_init(&allocator, HB_ALLOCATOR_MALLOC);
172193

173-
ck_assert(hb_narray_init(&array, sizeof(uint64_t), 2));
194+
ck_assert(hb_narray_init(&array, sizeof(uint64_t), 2, &allocator));
174195

175196
uint64_t number = 42;
176197
ck_assert(hb_narray_append(&array, &number));

0 commit comments

Comments
 (0)