DebugObject.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /**
  2. * @file DebugObject.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. *
  22. * @section DESCRIPTION
  23. *
  24. * Object used for detecting leaks.
  25. */
  26. #ifndef BADVPN_SYSTEM_DEBUGOBJECT_H
  27. #define BADVPN_SYSTEM_DEBUGOBJECT_H
  28. #include <stdint.h>
  29. #include <misc/debug.h>
  30. #include <misc/debugcounter.h>
  31. #define DEBUGOBJECT_VALID UINT32_C(0x31415926)
  32. /**
  33. * Object used for detecting leaks.
  34. */
  35. typedef struct {
  36. #ifndef NDEBUG
  37. uint32_t c;
  38. #endif
  39. } DebugObject;
  40. /**
  41. * Initializes the object.
  42. *
  43. * @param obj the object
  44. */
  45. static void DebugObject_Init (DebugObject *obj);
  46. /**
  47. * Frees the object.
  48. *
  49. * @param obj the object
  50. */
  51. static void DebugObject_Free (DebugObject *obj);
  52. /**
  53. * Does nothing.
  54. *
  55. * @param obj the object
  56. */
  57. static void DebugObject_Access (DebugObject *obj);
  58. /**
  59. * Does nothing.
  60. * There must be no {@link DebugObject}'s initialized.
  61. */
  62. static void DebugObjectGlobal_Finish (void);
  63. extern DebugCounter debugobject_counter;
  64. void DebugObject_Init (DebugObject *obj)
  65. {
  66. #ifndef NDEBUG
  67. obj->c = DEBUGOBJECT_VALID;
  68. DebugCounter_Increment(&debugobject_counter);
  69. #endif
  70. }
  71. void DebugObject_Free (DebugObject *obj)
  72. {
  73. ASSERT(obj->c == DEBUGOBJECT_VALID)
  74. #ifndef NDEBUG
  75. obj->c = 0;
  76. DebugCounter_Decrement(&debugobject_counter);
  77. #endif
  78. }
  79. void DebugObject_Access (DebugObject *obj)
  80. {
  81. ASSERT(obj->c == DEBUGOBJECT_VALID)
  82. }
  83. void DebugObjectGlobal_Finish (void)
  84. {
  85. #ifndef NDEBUG
  86. DebugCounter_Free(&debugobject_counter);
  87. #endif
  88. }
  89. #endif