![]() |
libghostty-vt 0.0.0
|
libghostty-vt may allocate memory and fully supports custom allocators.
Most programs never need to think about this. Any function that takes a const GhosttyAllocator* accepts NULL, which selects the default allocator. Pass your own when you want memory to come from a specific place, such as an arena, a pool, a tracking allocator, or a platform without a C library.
The library handles allocation failure. When an allocator returns NULL, the function that needed the memory returns an error such as GHOSTTY_OUT_OF_MEMORY. It doesn't crash.
Passing NULL uses:
A custom allocator is two structs:
Only alloc and free are required. Leave resize and remap NULL unless your allocator can grow memory more cheaply than allocating new memory and copying. Initialize the vtable with designated initializers (or memset it to zero) so every field you don't set, including the reserved slots, is NULL.
The library may call an allocator's functions from any thread that uses a handle created with it. If handles that share one allocator are used on different threads, its functions must be thread-safe. The default allocator is.
This allocator is built on plain malloc and free:
Pair it with a context and pass it to any function that takes an allocator:
Full program: c-vt-allocator/src/main.c
Every vtable function receives an alignment argument. It's an exponent, not a byte count. The address you return must be a multiple of 1 << alignment bytes.
| alignment | Address must be a multiple of |
|---|---|
| 0 | 1 byte |
| 3 | 8 bytes |
| 4 | 16 bytes |
| 12 | 4096 bytes |
malloc already returns memory aligned for any built-in C type, which covers anything up to alignof(max_align_t) (usually 8 or 16 bytes). For larger values, use aligned_alloc or posix_memalign with 1 << alignment, or over-allocate as the example above does. This is the same encoding as Zig's std.mem.Alignment.
ghostty_alloc() and ghostty_free() allocate and free plain byte buffers through an allocator. Use them when:
Don't free library memory with the C free(). On some platforms the library and your program use different heaps. On Windows, for example, Zig's libc and MSVC's C runtime each keep their own, so freeing memory from one with the other is undefined behavior. ghostty_free() always uses the right one.
Always free a buffer with the same allocator (or NULL) that allocated it.
Some callbacks receive an allocator from the library, such as the PNG decoder set with GHOSTTY_SYS_OPT_DECODE_PNG. Use that allocator only through ghostty_alloc() and ghostty_free(). Don't read its vtable or call its functions directly. A future release may fill in vtable slots that your code doesn't know about, and calling the functions directly would skip them.