fassert.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * Copyright : (C) 2022 Phytium Information Technology, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is OPEN SOURCE software: you can redistribute it and/or modify it
  6. * under the terms of the Phytium Public License as published by the Phytium Technology Co.,Ltd,
  7. * either version 1.0 of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY;
  10. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. * See the Phytium Public License for more details.
  12. *
  13. *
  14. * FilePath: fassert.h
  15. * Date: 2021-04-07 09:53:07
  16. * LastEditTime: 2022-02-17 18:04:35
  17. * Description:  This file is for assertion defintion.
  18. *
  19. * Modify History:
  20. * Ver   Who        Date         Changes
  21. * ----- ------     --------    --------------------------------------
  22. * 1.0 huanghe 2021/4/5 init commit
  23. * 1.1 zhugengyu 2022/3/7 re-define assert macro
  24. */
  25. #ifndef FASSERT_H
  26. #define FASSERT_H
  27. /***************************** Include Files *********************************/
  28. #include "fprintk.h"
  29. #include "ferror_code.h"
  30. #include "ftypes.h"
  31. #ifdef __cplusplus
  32. extern "C"
  33. {
  34. #endif
  35. /**************************** Type Definitions *******************************/
  36. typedef enum
  37. {
  38. FASSERT_NONE = 0,
  39. FASSERT_OCCURRED
  40. } FAssertStatus; /* 断言状态 */
  41. /* 断言处理回调函数 */
  42. typedef void (*FAssertCB)(const char *file, s32 line, int ret);
  43. /************************** Variable Definitions *****************************/
  44. /***************** Macros (Inline Functions) Definitions *********************/
  45. #define FASSERT_MSG(expression, fmt, ...) \
  46. { \
  47. if (expression) \
  48. { \
  49. FAssertSetStatus(FASSERT_NONE); \
  50. } \
  51. else \
  52. { \
  53. FAssertSetStatus(FASSERT_OCCURRED); \
  54. f_printk(fmt, ##__VA_ARGS__); \
  55. FAssert(__FILE__, __LINE__, 0xff); \
  56. } \
  57. }
  58. #define FASSERT(expression)\
  59. { \
  60. if (expression) \
  61. { \
  62. FAssertSetStatus(FASSERT_NONE); \
  63. } \
  64. else \
  65. { \
  66. FAssertSetStatus(FASSERT_OCCURRED); \
  67. FAssert(__FILE__, __LINE__, 0xff); \
  68. } \
  69. }
  70. /* 检查静态断言状态 */
  71. #define FASSERT_STATIC(expression) \
  72. extern int assert_static[(expression) ? 1 : -1]
  73. /************************** Function Prototypes ******************************/
  74. /* 设置断言状态 */
  75. void FAssertSetStatus(FAssertStatus status);
  76. /* 获取当前断言状态 */
  77. FAssertStatus FAssertGetStatus(void);
  78. /* 设置断言回调函数 */
  79. void FAssertSetCB(FAssertCB cb);
  80. /* 断言实现 */
  81. void FAssert(const char *file, s32 line, int code);
  82. #ifdef __cplusplus
  83. }
  84. #endif
  85. #endif // !