Branch data Line data Source code
1 : : // Copyright 2021 David Robillard <d@drobilla.net>
2 : : // SPDX-License-Identifier: ISC
3 : :
4 : : #include <zix/bump_allocator.h>
5 : :
6 : : #include <zix/allocator.h>
7 : : #include <zix/attributes.h>
8 : :
9 : : #include <assert.h>
10 : : #include <stddef.h>
11 : : #include <stdint.h>
12 : : #include <string.h>
13 : :
14 : : static const size_t min_alignment = sizeof(uintmax_t);
15 : :
16 : : ZIX_PURE_FUNC static size_t
17 : 27 : round_up_multiple(const size_t number, const size_t factor)
18 : : {
19 [ - + ]: 27 : assert(factor); // Factor must be non-zero
20 [ - + ]: 27 : assert((factor & (factor - 1U)) == 0U); // Factor must be a power of two
21 : :
22 : 27 : return (number + factor - 1U) & ~(factor - 1U);
23 : : }
24 : :
25 : : ZIX_MALLOC_FUNC static void*
26 : 15 : zix_bump_malloc(ZixAllocator* const allocator, const size_t size)
27 : : {
28 : 15 : ZixBumpAllocator* const state = (ZixBumpAllocator*)allocator;
29 : :
30 [ - + ]: 15 : assert((uintptr_t)((char*)state->buffer + state->top) % min_alignment == 0U);
31 : :
32 : : /* For C malloc(), the result is guaranteed to be aligned for any variable.
33 : : What that means is deliberately vague to accommodate diverse platforms,
34 : : but sizeof(uintmax_t) is more than enough on all the common ones. */
35 : :
36 : 15 : const size_t real_size = round_up_multiple(size, min_alignment);
37 [ + + ]: 15 : if (state->top + real_size > state->capacity) {
38 : 10 : return NULL;
39 : : }
40 : :
41 : 5 : state->last = state->top;
42 : 5 : state->top += real_size;
43 : :
44 : 5 : return (void*)((char*)state->buffer + state->last);
45 : : }
46 : :
47 : : ZIX_MALLOC_FUNC static void*
48 : 3 : zix_bump_calloc(ZixAllocator* const allocator,
49 : : const size_t nmemb,
50 : : const size_t size)
51 : : {
52 : 3 : const size_t total_size = nmemb * size;
53 : 3 : void* const ptr = zix_bump_malloc(allocator, total_size);
54 [ + + ]: 3 : if (ptr) {
55 : 1 : memset(ptr, 0, total_size);
56 : : }
57 : :
58 : 3 : return ptr;
59 : : }
60 : :
61 : : static void*
62 : 3 : zix_bump_realloc(ZixAllocator* const allocator,
63 : : void* const ptr,
64 : : const size_t size)
65 : : {
66 : 3 : ZixBumpAllocator* const state = (ZixBumpAllocator*)allocator;
67 : :
68 [ + + ]: 3 : if ((char*)ptr != (char*)state->buffer + state->last) {
69 : 1 : return NULL;
70 : : }
71 : :
72 : 2 : const size_t new_top = state->last + size;
73 [ + + ]: 2 : if (new_top > state->capacity) {
74 : 1 : return NULL;
75 : : }
76 : :
77 : 1 : state->top = new_top;
78 : 1 : return ptr;
79 : : }
80 : :
81 : : static void
82 : 4 : zix_bump_free(ZixAllocator* const allocator, void* const ptr)
83 : : {
84 : 4 : ZixBumpAllocator* const state = (ZixBumpAllocator*)allocator;
85 : :
86 [ + + ]: 4 : if (ptr == (char*)state->buffer + state->last) {
87 : 1 : state->top = state->last; // Reclaim the space of the last allocation
88 : : }
89 : 4 : }
90 : :
91 : : ZIX_MALLOC_FUNC static void*
92 : 12 : zix_bump_aligned_alloc(ZixAllocator* const allocator,
93 : : const size_t alignment,
94 : : const size_t size)
95 : : {
96 : 12 : ZixBumpAllocator* const state = (ZixBumpAllocator*)allocator;
97 : 12 : const size_t old_last = state->last;
98 : 12 : const size_t old_top = state->top;
99 : :
100 [ - + ]: 12 : assert(alignment >= min_alignment);
101 [ - + ]: 12 : assert(size % alignment == 0U);
102 : :
103 : : /* First, calculate how much we need to offset the top to achieve this
104 : : alignment. Note that it's not the offset that needs to be aligned (since
105 : : the buffer may not be), but the final address, so we do the calculation
106 : : with "global" addresses and calculate the offset from that. */
107 : :
108 : 12 : const char* top_ptr = (char*)state->buffer + state->top;
109 : 12 : const uintptr_t top_addr = (uintptr_t)top_ptr;
110 : 12 : const uintptr_t aligned_top_addr = round_up_multiple(top_addr, alignment);
111 : 12 : const uintptr_t offset = aligned_top_addr - top_addr;
112 [ + + ]: 12 : if (state->top + offset > state->capacity) {
113 : 3 : return NULL;
114 : : }
115 : :
116 : : /* Then, adjust the top to be aligned and try to allocate. If that fails,
117 : : reset everything to how it was before this call. */
118 : :
119 : 9 : state->top += offset;
120 : :
121 : 9 : void* const ptr = zix_bump_malloc(allocator, size);
122 [ + + ]: 9 : if (!ptr) {
123 : 7 : state->last = old_last;
124 : 7 : state->top = old_top;
125 : 7 : return NULL;
126 : : }
127 : :
128 : 2 : return ptr;
129 : : }
130 : :
131 : : static void
132 : 1 : zix_bump_aligned_free(ZixAllocator* const allocator, void* const ptr)
133 : : {
134 : 1 : zix_bump_free(allocator, ptr);
135 : 1 : }
136 : :
137 : : ZIX_CONST_FUNC ZIX_REALTIME ZixBumpAllocator
138 : 1 : zix_bump_allocator(const size_t capacity, void* buffer)
139 : : {
140 : 1 : const size_t aligned_top = (uintptr_t)buffer % min_alignment;
141 : :
142 : 1 : ZixBumpAllocator bump_allocator = {
143 : : {
144 : : zix_bump_malloc,
145 : : zix_bump_calloc,
146 : : zix_bump_realloc,
147 : : zix_bump_free,
148 : : zix_bump_aligned_alloc,
149 : : zix_bump_aligned_free,
150 : : },
151 : : buffer,
152 : : aligned_top,
153 : : aligned_top,
154 : : capacity,
155 : : };
156 : :
157 : 1 : return bump_allocator;
158 : : }
|