err.h 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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(const void *ptr)
  21. {
  22. return (long) ptr;
  23. }
  24. static inline long __must_check IS_ERR(const void *ptr)
  25. {
  26. return IS_ERR_VALUE((unsigned long)ptr);
  27. }
  28. static inline long __must_check IS_ERR_OR_NULL(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(const void *ptr)
  40. {
  41. /* cast away the const */
  42. return (void *) ptr;
  43. }
  44. #endif
  45. #endif /* _LINUX_ERR_H */