[sheepdog] [PATCH v3 05/12] util: add PTR_ERR helpers

Liu Yuan namei.unix at gmail.com
Wed Jan 29 09:26:19 CET 2014


Sometimes we need to return ERROR code from pointers

Signed-off-by: Liu Yuan <namei.unix at gmail.com>
---
 include/compiler.h |  2 ++
 include/util.h     | 30 ++++++++++++++++++++++++++++++
 2 files changed, 32 insertions(+)

diff --git a/include/compiler.h b/include/compiler.h
index 4a59b92..2fbd2a4 100644
--- a/include/compiler.h
+++ b/include/compiler.h
@@ -43,6 +43,8 @@
 /* Force a compilation error if the condition is true */
 #define BUILD_BUG_ON(condition) ((void)sizeof(struct { int: -!!(condition); }))
 
+#define __must_check            __attribute__((warn_unused_result))
+
 #ifdef HAVE_SYS_SIGNALFD_H
 #include <sys/signalfd.h>
 #else
diff --git a/include/util.h b/include/util.h
index 1932fe3..4b4fa27 100644
--- a/include/util.h
+++ b/include/util.h
@@ -493,4 +493,34 @@ static inline void clear_screen(void)
 extern mode_t sd_def_fmode;
 extern mode_t sd_def_dmode;
 
+#define MAX_ERRNO	4095
+
+#define IS_ERR_VALUE(x) unlikely((x) >= (unsigned long)-MAX_ERRNO)
+
+/*
+ * For SD, we should pass a '-SD_RES_SOME_ERROR' to ERR_PTR at first, then we
+ * use PTR_ERR() to get the actual error code, because we use positive integer
+ * for error code.
+ */
+static inline void * __must_check ERR_PTR(long error)
+{
+	assert(error < 0);
+	return (void *)error;
+}
+
+static inline long __must_check PTR_ERR(const void *ptr)
+{
+	return -(long)ptr;
+}
+
+static inline long __must_check IS_ERR(const void *ptr)
+{
+	return IS_ERR_VALUE((unsigned long)ptr);
+}
+
+static inline long __must_check IS_ERR_OR_NULL(const void *ptr)
+{
+	return !ptr || IS_ERR_VALUE((unsigned long)ptr);
+}
+
 #endif
-- 
1.8.1.2




More information about the sheepdog mailing list