DebugObject.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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_THREAD_SAFE
  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. #ifndef NDEBUG
  74. extern DebugCounter debugobject_counter;
  75. #ifdef BADVPN_THREAD_SAFE
  76. extern pthread_mutex_t debugobject_mutex;
  77. #endif
  78. #endif
  79. void DebugObject_Init (DebugObject *obj)
  80. {
  81. #ifndef NDEBUG
  82. obj->c = DEBUGOBJECT_VALID;
  83. #ifdef BADVPN_THREAD_SAFE
  84. ASSERT_FORCE(pthread_mutex_lock(&debugobject_mutex) == 0)
  85. #endif
  86. DebugCounter_Increment(&debugobject_counter);
  87. #ifdef BADVPN_THREAD_SAFE
  88. ASSERT_FORCE(pthread_mutex_unlock(&debugobject_mutex) == 0)
  89. #endif
  90. #endif
  91. }
  92. void DebugObject_Free (DebugObject *obj)
  93. {
  94. ASSERT(obj->c == DEBUGOBJECT_VALID)
  95. #ifndef NDEBUG
  96. obj->c = 0;
  97. #ifdef BADVPN_THREAD_SAFE
  98. ASSERT_FORCE(pthread_mutex_lock(&debugobject_mutex) == 0)
  99. #endif
  100. DebugCounter_Decrement(&debugobject_counter);
  101. #ifdef BADVPN_THREAD_SAFE
  102. ASSERT_FORCE(pthread_mutex_unlock(&debugobject_mutex) == 0)
  103. #endif
  104. #endif
  105. }
  106. void DebugObject_Access (const DebugObject *obj)
  107. {
  108. ASSERT(obj->c == DEBUGOBJECT_VALID)
  109. }
  110. void DebugObjectGlobal_Finish (void)
  111. {
  112. #ifndef NDEBUG
  113. DebugCounter_Free(&debugobject_counter);
  114. #endif
  115. }
  116. #endif