debugcounter.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. #else
  44. int dummy_field; // struct must have at least one field
  45. #endif
  46. } DebugCounter;
  47. #define DEBUGCOUNTER_STATIC { 0 }
  48. /**
  49. * Initializes the object.
  50. * The object is initialized with counter value zero.
  51. *
  52. * @param obj the object
  53. */
  54. static void DebugCounter_Init (DebugCounter *obj)
  55. {
  56. #ifndef NDEBUG
  57. obj->c = 0;
  58. #endif
  59. }
  60. /**
  61. * Frees the object.
  62. * This does not have to be called when the counter is no longer needed.
  63. * The counter value must be zero.
  64. *
  65. * @param obj the object
  66. */
  67. static void DebugCounter_Free (DebugCounter *obj)
  68. {
  69. #ifndef NDEBUG
  70. ASSERT(obj->c == 0 || obj->c == INT32_MAX)
  71. #endif
  72. }
  73. /**
  74. * Increments the counter.
  75. * Increments the counter value by one.
  76. *
  77. * @param obj the object
  78. */
  79. static void DebugCounter_Increment (DebugCounter *obj)
  80. {
  81. #ifndef NDEBUG
  82. ASSERT(obj->c >= 0)
  83. if (obj->c != INT32_MAX) {
  84. obj->c++;
  85. }
  86. #endif
  87. }
  88. /**
  89. * Decrements the counter.
  90. * The counter value must be >0.
  91. * Decrements the counter value by one.
  92. *
  93. * @param obj the object
  94. */
  95. static void DebugCounter_Decrement (DebugCounter *obj)
  96. {
  97. #ifndef NDEBUG
  98. ASSERT(obj->c > 0)
  99. if (obj->c != INT32_MAX) {
  100. obj->c--;
  101. }
  102. #endif
  103. }
  104. #endif