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