err.h 1.5 KB

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