Skip to content

Commit 83452d8

Browse files
committed
feat array: added class template
Signed-off-by: John Sanpe <sanpeqf@gmail.com>
1 parent adc2a31 commit 83452d8

2 files changed

Lines changed: 29 additions & 6 deletions

File tree

examples/array/simple.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,21 @@
1414
int
1515
main(int argc, const char *argv[])
1616
{
17-
BFDEV_DEFINE_ARRAY(array, NULL, TEST_SIZE);
17+
BFDEV_CLASS(bfdev_array, array)(NULL, TEST_SIZE);
1818
unsigned int count;
1919

20-
bfdev_array_append(&array, 0, 0);
21-
2220
for (count = 0; count < TEST_LOOP; ++count) {
2321
unsigned int num;
2422
void *buff;
2523

2624
num = rand() % TEST_SIZE;
27-
buff = bfdev_array_push(&array, num);
25+
buff = bfdev_array_push(array, num);
2826
if (!buff)
2927
return 1;
3028

3129
memset(buff, 0, TEST_SIZE * num);
3230
printf("array bfdev_array_push test: %02u: %u\n", count, num);
3331
}
3432

35-
bfdev_array_release(&array);
36-
3733
return 0;
3834
}

include/bfdev/array.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <bfdev/stddef.h>
1313
#include <bfdev/string.h>
1414
#include <bfdev/allocator.h>
15+
#include <bfdev/guards.h>
1516

1617
BFDEV_BEGIN_DECLS
1718

@@ -207,6 +208,32 @@ bfdev_array_reserve(bfdev_array_t *array, unsigned long num);
207208
extern void
208209
bfdev_array_release(bfdev_array_t *array);
209210

211+
static inline bfdev_array_t *
212+
bfdev_array_create(const bfdev_alloc_t *alloc, bfdev_size_t cells)
213+
{
214+
bfdev_array_t *obj;
215+
216+
obj = bfdev_malloc(alloc, sizeof(*obj));
217+
if (bfdev_unlikely(!obj))
218+
return BFDEV_NULL;
219+
bfdev_array_init(obj, alloc, cells);
220+
221+
return obj;
222+
}
223+
224+
static inline void
225+
bfdev_array_destroy(bfdev_array_t *obj)
226+
{
227+
bfdev_array_release(obj);
228+
bfdev_free(obj->alloc, obj);
229+
}
230+
231+
BFDEV_DEFINE_CLASS(bfdev_array, bfdev_array_t *,
232+
bfdev_array_create(alloc, cells),
233+
bfdev_array_destroy(_T),
234+
const bfdev_alloc_t *alloc, bfdev_size_t cells
235+
)
236+
210237
BFDEV_END_DECLS
211238

212239
#endif /* _BFDEV_ARRAY_H_ */

0 commit comments

Comments
 (0)