Merge pull request #3889 from laytan/darwin-remove-implicit-syscall-usage

darwin: remove syscall usage (without -no-crt) to comply to Apple guidelines
This commit is contained in:
Jeroen van Rijn
2024-07-08 15:54:24 +02:00
committed by GitHub
6 changed files with 78 additions and 22 deletions

View File

@@ -5,11 +5,24 @@ package runtime
import "base:intrinsics"
_stderr_write :: proc "contextless" (data: []byte) -> (int, _OS_Errno) {
WRITE :: 0x2000004
STDERR :: 2
ret := intrinsics.syscall(WRITE, STDERR, uintptr(raw_data(data)), uintptr(len(data)))
if ret < 0 {
return 0, _OS_Errno(-ret)
when ODIN_NO_CRT {
WRITE :: 0x2000004
ret := intrinsics.syscall(WRITE, STDERR, uintptr(raw_data(data)), uintptr(len(data)))
if ret < 0 {
return 0, _OS_Errno(-ret)
}
return int(ret), 0
} else {
foreign {
write :: proc(handle: i32, buffer: [^]byte, count: uint) -> int ---
__error :: proc() -> ^i32 ---
}
if ret := write(STDERR, raw_data(data), len(data)); ret >= 0 {
return int(ret), 0
}
return 0, _OS_Errno(__error()^)
}
return int(ret), 0
}

View File

@@ -3,6 +3,10 @@ package darwin
import "core:c"
import "base:runtime"
// IMPORTANT NOTE: direct syscall usage is not allowed by Apple's review process of apps and should
// be entirely avoided in the builtin Odin collections, these are here for users if they don't
// care about the Apple review process.
// this package uses the sys prefix for the proc names to indicate that these aren't native syscalls but directly call such
sys_write_string :: proc (fd: c.int, message: string) -> bool {
return syscall_write(fd, raw_data(message), cast(u64)len(message))

View File

@@ -1,5 +1,9 @@
package darwin
// IMPORTANT NOTE: direct syscall usage is not allowed by Apple's review process of apps and should
// be entirely avoided in the builtin Odin collections, these are here for users if they don't
// care about the Apple review process.
unix_offset_syscall :: proc "contextless" (number: System_Call_Number) -> uintptr {
return uintptr(number) + uintptr(0x2000000)
}

View File

@@ -3,6 +3,10 @@ package darwin
import "core:c"
import "base:intrinsics"
// IMPORTANT NOTE: direct syscall usage is not allowed by Apple's review process of apps and should
// be entirely avoided in the builtin Odin collections, these are here for users if they don't
// care about the Apple review process.
/* flock */
LOCK_SH :: 1 /* shared lock */
LOCK_EX :: 2 /* exclusive lock */

View File

@@ -7,7 +7,7 @@ init_cpu_features :: proc "contextless" () {
@(static) features: CPU_Features
defer cpu_features = features
try_set :: proc "contextless" (name: string, feature: CPU_Feature) -> (ok: bool) {
try_set :: proc "contextless" (name: cstring, feature: CPU_Feature) -> (ok: bool) {
support: b32
if ok = unix.sysctlbyname(name, &support); ok && support {
features += { feature }

View File

@@ -3,29 +3,60 @@ package unix
import "base:intrinsics"
import "core:c"
import "core:sys/darwin"
_ :: darwin
sysctl :: proc "contextless" (mib: []i32, val: ^$T) -> (ok: bool) {
result_size := c.size_t(size_of(T))
res := darwin.syscall_sysctl(
raw_data(mib), len(mib),
val, &result_size,
nil, 0,
)
return res == 0
result_size := uint(size_of(T))
when ODIN_NO_CRT {
res := darwin.syscall_sysctl(
raw_data(mib), len(mib),
val, &result_size,
nil, 0,
)
return res == 0
} else {
foreign {
@(link_name="sysctl") _sysctl :: proc(
name: [^]i32, namelen: u32,
oldp: rawptr, oldlenp: ^uint,
newp: rawptr, newlen: uint,
) -> i32 ---
}
res := _sysctl(
raw_data(mib), u32(len(mib)),
val, &result_size,
nil, 0,
)
return res == 0
}
}
sysctlbyname :: proc "contextless" (name: string, val: ^$T) -> (ok: bool) {
result_size := c.size_t(size_of(T))
res := darwin.syscall_sysctlbyname(
name,
val, &result_size,
nil, 0,
)
return res == 0
sysctlbyname :: proc "contextless" (name: cstring, val: ^$T) -> (ok: bool) {
result_size := uint(size_of(T))
when ODIN_NO_CRT {
res := darwin.syscall_sysctlbyname(
string(name),
val, &result_size,
nil, 0,
)
return res == 0
} else {
foreign {
@(link_name="sysctlbyname") _sysctlbyname :: proc(
name: cstring,
oldp: rawptr, oldlenp: ^uint,
newp: rawptr, newlen: uint,
) -> i32 ---
}
res := _sysctlbyname(
name,
val, &result_size,
nil, 0,
)
return res == 0
}
}
// See sysctl.h for darwin for details