xref: /DragonStub/inc/dragonstub/linux/err.h (revision f412fd2a1a248b546b7085648dece8d908077fab)
1*f412fd2aSLoGin /* SPDX-License-Identifier: GPL-2.0 */
2*f412fd2aSLoGin #ifndef _LINUX_ERR_H
3*f412fd2aSLoGin #define _LINUX_ERR_H
4*f412fd2aSLoGin 
5*f412fd2aSLoGin #include "compiler.h"
6*f412fd2aSLoGin #include "../types.h"
7*f412fd2aSLoGin 
8*f412fd2aSLoGin #include "errno.h"
9*f412fd2aSLoGin 
10*f412fd2aSLoGin /*
11*f412fd2aSLoGin  * Kernel pointers have redundant information, so we can use a
12*f412fd2aSLoGin  * scheme where we can return either an error code or a normal
13*f412fd2aSLoGin  * pointer with the same return value.
14*f412fd2aSLoGin  *
15*f412fd2aSLoGin  * This should be a per-architecture thing, to allow different
16*f412fd2aSLoGin  * error and pointer decisions.
17*f412fd2aSLoGin  */
18*f412fd2aSLoGin #define MAX_ERRNO	4095
19*f412fd2aSLoGin 
20*f412fd2aSLoGin #ifndef __ASSEMBLY__
21*f412fd2aSLoGin 
22*f412fd2aSLoGin /**
23*f412fd2aSLoGin  * IS_ERR_VALUE - Detect an error pointer.
24*f412fd2aSLoGin  * @x: The pointer to check.
25*f412fd2aSLoGin  *
26*f412fd2aSLoGin  * Like IS_ERR(), but does not generate a compiler warning if result is unused.
27*f412fd2aSLoGin  */
28*f412fd2aSLoGin #define IS_ERR_VALUE(x) unlikely((unsigned long)(void *)(x) >= (unsigned long)-MAX_ERRNO)
29*f412fd2aSLoGin 
30*f412fd2aSLoGin /**
31*f412fd2aSLoGin  * ERR_PTR - Create an error pointer.
32*f412fd2aSLoGin  * @error: A negative error code.
33*f412fd2aSLoGin  *
34*f412fd2aSLoGin  * Encodes @error into a pointer value. Users should consider the result
35*f412fd2aSLoGin  * opaque and not assume anything about how the error is encoded.
36*f412fd2aSLoGin  *
37*f412fd2aSLoGin  * Return: A pointer with @error encoded within its value.
38*f412fd2aSLoGin  */
ERR_PTR(long error)39*f412fd2aSLoGin static inline void * __must_check ERR_PTR(long error)
40*f412fd2aSLoGin {
41*f412fd2aSLoGin 	return (void *) error;
42*f412fd2aSLoGin }
43*f412fd2aSLoGin 
44*f412fd2aSLoGin /**
45*f412fd2aSLoGin  * PTR_ERR - Extract the error code from an error pointer.
46*f412fd2aSLoGin  * @ptr: An error pointer.
47*f412fd2aSLoGin  * Return: The error code within @ptr.
48*f412fd2aSLoGin  */
PTR_ERR(__force const void * ptr)49*f412fd2aSLoGin static inline long __must_check PTR_ERR(__force const void *ptr)
50*f412fd2aSLoGin {
51*f412fd2aSLoGin 	return (long) ptr;
52*f412fd2aSLoGin }
53*f412fd2aSLoGin 
54*f412fd2aSLoGin /**
55*f412fd2aSLoGin  * IS_ERR - Detect an error pointer.
56*f412fd2aSLoGin  * @ptr: The pointer to check.
57*f412fd2aSLoGin  * Return: true if @ptr is an error pointer, false otherwise.
58*f412fd2aSLoGin  */
IS_ERR(__force const void * ptr)59*f412fd2aSLoGin static inline bool __must_check IS_ERR(__force const void *ptr)
60*f412fd2aSLoGin {
61*f412fd2aSLoGin 	return IS_ERR_VALUE((unsigned long)ptr);
62*f412fd2aSLoGin }
63*f412fd2aSLoGin 
64*f412fd2aSLoGin /**
65*f412fd2aSLoGin  * IS_ERR_OR_NULL - Detect an error pointer or a null pointer.
66*f412fd2aSLoGin  * @ptr: The pointer to check.
67*f412fd2aSLoGin  *
68*f412fd2aSLoGin  * Like IS_ERR(), but also returns true for a null pointer.
69*f412fd2aSLoGin  */
IS_ERR_OR_NULL(__force const void * ptr)70*f412fd2aSLoGin static inline bool __must_check IS_ERR_OR_NULL(__force const void *ptr)
71*f412fd2aSLoGin {
72*f412fd2aSLoGin 	return unlikely(!ptr) || IS_ERR_VALUE((unsigned long)ptr);
73*f412fd2aSLoGin }
74*f412fd2aSLoGin 
75*f412fd2aSLoGin /**
76*f412fd2aSLoGin  * ERR_CAST - Explicitly cast an error-valued pointer to another pointer type
77*f412fd2aSLoGin  * @ptr: The pointer to cast.
78*f412fd2aSLoGin  *
79*f412fd2aSLoGin  * Explicitly cast an error-valued pointer to another pointer type in such a
80*f412fd2aSLoGin  * way as to make it clear that's what's going on.
81*f412fd2aSLoGin  */
ERR_CAST(__force const void * ptr)82*f412fd2aSLoGin static inline void * __must_check ERR_CAST(__force const void *ptr)
83*f412fd2aSLoGin {
84*f412fd2aSLoGin 	/* cast away the const */
85*f412fd2aSLoGin 	return (void *) ptr;
86*f412fd2aSLoGin }
87*f412fd2aSLoGin 
88*f412fd2aSLoGin /**
89*f412fd2aSLoGin  * PTR_ERR_OR_ZERO - Extract the error code from a pointer if it has one.
90*f412fd2aSLoGin  * @ptr: A potential error pointer.
91*f412fd2aSLoGin  *
92*f412fd2aSLoGin  * Convenience function that can be used inside a function that returns
93*f412fd2aSLoGin  * an error code to propagate errors received as error pointers.
94*f412fd2aSLoGin  * For example, ``return PTR_ERR_OR_ZERO(ptr);`` replaces:
95*f412fd2aSLoGin  *
96*f412fd2aSLoGin  * .. code-block:: c
97*f412fd2aSLoGin  *
98*f412fd2aSLoGin  *	if (IS_ERR(ptr))
99*f412fd2aSLoGin  *		return PTR_ERR(ptr);
100*f412fd2aSLoGin  *	else
101*f412fd2aSLoGin  *		return 0;
102*f412fd2aSLoGin  *
103*f412fd2aSLoGin  * Return: The error code within @ptr if it is an error pointer; 0 otherwise.
104*f412fd2aSLoGin  */
PTR_ERR_OR_ZERO(__force const void * ptr)105*f412fd2aSLoGin static inline int __must_check PTR_ERR_OR_ZERO(__force const void *ptr)
106*f412fd2aSLoGin {
107*f412fd2aSLoGin 	if (IS_ERR(ptr))
108*f412fd2aSLoGin 		return PTR_ERR(ptr);
109*f412fd2aSLoGin 	else
110*f412fd2aSLoGin 		return 0;
111*f412fd2aSLoGin }
112*f412fd2aSLoGin 
113*f412fd2aSLoGin #endif
114*f412fd2aSLoGin 
115*f412fd2aSLoGin #endif /* _LINUX_ERR_H */
116