blob: 87be24350e91c2788fd68a06dce1da3499d3fc0d [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002#ifndef _LINUX_ERR_H
3#define _LINUX_ERR_H
4
5#include <linux/compiler.h>
Joe Perchesa5ed3ce2014-04-03 14:48:31 -07006#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007
8#include <asm/errno.h>
9
10/*
11 * Kernel pointers have redundant information, so we can use a
Joe Perchesa5ed3ce2014-04-03 14:48:31 -070012 * scheme where we can return either an error code or a normal
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * 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 Baechlefa798372006-07-01 04:36:25 -070018#define MAX_ERRNO 4095
19
Randy Dunlapebba5f92006-09-27 01:50:55 -070020#ifndef __ASSEMBLY__
21
Linus Torvaldsaa00edc2016-05-27 16:03:22 -070022#define IS_ERR_VALUE(x) unlikely((unsigned long)(void *)(x) >= (unsigned long)-MAX_ERRNO)
Linus Torvalds07ab67c2005-05-19 22:43:37 -070023
Jani Nikulae47103b2010-05-24 14:33:00 -070024static inline void * __must_check ERR_PTR(long error)
Linus Torvalds1da177e2005-04-16 15:20:36 -070025{
26 return (void *) error;
27}
28
Dan Carpentere7152b92013-07-03 15:04:54 -070029static inline long __must_check PTR_ERR(__force const void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070030{
31 return (long) ptr;
32}
33
Joe Perchesa5ed3ce2014-04-03 14:48:31 -070034static inline bool __must_check IS_ERR(__force const void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070035{
Linus Torvalds07ab67c2005-05-19 22:43:37 -070036 return IS_ERR_VALUE((unsigned long)ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037}
38
Joe Perchesa5ed3ce2014-04-03 14:48:31 -070039static inline bool __must_check IS_ERR_OR_NULL(__force const void *ptr)
Phil Carmody603c4ba2009-12-14 18:00:29 -080040{
Viresh Kumardfffa582016-01-15 16:58:10 -080041 return unlikely(!ptr) || IS_ERR_VALUE((unsigned long)ptr);
Phil Carmody603c4ba2009-12-14 18:00:29 -080042}
43
David Howellsd1bc8e92008-02-07 00:15:26 -080044/**
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 Carpentere7152b92013-07-03 15:04:54 -070051static inline void * __must_check ERR_CAST(__force const void *ptr)
David Howellsd1bc8e92008-02-07 00:15:26 -080052{
53 /* cast away the const */
54 return (void *) ptr;
55}
56
Rusty Russell6e8b8722013-07-15 11:19:32 +093057static inline int __must_check PTR_ERR_OR_ZERO(__force const void *ptr)
Uwe Kleine-Königfa9ee9c2011-03-22 16:34:05 -070058{
59 if (IS_ERR(ptr))
60 return PTR_ERR(ptr);
61 else
62 return 0;
63}
64
Rusty Russell6e8b8722013-07-15 11:19:32 +093065/* Deprecated */
66#define PTR_RET(p) PTR_ERR_OR_ZERO(p)
67
Randy Dunlapebba5f92006-09-27 01:50:55 -070068#endif
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#endif /* _LINUX_ERR_H */