Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | #ifndef _LINUX_ERR_H |
| 2 | #define _LINUX_ERR_H |
| 3 | |
| 4 | #include <linux/compiler.h> |
Joe Perches | a5ed3ce | 2014-04-03 14:48:31 -0700 | [diff] [blame] | 5 | #include <linux/types.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | |
| 7 | #include <asm/errno.h> |
| 8 | |
| 9 | /* |
| 10 | * Kernel pointers have redundant information, so we can use a |
Joe Perches | a5ed3ce | 2014-04-03 14:48:31 -0700 | [diff] [blame] | 11 | * scheme where we can return either an error code or a normal |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Ralf Baechle | fa79837 | 2006-07-01 04:36:25 -0700 | [diff] [blame] | 17 | #define MAX_ERRNO 4095 |
| 18 | |
Randy Dunlap | ebba5f9 | 2006-09-27 01:50:55 -0700 | [diff] [blame] | 19 | #ifndef __ASSEMBLY__ |
| 20 | |
Linus Torvalds | aa00edc | 2016-05-27 16:03:22 -0700 | [diff] [blame] | 21 | #define IS_ERR_VALUE(x) unlikely((unsigned long)(void *)(x) >= (unsigned long)-MAX_ERRNO) |
Linus Torvalds | 07ab67c | 2005-05-19 22:43:37 -0700 | [diff] [blame] | 22 | |
Jani Nikula | e47103b | 2010-05-24 14:33:00 -0700 | [diff] [blame] | 23 | static inline void * __must_check ERR_PTR(long error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | { |
| 25 | return (void *) error; |
| 26 | } |
| 27 | |
Dan Carpenter | e7152b9 | 2013-07-03 15:04:54 -0700 | [diff] [blame] | 28 | static inline long __must_check PTR_ERR(__force const void *ptr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | { |
| 30 | return (long) ptr; |
| 31 | } |
| 32 | |
Joe Perches | a5ed3ce | 2014-04-03 14:48:31 -0700 | [diff] [blame] | 33 | static inline bool __must_check IS_ERR(__force const void *ptr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | { |
Linus Torvalds | 07ab67c | 2005-05-19 22:43:37 -0700 | [diff] [blame] | 35 | return IS_ERR_VALUE((unsigned long)ptr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | } |
| 37 | |
Joe Perches | a5ed3ce | 2014-04-03 14:48:31 -0700 | [diff] [blame] | 38 | static inline bool __must_check IS_ERR_OR_NULL(__force const void *ptr) |
Phil Carmody | 603c4ba | 2009-12-14 18:00:29 -0800 | [diff] [blame] | 39 | { |
Viresh Kumar | dfffa58 | 2016-01-15 16:58:10 -0800 | [diff] [blame] | 40 | return unlikely(!ptr) || IS_ERR_VALUE((unsigned long)ptr); |
Phil Carmody | 603c4ba | 2009-12-14 18:00:29 -0800 | [diff] [blame] | 41 | } |
| 42 | |
David Howells | d1bc8e9 | 2008-02-07 00:15:26 -0800 | [diff] [blame] | 43 | /** |
| 44 | * ERR_CAST - Explicitly cast an error-valued pointer to another pointer type |
| 45 | * @ptr: The pointer to cast. |
| 46 | * |
| 47 | * Explicitly cast an error-valued pointer to another pointer type in such a |
| 48 | * way as to make it clear that's what's going on. |
| 49 | */ |
Dan Carpenter | e7152b9 | 2013-07-03 15:04:54 -0700 | [diff] [blame] | 50 | static inline void * __must_check ERR_CAST(__force const void *ptr) |
David Howells | d1bc8e9 | 2008-02-07 00:15:26 -0800 | [diff] [blame] | 51 | { |
| 52 | /* cast away the const */ |
| 53 | return (void *) ptr; |
| 54 | } |
| 55 | |
Rusty Russell | 6e8b872 | 2013-07-15 11:19:32 +0930 | [diff] [blame] | 56 | static inline int __must_check PTR_ERR_OR_ZERO(__force const void *ptr) |
Uwe Kleine-König | fa9ee9c | 2011-03-22 16:34:05 -0700 | [diff] [blame] | 57 | { |
| 58 | if (IS_ERR(ptr)) |
| 59 | return PTR_ERR(ptr); |
| 60 | else |
| 61 | return 0; |
| 62 | } |
| 63 | |
Rusty Russell | 6e8b872 | 2013-07-15 11:19:32 +0930 | [diff] [blame] | 64 | /* Deprecated */ |
| 65 | #define PTR_RET(p) PTR_ERR_OR_ZERO(p) |
| 66 | |
Randy Dunlap | ebba5f9 | 2006-09-27 01:50:55 -0700 | [diff] [blame] | 67 | #endif |
| 68 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | #endif /* _LINUX_ERR_H */ |