Derive-C
Loading...
Searching...
No Matches
alloc.c File Reference

Go to the source code of this file.

Macros

#define CAPACITY   512
#define NAME   hybrid
#define NAME   test_alloc
#define NAME   dbg
#define ALLOC   dbg
#define BLOCK_SIZE   1024
#define SLAB_SIZE   8096
#define NAME   slab_large
#define ALLOC   slab_large
#define NAME   slab_large_alloc_dbg
#define ALLOC   slab_large_alloc_dbg
#define BLOCK_SIZE   32
#define SLAB_SIZE   8096
#define NAME   slab_small
#define ALLOC   slab_small
#define NAME   slab_small_alloc_dbg
#define ALLOC   dbg
#define BLOCK_SIZE   256
#define NAME   chunked

Functions

static void example_std (DC_LOGGER *parent)
static void example_hybridstatic (DC_LOGGER *parent)
static void example_test (DC_LOGGER *parent)
static void example_slab (DC_LOGGER *parent)
static void example_chunkedbump (DC_LOGGER *parent)
int main ()

Macro Definition Documentation

◆ ALLOC [1/5]

#define ALLOC   dbg

Definition at line 55 of file alloc.c.

◆ ALLOC [2/5]

#define ALLOC   slab_small

Definition at line 55 of file alloc.c.

◆ ALLOC [3/5]

#define ALLOC   slab_large_alloc_dbg

Definition at line 55 of file alloc.c.

◆ ALLOC [4/5]

#define ALLOC   slab_large

Definition at line 55 of file alloc.c.

◆ ALLOC [5/5]

#define ALLOC   dbg

Definition at line 55 of file alloc.c.

◆ BLOCK_SIZE [1/3]

#define BLOCK_SIZE   256

Definition at line 56 of file alloc.c.

◆ BLOCK_SIZE [2/3]

#define BLOCK_SIZE   32

Definition at line 56 of file alloc.c.

◆ BLOCK_SIZE [3/3]

#define BLOCK_SIZE   1024

Definition at line 56 of file alloc.c.

◆ CAPACITY

#define CAPACITY   512

Definition at line 16 of file alloc.c.

◆ NAME [1/8]

#define NAME   chunked

Definition at line 17 of file alloc.c.

◆ NAME [2/8]

#define NAME   slab_small_alloc_dbg

Definition at line 17 of file alloc.c.

◆ NAME [3/8]

#define NAME   slab_small

Definition at line 17 of file alloc.c.

◆ NAME [4/8]

#define NAME   slab_large_alloc_dbg

Definition at line 17 of file alloc.c.

◆ NAME [5/8]

#define NAME   slab_large

Definition at line 17 of file alloc.c.

◆ NAME [6/8]

#define NAME   dbg

Definition at line 17 of file alloc.c.

◆ NAME [7/8]

#define NAME   test_alloc

Definition at line 17 of file alloc.c.

◆ NAME [8/8]

#define NAME   hybrid

Definition at line 17 of file alloc.c.

◆ SLAB_SIZE [1/2]

#define SLAB_SIZE   8096

Definition at line 57 of file alloc.c.

◆ SLAB_SIZE [2/2]

#define SLAB_SIZE   8096

Definition at line 57 of file alloc.c.

Function Documentation

◆ example_chunkedbump()

void example_chunkedbump ( DC_LOGGER * parent)
static

Definition at line 104 of file alloc.c.

104 {
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}
#define DC_DEBUG(DEBUG_FN, DEBUG_PTR)
Definition dump.h:92
#define DC_LOGGER
Definition file.h:168
#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

◆ example_hybridstatic()

void example_hybridstatic ( DC_LOGGER * parent)
static

Definition at line 20 of file alloc.c.

20 {
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}

◆ example_slab()

void example_slab ( DC_LOGGER * parent)
static

Definition at line 75 of file alloc.c.

75 {
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}

◆ example_std()

void example_std ( DC_LOGGER * parent)
static

Definition at line 6 of file alloc.c.

6 {
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}

◆ example_test()

void example_test ( DC_LOGGER * parent)
static

Definition at line 38 of file alloc.c.

38 {
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}

◆ main()

int main ( )
Examples
complex/employees.c, complex/prime_sieve.c, container/arena.c, container/bitset.c, container/map.c, container/queue.c, container/set.c, container/vector.c, utils/log.c, utils/option.c, utils/result.c, and utils/string_builder.c.

Definition at line 120 of file alloc.c.

120 {
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
#define NS(pre, post)
Definition namespace.h:14