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 | ||
aa00edc1 | 22 | #define IS_ERR_VALUE(x) unlikely((unsigned long)(void *)(x) >= (unsigned long)-MAX_ERRNO) |
07ab67c8 | 23 | |
e47103b1 | 24 | static inline void * __must_check ERR_PTR(long error) |
1da177e4 LT |
25 | { |
26 | return (void *) error; | |
27 | } | |
28 | ||
e7152b97 | 29 | static inline long __must_check PTR_ERR(__force const void *ptr) |
1da177e4 LT |
30 | { |
31 | return (long) ptr; | |
32 | } | |
33 | ||
a5ed3cee | 34 | static inline bool __must_check IS_ERR(__force const void *ptr) |
1da177e4 | 35 | { |
07ab67c8 | 36 | return IS_ERR_VALUE((unsigned long)ptr); |
1da177e4 LT |
37 | } |
38 | ||
a5ed3cee | 39 | static inline bool __must_check IS_ERR_OR_NULL(__force const void *ptr) |
603c4ba9 | 40 | { |
dfffa587 | 41 | return unlikely(!ptr) || IS_ERR_VALUE((unsigned long)ptr); |
603c4ba9 PC |
42 | } |
43 | ||
d1bc8e95 DH |
44 | /** |
45 | * ERR_CAST - Explicitly cast an error-valued pointer to another pointer type | |
46 | * @ptr: The pointer to cast. | |
47 | * | |
48 | * Explicitly cast an error-valued pointer to another pointer type in such a | |
49 | * way as to make it clear that's what's going on. | |
50 | */ | |
e7152b97 | 51 | static inline void * __must_check ERR_CAST(__force const void *ptr) |
d1bc8e95 DH |
52 | { |
53 | /* cast away the const */ | |
54 | return (void *) ptr; | |
55 | } | |
56 | ||
6e8b8726 | 57 | static inline int __must_check PTR_ERR_OR_ZERO(__force const void *ptr) |
fa9ee9c4 UKK |
58 | { |
59 | if (IS_ERR(ptr)) | |
60 | return PTR_ERR(ptr); | |
61 | else | |
62 | return 0; | |
63 | } | |
64 | ||
ebba5f9f RD |
65 | #endif |
66 | ||
1da177e4 | 67 | #endif /* _LINUX_ERR_H */ |