![]() |
libghostty-vt 1.0.0
|
Run the library on your own memory allocator. More...
Run the library on your own memory allocator.
An allocator is the set of functions libghostty-vt calls to get and free memory.
Most programs never need this. Any function that takes a const GhosttyAllocator* accepts NULL, which selects the default allocator. You only need your own to run the library on memory you control.
Full program: c-vt-allocator/src/main.c
See Memory for what the default is, how to write your own allocator, and how to free memory the library gives you.
Typedefs | |
| typedef void *(* | GhosttyAllocatorAllocFn) (void *ctx, size_t len, uint8_t alignment, uintptr_t ret_addr) |
| typedef bool(* | GhosttyAllocatorResizeFn) (void *ctx, void *memory, size_t memory_len, uint8_t alignment, size_t new_len, uintptr_t ret_addr) |
| typedef void *(* | GhosttyAllocatorRemapFn) (void *ctx, void *memory, size_t memory_len, uint8_t alignment, size_t new_len, uintptr_t ret_addr) |
| typedef void(* | GhosttyAllocatorFreeFn) (void *ctx, void *memory, size_t memory_len, uint8_t alignment, uintptr_t ret_addr) |
Functions | |
| GHOSTTY_API uint8_t * | ghostty_alloc (const GhosttyAllocator *allocator, size_t len) |
| GHOSTTY_API void | ghostty_free (const GhosttyAllocator *allocator, uint8_t *ptr, size_t len) |
Data Structures | |
| struct | GhosttyAllocatorVtable |
| struct | GhosttyAllocator |
| typedef void *(* GhosttyAllocatorAllocFn) (void *ctx, size_t len, uint8_t alignment, uintptr_t ret_addr) |
Allocate a block of memory.
This is the alloc function of a GhosttyAllocatorVtable. It's required.
| ctx | The allocator's context pointer |
| len | Number of bytes to allocate |
| alignment | The block's address must be a multiple of 1 << alignment bytes. See Alignment. |
| ret_addr | Address of the requesting code, or 0. Safe to ignore. |
Definition at line 50 of file allocator.h.
| typedef void(* GhosttyAllocatorFreeFn) (void *ctx, void *memory, size_t memory_len, uint8_t alignment, uintptr_t ret_addr) |
Free a block.
This is the free function of a GhosttyAllocatorVtable. It's required. After it returns, the library never uses memory again.
| ctx | The allocator's context pointer |
| memory | Start of a block returned by alloc or remap |
| memory_len | Current length of the block |
| alignment | The alignment the block was allocated with |
| ret_addr | Address of the requesting code, or 0. Safe to ignore. |
Definition at line 140 of file allocator.h.
| typedef void *(* GhosttyAllocatorRemapFn) (void *ctx, void *memory, size_t memory_len, uint8_t alignment, size_t new_len, uintptr_t ret_addr) |
Grow or shrink a block, moving it if that is cheaper than a copy.
This is the remap function of a GhosttyAllocatorVtable. It's optional, and leaving it NULL is the same as always returning NULL.
Unlike GhosttyAllocatorResizeFn, this may return a new address, for example after remapping virtual memory pages. The block must hold the old contents at that address, and the old address is no longer valid.
Return NULL when the only way to do it would be to allocate, copy, and free. The library does that itself, so there is no benefit in doing it here.
| ctx | The allocator's context pointer |
| memory | Start of a block returned by alloc or remap |
| memory_len | Current length of the block |
| alignment | The alignment the block was allocated with |
| new_len | Requested length, always greater than zero |
| ret_addr | Address of the requesting code, or 0. Safe to ignore. |
Definition at line 124 of file allocator.h.
| typedef bool(* GhosttyAllocatorResizeFn) (void *ctx, void *memory, size_t memory_len, uint8_t alignment, size_t new_len, uintptr_t ret_addr) |
Try to grow or shrink a block without moving it.
This is the resize function of a GhosttyAllocatorVtable. It's optional, and leaving it NULL is the same as always returning false.
Return true only if the block at memory is now new_len bytes long at the same address. Return false if that would require moving it. The library then decides whether to allocate a new block and copy.
For example, an arena that hands out memory from one buffer can resize its most recent block, because nothing comes after it:
| ctx | The allocator's context pointer |
| memory | Start of a block returned by alloc or remap |
| memory_len | Current length of the block |
| alignment | The alignment the block was allocated with |
| new_len | Requested length, always greater than zero |
| ret_addr | Address of the requesting code, or 0. Safe to ignore. |
Definition at line 99 of file allocator.h.
| GHOSTTY_API uint8_t * ghostty_alloc | ( | const GhosttyAllocator * | allocator, |
| size_t | len ) |
Allocate a buffer of len bytes.
Uses the provided allocator, or the default allocator if NULL is passed. The returned buffer must be freed with ghostty_free() using the same allocator.
| allocator | Pointer to the allocator to use, or NULL for the default |
| len | Number of bytes to allocate |
| GHOSTTY_API void ghostty_free | ( | const GhosttyAllocator * | allocator, |
| uint8_t * | ptr, | ||
| size_t | len ) |
Free memory that was allocated by a libghostty-vt function.
Use this to free buffers returned by functions such as ghostty_formatter_format_alloc(). Pass the same allocator that was used for the allocation, or NULL if the default allocator was used.
Use this instead of the C free(), which can use a different heap than the library. See Allocating and Freeing Buffers for why.
Passing a NULL ptr does nothing.
| allocator | Pointer to the allocator that was used to allocate the memory, or NULL if the default allocator was used |
| ptr | Pointer to the memory to free (may be NULL) |
| len | Length of the allocation in bytes (must match the original allocation size) |