DebugObject.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. #else
  52. int dummy_field; // struct must have at least one field
  53. #endif
  54. } DebugObject;
  55. /**
  56. * Initializes the object.
  57. *
  58. * @param obj the object
  59. */
  60. static void DebugObject_Init (DebugObject *obj);
  61. /**
  62. * Frees the object.
  63. *
  64. * @param obj the object
  65. */
  66. static void DebugObject_Free (DebugObject *obj);
  67. /**
  68. * Does nothing.
  69. *
  70. * @param obj the object
  71. */
  72. static void DebugObject_Access (const DebugObject *obj);
  73. /**
  74. * Does nothing.
  75. * There must be no {@link DebugObject}'s initialized.
  76. */
  77. static void DebugObjectGlobal_Finish (void);
  78. #ifndef NDEBUG
  79. extern DebugCounter debugobject_counter;
  80. #if BADVPN_THREAD_SAFE
  81. extern pthread_mutex_t debugobject_mutex;
  82. #endif
  83. #endif
  84. void DebugObject_Init (DebugObject *obj)
  85. {
  86. #ifndef NDEBUG
  87. obj->c = DEBUGOBJECT_VALID;
  88. #if BADVPN_THREAD_SAFE
  89. ASSERT_FORCE(pthread_mutex_lock(&debugobject_mutex) == 0)
  90. #endif
  91. DebugCounter_Increment(&debugobject_counter);
  92. #if BADVPN_THREAD_SAFE
  93. ASSERT_FORCE(pthread_mutex_unlock(&debugobject_mutex) == 0)
  94. #endif
  95. #endif
  96. }
  97. void DebugObject_Free (DebugObject *obj)
  98. {
  99. ASSERT(obj->c == DEBUGOBJECT_VALID)
  100. #ifndef NDEBUG
  101. obj->c = 0;
  102. #if BADVPN_THREAD_SAFE
  103. ASSERT_FORCE(pthread_mutex_lock(&debugobject_mutex) == 0)
  104. #endif
  105. DebugCounter_Decrement(&debugobject_counter);
  106. #if BADVPN_THREAD_SAFE
  107. ASSERT_FORCE(pthread_mutex_unlock(&debugobject_mutex) == 0)
  108. #endif
  109. #endif
  110. }
  111. void DebugObject_Access (const DebugObject *obj)
  112. {
  113. ASSERT(obj->c == DEBUGOBJECT_VALID)
  114. }
  115. void DebugObjectGlobal_Finish (void)
  116. {
  117. #ifndef NDEBUG
  118. DebugCounter_Free(&debugobject_counter);
  119. #endif
  120. }
  121. #endif