TachyonUI
1.1.0
High-performance UI for building tools.
Toggle main menu visibility
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
:
#include <
gui_core/memory/gui_allocation_hooks.h
>
tachyon::AllocationHooks
hooks;
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);
gui_allocation_hooks.h
tachyon::AllocationHooks
Memory functions a host gives the library, for the allocations that can be routed: Vulkan objects,...
Definition
gui_allocation_hooks.h:17
tachyon::AllocationHooks::free
void(*) free(void *ptr, void *user)
Frees ptr, which may be null.
Definition
gui_allocation_hooks.h:24
tachyon::AllocationHooks::user
void * user
passed to every call
Definition
gui_allocation_hooks.h:25
tachyon::AllocationHooks::allocate
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:19
tachyon::AllocationHooks::reallocate
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:22
The hooks cover:
Vulkan objects
VMA
vk-bootstrap
SDL
the text and SVG libraries: HarfBuzz, skribidi, SheenBidi and nanosvg
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 text and SVG libraries use the hooks while the app's text system lives:
from
Initialize
until
Cleanup
after
Cleanup
they are back on the C heap (
malloc
and
free
)
memory they took from the hooks is still given back to the hooks, even after
Cleanup
A text system you make yourself can take the hooks too:
#include <
gui_core/text/skribidi/gui_text_system_skribidi.h
>
tachyon::SkribidiTextSystem
text(hooks);
// the libraries use the hooks while `text` lives
tachyon::SkribidiTextSystem
Skribidi-based implementation of ITextSystem.
Definition
gui_text_system_skribidi.h:31
gui_text_system_skribidi.h
With a HarfBuzz of your own (
TACHYON_SYSTEM_HARFBUZZ
), HarfBuzz keeps its own allocator; skribidi, SheenBidi and nanosvg still use the hooks.
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.
HarfBuzz keeps some data until the process ends and frees it at exit:
its Unicode tables, and the languages skribidi and the text system use, are made on the C heap, so the hooks never see them
a language first seen while the hooks are in use (a text style's own
language
, for example) is made through the hooks, and given back to them at exit
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
#include <
gui_core/memory/gui_tracking_allocator.h
>
static
tachyon::TrackingAllocator
tracker;
// lives as long as the process
app.SetAllocationHooks(tracker.
Hooks
());
// ...
tracker.
ReportLeaks
();
// logs each allocation still live
tachyon::TrackingAllocator
Allocation hooks that count what is live, for finding leaks.
Definition
gui_tracking_allocator.h:15
tachyon::TrackingAllocator::ReportLeaks
size_t ReportLeaks() const
Logs a warning per live allocation; returns how many there are.
tachyon::TrackingAllocator::Hooks
AllocationHooks Hooks()
Hooks that allocate through this allocator; valid while it lives.
gui_tracking_allocator.h
tachyon::LibraryAllocator
(
gui_core/memory/gui_library_allocator.h
) is what sends the text and SVG libraries' memory to the hooks:
IsRouted()
tells whether they use hooks right now
a
HeapScope
keeps what they allocate on its thread on the C heap, for data a library keeps until the process ends:
#include <
gui_core/memory/gui_library_allocator.h
>
{
tachyon::LibraryAllocator::HeapScope
heap;
hb_language_from_string(
"fr"
, -1);
// HarfBuzz keeps it: made on the C heap
}
tachyon::LibraryAllocator::HeapScope
While one lives, what the libraries allocate on this thread comes from the C heap,...
Definition
gui_library_allocator.h:45
gui_library_allocator.h
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>());
They are also the smallest backends to start from when writing your own:
a graphics backend derives from
tachyon::GraphicsContext
a window backend derives from
tachyon::IWindowBackend
Previous: Rendering the UI into a host image
|
Integration
TachyonUI documentation
Integration
Generated by
1.18.0