aboutsummaryrefslogtreecommitdiff
path: root/src/err.h
diff options
context:
space:
mode:
authorallusive-dev <[email protected]>2023-09-19 17:47:33 +1000
committerallusive-dev <[email protected]>2023-09-19 17:47:33 +1000
commita93aba600b1c5d019b680b9f4ff3fa85d5d43a60 (patch)
tree77f8152222655657472a70e0bfa413a0495dd555 /src/err.h
parentreset (diff)
downloadcompfy-a93aba600b1c5d019b680b9f4ff3fa85d5d43a60.tar.xz
compfy-a93aba600b1c5d019b680b9f4ff3fa85d5d43a60.zip
Fixed broken files/code and other errors
Diffstat (limited to 'src/err.h')
-rw-r--r--src/err.h37
1 files changed, 37 insertions, 0 deletions
diff --git a/src/err.h b/src/err.h
new file mode 100644
index 0000000..f989bf9
--- /dev/null
+++ b/src/err.h
@@ -0,0 +1,37 @@
+// SPDX-License-Identifier: MPL-2.0
+// Copyright (c) 2019 Yuxuan Shui <[email protected]>
+
+#pragma once
+#include <stdbool.h>
+#include <stdint.h>
+#include "compiler.h"
+
+// Functions for error reporting, adopted from Linux
+
+// INFO in user space we can probably be more liberal about what pointer we consider
+// error. e.g. In x86_64 Linux, all addresses with the highest bit set is invalid in user
+// space.
+#define MAX_ERRNO 4095
+
+static inline void *must_use ERR_PTR(intptr_t err) {
+ return (void *)err;
+}
+
+static inline intptr_t must_use PTR_ERR(void *ptr) {
+ return (intptr_t)ptr;
+}
+
+static inline bool must_use IS_ERR(void *ptr) {
+ return unlikely((uintptr_t)ptr > (uintptr_t)-MAX_ERRNO);
+}
+
+static inline bool must_use IS_ERR_OR_NULL(void *ptr) {
+ return unlikely(!ptr) || IS_ERR(ptr);
+}
+
+static inline intptr_t must_use PTR_ERR_OR_ZERO(void *ptr) {
+ if (IS_ERR(ptr)) {
+ return PTR_ERR(ptr);
+ }
+ return 0;
+}