Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | #ifndef _LINUX_ERR_H |
| 3 | #define _LINUX_ERR_H |
| 4 | |
| 5 | #include <linux/compiler.h> |
Joe Perches | a5ed3ce | 2014-04-03 14:48:31 -0700 | [diff] [blame] | 6 | #include <linux/types.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | |
| 8 | #include <asm/errno.h> |
| 9 | |
| 10 | /* |
| 11 | * Kernel pointers have redundant information, so we can use a |
Joe Perches | a5ed3ce | 2014-04-03 14:48:31 -0700 | [diff] [blame] | 12 | * scheme where we can return either an error code or a normal |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Ralf Baechle | fa79837 | 2006-07-01 04:36:25 -0700 | [diff] [blame] | 18 | #define MAX_ERRNO 4095 |
| 19 | |
Randy Dunlap | ebba5f9 | 2006-09-27 01:50:55 -0700 | [diff] [blame] | 20 | #ifndef __ASSEMBLY__ |
| 21 | |
Linus Torvalds | aa00edc | 2016-05-27 16:03:22 -0700 | [diff] [blame] | 22 | #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] | 23 | |
Jani Nikula | e47103b | 2010-05-24 14:33:00 -0700 | [diff] [blame] | 24 | static inline void * __must_check ERR_PTR(long error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | { |
| 26 | return (void *) error; |
| 27 | } |
| 28 | |
Dan Carpenter | e7152b9 | 2013-07-03 15:04:54 -0700 | [diff] [blame] | 29 | static inline long __must_check PTR_ERR(__force const void *ptr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | { |
| 31 | return (long) ptr; |
| 32 | } |
| 33 | |
Joe Perches | a5ed3ce | 2014-04-03 14:48:31 -0700 | [diff] [blame] | 34 | static inline bool __must_check IS_ERR(__force const void *ptr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | { |
Linus Torvalds | 07ab67c | 2005-05-19 22:43:37 -0700 | [diff] [blame] | 36 | return IS_ERR_VALUE((unsigned long)ptr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | } |
| 38 | |
Joe Perches | a5ed3ce | 2014-04-03 14:48:31 -0700 | [diff] [blame] | 39 | 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] | 40 | { |
Viresh Kumar | dfffa58 | 2016-01-15 16:58:10 -0800 | [diff] [blame] | 41 | return unlikely(!ptr) || IS_ERR_VALUE((unsigned long)ptr); |
Phil Carmody | 603c4ba | 2009-12-14 18:00:29 -0800 | [diff] [blame] | 42 | } |
| 43 | |
David Howells | d1bc8e9 | 2008-02-07 00:15:26 -0800 | [diff] [blame] | 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 | */ |
Dan Carpenter | e7152b9 | 2013-07-03 15:04:54 -0700 | [diff] [blame] | 51 | static inline void * __must_check ERR_CAST(__force const void *ptr) |
David Howells | d1bc8e9 | 2008-02-07 00:15:26 -0800 | [diff] [blame] | 52 | { |
| 53 | /* cast away the const */ |
| 54 | return (void *) ptr; |
| 55 | } |
| 56 | |
Rusty Russell | 6e8b872 | 2013-07-15 11:19:32 +0930 | [diff] [blame] | 57 | 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] | 58 | { |
| 59 | if (IS_ERR(ptr)) |
| 60 | return PTR_ERR(ptr); |
| 61 | else |
| 62 | return 0; |
| 63 | } |
| 64 | |
Rusty Russell | 6e8b872 | 2013-07-15 11:19:32 +0930 | [diff] [blame] | 65 | /* Deprecated */ |
| 66 | #define PTR_RET(p) PTR_ERR_OR_ZERO(p) |
| 67 | |
Randy Dunlap | ebba5f9 | 2006-09-27 01:50:55 -0700 | [diff] [blame] | 68 | #endif |
| 69 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | #endif /* _LINUX_ERR_H */ |