mirror of
https://github.com/ghostty-org/ghostty.git
synced 2025-09-29 06:28:37 +00:00
lib-vt: setup a default allocator if null
This commit is contained in:
@@ -1,3 +1,9 @@
|
||||
#include <stddef.h>
|
||||
#include <ghostty-vt.h>
|
||||
|
||||
int main() {
|
||||
return 42;
|
||||
GhosttyOscParser parser;
|
||||
ghostty_vt_osc_new(NULL, &parser);
|
||||
ghostty_vt_osc_free(parser);
|
||||
return 0;
|
||||
}
|
||||
|
@@ -14,7 +14,7 @@ extern "C" {
|
||||
//-------------------------------------------------------------------
|
||||
// Types
|
||||
|
||||
typedef struct GhosttyOscParser GhosttyOscParser;
|
||||
typedef struct GhosttyOscParser *GhosttyOscParser;
|
||||
|
||||
typedef enum {
|
||||
GHOSTTY_VT_SUCCESS = 0,
|
||||
@@ -131,6 +131,9 @@ typedef struct {
|
||||
//-------------------------------------------------------------------
|
||||
// Functions
|
||||
|
||||
GhosttyVtResult ghostty_vt_osc_new(const GhosttyVtAllocator*, GhosttyOscParser*);
|
||||
void ghostty_vt_osc_free(GhosttyOscParser);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
@@ -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,
|
||||
};
|
||||
|
||||
/// 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
|
||||
/// within C libghostty APIs.
|
||||
///
|
||||
|
@@ -1,4 +1,5 @@
|
||||
const std = @import("std");
|
||||
const builtin = @import("builtin");
|
||||
const lib_alloc = @import("../lib/allocator.zig");
|
||||
const CAllocator = lib_alloc.Allocator;
|
||||
const osc = @import("osc.zig");
|
||||
@@ -13,10 +14,10 @@ pub const Result = enum(c_int) {
|
||||
};
|
||||
|
||||
pub fn ghostty_vt_osc_new(
|
||||
c_alloc: *const CAllocator,
|
||||
c_alloc_: ?*const CAllocator,
|
||||
result: *GhosttyOscParser,
|
||||
) 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;
|
||||
ptr.* = .initAlloc(alloc);
|
||||
result.* = .{ .parser = ptr };
|
||||
|
Reference in New Issue
Block a user