err.h 991 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #pragma once
  2. #include <common/compiler.h>
  3. #include <stdint.h>
  4. #define MAX_ERRNO 4095
  5. #define IS_ERR_VALUE(x) unlikely((x) >= (uint64_t)-MAX_ERRNO)
  6. /**
  7. * @brief 判断返回的指针是否为errno
  8. *
  9. * @param ptr 待校验的指针
  10. * @return long 1 => 是错误码
  11. * 0 => 不是错误码
  12. */
  13. static inline long __must_check IS_ERR(const void* ptr)
  14. {
  15. return IS_ERR_VALUE((uint64_t)ptr);
  16. }
  17. /**
  18. * @brief 判断返回的指针是否为errno或者为空
  19. *
  20. * @param ptr 待校验的指针
  21. * @return long 1 => 是错误码或NULL
  22. * 0 => 不是错误码或NULL
  23. */
  24. static inline long __must_check IS_ERR_OR_NULL(const void* ptr)
  25. {
  26. return !ptr || IS_ERR_VALUE((uint64_t)ptr);
  27. }
  28. /**
  29. * @brief 将错误码转换为指针
  30. *
  31. * @param error 错误码
  32. * @return void* 转换后的指针
  33. */
  34. static inline void* __must_check ERR_PTR(long error)
  35. {
  36. return (void*)(error);
  37. }
  38. static inline long __must_check PTR_ERR(void * ptr)
  39. {
  40. return (long)ptr;
  41. }