-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathalloc.go
More file actions
36 lines (31 loc) · 1.19 KB
/
Copy pathalloc.go
File metadata and controls
36 lines (31 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package libghostty
// Memory allocation helpers wrapping the upstream ghostty_alloc() and
// ghostty_free() functions from allocator.h.
//
// These replace direct C.malloc/C.free calls so that all memory is
// allocated and freed through libghostty's allocator. This is critical
// on platforms where the library's internal allocator differs from the
// consumer's C runtime (e.g. Windows, where Zig's libc and MSVC's CRT
// maintain separate heaps).
/*
#include <ghostty/vt.h>
*/
import "C"
import "unsafe"
// Alloc allocates len bytes through the default libghostty allocator
// (NULL allocator). Returns a pointer to the allocated memory or nil
// if the allocation failed.
//
// The returned memory must be freed with Free using the same length.
// C: ghostty_alloc
func Alloc(len uintptr) unsafe.Pointer {
return unsafe.Pointer(C.ghostty_alloc(nil, C.size_t(len)))
}
// Free frees memory allocated by Alloc (or returned by a libghostty
// function) using the default libghostty allocator (NULL allocator).
// The len must match the original allocation size. It is safe to pass
// nil.
// C: ghostty_free
func Free(ptr unsafe.Pointer, len uintptr) {
C.ghostty_free(nil, (*C.uint8_t)(ptr), C.size_t(len))
}