Branch data Line data Source code
1 : : // Copyright 2014-2023 David Robillard <d@drobilla.net>
2 : : // SPDX-License-Identifier: ISC
3 : :
4 : : #undef NDEBUG
5 : :
6 : : #include "failing_allocator.h"
7 : :
8 : : #include <zix/allocator.h>
9 : : #include <zix/bump_allocator.h>
10 : :
11 : : #include <assert.h>
12 : : #include <stddef.h>
13 : : #include <stdint.h>
14 : :
15 : : static void
16 : 1 : test_allocator(void)
17 : : {
18 : : // Just a basic smoke test to check that things seem to be working
19 : :
20 : 1 : ZixAllocator* const allocator = zix_default_allocator();
21 : :
22 : 1 : char* const malloced = (char*)zix_malloc(allocator, 4);
23 : 1 : malloced[0] = 0;
24 : 1 : malloced[3] = 3;
25 [ - + ]: 1 : assert(malloced[0] == 0);
26 [ - + ]: 1 : assert(malloced[3] == 3);
27 : :
28 : 1 : char* const calloced = (char*)zix_calloc(allocator, 4, 1);
29 [ - + ]: 1 : assert(calloced[0] == 0);
30 [ - + ]: 1 : assert(calloced[1] == 0);
31 [ - + ]: 1 : assert(calloced[2] == 0);
32 [ - + ]: 1 : assert(calloced[3] == 0);
33 : :
34 : 1 : char* const realloced = (char*)zix_realloc(allocator, calloced, 8);
35 [ - + ]: 1 : assert(realloced[0] == 0);
36 [ - + ]: 1 : assert(realloced[1] == 0);
37 [ - + ]: 1 : assert(realloced[2] == 0);
38 [ - + ]: 1 : assert(realloced[3] == 0);
39 : 1 : realloced[4] = 4;
40 : 1 : realloced[5] = 5;
41 : 1 : realloced[6] = 6;
42 : 1 : realloced[7] = 7;
43 [ - + ]: 1 : assert(realloced[4] == 4);
44 [ - + ]: 1 : assert(realloced[5] == 5);
45 [ - + ]: 1 : assert(realloced[6] == 6);
46 [ - + ]: 1 : assert(realloced[7] == 7);
47 : :
48 : 1 : char* const aligned = (char*)zix_aligned_alloc(allocator, 4096, 4096);
49 [ - + ]: 1 : assert((uintptr_t)aligned % 4096 == 0);
50 : 1 : aligned[0] = 0;
51 : 1 : aligned[3] = 3;
52 [ - + ]: 1 : assert(aligned[0] == 0);
53 [ - + ]: 1 : assert(aligned[3] == 3);
54 : :
55 : 1 : zix_aligned_free(allocator, aligned);
56 : 1 : zix_free(allocator, realloced);
57 : 1 : zix_free(allocator, malloced);
58 : 1 : }
59 : :
60 : : static void
61 : 1 : test_bump_allocator(void)
62 : : {
63 : 1 : char buffer[1024] = {0};
64 : 1 : ZixBumpAllocator allocator = zix_bump_allocator(sizeof(buffer), buffer);
65 : :
66 [ - + ]: 1 : assert(!zix_malloc(&allocator.base, 1025));
67 : :
68 : 1 : char* const malloced = (char*)zix_malloc(&allocator.base, 3);
69 [ - + ]: 1 : assert(malloced >= buffer);
70 [ - + ]: 1 : assert(malloced <= buffer + sizeof(buffer));
71 [ - + ]: 1 : assert((uintptr_t)malloced % sizeof(uintmax_t) == 0U);
72 : :
73 [ - + ]: 1 : assert(!zix_calloc(&allocator.base, 1017, 1));
74 : :
75 : 1 : char* const calloced = (char*)zix_calloc(&allocator.base, 4, 1);
76 [ - + ]: 1 : assert(calloced > malloced);
77 [ - + ]: 1 : assert(calloced <= buffer + sizeof(buffer));
78 [ - + ]: 1 : assert((uintptr_t)calloced % sizeof(uintmax_t) == 0U);
79 [ - + ]: 1 : assert(!calloced[0]);
80 [ - + ]: 1 : assert(!calloced[1]);
81 [ - + ]: 1 : assert(!calloced[2]);
82 [ - + ]: 1 : assert(!calloced[3]);
83 : :
84 : 1 : char* const realloced = (char*)zix_realloc(&allocator.base, calloced, 8);
85 [ - + ]: 1 : assert(realloced == calloced);
86 : :
87 [ - + ]: 1 : assert(!zix_realloc(&allocator.base, malloced, 8)); // Not the top
88 [ - + ]: 1 : assert(!zix_realloc(&allocator.base, realloced, 4089)); // No space
89 [ - + ]: 1 : assert(!zix_calloc(&allocator.base, 4089, 1)); // No space
90 : :
91 : 1 : zix_free(&allocator.base, realloced);
92 : :
93 : 1 : char* const reclaimed = (char*)zix_malloc(&allocator.base, 512);
94 [ - + ]: 1 : assert(reclaimed);
95 [ - + ]: 1 : assert(reclaimed == realloced);
96 : :
97 [ - + ]: 1 : assert(!zix_aligned_alloc(&allocator.base, sizeof(uintmax_t), 1024));
98 [ - + ]: 1 : assert(!zix_aligned_alloc(&allocator.base, 1024, 1024));
99 [ - + ]: 1 : assert(!zix_aligned_alloc(&allocator.base, 2048, 2048));
100 [ - + ]: 1 : assert(!zix_aligned_alloc(&allocator.base, 4096, 4096));
101 [ - + ]: 1 : assert(!zix_aligned_alloc(&allocator.base, 8192, 8192));
102 [ - + ]: 1 : assert(!zix_aligned_alloc(&allocator.base, 4096, 4096));
103 [ - + ]: 1 : assert(!zix_aligned_alloc(&allocator.base, 2048, 2048));
104 [ - + ]: 1 : assert(!zix_aligned_alloc(&allocator.base, 1024, 1024));
105 [ - + ]: 1 : assert(!zix_aligned_alloc(&allocator.base, 512, 512));
106 : :
107 : 1 : char* const aligned = (char*)zix_aligned_alloc(&allocator.base, 128, 128);
108 [ - + ]: 1 : assert(aligned);
109 [ - + ]: 1 : assert(aligned >= reclaimed);
110 [ - + ]: 1 : assert(aligned <= buffer + sizeof(buffer));
111 [ - + ]: 1 : assert((uintptr_t)aligned % 128 == 0U);
112 : :
113 [ - + ]: 1 : assert(!zix_aligned_alloc(&allocator.base, 8, 896));
114 [ - + ]: 1 : assert(zix_aligned_alloc(&allocator.base, 8, 8));
115 : :
116 : 1 : zix_aligned_free(&allocator.base, aligned);
117 : 1 : zix_free(&allocator.base, reclaimed); // Correct, but a noop
118 : 1 : zix_free(&allocator.base, malloced); // Correct, but a noop
119 : 1 : }
120 : :
121 : : static void
122 : 1 : test_failing_allocator(void)
123 : : {
124 : 1 : ZixFailingAllocator allocator = zix_failing_allocator();
125 : 1 : zix_failing_allocator_reset(&allocator, 0);
126 : :
127 [ - + ]: 1 : assert(!zix_malloc(&allocator.base, 16U));
128 [ - + ]: 1 : assert(!zix_calloc(&allocator.base, 16U, 1U));
129 [ - + ]: 1 : assert(!zix_realloc(&allocator.base, NULL, 32U));
130 : 1 : zix_free(&allocator.base, NULL);
131 [ - + ]: 1 : assert(!zix_aligned_alloc(&allocator.base, 8U, 16U));
132 : 1 : zix_aligned_free(&allocator.base, NULL);
133 : 1 : }
134 : :
135 : : int
136 : 1 : main(void)
137 : : {
138 : 1 : test_allocator();
139 : 1 : test_bump_allocator();
140 : 1 : test_failing_allocator();
141 : :
142 : 1 : return 0;
143 : : }
|