blob: 1e3558845e4c3d986411d34b066b3e5d2518bab8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef _LINUX_ERR_H
2#define _LINUX_ERR_H
3
4#include <linux/compiler.h>
Joe Perchesa5ed3ce2014-04-03 14:48:31 -07005#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006
7#include <asm/errno.h>
8
9/*
10 * Kernel pointers have redundant information, so we can use a
Joe Perchesa5ed3ce2014-04-03 14:48:31 -070011 * scheme where we can return either an error code or a normal
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * 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 Baechlefa798372006-07-01 04:36:25 -070017#define MAX_ERRNO 4095
18
Randy Dunlapebba5f92006-09-27 01:50:55 -070019#ifndef __ASSEMBLY__
20
Linus Torvaldsaa00edc2016-05-27 16:03:22 -070021#define IS_ERR_VALUE(x) unlikely((unsigned long)(void *)(x) >= (unsigned long)-MAX_ERRNO)
Linus Torvalds07ab67c2005-05-19 22:43:37 -070022
Jani Nikulae47103b2010-05-24 14:33:00 -070023static inline void * __must_check ERR_PTR(long error)
Linus Torvalds1da177e2005-04-16 15:20:36 -070024{
25 return (void *) error;
26}
27
Dan Carpentere7152b92013-07-03 15:04:54 -070028static inline long __must_check PTR_ERR(__force const void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070029{
30 return (long) ptr;
31}
32
Joe Perchesa5ed3ce2014-04-03 14:48:31 -070033static inline bool __must_check IS_ERR(__force const void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070034{
Linus Torvalds07ab67c2005-05-19 22:43:37 -070035 return IS_ERR_VALUE((unsigned long)ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070036}
37
Joe Perchesa5ed3ce2014-04-03 14:48:31 -070038static inline bool __must_check IS_ERR_OR_NULL(__force const void *ptr)
Phil Carmody603c4ba2009-12-14 18:00:29 -080039{
Viresh Kumardfffa582016-01-15 16:58:10 -080040 return unlikely(!ptr) || IS_ERR_VALUE((unsigned long)ptr);
Phil Carmody603c4ba2009-12-14 18:00:29 -080041}
42
David Howellsd1bc8e92008-02-07 00:15:26 -080043/**
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 Carpentere7152b92013-07-03 15:04:54 -070050static inline void * __must_check ERR_CAST(__force const void *ptr)
David Howellsd1bc8e92008-02-07 00:15:26 -080051{
52 /* cast away the const */
53 return (void *) ptr;
54}
55
Rusty Russell6e8b8722013-07-15 11:19:32 +093056static inline int __must_check PTR_ERR_OR_ZERO(__force const void *ptr)
Uwe Kleine-Königfa9ee9c2011-03-22 16:34:05 -070057{
58 if (IS_ERR(ptr))
59 return PTR_ERR(ptr);
60 else
61 return 0;
62}
63
Rusty Russell6e8b8722013-07-15 11:19:32 +093064/* Deprecated */
65#define PTR_RET(p) PTR_ERR_OR_ZERO(p)
66
Randy Dunlapebba5f92006-09-27 01:50:55 -070067#endif
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#endif /* _LINUX_ERR_H */