NCDRefTarget.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /**
  2. * @file NCDRefTarget.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. #ifndef BADVPN_NCD_REF_TARGET_H
  30. #define BADVPN_NCD_REF_TARGET_H
  31. #include <limits.h>
  32. #include <misc/debug.h>
  33. #include <base/DebugObject.h>
  34. /**
  35. * Represents a reference-counted object.
  36. */
  37. typedef struct NCDRefTarget_s NCDRefTarget;
  38. /**
  39. * Callback function called after the reference count of a {@link NCDRefTarget}
  40. * reaches has reached zero. At this point the NCDRefTarget object has already
  41. * been invalidated, i.e. {@link NCDRefTarget_Ref} must not be called on this
  42. * object after this handler is called.
  43. */
  44. typedef void (*NCDRefTarget_func_release) (NCDRefTarget *o);
  45. struct NCDRefTarget_s {
  46. NCDRefTarget_func_release func_release;
  47. int refcnt;
  48. DebugObject d_obj;
  49. };
  50. /**
  51. * Initializes a reference target object. The initial reference count of the object
  52. * is 1. The \a func_release argument specifies the function to be called from
  53. * {@link NCDRefTarget_Deref} when the reference count reaches zero.
  54. */
  55. static void NCDRefTarget_Init (NCDRefTarget *o, NCDRefTarget_func_release func_release);
  56. /**
  57. * Decrements the reference count of a reference target object. If the reference
  58. * count has reached zero, the object's {@link NCDRefTarget_func_release} function
  59. * is called, and the object is considered destroyed.
  60. */
  61. static void NCDRefTarget_Deref (NCDRefTarget *o);
  62. /**
  63. * Increments the reference count of a reference target object.
  64. * Returns 1 on success and 0 on failure.
  65. */
  66. static int NCDRefTarget_Ref (NCDRefTarget *o) WARN_UNUSED;
  67. static void NCDRefTarget_Init (NCDRefTarget *o, NCDRefTarget_func_release func_release)
  68. {
  69. ASSERT(func_release)
  70. o->func_release = func_release;
  71. o->refcnt = 1;
  72. DebugObject_Init(&o->d_obj);
  73. }
  74. static void NCDRefTarget_Deref (NCDRefTarget *o)
  75. {
  76. DebugObject_Access(&o->d_obj);
  77. ASSERT(o->refcnt > 0)
  78. o->refcnt--;
  79. if (o->refcnt == 0) {
  80. DebugObject_Free(&o->d_obj);
  81. o->func_release(o);
  82. }
  83. }
  84. static int NCDRefTarget_Ref (NCDRefTarget *o)
  85. {
  86. DebugObject_Access(&o->d_obj);
  87. ASSERT(o->refcnt > 0)
  88. if (o->refcnt == INT_MAX) {
  89. return 0;
  90. }
  91. o->refcnt++;
  92. return 1;
  93. }
  94. #endif