debugerror.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /**
  2. * @file debugerror.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. *
  22. * @section DESCRIPTION
  23. *
  24. * Mechanism for ensuring an object is destroyed from inside an error handler.
  25. */
  26. #ifndef BADVPN_MISC_DEBUGERROR_H
  27. #define BADVPN_MISC_DEBUGERROR_H
  28. #include <misc/dead.h>
  29. #include <misc/debug.h>
  30. #ifndef NDEBUG
  31. #define DEBUGERROR(de, call) \
  32. { \
  33. ASSERT(!(de)->error) \
  34. (de)->error = 1; \
  35. DEAD_ENTER((de)->dead) \
  36. (call); \
  37. ASSERT(DEAD_KILLED) \
  38. DEAD_LEAVE((de)->dead); \
  39. }
  40. #else
  41. #define DEBUGERROR(de, call) { (call); }
  42. #endif
  43. typedef struct {
  44. #ifndef NDEBUG
  45. dead_t dead;
  46. int error;
  47. #endif
  48. } DebugError;
  49. static void DebugError_Init (DebugError *o);
  50. static void DebugError_Free (DebugError *o);
  51. static void DebugError_AssertNoError (DebugError *o);
  52. void DebugError_Init (DebugError *o)
  53. {
  54. #ifndef NDEBUG
  55. DEAD_INIT(o->dead);
  56. o->error = 0;
  57. #endif
  58. }
  59. void DebugError_Free (DebugError *o)
  60. {
  61. #ifndef NDEBUG
  62. DEAD_KILL(o->dead);
  63. #endif
  64. }
  65. void DebugError_AssertNoError (DebugError *o)
  66. {
  67. #ifndef NDEBUG
  68. ASSERT(!o->error)
  69. #endif
  70. }
  71. #endif