DebugObject.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. #if !defined(BADVPN_THREAD_SAFE) || (BADVPN_THREAD_SAFE != 0 && BADVPN_THREAD_SAFE != 1)
  37. #error BADVPN_THREAD_SAFE is not defined or incorrect
  38. #endif
  39. #if BADVPN_THREAD_SAFE
  40. #include <pthread.h>
  41. #endif
  42. #include <misc/debug.h>
  43. #include <misc/debugcounter.h>
  44. #define DEBUGOBJECT_VALID UINT32_C(0x31415926)
  45. /**
  46. * Object used for detecting leaks.
  47. */
  48. typedef struct {
  49. #ifndef NDEBUG
  50. uint32_t c;
  51. #endif
  52. } DebugObject;
  53. /**
  54. * Initializes the object.
  55. *
  56. * @param obj the object
  57. */
  58. static void DebugObject_Init (DebugObject *obj);
  59. /**
  60. * Frees the object.
  61. *
  62. * @param obj the object
  63. */
  64. static void DebugObject_Free (DebugObject *obj);
  65. /**
  66. * Does nothing.
  67. *
  68. * @param obj the object
  69. */
  70. static void DebugObject_Access (const DebugObject *obj);
  71. /**
  72. * Does nothing.
  73. * There must be no {@link DebugObject}'s initialized.
  74. */
  75. static void DebugObjectGlobal_Finish (void);
  76. #ifndef NDEBUG
  77. extern DebugCounter debugobject_counter;
  78. #if BADVPN_THREAD_SAFE
  79. extern pthread_mutex_t debugobject_mutex;
  80. #endif
  81. #endif
  82. void DebugObject_Init (DebugObject *obj)
  83. {
  84. #ifndef NDEBUG
  85. obj->c = DEBUGOBJECT_VALID;
  86. #if BADVPN_THREAD_SAFE
  87. ASSERT_FORCE(pthread_mutex_lock(&debugobject_mutex) == 0)
  88. #endif
  89. DebugCounter_Increment(&debugobject_counter);
  90. #if BADVPN_THREAD_SAFE
  91. ASSERT_FORCE(pthread_mutex_unlock(&debugobject_mutex) == 0)
  92. #endif
  93. #endif
  94. }
  95. void DebugObject_Free (DebugObject *obj)
  96. {
  97. ASSERT(obj->c == DEBUGOBJECT_VALID)
  98. #ifndef NDEBUG
  99. obj->c = 0;
  100. #if BADVPN_THREAD_SAFE
  101. ASSERT_FORCE(pthread_mutex_lock(&debugobject_mutex) == 0)
  102. #endif
  103. DebugCounter_Decrement(&debugobject_counter);
  104. #if BADVPN_THREAD_SAFE
  105. ASSERT_FORCE(pthread_mutex_unlock(&debugobject_mutex) == 0)
  106. #endif
  107. #endif
  108. }
  109. void DebugObject_Access (const DebugObject *obj)
  110. {
  111. ASSERT(obj->c == DEBUGOBJECT_VALID)
  112. }
  113. void DebugObjectGlobal_Finish (void)
  114. {
  115. #ifndef NDEBUG
  116. DebugCounter_Free(&debugobject_counter);
  117. #endif
  118. }
  119. #endif