Skip to content

Commit e9cfb8e

Browse files
committed
feat allocator: added malloc zalloc class templates examples
Signed-off-by: John Sanpe <sanpeqf@gmail.com>
1 parent c0ee8e3 commit e9cfb8e

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

examples/allocator/guards.c

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/* SPDX-License-Identifier: GPL-2.0-or-later */
2+
/*
3+
* Copyright(c) 2025 John Sanpe <sanpeqf@gmail.com>
4+
*/
5+
6+
#include <string.h>
7+
#include <bfdev/allocator.h>
8+
#include <bfdev/size.h>
9+
10+
#define TEST_SIZE BFDEV_SZ_64KiB
11+
12+
static int
13+
test_cleanup(void)
14+
{
15+
BFDEV_CLEAN(bfdev_free) void *ptr;
16+
17+
ptr = bfdev_malloc(NULL, TEST_SIZE);
18+
if (!ptr)
19+
return 1;
20+
bzero(ptr, TEST_SIZE);
21+
22+
return 0;
23+
}
24+
25+
static int
26+
test_class_malloc(void)
27+
{
28+
BFDEV_CLASS(bfdev_malloc, ptr)(TEST_SIZE);
29+
30+
if (!ptr)
31+
return 1;
32+
bzero(ptr, TEST_SIZE);
33+
34+
return 0;
35+
}
36+
37+
static int
38+
test_class_zalloc(void)
39+
{
40+
BFDEV_CLASS(bfdev_zalloc, ptr)(TEST_SIZE);
41+
42+
if (!ptr)
43+
return 1;
44+
bzero(ptr, TEST_SIZE);
45+
46+
return 0;
47+
}
48+
49+
int
50+
main(int argc, char const *argv[])
51+
{
52+
int retval;
53+
54+
if ((retval = test_cleanup()) ||
55+
(retval = test_class_malloc()) ||
56+
(retval = test_class_zalloc()))
57+
return retval;
58+
59+
return 0;
60+
}

0 commit comments

Comments
 (0)