xref: /kvmtool/include/linux/err.h (revision 443cd881b47126bde6f442abc1f6c76998d713b7)
17e841c88SAndre Przywara #ifndef _LINUX_ERR_H
27e841c88SAndre Przywara #define _LINUX_ERR_H
37e841c88SAndre Przywara 
4*443cd881SAlexandru Elisei #include <stdbool.h>
5*443cd881SAlexandru Elisei 
67e841c88SAndre Przywara #include <linux/compiler.h>
77e841c88SAndre Przywara #include <linux/types.h>
87e841c88SAndre Przywara 
97e841c88SAndre Przywara #include <asm/errno.h>
107e841c88SAndre Przywara 
117e841c88SAndre Przywara /*
127e841c88SAndre Przywara  * Kernel pointers have redundant information, so we can use a
137e841c88SAndre Przywara  * scheme where we can return either an error code or a normal
147e841c88SAndre Przywara  * pointer with the same return value.
157e841c88SAndre Przywara  *
167e841c88SAndre Przywara  * This should be a per-architecture thing, to allow different
177e841c88SAndre Przywara  * error and pointer decisions.
187e841c88SAndre Przywara  */
197e841c88SAndre Przywara #define MAX_ERRNO	4095
207e841c88SAndre Przywara 
217e841c88SAndre Przywara #ifndef __ASSEMBLY__
227e841c88SAndre Przywara 
237e841c88SAndre Przywara #define IS_ERR_VALUE(x) unlikely((x) >= (unsigned long)-MAX_ERRNO)
247e841c88SAndre Przywara 
ERR_PTR(long error)257e841c88SAndre Przywara static inline void * __must_check ERR_PTR(long error)
267e841c88SAndre Przywara {
277e841c88SAndre Przywara 	return (void *) error;
287e841c88SAndre Przywara }
297e841c88SAndre Przywara 
PTR_ERR(__force const void * ptr)307e841c88SAndre Przywara static inline long __must_check PTR_ERR(__force const void *ptr)
317e841c88SAndre Przywara {
327e841c88SAndre Przywara 	return (long) ptr;
337e841c88SAndre Przywara }
347e841c88SAndre Przywara 
IS_ERR(__force const void * ptr)357e841c88SAndre Przywara static inline bool __must_check IS_ERR(__force const void *ptr)
367e841c88SAndre Przywara {
377e841c88SAndre Przywara 	return IS_ERR_VALUE((unsigned long)ptr);
387e841c88SAndre Przywara }
397e841c88SAndre Przywara 
IS_ERR_OR_NULL(__force const void * ptr)407e841c88SAndre Przywara static inline bool __must_check IS_ERR_OR_NULL(__force const void *ptr)
417e841c88SAndre Przywara {
427e841c88SAndre Przywara 	return !ptr || IS_ERR_VALUE((unsigned long)ptr);
437e841c88SAndre Przywara }
447e841c88SAndre Przywara 
457e841c88SAndre Przywara /**
467e841c88SAndre Przywara  * ERR_CAST - Explicitly cast an error-valued pointer to another pointer type
477e841c88SAndre Przywara  * @ptr: The pointer to cast.
487e841c88SAndre Przywara  *
497e841c88SAndre Przywara  * Explicitly cast an error-valued pointer to another pointer type in such a
507e841c88SAndre Przywara  * way as to make it clear that's what's going on.
517e841c88SAndre Przywara  */
ERR_CAST(__force const void * ptr)527e841c88SAndre Przywara static inline void * __must_check ERR_CAST(__force const void *ptr)
537e841c88SAndre Przywara {
547e841c88SAndre Przywara 	/* cast away the const */
557e841c88SAndre Przywara 	return (void *) ptr;
567e841c88SAndre Przywara }
577e841c88SAndre Przywara 
PTR_ERR_OR_ZERO(__force const void * ptr)587e841c88SAndre Przywara static inline int __must_check PTR_ERR_OR_ZERO(__force const void *ptr)
597e841c88SAndre Przywara {
607e841c88SAndre Przywara 	if (IS_ERR(ptr))
617e841c88SAndre Przywara 		return PTR_ERR(ptr);
627e841c88SAndre Przywara 	else
637e841c88SAndre Przywara 		return 0;
647e841c88SAndre Przywara }
657e841c88SAndre Przywara 
667e841c88SAndre Przywara /* Deprecated */
677e841c88SAndre Przywara #define PTR_RET(p) PTR_ERR_OR_ZERO(p)
687e841c88SAndre Przywara 
697e841c88SAndre Przywara #endif
707e841c88SAndre Przywara 
717e841c88SAndre Przywara #endif /* _LINUX_ERR_H */
72