Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef _LINUX_ERR_H
0003 #define _LINUX_ERR_H
0004 
0005 #include <linux/compiler.h>
0006 #include <linux/types.h>
0007 
0008 #include <asm/errno.h>
0009 
0010 /*
0011  * Kernel pointers have redundant information, so we can use a
0012  * scheme where we can return either an error code or a normal
0013  * pointer with the same return value.
0014  *
0015  * This should be a per-architecture thing, to allow different
0016  * error and pointer decisions.
0017  */
0018 #define MAX_ERRNO   4095
0019 
0020 #ifndef __ASSEMBLY__
0021 
0022 #define IS_ERR_VALUE(x) unlikely((unsigned long)(void *)(x) >= (unsigned long)-MAX_ERRNO)
0023 
0024 static inline void * __must_check ERR_PTR(long error)
0025 {
0026     return (void *) error;
0027 }
0028 
0029 static inline long __must_check PTR_ERR(__force const void *ptr)
0030 {
0031     return (long) ptr;
0032 }
0033 
0034 static inline bool __must_check IS_ERR(__force const void *ptr)
0035 {
0036     return IS_ERR_VALUE((unsigned long)ptr);
0037 }
0038 
0039 static inline bool __must_check IS_ERR_OR_NULL(__force const void *ptr)
0040 {
0041     return unlikely(!ptr) || IS_ERR_VALUE((unsigned long)ptr);
0042 }
0043 
0044 /**
0045  * ERR_CAST - Explicitly cast an error-valued pointer to another pointer type
0046  * @ptr: The pointer to cast.
0047  *
0048  * Explicitly cast an error-valued pointer to another pointer type in such a
0049  * way as to make it clear that's what's going on.
0050  */
0051 static inline void * __must_check ERR_CAST(__force const void *ptr)
0052 {
0053     /* cast away the const */
0054     return (void *) ptr;
0055 }
0056 
0057 static inline int __must_check PTR_ERR_OR_ZERO(__force const void *ptr)
0058 {
0059     if (IS_ERR(ptr))
0060         return PTR_ERR(ptr);
0061     else
0062         return 0;
0063 }
0064 
0065 #endif
0066 
0067 #endif /* _LINUX_ERR_H */