Whamcloud - gitweb
LU-4699 libcfs: unify ERR_PTR definitions
[fs/lustre-release.git] / libcfs / include / libcfs / err.h
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2014, Intel Corporation.
24  * Author: John L. Hammond <john.hammond@intel.com>
25  */
26 #ifndef LIBCFS_ERR_H_
27 #define LIBCFS_ERR_H_
28
29 #if defined(__linux__) && defined(__KERNEL__)
30 # include <linux/err.h>
31 #else /* __KERNEL__ */
32
33 # define IS_ERR_VALUE(x) ((x) >= (unsigned long)-4095)
34
35 static inline void *ERR_PTR(long error)
36 {
37         return (void *)error;
38 }
39
40 static inline long PTR_ERR(const void *ptr)
41 {
42         return (long)ptr;
43 }
44
45 static inline long IS_ERR(const void *ptr)
46 {
47         return IS_ERR_VALUE((unsigned long)ptr);
48 }
49
50 static inline long IS_ERR_OR_NULL(const void *ptr)
51 {
52         return IS_ERR_VALUE((unsigned long)ptr) || ptr == NULL;
53 }
54
55 /**
56  * ERR_CAST - Explicitly cast an error-valued pointer to another pointer type
57  * @ptr: The pointer to cast.
58  *
59  * Explicitly cast an error-valued pointer to another pointer type in such a
60  * way as to make it clear that's what's going on.
61  */
62 static inline void *ERR_CAST(const void *ptr)
63 {
64         /* Cast away the const. */
65         return (void *)ptr;
66 }
67
68 # endif /* !__KERNEL__ */
69
70 #endif /* LIBCFS_ERR_H_ */