![]() |
libghostty-vt 0.0.0
|
Struct size, layout, and alignment is part of the C ABI. We want to have the ability to add fields to structs without breaking existing programs. To achieve this, we use a common pattern known as "sized structs." Sized structs are mildly annoying so we do our best to limit this as much as possible.
A sized struct starts with a size_t size field. It tells us how big the struct was when you compiled, and we only touch that much memory. New fields always go at the end. That way, we never write beyond the amount of memory you allocate.
In C you never set size by hand. Use GHOSTTY_INIT_SIZED():
It sets size for you and sets every other field to zero. Then you fill in the fields you need. Language bindings that can't use a C macro can call ghostty_sized_init() instead.
Zero is always the default value of a field. A field you leave at zero behaves the way the library did before that field existed. This is why GHOSTTY_INIT_SIZED() matters. It keeps every field you didn't set at its default, including fields added in later releases.
Your program and the library it loads may come from different releases. The size field lets the library handle either case:
In every case, a size smaller than the struct has ever been returns GHOSTTY_INVALID_VALUE. Clipboard replies are the one exception. The reply function has no return value, so a reply with an invalid size is treated as unsupported instead.
Here is how you can use a newer field when the library supports it and fall back when it doesn't. Imagine that semantic_prompt_boundary was added in a later release than the library your program loads: