#include <stdalign.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct {
size_t allocs;
size_t live_bytes;
} Stats;
static size_t align_bytes(uint8_t alignment) {
return (size_t)1 << alignment;
}
static int needs_padding(uint8_t alignment) {
return align_bytes(alignment) > alignof(max_align_t);
}
static void* my_alloc(void* ctx, size_t len, uint8_t alignment,
uintptr_t ret_addr) {
(void)ret_addr;
Stats* stats = ctx;
void* result;
if (!needs_padding(alignment)) {
result = malloc(len);
} else {
size_t align = align_bytes(alignment);
void* raw = malloc(len + align + sizeof(void*));
if (raw == NULL) return NULL;
uintptr_t start = (uintptr_t)raw + sizeof(void*);
uintptr_t aligned = (start + align - 1) & ~(uintptr_t)(align - 1);
((void**)aligned)[-1] = raw;
result = (void*)aligned;
}
if (result != NULL) {
stats->allocs++;
stats->live_bytes += len;
}
return result;
}
static void my_free(void* ctx, void* memory, size_t memory_len,
uint8_t alignment, uintptr_t ret_addr) {
(void)ret_addr;
Stats* stats = ctx;
stats->live_bytes -= memory_len;
if (!needs_padding(alignment)) {
free(memory);
} else {
free(((void**)memory)[-1]);
}
}
.alloc = my_alloc,
.free = my_free,
};
int main(void) {
Stats stats = {0};
.ctx = &stats,
.vtable = &my_vtable,
};
fprintf(stderr, "ghostty_terminal_new failed\n");
return 1;
}
char line[64];
for (int i = 0; i < 20000; i++) {
int n = snprintf(line, sizeof(line),
"\x1b[3%dmline %d with some text\x1b[0m\r\n", i % 8, i);
}
printf("Allocations: %zu\n", stats.allocs);
printf("Live bytes before free: %zu\n", stats.live_bytes);
printf("Live bytes after free: %zu\n", stats.live_bytes);
return stats.live_bytes == 0 ? 0 : 1;
}
struct GhosttyTerminalImpl * GhosttyTerminal
GHOSTTY_API void ghostty_terminal_free(GhosttyTerminal terminal)
GHOSTTY_API void ghostty_terminal_vt_write(GhosttyTerminal terminal, const uint8_t *data, size_t len)
GHOSTTY_API GhosttyResult ghostty_terminal_new(const GhosttyAllocator *allocator, GhosttyTerminal *terminal, uint16_t cols, uint16_t rows)