lib-vt: setup a default allocator if null

This commit is contained in:
Mitchell Hashimoto
2025-09-24 10:19:13 -07:00
parent de013148d3
commit 2c78ad8889
4 changed files with 30 additions and 4 deletions

View File

@@ -1,3 +1,9 @@
#include <stddef.h>
#include <ghostty-vt.h>
int main() { int main() {
return 42; GhosttyOscParser parser;
ghostty_vt_osc_new(NULL, &parser);
ghostty_vt_osc_free(parser);
return 0;
} }

View File

@@ -14,7 +14,7 @@ extern "C" {
//------------------------------------------------------------------- //-------------------------------------------------------------------
// Types // Types
typedef struct GhosttyOscParser GhosttyOscParser; typedef struct GhosttyOscParser *GhosttyOscParser;
typedef enum { typedef enum {
GHOSTTY_VT_SUCCESS = 0, GHOSTTY_VT_SUCCESS = 0,
@@ -131,6 +131,9 @@ typedef struct {
//------------------------------------------------------------------- //-------------------------------------------------------------------
// Functions // Functions
GhosttyVtResult ghostty_vt_osc_new(const GhosttyVtAllocator*, GhosttyOscParser*);
void ghostty_vt_osc_free(GhosttyOscParser);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@@ -13,6 +13,22 @@ pub const VTable = extern struct {
free: *const fn (*anyopaque, memory: [*]u8, memory_len: usize, alignment: u8, ret_addr: usize) callconv(.c) void, free: *const fn (*anyopaque, memory: [*]u8, memory_len: usize, alignment: u8, ret_addr: usize) callconv(.c) void,
}; };
/// Returns an allocator to use for the given possibly-null C allocator,
/// ensuring some allocator is always returned.
pub fn default(c_alloc_: ?*const Allocator) std.mem.Allocator {
// If we're given an allocator, use it.
if (c_alloc_) |c_alloc| return c_alloc.zig();
// If we have libc, use that. We prefer libc if we have it because
// its generally fast but also lets the embedder easily override
// malloc/free with custom allocators like mimalloc or something.
if (comptime builtin.link_libc) return std.heap.c_allocator;
// No libc, use the preferred allocator for releases which is the
// Zig SMP allocator.
return std.heap.smp_allocator;
}
/// The Allocator interface for custom memory allocation strategies /// The Allocator interface for custom memory allocation strategies
/// within C libghostty APIs. /// within C libghostty APIs.
/// ///

View File

@@ -1,4 +1,5 @@
const std = @import("std"); const std = @import("std");
const builtin = @import("builtin");
const lib_alloc = @import("../lib/allocator.zig"); const lib_alloc = @import("../lib/allocator.zig");
const CAllocator = lib_alloc.Allocator; const CAllocator = lib_alloc.Allocator;
const osc = @import("osc.zig"); const osc = @import("osc.zig");
@@ -13,10 +14,10 @@ pub const Result = enum(c_int) {
}; };
pub fn ghostty_vt_osc_new( pub fn ghostty_vt_osc_new(
c_alloc: *const CAllocator, c_alloc_: ?*const CAllocator,
result: *GhosttyOscParser, result: *GhosttyOscParser,
) callconv(.c) Result { ) callconv(.c) Result {
const alloc = c_alloc.zig(); const alloc = lib_alloc.default(c_alloc_);
const ptr = alloc.create(osc.Parser) catch return .out_of_memory; const ptr = alloc.create(osc.Parser) catch return .out_of_memory;
ptr.* = .initAlloc(alloc); ptr.* = .initAlloc(alloc);
result.* = .{ .parser = ptr }; result.* = .{ .parser = ptr };