err.h 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #ifndef _LINUX_ERR_H
  2. #define _LINUX_ERR_H
  3. #include <linux/compiler.h>
  4. #include <linux/compat.h>
  5. #include <linux/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 dentry
  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 *ERR_PTR(long error)
  18. {
  19. return (void *)(CONFIG_ERR_PTR_OFFSET + error);
  20. }
  21. static inline long PTR_ERR(const void *ptr)
  22. {
  23. return ((long)ptr - CONFIG_ERR_PTR_OFFSET);
  24. }
  25. static inline long IS_ERR(const void *ptr)
  26. {
  27. return IS_ERR_VALUE((unsigned long)PTR_ERR(ptr));
  28. }
  29. static inline bool IS_ERR_OR_NULL(const void *ptr)
  30. {
  31. return !ptr || IS_ERR_VALUE((unsigned long)PTR_ERR(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. #endif
  46. #endif /* _LINUX_ERR_H */