xref: /kvmtool/include/linux/err.h (revision 443cd881b47126bde6f442abc1f6c76998d713b7)
1 #ifndef _LINUX_ERR_H
2 #define _LINUX_ERR_H
3 
4 #include <stdbool.h>
5 
6 #include <linux/compiler.h>
7 #include <linux/types.h>
8 
9 #include <asm/errno.h>
10 
11 /*
12  * Kernel pointers have redundant information, so we can use a
13  * scheme where we can return either an error code or a normal
14  * pointer with the same return value.
15  *
16  * This should be a per-architecture thing, to allow different
17  * error and pointer decisions.
18  */
19 #define MAX_ERRNO	4095
20 
21 #ifndef __ASSEMBLY__
22 
23 #define IS_ERR_VALUE(x) unlikely((x) >= (unsigned long)-MAX_ERRNO)
24 
ERR_PTR(long error)25 static inline void * __must_check ERR_PTR(long error)
26 {
27 	return (void *) error;
28 }
29 
PTR_ERR(__force const void * ptr)30 static inline long __must_check PTR_ERR(__force const void *ptr)
31 {
32 	return (long) ptr;
33 }
34 
IS_ERR(__force const void * ptr)35 static inline bool __must_check IS_ERR(__force const void *ptr)
36 {
37 	return IS_ERR_VALUE((unsigned long)ptr);
38 }
39 
IS_ERR_OR_NULL(__force const void * ptr)40 static inline bool __must_check IS_ERR_OR_NULL(__force const void *ptr)
41 {
42 	return !ptr || IS_ERR_VALUE((unsigned long)ptr);
43 }
44 
45 /**
46  * ERR_CAST - Explicitly cast an error-valued pointer to another pointer type
47  * @ptr: The pointer to cast.
48  *
49  * Explicitly cast an error-valued pointer to another pointer type in such a
50  * way as to make it clear that's what's going on.
51  */
ERR_CAST(__force const void * ptr)52 static inline void * __must_check ERR_CAST(__force const void *ptr)
53 {
54 	/* cast away the const */
55 	return (void *) ptr;
56 }
57 
PTR_ERR_OR_ZERO(__force const void * ptr)58 static inline int __must_check PTR_ERR_OR_ZERO(__force const void *ptr)
59 {
60 	if (IS_ERR(ptr))
61 		return PTR_ERR(ptr);
62 	else
63 		return 0;
64 }
65 
66 /* Deprecated */
67 #define PTR_RET(p) PTR_ERR_OR_ZERO(p)
68 
69 #endif
70 
71 #endif /* _LINUX_ERR_H */
72