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 ()
static void example_hybridstatic ()
static void example_test ()
static void example_slab ()
static void example_chunkedbump ()
int main ()

Macro Definition Documentation

◆ ALLOC [1/5]

#define ALLOC   dbg

Definition at line 47 of file alloc.c.

◆ ALLOC [2/5]

#define ALLOC   slab_small

Definition at line 47 of file alloc.c.

◆ ALLOC [3/5]

#define ALLOC   slab_large_alloc_dbg

Definition at line 47 of file alloc.c.

◆ ALLOC [4/5]

#define ALLOC   slab_large

Definition at line 47 of file alloc.c.

◆ ALLOC [5/5]

#define ALLOC   dbg

Definition at line 47 of file alloc.c.

◆ BLOCK_SIZE [1/3]

#define BLOCK_SIZE   256

Definition at line 48 of file alloc.c.

◆ BLOCK_SIZE [2/3]

#define BLOCK_SIZE   32

Definition at line 48 of file alloc.c.

◆ BLOCK_SIZE [3/3]

#define BLOCK_SIZE   1024

Definition at line 48 of file alloc.c.

◆ CAPACITY

#define CAPACITY   512

Definition at line 13 of file alloc.c.

◆ NAME [1/8]

#define NAME   chunked

Definition at line 14 of file alloc.c.

◆ NAME [2/8]

#define NAME   slab_small_alloc_dbg

Definition at line 14 of file alloc.c.

◆ NAME [3/8]

#define NAME   slab_small

Definition at line 14 of file alloc.c.

◆ NAME [4/8]

#define NAME   slab_large_alloc_dbg

Definition at line 14 of file alloc.c.

◆ NAME [5/8]

#define NAME   slab_large

Definition at line 14 of file alloc.c.

◆ NAME [6/8]

#define NAME   dbg

Definition at line 14 of file alloc.c.

◆ NAME [7/8]

#define NAME   test_alloc

Definition at line 14 of file alloc.c.

◆ NAME [8/8]

#define NAME   hybrid

Definition at line 14 of file alloc.c.

◆ SLAB_SIZE [1/2]

#define SLAB_SIZE   8096

Definition at line 49 of file alloc.c.

◆ SLAB_SIZE [2/2]

#define SLAB_SIZE   8096

Definition at line 49 of file alloc.c.

Function Documentation

◆ example_chunkedbump()

void example_chunkedbump ( )
static

Definition at line 95 of file alloc.c.

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

◆ example_hybridstatic()

void example_hybridstatic ( )
static

Definition at line 17 of file alloc.c.

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

◆ example_slab()

void example_slab ( )
static

Definition at line 67 of file alloc.c.

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

◆ example_std()

void example_std ( )
static

Definition at line 6 of file alloc.c.

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

◆ example_test()

void example_test ( )
static

Definition at line 32 of file alloc.c.

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

◆ 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/option.c, utils/result.c, and utils/string_builder.c.

Definition at line 109 of file alloc.c.

109 {
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