err.h 1.5 KB

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