Skip to content

Commit

Permalink
Use libc to free pointer, closes #1
Browse files Browse the repository at this point in the history
  • Loading branch information
bored-engineer committed Sep 5, 2023
1 parent 1968cfd commit 6aff118
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
16 changes: 16 additions & 0 deletions libc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//go:build darwin
// +build darwin

package launchd

import "unsafe"

// free is defined in libxpc.dylib
var libc_free_trampoline_addr uintptr

//go:cgo_import_dynamic libc_free free "/usr/lib/libSystem.B.dylib"

//go:nosplit
func free(ptr unsafe.Pointer) {
syscall_syscall(libc_free_trampoline_addr, uintptr(ptr), 0, 0)
}
13 changes: 13 additions & 0 deletions libc.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//go:build darwin
// +build darwin

#include "textflag.h"

// The trampolines are ABIInternal as they are address-taken in
// Go code.

TEXT libc_free_trampoline<>(SB),NOSPLIT,$0-0
JMP libc_free(SB)

GLOBL ·libc_free_trampoline_addr(SB), RODATA, $8
DATA ·libc_free_trampoline_addr(SB)/8, $libc_free_trampoline<>(SB)
8 changes: 6 additions & 2 deletions libxpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ func libxpc_launch_activate_socket(name string) ([]int, error) {
return nil, err
}
var c_fds_ptr *uintptr // *C.int
var c_cnt uint // C.size_t
defer func() {
if c_fds_ptr != nil {
free(unsafe.Pointer(c_fds_ptr))
}
}()
var c_cnt uint // C.size_t
res, _, _ := syscall_syscall(
libxpc_launch_activate_socket_trampoline_addr,
uintptr(unsafe.Pointer(c_name_ptr)),
Expand All @@ -41,7 +46,6 @@ func libxpc_launch_activate_socket(name string) ([]int, error) {
return nil, syscall.EINVAL
}
c_fds := (*[maxFDs]int)(unsafe.Pointer(c_fds_ptr))
// TODO: free c_fds_ptr after copy?
fds := make([]int, c_cnt)
copy(fds, (*c_fds)[0:c_cnt])
return fds, nil
Expand Down

0 comments on commit 6aff118

Please sign in to comment.