DebugObject.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /**
  2. * @file DebugObject.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * @section DESCRIPTION
  30. *
  31. * Object used for detecting leaks.
  32. */
  33. #ifndef BADVPN_DEBUGOBJECT_H
  34. #define BADVPN_DEBUGOBJECT_H
  35. #include <stdint.h>
  36. #ifdef BADVPN_THREADWORK_USE_PTHREAD
  37. #include <pthread.h>
  38. #endif
  39. #include <misc/debug.h>
  40. #include <misc/debugcounter.h>
  41. #define DEBUGOBJECT_VALID UINT32_C(0x31415926)
  42. /**
  43. * Object used for detecting leaks.
  44. */
  45. typedef struct {
  46. #ifndef NDEBUG
  47. uint32_t c;
  48. #endif
  49. } DebugObject;
  50. /**
  51. * Initializes the object.
  52. *
  53. * @param obj the object
  54. */
  55. static void DebugObject_Init (DebugObject *obj);
  56. /**
  57. * Frees the object.
  58. *
  59. * @param obj the object
  60. */
  61. static void DebugObject_Free (DebugObject *obj);
  62. /**
  63. * Does nothing.
  64. *
  65. * @param obj the object
  66. */
  67. static void DebugObject_Access (const DebugObject *obj);
  68. /**
  69. * Does nothing.
  70. * There must be no {@link DebugObject}'s initialized.
  71. */
  72. static void DebugObjectGlobal_Finish (void);
  73. extern DebugCounter debugobject_counter;
  74. #ifdef BADVPN_THREADWORK_USE_PTHREAD
  75. extern pthread_mutex_t debugobject_mutex;
  76. #endif
  77. void DebugObject_Init (DebugObject *obj)
  78. {
  79. #ifndef NDEBUG
  80. obj->c = DEBUGOBJECT_VALID;
  81. #ifdef BADVPN_THREADWORK_USE_PTHREAD
  82. ASSERT_FORCE(pthread_mutex_lock(&debugobject_mutex) == 0)
  83. #endif
  84. DebugCounter_Increment(&debugobject_counter);
  85. #ifdef BADVPN_THREADWORK_USE_PTHREAD
  86. ASSERT_FORCE(pthread_mutex_unlock(&debugobject_mutex) == 0)
  87. #endif
  88. #endif
  89. }
  90. void DebugObject_Free (DebugObject *obj)
  91. {
  92. ASSERT(obj->c == DEBUGOBJECT_VALID)
  93. #ifndef NDEBUG
  94. obj->c = 0;
  95. #ifdef BADVPN_THREADWORK_USE_PTHREAD
  96. ASSERT_FORCE(pthread_mutex_lock(&debugobject_mutex) == 0)
  97. #endif
  98. DebugCounter_Decrement(&debugobject_counter);
  99. #ifdef BADVPN_THREADWORK_USE_PTHREAD
  100. ASSERT_FORCE(pthread_mutex_unlock(&debugobject_mutex) == 0)
  101. #endif
  102. #endif
  103. }
  104. void DebugObject_Access (const DebugObject *obj)
  105. {
  106. ASSERT(obj->c == DEBUGOBJECT_VALID)
  107. }
  108. void DebugObjectGlobal_Finish (void)
  109. {
  110. #ifndef NDEBUG
  111. DebugCounter_Free(&debugobject_counter);
  112. #endif
  113. }
  114. #endif