Open History to see the last commit
1/**
2 * @file allocator.h
3 *
4 * Memory management interface for libghostty-vt.
5 */
6
7#ifndef GHOSTTY_VT_ALLOCATOR_H
8#define GHOSTTY_VT_ALLOCATOR_H
9
10#include <stdbool.h>
11#include <stddef.h>
12#include <stdint.h>
13#include <ghostty/vt/types.h>
14
15/** @addtogroup allocator
16 *
17 * An allocator is the set of functions libghostty-vt calls to get and free
18 * memory.
19 *
20 * Most programs never need this. Any function that takes a
21 * `const GhosttyAllocator*` accepts NULL, which selects the default
22 * allocator. You only need your own to run the library on memory you control.
23 *
24 * ## Example
25 *
26 * @snippet c-vt-allocator/src/main.c allocator-use
27 *
28 * Full program: @ref c-vt-allocator/src/main.c
29 *
30 * See @ref memory for what the default is, how to write your own allocator,
31 * and how to free memory the library gives you.
32 *
33 * @{
34 */
35
36/**
37 * Allocate a block of memory.
38 *
39 * This is the `alloc` function of a GhosttyAllocatorVtable. It's required.
40 *
41 * @param ctx The allocator's context pointer
42 * @param len Number of bytes to allocate
43 * @param alignment The block's address must be a multiple of
44 * `1 << alignment` bytes. See @ref memory-alignment.
45 * @param ret_addr Address of the requesting code, or 0. Safe to ignore.
46 * @return The new block, or NULL if it could not be allocated
47 *
48 * @ingroup allocator
49 */
50typedef void* (*GhosttyAllocatorAllocFn)(void *ctx, size_t len, uint8_t alignment, uintptr_t ret_addr);
51
52/**
53 * Try to grow or shrink a block without moving it.
54 *
55 * This is the `resize` function of a GhosttyAllocatorVtable. It's optional,
56 * and leaving it NULL is the same as always returning false.
57 *
58 * Return true only if the block at `memory` is now `new_len` bytes long at
59 * the same address. Return false if that would require moving it. The library
60 * then decides whether to allocate a new block and copy.
61 *
62 * For example, an arena that hands out memory from one buffer can resize its
63 * most recent block, because nothing comes after it:
64 *
65 * @code{.c}
66 * typedef struct {
67 * uint8_t* base;
68 * size_t used;
69 * size_t capacity;
70 * } Arena;
71 *
72 * static bool arena_resize(void* ctx, void* memory, size_t memory_len,
73 * uint8_t alignment, size_t new_len,
74 * uintptr_t ret_addr) {
75 * (void)alignment;
76 * (void)ret_addr;
77 * Arena* arena = ctx;
78 * size_t start = (size_t)((uint8_t*)memory - arena->base);
79 *
80 * // Only the last block can change size in place.
81 * if (start + memory_len != arena->used) return false;
82 * if (start + new_len > arena->capacity) return false;
83 *
84 * arena->used = start + new_len;
85 * return true;
86 * }
87 * @endcode
88 *
89 * @param ctx The allocator's context pointer
90 * @param memory Start of a block returned by `alloc` or `remap`
91 * @param memory_len Current length of the block
92 * @param alignment The alignment the block was allocated with
93 * @param new_len Requested length, always greater than zero
94 * @param ret_addr Address of the requesting code, or 0. Safe to ignore.
95 * @return true if the block was resized in place, false otherwise
96 *
97 * @ingroup allocator
98 */
99typedef bool (*GhosttyAllocatorResizeFn)(void *ctx, void *memory, size_t memory_len, uint8_t alignment, size_t new_len, uintptr_t ret_addr);
100
101/**
102 * Grow or shrink a block, moving it if that is cheaper than a copy.
103 *
104 * This is the `remap` function of a GhosttyAllocatorVtable. It's optional,
105 * and leaving it NULL is the same as always returning NULL.
106 *
107 * Unlike GhosttyAllocatorResizeFn, this may return a new address, for example
108 * after remapping virtual memory pages. The block must hold the old contents
109 * at that address, and the old address is no longer valid.
110 *
111 * Return NULL when the only way to do it would be to allocate, copy, and free.
112 * The library does that itself, so there is no benefit in doing it here.
113 *
114 * @param ctx The allocator's context pointer
115 * @param memory Start of a block returned by `alloc` or `remap`
116 * @param memory_len Current length of the block
117 * @param alignment The alignment the block was allocated with
118 * @param new_len Requested length, always greater than zero
119 * @param ret_addr Address of the requesting code, or 0. Safe to ignore.
120 * @return The resized block, or NULL to let the library copy instead
121 *
122 * @ingroup allocator
123 */
124typedef void* (*GhosttyAllocatorRemapFn)(void *ctx, void *memory, size_t memory_len, uint8_t alignment, size_t new_len, uintptr_t ret_addr);
125
126/**
127 * Free a block.
128 *
129 * This is the `free` function of a GhosttyAllocatorVtable. It's required.
130 * After it returns, the library never uses `memory` again.
131 *
132 * @param ctx The allocator's context pointer
133 * @param memory Start of a block returned by `alloc` or `remap`
134 * @param memory_len Current length of the block
135 * @param alignment The alignment the block was allocated with
136 * @param ret_addr Address of the requesting code, or 0. Safe to ignore.
137 *
138 * @ingroup allocator
139 */
140typedef void (*GhosttyAllocatorFreeFn)(void *ctx, void *memory, size_t memory_len, uint8_t alignment, uintptr_t ret_addr);
141
142/**
143 * The functions that make up a custom allocator.
144 *
145 * `alloc` and `free` are required. `resize` and `remap` are optional, and
146 * when they are NULL the library grows memory by allocating a new block,
147 * copying, and freeing the old one. See @ref memory-custom for a complete
148 * example and @ref memory-alignment for an explanation of `alignment`.
149 *
150 * Every function receives the `ctx` pointer from the GhosttyAllocator it
151 * belongs to. When the library resizes, remaps, or frees memory, it always
152 * passes the same `alignment` it used for the original allocation and the
153 * current length of the block, so your allocator does not need to record
154 * either.
155 *
156 * Every function also receives `ret_addr`, the address of the code that
157 * requested the memory, or 0 if it is unknown. Allocators that track
158 * leaks or report where memory came from can use it. Most allocators
159 * ignore it.
160 *
161 * Initialize the whole struct to zero before setting the fields you use,
162 * so the reserved slots are NULL.
163 *
164 * The interface matches Zig's `std.mem.Allocator`. The library is written
165 * in Zig, so this lets it use your allocator directly, and it is an
166 * interface already proven by a wide range of allocators in the Zig
167 * ecosystem. Simple allocators can ignore most of the arguments. Zig's
168 * own libc allocator (CAllocator in `lib/std/heap.zig`) is a good short
169 * reference.
170 *
171 * @ingroup allocator
172 */
173typedef struct {
174 /** Allocates a block. Required. */
175 GhosttyAllocatorAllocFn alloc;
176
177 /** Resizes a block without moving it. Optional. */
178 GhosttyAllocatorResizeFn resize;
179
180 /** Resizes a block and may move it. Optional. */
181 GhosttyAllocatorRemapFn remap;
182
183 /** Frees a block. Required. */
184 GhosttyAllocatorFreeFn free;
185
186 /**
187 * Reserved for future versions. Must be NULL.
188 *
189 * These slots let a future release add new allocator functions
190 * without changing the size of this struct. The library ignores any
191 * slot it does not know about, so leaving them NULL keeps your code
192 * working with future versions.
193 *
194 * Debug builds of the library stop with an error if a reserved slot
195 * is not NULL, to catch a vtable that was not zero-initialized.
196 */
197 void* reserved[4];
198} GhosttyAllocatorVtable;
199
200/**
201 * A custom allocator: a vtable and the context it operates on.
202 *
203 * Pass a pointer to one of these to any function that takes an allocator,
204 * or pass NULL to use the default allocator described in @ref memory-default.
205 * The library does not copy the allocator. It must stay valid for as long
206 * as anything allocated with it is in use.
207 *
208 * @ingroup allocator
209 */
210typedef struct GhosttyAllocator {
211 /**
212 * Your allocator's state, passed unchanged to every vtable function.
213 * May be NULL if your allocator has no state.
214 */
215 void *ctx;
216
217 /**
218 * The functions that implement this allocator. Must not be NULL.
219 */
220 const GhosttyAllocatorVtable *vtable;
221} GhosttyAllocator;
222
223/**
224 * Allocate a buffer of `len` bytes.
225 *
226 * Uses the provided allocator, or the default allocator if NULL is passed.
227 * The returned buffer must be freed with ghostty_free() using the same
228 * allocator.
229 *
230 * @param allocator Pointer to the allocator to use, or NULL for the default
231 * @param len Number of bytes to allocate
232 * @return Pointer to the allocated buffer, or NULL if len is zero or
233 * allocation failed
234 *
235 * @ingroup allocator
236 */
237GHOSTTY_API uint8_t* ghostty_alloc(const GhosttyAllocator* allocator, size_t len);
238
239/**
240 * Free memory that was allocated by a libghostty-vt function.
241 *
242 * Use this to free buffers returned by functions such as
243 * ghostty_formatter_format_alloc(). Pass the same allocator that was
244 * used for the allocation, or NULL if the default allocator was used.
245 *
246 * Use this instead of the C `free()`, which can use a different heap than
247 * the library. See @ref memory-buffers for why.
248 *
249 * Passing a NULL `ptr` does nothing.
250 *
251 * @param allocator Pointer to the allocator that was used to allocate the
252 * memory, or NULL if the default allocator was used
253 * @param ptr Pointer to the memory to free (may be NULL)
254 * @param len Length of the allocation in bytes (must match the original
255 * allocation size)
256 *
257 * @ingroup allocator
258 */
259GHOSTTY_API void ghostty_free(const GhosttyAllocator* allocator, uint8_t* ptr, size_t len);
260
261/** @} */
262
263#endif /* GHOSTTY_VT_ALLOCATOR_H */