mirror of
https://github.com/ghostty-org/ghostty.git
synced 2026-08-04 14:48:39 +00:00
libghostty: add ghostty_free for cross-runtime memory safety
On Windows, Zig's built-in libc and MSVC's CRT maintain separate heaps, so calling free() on memory allocated by the library causes undefined behavior. Add ghostty_free() that frees through the same allocator that performed the allocation, making it safe on all platforms. Update format_alloc docs and all examples to use ghostty_free() instead of free(). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -218,6 +218,7 @@ comptime {
|
||||
@export(&c.grid_ref_graphemes, .{ .name = "ghostty_grid_ref_graphemes" });
|
||||
@export(&c.grid_ref_style, .{ .name = "ghostty_grid_ref_style" });
|
||||
@export(&c.build_info, .{ .name = "ghostty_build_info" });
|
||||
@export(&c.free_alloc, .{ .name = "ghostty_free" });
|
||||
|
||||
// On Wasm we need to export our allocator convenience functions.
|
||||
if (builtin.target.cpu.arch.isWasm()) {
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
const lib_alloc = @import("../../lib/allocator.zig");
|
||||
const CAllocator = lib_alloc.Allocator;
|
||||
|
||||
const buildpkg = @import("build_info.zig");
|
||||
pub const cell = @import("cell.zig");
|
||||
pub const color = @import("color.zig");
|
||||
@@ -112,6 +115,21 @@ pub const mouse_encoder_encode = mouse_encode.encode;
|
||||
|
||||
pub const paste_is_safe = paste.is_safe;
|
||||
|
||||
/// Free memory that was allocated by a libghostty-vt function.
|
||||
///
|
||||
/// This must be used to free buffers returned by functions like
|
||||
/// `format_alloc`. Pass the same allocator (or NULL for the default)
|
||||
/// that was used for the allocation.
|
||||
pub fn free_alloc(
|
||||
alloc_: ?*const CAllocator,
|
||||
ptr: ?[*]u8,
|
||||
len: usize,
|
||||
) callconv(.c) void {
|
||||
const mem = ptr orelse return;
|
||||
const alloc = lib_alloc.default(alloc_);
|
||||
alloc.free(mem[0..len]);
|
||||
}
|
||||
|
||||
pub const size_report_encode = size_report.encode;
|
||||
|
||||
pub const cell_get = cell.get;
|
||||
|
||||
Reference in New Issue
Block a user