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