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