err.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _LINUX_ERR_H
  3. #define _LINUX_ERR_H
  4. #include "compiler.h"
  5. #include "../types.h"
  6. #include "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. /**
  18. * IS_ERR_VALUE - Detect an error pointer.
  19. * @x: The pointer to check.
  20. *
  21. * Like IS_ERR(), but does not generate a compiler warning if result is unused.
  22. */
  23. #define IS_ERR_VALUE(x) unlikely((unsigned long)(void *)(x) >= (unsigned long)-MAX_ERRNO)
  24. /**
  25. * ERR_PTR - Create an error pointer.
  26. * @error: A negative error code.
  27. *
  28. * Encodes @error into a pointer value. Users should consider the result
  29. * opaque and not assume anything about how the error is encoded.
  30. *
  31. * Return: A pointer with @error encoded within its value.
  32. */
  33. static inline void * __must_check ERR_PTR(long error)
  34. {
  35. return (void *) error;
  36. }
  37. /**
  38. * PTR_ERR - Extract the error code from an error pointer.
  39. * @ptr: An error pointer.
  40. * Return: The error code within @ptr.
  41. */
  42. static inline long __must_check PTR_ERR(__force const void *ptr)
  43. {
  44. return (long) ptr;
  45. }
  46. /**
  47. * IS_ERR - Detect an error pointer.
  48. * @ptr: The pointer to check.
  49. * Return: true if @ptr is an error pointer, false otherwise.
  50. */
  51. static inline bool __must_check IS_ERR(__force const void *ptr)
  52. {
  53. return IS_ERR_VALUE((unsigned long)ptr);
  54. }
  55. /**
  56. * IS_ERR_OR_NULL - Detect an error pointer or a null pointer.
  57. * @ptr: The pointer to check.
  58. *
  59. * Like IS_ERR(), but also returns true for a null pointer.
  60. */
  61. static inline bool __must_check IS_ERR_OR_NULL(__force const void *ptr)
  62. {
  63. return unlikely(!ptr) || IS_ERR_VALUE((unsigned long)ptr);
  64. }
  65. /**
  66. * ERR_CAST - Explicitly cast an error-valued pointer to another pointer type
  67. * @ptr: The pointer to cast.
  68. *
  69. * Explicitly cast an error-valued pointer to another pointer type in such a
  70. * way as to make it clear that's what's going on.
  71. */
  72. static inline void * __must_check ERR_CAST(__force const void *ptr)
  73. {
  74. /* cast away the const */
  75. return (void *) ptr;
  76. }
  77. /**
  78. * PTR_ERR_OR_ZERO - Extract the error code from a pointer if it has one.
  79. * @ptr: A potential error pointer.
  80. *
  81. * Convenience function that can be used inside a function that returns
  82. * an error code to propagate errors received as error pointers.
  83. * For example, ``return PTR_ERR_OR_ZERO(ptr);`` replaces:
  84. *
  85. * .. code-block:: c
  86. *
  87. * if (IS_ERR(ptr))
  88. * return PTR_ERR(ptr);
  89. * else
  90. * return 0;
  91. *
  92. * Return: The error code within @ptr if it is an error pointer; 0 otherwise.
  93. */
  94. static inline int __must_check PTR_ERR_OR_ZERO(__force const void *ptr)
  95. {
  96. if (IS_ERR(ptr))
  97. return PTR_ERR(ptr);
  98. else
  99. return 0;
  100. }
  101. #endif
  102. #endif /* _LINUX_ERR_H */