xref: /DragonOS/kernel/src/common/err.h (revision f049d1af01da7b92f312245ed411b22475b76065)
1 #pragma once
2 #include <common/compiler.h>
3 #include <DragonOS/stdint.h>
4 #define MAX_ERRNO 4095
5 
6 #define IS_ERR_VALUE(x) unlikely((x) >= (uint64_t)-MAX_ERRNO)
7 
8 /**
9  * @brief 判断返回的指针是否为errno
10  *
11  * @param ptr 待校验的指针
12  * @return long 1 => 是错误码
13  *              0 => 不是错误码
14  */
15 static inline long __must_check IS_ERR(const void* ptr)
16 {
17     return IS_ERR_VALUE((uint64_t)ptr);
18 }
19 
20 /**
21  * @brief 判断返回的指针是否为errno或者为空
22  *
23  * @param ptr 待校验的指针
24  * @return long 1 => 是错误码或NULL
25  *              0 => 不是错误码或NULL
26  */
27 static inline long __must_check IS_ERR_OR_NULL(const void* ptr)
28 {
29     return !ptr || IS_ERR_VALUE((uint64_t)ptr);
30 }
31 
32 /**
33  * @brief 将错误码转换为指针
34  *
35  * @param error 错误码
36  * @return void* 转换后的指针
37  */
38 static inline void* __must_check ERR_PTR(long error)
39 {
40     return (void*)(error);
41 }
42 
43 static inline long __must_check PTR_ERR(void * ptr)
44 {
45     return (long)ptr;
46 }