Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef __TOOLS_LINUX_ERR_H
0003 #define __TOOLS_LINUX_ERR_H
0004 
0005 #include <linux/compiler.h>
0006 #include <linux/types.h>
0007 
0008 #include <asm/errno.h>
0009 
0010 /*
0011  * Original kernel header comment:
0012  *
0013  * Kernel pointers have redundant information, so we can use a
0014  * scheme where we can return either an error code or a normal
0015  * pointer with the same return value.
0016  *
0017  * This should be a per-architecture thing, to allow different
0018  * error and pointer decisions.
0019  *
0020  * Userspace note:
0021  * The same principle works for userspace, because 'error' pointers
0022  * fall down to the unused hole far from user space, as described
0023  * in Documentation/x86/x86_64/mm.rst for x86_64 arch:
0024  *
0025  * 0000000000000000 - 00007fffffffffff (=47 bits) user space, different per mm hole caused by [48:63] sign extension
0026  * ffffffffffe00000 - ffffffffffffffff (=2 MB) unused hole
0027  *
0028  * It should be the same case for other architectures, because
0029  * this code is used in generic kernel code.
0030  */
0031 #define MAX_ERRNO   4095
0032 
0033 #define IS_ERR_VALUE(x) unlikely((x) >= (unsigned long)-MAX_ERRNO)
0034 
0035 static inline void * __must_check ERR_PTR(long error_)
0036 {
0037     return (void *) error_;
0038 }
0039 
0040 static inline long __must_check PTR_ERR(__force const void *ptr)
0041 {
0042     return (long) ptr;
0043 }
0044 
0045 static inline bool __must_check IS_ERR(__force const void *ptr)
0046 {
0047     return IS_ERR_VALUE((unsigned long)ptr);
0048 }
0049 
0050 static inline bool __must_check IS_ERR_OR_NULL(__force const void *ptr)
0051 {
0052     return unlikely(!ptr) || IS_ERR_VALUE((unsigned long)ptr);
0053 }
0054 
0055 static inline int __must_check PTR_ERR_OR_ZERO(__force const void *ptr)
0056 {
0057     if (IS_ERR(ptr))
0058         return PTR_ERR(ptr);
0059     else
0060         return 0;
0061 }
0062 
0063 /**
0064  * ERR_CAST - Explicitly cast an error-valued pointer to another pointer type
0065  * @ptr: The pointer to cast.
0066  *
0067  * Explicitly cast an error-valued pointer to another pointer type in such a
0068  * way as to make it clear that's what's going on.
0069  */
0070 static inline void * __must_check ERR_CAST(__force const void *ptr)
0071 {
0072     /* cast away the const */
0073     return (void *) ptr;
0074 }
0075 #endif /* _LINUX_ERR_H */