Derive-C
Loading...
Searching...
No Matches
alloc.c
Go to the documentation of this file.
1#include <stdio.h>
2
4#include <derive-c/prelude.h>
5
6static void example_std() {
8 void* ptr = stdalloc_allocate_uninit(stdalloc_get_ref(), 256);
9 ptr = stdalloc_reallocate(stdalloc_get_ref(), ptr, 256, 512);
10 stdalloc_deallocate(stdalloc_get_ref(), ptr, 512);
11}
12
13#define CAPACITY 512
14#define NAME hybrid
16
17static void example_hybridstatic() {
19 hybrid_buffer buf;
20 DC_SCOPED(hybrid) alloc = hybrid_new(&buf, stdalloc_get_ref());
21
22 void* ptr = hybrid_allocate_uninit(&alloc, 100);
23 ptr = hybrid_reallocate(&alloc, ptr, 100, 200);
24 hybrid_deallocate(&alloc, ptr, 200);
25}
26
27// We can only unleak in debug builds
28#if !defined(NDEBUG)
29 #define NAME test_alloc
31
32static void example_test() {
34 DC_SCOPED(test_alloc) alloc = test_alloc_new(stdalloc_get_ref());
35
36 test_alloc_allocate_uninit(&alloc, 128);
37 test_alloc_debug(&alloc, dc_debug_fmt_new(), stdout);
38 test_alloc_unleak(&alloc);
39}
40#else
41static void example_test() {}
42#endif
43
44#define NAME dbg
46
47#define ALLOC dbg
48#define BLOCK_SIZE 1024
49#define SLAB_SIZE 8096
50#define NAME slab_large
52
53#define ALLOC slab_large
54#define NAME slab_large_alloc_dbg
56
57#define ALLOC slab_large_alloc_dbg
58#define BLOCK_SIZE 32
59#define SLAB_SIZE 8096
60#define NAME slab_small
62
63#define ALLOC slab_small
64#define NAME slab_small_alloc_dbg
66
67static void example_slab() {
69 DC_SCOPED(dbg) user_alloc = dbg_new("user_alloc", stdout, stdalloc_get_ref());
70 DC_SCOPED(slab_large) large_slab = slab_large_new(&user_alloc);
71 DC_SCOPED(slab_large_alloc_dbg)
72 slab_large_alloc = slab_large_alloc_dbg_new("slab_large alloc", stdout, &large_slab);
73 DC_SCOPED(slab_small) small_slab = slab_small_new(&slab_large_alloc);
74 DC_SCOPED(slab_small_alloc_dbg)
75 slab_small_alloc = slab_small_alloc_dbg_new("slab_small alloc", stdout, &small_slab);
76
77 void* ptr1 = slab_small_alloc_dbg_allocate_uninit(&slab_small_alloc, 32);
78 void* ptr2 =
79 slab_small_alloc_dbg_allocate_uninit(&slab_small_alloc, 64); // Larger than block size (32)
80 void* ptr3 = slab_small_alloc_dbg_allocate_uninit(&slab_small_alloc,
81 2048); // Larger than large block size (1024)
82
83 dbg_debug(&user_alloc, dc_debug_fmt_new(), stdout);
84
85 slab_small_alloc_dbg_deallocate(&slab_small_alloc, ptr1, 32);
86 slab_small_alloc_dbg_deallocate(&slab_small_alloc, ptr2, 64);
87 slab_small_alloc_dbg_deallocate(&slab_small_alloc, ptr3, 2048);
88}
89
90#define ALLOC dbg
91#define BLOCK_SIZE 256
92#define NAME chunked
94
95static void example_chunkedbump() {
97 DC_SCOPED(dbg) debug_alloc = dbg_new("chunked_example", stdout, stdalloc_get_ref());
98 DC_SCOPED(chunked) alloc = chunked_new(&debug_alloc);
99
100 void* small = chunked_allocate_uninit(&alloc, 64);
101 void* large = chunked_allocate_uninit(&alloc, 512);
102
103 dbg_debug(&debug_alloc, dc_debug_fmt_new(), stdout);
104
105 chunked_deallocate(&alloc, small, 64);
106 chunked_deallocate(&alloc, large, 512);
107}
108
109int main() {
110 example_std();
112 example_test();
113 example_slab();
115 return 0;
116}
static void example_chunkedbump()
Definition alloc.c:95
static void example_hybridstatic()
Definition alloc.c:17
static void example_slab()
Definition alloc.c:67
static void example_std()
Definition alloc.c:6
static void example_test()
Definition alloc.c:32
int main()
Definition alloc.c:109
static DC_PUBLIC dc_debug_fmt dc_debug_fmt_new()
Definition fmt.h:15
#define DC_SCOPED(type,...)
RAII in C. Call the destructor when the variable goes out of scope.
Definition scope.h:5
#define DC_DEBUG_TRACE
Definition debug.h:17