gsrefct.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /* Copyright (C) 1993, 1994, 1996, 1997, 1998, 1999 Aladdin Enterprises. All rights reserved.
  2. This software is provided AS-IS with no warranty, either express or
  3. implied.
  4. This software is distributed under license and may not be copied,
  5. modified or distributed except as expressly authorized under the terms
  6. of the license contained in the file LICENSE in this distribution.
  7. For more information about licensing, please refer to
  8. http://www.ghostscript.com/licensing/. For information on
  9. commercial licensing, go to http://www.artifex.com/licensing/ or
  10. contact Artifex Software, Inc., 101 Lucas Valley Road #110,
  11. San Rafael, CA 94903, U.S.A., +1(415)492-9861.
  12. */
  13. /* $Id: gsrefct.h,v 1.5 2002/06/16 08:45:42 lpd Exp $ */
  14. /* Reference counting definitions */
  15. #ifndef gsrefct_INCLUDED
  16. # define gsrefct_INCLUDED
  17. /*
  18. * A reference-counted object must include the following header:
  19. * rc_header rc;
  20. * The header need not be the first element of the object.
  21. *
  22. * Reference-counted objects have a freeing procedure that gets called when
  23. * the reference count reaches zero. In retrospect, we probably should have
  24. * used finalization for this, but it's too difficult to change now.
  25. * Because of the interaction between these two features, the freeing
  26. * procedure for reference-counted objects that do use finalization must
  27. * free the object itself first, before decrementing the reference counts
  28. * of referenced objects (which of course requires saving pointers to those
  29. * objects before freeing the containing object).
  30. */
  31. typedef struct rc_header_s rc_header;
  32. struct rc_header_s {
  33. long ref_count;
  34. gs_memory_t *memory;
  35. #define rc_free_proc(proc)\
  36. void proc(gs_memory_t *, void *, client_name_t)
  37. rc_free_proc((*free));
  38. };
  39. #ifdef DEBUG
  40. void rc_trace_init_free(const void *vp, const rc_header *prc);
  41. void rc_trace_free_struct(const void *vp, const rc_header *prc,
  42. client_name_t cname);
  43. void rc_trace_increment(const void *vp, const rc_header *prc);
  44. void rc_trace_adjust(const void *vp, const rc_header *prc, int delta);
  45. #define IF_RC_DEBUG(call) if (gs_debug_c('^')) dlputs(""), call
  46. #else
  47. #define IF_RC_DEBUG(call) DO_NOTHING
  48. #endif
  49. /* ------ Allocate/free ------ */
  50. rc_free_proc(rc_free_struct_only);
  51. /* rc_init[_free] is only used to initialize stack-allocated structures. */
  52. #define rc_init_free(vp, mem, rcinit, proc)\
  53. BEGIN\
  54. (vp)->rc.ref_count = rcinit;\
  55. (vp)->rc.memory = mem;\
  56. (vp)->rc.free = proc;\
  57. IF_RC_DEBUG(rc_trace_init_free(vp, &(vp)->rc));\
  58. END
  59. #define rc_init(vp, mem, rcinit)\
  60. rc_init_free(vp, mem, rcinit, rc_free_struct_only)
  61. #define rc_alloc_struct_n(vp, typ, pstyp, mem, errstat, cname, rcinit)\
  62. BEGIN\
  63. if ( ((vp) = gs_alloc_struct(mem, typ, pstyp, cname)) == 0 ) {\
  64. errstat;\
  65. } else {\
  66. rc_init(vp, mem, rcinit);\
  67. }\
  68. END
  69. #define rc_alloc_struct_0(vp, typ, pstype, mem, errstat, cname)\
  70. rc_alloc_struct_n(vp, typ, pstype, mem, errstat, cname, 0)
  71. #define rc_alloc_struct_1(vp, typ, pstype, mem, errstat, cname)\
  72. rc_alloc_struct_n(vp, typ, pstype, mem, errstat, cname, 1)
  73. #define rc_free_struct(vp, cname)\
  74. BEGIN\
  75. IF_RC_DEBUG(rc_trace_free_struct(vp, &(vp)->rc, cname));\
  76. (vp)->rc.free((vp)->rc.memory, (void *)(vp), cname);\
  77. END
  78. /* ------ Reference counting ------ */
  79. /* Increment a reference count. */
  80. #define RC_DO_INCREMENT(vp)\
  81. BEGIN\
  82. (vp)->rc.ref_count++;\
  83. IF_RC_DEBUG(rc_trace_increment(vp, &(vp)->rc));\
  84. END
  85. #define rc_increment(vp)\
  86. BEGIN\
  87. if (vp) RC_DO_INCREMENT(vp);\
  88. END
  89. /* Increment a reference count, allocating the structure if necessary. */
  90. #define rc_allocate_struct(vp, typ, pstype, mem, errstat, cname)\
  91. BEGIN\
  92. if (vp)\
  93. RC_DO_INCREMENT(vp);\
  94. else\
  95. rc_alloc_struct_1(vp, typ, pstype, mem, errstat, cname);\
  96. END
  97. /* Guarantee that a structure is allocated and is not shared. */
  98. #define RC_DO_ADJUST(vp, delta)\
  99. BEGIN\
  100. IF_RC_DEBUG(rc_trace_adjust(vp, &(vp)->rc, delta));\
  101. (vp)->rc.ref_count += (delta);\
  102. END
  103. #define rc_unshare_struct(vp, typ, pstype, mem, errstat, cname)\
  104. BEGIN\
  105. if ( (vp) == 0 || (vp)->rc.ref_count > 1 || (vp)->rc.memory != (mem) ) {\
  106. typ *new;\
  107. rc_alloc_struct_1(new, typ, pstype, mem, errstat, cname);\
  108. if ( vp ) RC_DO_ADJUST(vp, -1);\
  109. (vp) = new;\
  110. }\
  111. END
  112. /* Adjust a reference count either up or down. */
  113. #ifdef DEBUG
  114. # define rc_check_(vp)\
  115. BEGIN\
  116. if (gs_debug_c('?') && (vp)->rc.ref_count < 0)\
  117. lprintf2("0x%lx has ref_count of %ld!\n", (ulong)(vp),\
  118. (vp)->rc.ref_count);\
  119. END
  120. #else
  121. # define rc_check_(vp) DO_NOTHING
  122. #endif
  123. #define rc_adjust_(vp, delta, cname, body)\
  124. BEGIN\
  125. if (vp) {\
  126. RC_DO_ADJUST(vp, delta);\
  127. if (!(vp)->rc.ref_count) {\
  128. rc_free_struct(vp, cname);\
  129. body;\
  130. } else\
  131. rc_check_(vp);\
  132. }\
  133. END
  134. #define rc_adjust(vp, delta, cname)\
  135. rc_adjust_(vp, delta, cname, (vp) = 0)
  136. #define rc_adjust_only(vp, delta, cname)\
  137. rc_adjust_(vp, delta, cname, DO_NOTHING)
  138. #define rc_adjust_const(vp, delta, cname)\
  139. rc_adjust_only(vp, delta, cname)
  140. #define rc_decrement(vp, cname)\
  141. rc_adjust(vp, -1, cname)
  142. #define rc_decrement_only(vp, cname)\
  143. rc_adjust_only(vp, -1, cname)
  144. /*
  145. * Assign a pointer, adjusting reference counts. vpfrom might be a local
  146. * variable with a copy of the last reference to the object, and freeing
  147. * vpto might decrement the object's reference count and cause it to be
  148. * freed (incorrectly); for that reason, we do the increment first.
  149. */
  150. #define rc_assign(vpto, vpfrom, cname)\
  151. BEGIN\
  152. if ((vpto) != (vpfrom)) {\
  153. rc_increment(vpfrom);\
  154. rc_decrement_only(vpto, cname);\
  155. (vpto) = (vpfrom);\
  156. }\
  157. END
  158. /*
  159. * Adjust reference counts for assigning a pointer,
  160. * but don't do the assignment. We use this before assigning
  161. * an entire structure containing reference-counted pointers.
  162. */
  163. #define rc_pre_assign(vpto, vpfrom, cname)\
  164. BEGIN\
  165. if ((vpto) != (vpfrom)) {\
  166. rc_increment(vpfrom);\
  167. rc_decrement_only(vpto, cname);\
  168. }\
  169. END
  170. #endif /* gsrefct_INCLUDED */