[libc-commits] [libc] [libc] Introduce a typed syscall wrapper and use it in mmap (PR #197459)
Jeff Bailey via libc-commits
libc-commits at lists.llvm.org
Wed May 13 08:30:59 PDT 2026
================
@@ -29,24 +30,31 @@
namespace LIBC_NAMESPACE_DECL {
+// This function performs no error checking. For most syscalls, it's better to
+// use __syscall below.
template <typename R, typename... Ts>
LIBC_INLINE R syscall_impl(long __number, Ts... ts) {
static_assert(sizeof...(Ts) <= 6, "Too many arguments for syscall");
return cpp::bit_or_static_cast<R>(syscall_impl(__number, (long)ts...));
}
-// Linux-specific function for checking
-namespace linux_utils {
+namespace linux_syscalls {
LIBC_INLINE_VAR constexpr unsigned long MAX_ERRNO = 4095;
-// Ideally, this should be defined using PAGE_OFFSET
-// However, that is a configurable parameter. We mimic kernel's behavior
-// by checking against MAX_ERRNO.
-template <typename PointerLike>
-LIBC_INLINE constexpr bool is_valid_mmap(PointerLike ptr) {
- long addr = cpp::bit_cast<long>(ptr);
- return LIBC_LIKELY(addr > 0 || addr < -static_cast<long>(MAX_ERRNO));
+
+// Helper function to perform a system call, check the result and cast the
+// result to the expected type. This function is safe to use on most syscalls,
+// with the exception of a handful of syscalls (getpid, getuid, ...) that never
+// fail.
+template <typename R, typename... Ts>
+LIBC_INLINE ErrorOr<R> __syscall(long __number, Ts... ts) {
----------------
kaladron wrote:
I don't love the __ prefix. We're already namespaced. I think it makes sense to call it something obvious with tricks that look C-ish.
https://github.com/llvm/llvm-project/pull/197459
More information about the libc-commits
mailing list