DebugObject.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /**
  2. * @file DebugObject.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. * Object used for detecting leaks.
  25. */
  26. #ifndef BADVPN_DEBUGOBJECT_H
  27. #define BADVPN_DEBUGOBJECT_H
  28. #include <stdint.h>
  29. #ifdef BADVPN_THREADWORK_USE_PTHREAD
  30. #include <pthread.h>
  31. #endif
  32. #include <misc/debug.h>
  33. #include <misc/debugcounter.h>
  34. #define DEBUGOBJECT_VALID UINT32_C(0x31415926)
  35. /**
  36. * Object used for detecting leaks.
  37. */
  38. typedef struct {
  39. #ifndef NDEBUG
  40. uint32_t c;
  41. #endif
  42. } DebugObject;
  43. /**
  44. * Initializes the object.
  45. *
  46. * @param obj the object
  47. */
  48. static void DebugObject_Init (DebugObject *obj);
  49. /**
  50. * Frees the object.
  51. *
  52. * @param obj the object
  53. */
  54. static void DebugObject_Free (DebugObject *obj);
  55. /**
  56. * Does nothing.
  57. *
  58. * @param obj the object
  59. */
  60. static void DebugObject_Access (const DebugObject *obj);
  61. /**
  62. * Does nothing.
  63. * There must be no {@link DebugObject}'s initialized.
  64. */
  65. static void DebugObjectGlobal_Finish (void);
  66. extern DebugCounter debugobject_counter;
  67. #ifdef BADVPN_THREADWORK_USE_PTHREAD
  68. extern pthread_mutex_t debugobject_mutex;
  69. #endif
  70. void DebugObject_Init (DebugObject *obj)
  71. {
  72. #ifndef NDEBUG
  73. obj->c = DEBUGOBJECT_VALID;
  74. #ifdef BADVPN_THREADWORK_USE_PTHREAD
  75. ASSERT_FORCE(pthread_mutex_lock(&debugobject_mutex) == 0)
  76. #endif
  77. DebugCounter_Increment(&debugobject_counter);
  78. #ifdef BADVPN_THREADWORK_USE_PTHREAD
  79. ASSERT_FORCE(pthread_mutex_unlock(&debugobject_mutex) == 0)
  80. #endif
  81. #endif
  82. }
  83. void DebugObject_Free (DebugObject *obj)
  84. {
  85. ASSERT(obj->c == DEBUGOBJECT_VALID)
  86. #ifndef NDEBUG
  87. obj->c = 0;
  88. #ifdef BADVPN_THREADWORK_USE_PTHREAD
  89. ASSERT_FORCE(pthread_mutex_lock(&debugobject_mutex) == 0)
  90. #endif
  91. DebugCounter_Decrement(&debugobject_counter);
  92. #ifdef BADVPN_THREADWORK_USE_PTHREAD
  93. ASSERT_FORCE(pthread_mutex_unlock(&debugobject_mutex) == 0)
  94. #endif
  95. #endif
  96. }
  97. void DebugObject_Access (const DebugObject *obj)
  98. {
  99. ASSERT(obj->c == DEBUGOBJECT_VALID)
  100. }
  101. void DebugObjectGlobal_Finish (void)
  102. {
  103. #ifndef NDEBUG
  104. DebugCounter_Free(&debugobject_counter);
  105. #endif
  106. }
  107. #endif