err.h 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _LINUX_ERR_H
  3. #define _LINUX_ERR_H
  4. #include <linux/compiler.h>
  5. #include <linux/types.h>
  6. #include <asm/errno.h>
  7. /*
  8. * Kernel pointers have redundant information, so we can use a
  9. * scheme where we can return either an error code or a normal
  10. * pointer with the same return value.
  11. *
  12. * This should be a per-architecture thing, to allow different
  13. * error and pointer decisions.
  14. */
  15. #define MAX_ERRNO 4095
  16. #ifndef __ASSEMBLY__
  17. #define IS_ERR_VALUE(x) unlikely((unsigned long)(void *)(x) >= (unsigned long)-MAX_ERRNO)
  18. static inline void * __must_check ERR_PTR(long error)
  19. {
  20. return (void *) error;
  21. }
  22. static inline long __must_check PTR_ERR(__force const void *ptr)
  23. {
  24. return (long) ptr;
  25. }
  26. static inline bool __must_check IS_ERR(__force const void *ptr)
  27. {
  28. return IS_ERR_VALUE((unsigned long)ptr);
  29. }
  30. static inline bool __must_check IS_ERR_OR_NULL(__force const void *ptr)
  31. {
  32. return unlikely(!ptr) || IS_ERR_VALUE((unsigned long)ptr);
  33. }
  34. /**
  35. * ERR_CAST - Explicitly cast an error-valued pointer to another pointer type
  36. * @ptr: The pointer to cast.
  37. *
  38. * Explicitly cast an error-valued pointer to another pointer type in such a
  39. * way as to make it clear that's what's going on.
  40. */
  41. static inline void * __must_check ERR_CAST(__force const void *ptr)
  42. {
  43. /* cast away the const */
  44. return (void *) ptr;
  45. }
  46. static inline int __must_check PTR_ERR_OR_ZERO(__force const void *ptr)
  47. {
  48. if (IS_ERR(ptr))
  49. return PTR_ERR(ptr);
  50. else
  51. return 0;
  52. }
  53. #endif
  54. #endif /* _LINUX_ERR_H */