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