xref: /src/sys/compat/linuxkpi/common/include/linux/err.h (revision 95ee2897e98f5d444f26ed2334cc7c439f9c16c6)
1aa0a1e58SJeff Roberson /*-
2aa0a1e58SJeff Roberson  * Copyright (c) 2010 Isilon Systems, Inc.
3aa0a1e58SJeff Roberson  * Copyright (c) 2010 iX Systems, Inc.
4aa0a1e58SJeff Roberson  * Copyright (c) 2010 Panasas, Inc.
5677a229cSHans Petter Selasky  * Copyright (c) 2013-2016 Mellanox Technologies, Ltd.
6aa0a1e58SJeff Roberson  * All rights reserved.
7aa0a1e58SJeff Roberson  *
8aa0a1e58SJeff Roberson  * Redistribution and use in source and binary forms, with or without
9aa0a1e58SJeff Roberson  * modification, are permitted provided that the following conditions
10aa0a1e58SJeff Roberson  * are met:
11aa0a1e58SJeff Roberson  * 1. Redistributions of source code must retain the above copyright
12aa0a1e58SJeff Roberson  *    notice unmodified, this list of conditions, and the following
13aa0a1e58SJeff Roberson  *    disclaimer.
14aa0a1e58SJeff Roberson  * 2. Redistributions in binary form must reproduce the above copyright
15aa0a1e58SJeff Roberson  *    notice, this list of conditions and the following disclaimer in the
16aa0a1e58SJeff Roberson  *    documentation and/or other materials provided with the distribution.
17aa0a1e58SJeff Roberson  *
18aa0a1e58SJeff Roberson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19aa0a1e58SJeff Roberson  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20aa0a1e58SJeff Roberson  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21aa0a1e58SJeff Roberson  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22aa0a1e58SJeff Roberson  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23aa0a1e58SJeff Roberson  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24aa0a1e58SJeff Roberson  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25aa0a1e58SJeff Roberson  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26aa0a1e58SJeff Roberson  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27aa0a1e58SJeff Roberson  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28aa0a1e58SJeff Roberson  */
29307f78f3SVladimir Kondratyev #ifndef	_LINUXKPI_LINUX_ERR_H_
30307f78f3SVladimir Kondratyev #define	_LINUXKPI_LINUX_ERR_H_
31aa0a1e58SJeff Roberson 
3247c0672bSHans Petter Selasky #include <sys/types.h>
3347c0672bSHans Petter Selasky 
34677a229cSHans Petter Selasky #include <linux/compiler.h>
35677a229cSHans Petter Selasky 
36aa0a1e58SJeff Roberson #define MAX_ERRNO	4095
37aa0a1e58SJeff Roberson 
38de66c9a1SJohn Baldwin #define IS_ERR_VALUE(x) unlikely((x) >= (uintptr_t)-MAX_ERRNO)
39aa0a1e58SJeff Roberson 
40aa0a1e58SJeff Roberson static inline void *
ERR_PTR(long error)41aa0a1e58SJeff Roberson ERR_PTR(long error)
42aa0a1e58SJeff Roberson {
43de66c9a1SJohn Baldwin 	return (void *)(intptr_t)error;
44aa0a1e58SJeff Roberson }
45aa0a1e58SJeff Roberson 
46aa0a1e58SJeff Roberson static inline long
PTR_ERR(const void * ptr)47aa0a1e58SJeff Roberson PTR_ERR(const void *ptr)
48aa0a1e58SJeff Roberson {
49de66c9a1SJohn Baldwin 	return (intptr_t)ptr;
50aa0a1e58SJeff Roberson }
51aa0a1e58SJeff Roberson 
52de66c9a1SJohn Baldwin static inline bool
IS_ERR(const void * ptr)53aa0a1e58SJeff Roberson IS_ERR(const void *ptr)
54aa0a1e58SJeff Roberson {
55de66c9a1SJohn Baldwin 	return IS_ERR_VALUE((uintptr_t)ptr);
56aa0a1e58SJeff Roberson }
57aa0a1e58SJeff Roberson 
58de66c9a1SJohn Baldwin static inline bool
IS_ERR_OR_NULL(const void * ptr)59af5648c4SHans Petter Selasky IS_ERR_OR_NULL(const void *ptr)
60af5648c4SHans Petter Selasky {
61de66c9a1SJohn Baldwin 	return !ptr || IS_ERR_VALUE((uintptr_t)ptr);
62af5648c4SHans Petter Selasky }
63af5648c4SHans Petter Selasky 
64aa0a1e58SJeff Roberson static inline void *
ERR_CAST(const void * ptr)6547c0672bSHans Petter Selasky ERR_CAST(const void *ptr)
66aa0a1e58SJeff Roberson {
6747c0672bSHans Petter Selasky 	return __DECONST(void *, ptr);
68aa0a1e58SJeff Roberson }
69aa0a1e58SJeff Roberson 
70c7818b48SHans Petter Selasky static inline int
PTR_ERR_OR_ZERO(const void * ptr)71c7818b48SHans Petter Selasky PTR_ERR_OR_ZERO(const void *ptr)
72c7818b48SHans Petter Selasky {
73c7818b48SHans Petter Selasky 	if (IS_ERR(ptr))
74c7818b48SHans Petter Selasky 		return PTR_ERR(ptr);
75c7818b48SHans Petter Selasky 	else
76c7818b48SHans Petter Selasky 		return 0;
77c7818b48SHans Petter Selasky }
78c7818b48SHans Petter Selasky 
79c7818b48SHans Petter Selasky #define PTR_RET(p) PTR_ERR_OR_ZERO(p)
80c7818b48SHans Petter Selasky 
81307f78f3SVladimir Kondratyev #endif	/* _LINUXKPI_LINUX_ERR_H_ */
82