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(DC_LOGGER* parent) {
7 DC_SCOPED(DC_LOGGER) log = DC_LOGGER_NEW(parent, "%s", __func__);
8 DC_LOG(log, DC_INFO, "allocating 256 bytes");
9 void* ptr = stdalloc_allocate_uninit(stdalloc_get_ref(), 256);
10 DC_LOG(log, DC_INFO, "reallocating to 512 bytes");
11 ptr = stdalloc_reallocate(stdalloc_get_ref(), ptr, 256, 512);
12 DC_LOG(log, DC_INFO, "deallocating");
13 stdalloc_deallocate(stdalloc_get_ref(), ptr, 512);
14}
15
16#define CAPACITY 512
17#define NAME hybrid
19
20static void example_hybridstatic(DC_LOGGER* parent) {
21 DC_SCOPED(DC_LOGGER) log = DC_LOGGER_NEW(parent, "%s", __func__);
22 hybrid_buffer buf;
23 DC_SCOPED(hybrid) alloc = hybrid_new(&buf, stdalloc_get_ref());
24
25 DC_LOG(log, DC_INFO, "allocating 100 bytes");
26 void* ptr = hybrid_allocate_uninit(&alloc, 100);
27 DC_LOG(log, DC_INFO, "reallocating to 200 bytes");
28 ptr = hybrid_reallocate(&alloc, ptr, 100, 200);
29 DC_LOG(log, DC_INFO, "deallocating");
30 hybrid_deallocate(&alloc, ptr, 200);
31}
32
33// We can only unleak in debug builds
34#if !defined(NDEBUG)
35 #define NAME test_alloc
37
38static void example_test(DC_LOGGER* parent) {
39 DC_SCOPED(DC_LOGGER) log = DC_LOGGER_NEW(parent, "%s", __func__);
40 DC_SCOPED(test_alloc) alloc = test_alloc_new(stdalloc_get_ref());
41
42 DC_LOG(log, DC_INFO, "allocating 128 bytes (will leak)");
43 test_alloc_allocate_uninit(&alloc, 128);
44 DC_LOG(log, DC_INFO, "debug: %s", DC_DEBUG(test_alloc_debug, &alloc));
45 DC_LOG(log, DC_INFO, "unleaking");
46 test_alloc_unleak(&alloc);
47}
48#else
49static void example_test(DC_LOGGER* parent) { (void)parent; }
50#endif
51
52#define NAME dbg
54
55#define ALLOC dbg
56#define BLOCK_SIZE 1024
57#define SLAB_SIZE 8096
58#define NAME slab_large
60
61#define ALLOC slab_large
62#define NAME slab_large_alloc_dbg
64
65#define ALLOC slab_large_alloc_dbg
66#define BLOCK_SIZE 32
67#define SLAB_SIZE 8096
68#define NAME slab_small
70
71#define ALLOC slab_small
72#define NAME slab_small_alloc_dbg
74
75static void example_slab(DC_LOGGER* parent) {
76 DC_SCOPED(DC_LOGGER) log = DC_LOGGER_NEW(parent, "%s", __func__);
77 DC_SCOPED(dbg) user_alloc = dbg_new("user_alloc", stdout, stdalloc_get_ref());
78 DC_SCOPED(slab_large) large_slab = slab_large_new(&user_alloc);
79 DC_SCOPED(slab_large_alloc_dbg)
80 slab_large_alloc = slab_large_alloc_dbg_new("slab_large alloc", stdout, &large_slab);
81 DC_SCOPED(slab_small) small_slab = slab_small_new(&slab_large_alloc);
82 DC_SCOPED(slab_small_alloc_dbg)
83 slab_small_alloc = slab_small_alloc_dbg_new("slab_small alloc", stdout, &small_slab);
84
85 DC_LOG(log, DC_INFO, "allocating 32 bytes from small slab");
86 void* ptr1 = slab_small_alloc_dbg_allocate_uninit(&slab_small_alloc, 32);
87 DC_LOG(log, DC_INFO, "allocating 64 bytes (larger than block size 32)");
88 void* ptr2 = slab_small_alloc_dbg_allocate_uninit(&slab_small_alloc, 64);
89 DC_LOG(log, DC_INFO, "allocating 2048 bytes (larger than large block size 1024)");
90 void* ptr3 = slab_small_alloc_dbg_allocate_uninit(&slab_small_alloc, 2048);
91
92 DC_LOG(log, DC_INFO, "debug: %s", DC_DEBUG(dbg_debug, &user_alloc));
93
94 slab_small_alloc_dbg_deallocate(&slab_small_alloc, ptr1, 32);
95 slab_small_alloc_dbg_deallocate(&slab_small_alloc, ptr2, 64);
96 slab_small_alloc_dbg_deallocate(&slab_small_alloc, ptr3, 2048);
97}
98
99#define ALLOC dbg
100#define BLOCK_SIZE 256
101#define NAME chunked
103
104static void example_chunkedbump(DC_LOGGER* parent) {
105 DC_SCOPED(DC_LOGGER) log = DC_LOGGER_NEW(parent, "%s", __func__);
106 DC_SCOPED(dbg) debug_alloc = dbg_new("chunked_example", stdout, stdalloc_get_ref());
107 DC_SCOPED(chunked) alloc = chunked_new(&debug_alloc);
108
109 DC_LOG(log, DC_INFO, "allocating 64 bytes (small)");
110 void* small = chunked_allocate_uninit(&alloc, 64);
111 DC_LOG(log, DC_INFO, "allocating 512 bytes (large)");
112 void* large = chunked_allocate_uninit(&alloc, 512);
113
114 DC_LOG(log, DC_INFO, "debug: %s", DC_DEBUG(dbg_debug, &debug_alloc));
115
116 chunked_deallocate(&alloc, small, 64);
117 chunked_deallocate(&alloc, large, 512);
118}
119
120int main() {
122 root = NS(DC_LOGGER,
123 new_global)((NS(DC_LOGGER, global_config)){.stream = stdout, .ansi_colours = true},
124 (dc_log_id){"alloc"});
125
126 example_std(&root);
128 example_test(&root);
129 example_slab(&root);
130 example_chunkedbump(&root);
131 return 0;
132}
static void example_hybridstatic(DC_LOGGER *parent)
Definition alloc.c:20
static void example_slab(DC_LOGGER *parent)
Definition alloc.c:75
static void example_std(DC_LOGGER *parent)
Definition alloc.c:6
static void example_test(DC_LOGGER *parent)
Definition alloc.c:38
static void example_chunkedbump(DC_LOGGER *parent)
Definition alloc.c:104
int main()
Definition alloc.c:120
#define DC_DEBUG(DEBUG_FN, DEBUG_PTR)
Definition dump.h:92
#define DC_LOGGER
Definition file.h:168
#define NS(pre, post)
Definition namespace.h:14
#define DC_SCOPED(type,...)
RAII in C. Call the destructor when the variable goes out of scope.
Definition scope.h:5
#define DC_LOGGER_NEW(...)
Definition prelude.h:21
#define DC_LOG(...)
Definition prelude.h:20
@ DC_INFO
Definition trait.h:8