debugerror.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. * or its jobs.
  26. */
  27. #ifndef BADVPN_MISC_DEBUGERROR_H
  28. #define BADVPN_MISC_DEBUGERROR_H
  29. #include <misc/debug.h>
  30. #include <system/BPending.h>
  31. #ifndef NDEBUG
  32. #define DEBUGERROR(de, call) \
  33. { \
  34. ASSERT(!BPending_IsSet(&(de)->job)) \
  35. BPending_Set(&(de)->job); \
  36. (call); \
  37. }
  38. #else
  39. #define DEBUGERROR(de, call) { (call); }
  40. #endif
  41. typedef struct {
  42. #ifndef NDEBUG
  43. BPending job;
  44. #endif
  45. } DebugError;
  46. static void DebugError_Init (DebugError *o, BPendingGroup *pg);
  47. static void DebugError_Free (DebugError *o);
  48. static void DebugError_AssertNoError (DebugError *o);
  49. #ifndef NDEBUG
  50. static void _DebugError_job_handler (DebugError *o)
  51. {
  52. ASSERT(0);
  53. }
  54. #endif
  55. void DebugError_Init (DebugError *o, BPendingGroup *pg)
  56. {
  57. #ifndef NDEBUG
  58. BPending_Init(&o->job, pg, (BPending_handler)_DebugError_job_handler, o);
  59. #endif
  60. }
  61. void DebugError_Free (DebugError *o)
  62. {
  63. #ifndef NDEBUG
  64. BPending_Free(&o->job);
  65. #endif
  66. }
  67. void DebugError_AssertNoError (DebugError *o)
  68. {
  69. #ifndef NDEBUG
  70. ASSERT(!BPending_IsSet(&o->job))
  71. #endif
  72. }
  73. #endif