TachyonUI 0.1.0
High-performance UI for building tools.
Loading...
Searching...
No Matches
Allocation hooks and headless backends

Allocation hooks

  • Allocation hooks are your own memory functions, which the library then uses instead of its default ones.
  • Set them before Initialize:
hooks.allocate = &MyAllocate; // void* (size_t size, size_t alignment, void* user)
hooks.reallocate = &MyReallocate; // void* (void* ptr, size_t size, size_t alignment, void* user)
hooks.free = &MyFree; // void (void* ptr, void* user)
hooks.user = &myHeap;
app.SetAllocationHooks(hooks);
Memory functions a host gives the library, for the allocations that can be routed: Vulkan objects,...
Definition gui_allocation_hooks.h:15
void(*) free(void *ptr, void *user)
Frees ptr, which may be null.
Definition gui_allocation_hooks.h:22
void * user
passed to every call
Definition gui_allocation_hooks.h:23
void *(*) allocate(size_t size, size_t alignment, void *user)
Returns size bytes aligned to alignment (a power of two).
Definition gui_allocation_hooks.h:17
void *(*) reallocate(void *ptr, size_t size, size_t alignment, void *user)
Resizes ptr, keeping its alignment; ptr may be null (then it allocates).
Definition gui_allocation_hooks.h:20
  • The hooks cover:
    • Vulkan objects
    • VMA
    • vk-bootstrap
    • SDL
  • SDL's memory functions are shared by the whole process. SDL uses the hooks only if:
    • the GUI starts SDL
    • and SDL hasn't allocated anything yet
  • The hooks must be thread-safe, because drivers call them from their own threads.
  • They must outlive the GUI.
    • SDL keeps using them for a few allocations after SDL_Quit, so keep them alive for the whole process.
  • tachyon::TrackingAllocator (gui_core/memory/gui_tracking_allocator.h) has ready-made hooks:
    • they count the allocations that are still live
    • they report leaks
    • Hooks() returns them
static tachyon::TrackingAllocator tracker; // lives as long as the process
app.SetAllocationHooks(tracker.Hooks());
// ...
tracker.ReportLeaks(); // logs each allocation still live
Allocation hooks that count what is live, for finding leaks.
Definition gui_tracking_allocator.h:15
size_t ReportLeaks() const
Logs a warning per live allocation; returns how many there are.
AllocationHooks Hooks()
Hooks that allocate through this allocator; valid while it lives.

Headless backends

  • Headless means running without a GPU or without a display.
  • NullGraphicsContext (gui_core/graphics/null/gui_null_graphics_context.h) is a graphics backend without a GPU:
    • it draws nothing
    • the UI is still built, laid out and recorded
    • windows stay empty
  • NullWindowBackend is a window backend without an OS:
    • input arrives only through the Inject* calls
  • Together they need no GPU and no display, for example in tests.
app.SetGraphicsBackend(std::make_unique<tachyon::NullGraphicsContext>());
app.SetWindowBackend(std::make_unique<tachyon::NullWindowBackend>());

Previous: Rendering the UI into a host image | Integration