debugcounter.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /**
  2. * @file debugcounter.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. * Counter for detecting leaks.
  32. */
  33. #ifndef BADVPN_MISC_DEBUGCOUNTER_H
  34. #define BADVPN_MISC_DEBUGCOUNTER_H
  35. #include <stdint.h>
  36. #include <misc/debug.h>
  37. /**
  38. * Counter for detecting leaks.
  39. */
  40. typedef struct {
  41. #ifndef NDEBUG
  42. int32_t c;
  43. #endif
  44. } DebugCounter;
  45. #ifndef NDEBUG
  46. #define DEBUGCOUNTER_STATIC { .c = 0 }
  47. #else
  48. #define DEBUGCOUNTER_STATIC {}
  49. #endif
  50. /**
  51. * Initializes the object.
  52. * The object is initialized with counter value zero.
  53. *
  54. * @param obj the object
  55. */
  56. static void DebugCounter_Init (DebugCounter *obj)
  57. {
  58. #ifndef NDEBUG
  59. obj->c = 0;
  60. #endif
  61. }
  62. /**
  63. * Frees the object.
  64. * This does not have to be called when the counter is no longer needed.
  65. * The counter value must be zero.
  66. *
  67. * @param obj the object
  68. */
  69. static void DebugCounter_Free (DebugCounter *obj)
  70. {
  71. #ifndef NDEBUG
  72. ASSERT(obj->c == 0 || obj->c == INT32_MAX)
  73. #endif
  74. }
  75. /**
  76. * Increments the counter.
  77. * Increments the counter value by one.
  78. *
  79. * @param obj the object
  80. */
  81. static void DebugCounter_Increment (DebugCounter *obj)
  82. {
  83. #ifndef NDEBUG
  84. ASSERT(obj->c >= 0)
  85. if (obj->c != INT32_MAX) {
  86. obj->c++;
  87. }
  88. #endif
  89. }
  90. /**
  91. * Decrements the counter.
  92. * The counter value must be >0.
  93. * Decrements the counter value by one.
  94. *
  95. * @param obj the object
  96. */
  97. static void DebugCounter_Decrement (DebugCounter *obj)
  98. {
  99. #ifndef NDEBUG
  100. ASSERT(obj->c > 0)
  101. if (obj->c != INT32_MAX) {
  102. obj->c--;
  103. }
  104. #endif
  105. }
  106. #endif