gsrefct.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /* Copyright (C) 1993, 1994, 1996, 1997, 1998, 1999 Aladdin Enterprises. All rights reserved.
  2. This file is part of AFPL Ghostscript.
  3. AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND. No author or
  4. distributor accepts any responsibility for the consequences of using it, or
  5. for whether it serves any particular purpose or works at all, unless he or
  6. she says so in writing. Refer to the Aladdin Free Public License (the
  7. "License") for full details.
  8. Every copy of AFPL Ghostscript must include a copy of the License, normally
  9. in a plain ASCII text file named PUBLIC. The License grants you the right
  10. to copy, modify and redistribute AFPL Ghostscript, but only under certain
  11. conditions described in the License. Among other things, the License
  12. requires that the copyright notice and this notice be preserved on all
  13. copies.
  14. */
  15. /*$Id: gsrefct.h,v 1.2 2000/09/19 19:00:32 lpd Exp $ */
  16. /* Reference counting definitions */
  17. #ifndef gsrefct_INCLUDED
  18. # define gsrefct_INCLUDED
  19. /*
  20. * A reference-counted object must include the following header:
  21. * rc_header rc;
  22. * The header need not be the first element of the object.
  23. *
  24. * Reference-counted objects have a freeing procedure that gets called when
  25. * the reference count reaches zero. In retrospect, we probably should have
  26. * used finalization for this, but it's too difficult to change now.
  27. * Because of the interaction between these two features, the freeing
  28. * procedure for reference-counted objects that do use finalization must
  29. * free the object itself first, before decrementing the reference counts
  30. * of referenced objects (which of course requires saving pointers to those
  31. * objects before freeing the containing object).
  32. */
  33. typedef struct rc_header_s rc_header;
  34. struct rc_header_s {
  35. long ref_count;
  36. gs_memory_t *memory;
  37. #define rc_free_proc(proc)\
  38. void proc(P3(gs_memory_t *, void *, client_name_t))
  39. rc_free_proc((*free));
  40. };
  41. #ifdef DEBUG
  42. void rc_trace_init_free(P2(const void *vp, const rc_header *prc));
  43. void rc_trace_free_struct(P3(const void *vp, const rc_header *prc,
  44. client_name_t cname));
  45. void rc_trace_increment(P2(const void *vp, const rc_header *prc));
  46. void rc_trace_adjust(P3(const void *vp, const rc_header *prc, int delta));
  47. #define IF_RC_DEBUG(call) if (gs_debug_c('^')) dlputs(""), call
  48. #else
  49. #define IF_RC_DEBUG(call) DO_NOTHING
  50. #endif
  51. /* ------ Allocate/free ------ */
  52. rc_free_proc(rc_free_struct_only);
  53. /* rc_init[_free] is only used to initialize stack-allocated structures. */
  54. #define rc_init_free(vp, mem, rcinit, proc)\
  55. BEGIN\
  56. (vp)->rc.ref_count = rcinit;\
  57. (vp)->rc.memory = mem;\
  58. (vp)->rc.free = proc;\
  59. IF_RC_DEBUG(rc_trace_init_free(vp, &(vp)->rc));\
  60. END
  61. #define rc_init(vp, mem, rcinit)\
  62. rc_init_free(vp, mem, rcinit, rc_free_struct_only)
  63. #define rc_alloc_struct_n(vp, typ, pstyp, mem, errstat, cname, rcinit)\
  64. BEGIN\
  65. if ( ((vp) = gs_alloc_struct(mem, typ, pstyp, cname)) == 0 ) {\
  66. errstat;\
  67. } else {\
  68. rc_init(vp, mem, rcinit);\
  69. }\
  70. END
  71. #define rc_alloc_struct_0(vp, typ, pstype, mem, errstat, cname)\
  72. rc_alloc_struct_n(vp, typ, pstype, mem, errstat, cname, 0)
  73. #define rc_alloc_struct_1(vp, typ, pstype, mem, errstat, cname)\
  74. rc_alloc_struct_n(vp, typ, pstype, mem, errstat, cname, 1)
  75. #define rc_free_struct(vp, cname)\
  76. BEGIN\
  77. IF_RC_DEBUG(rc_trace_free_struct(vp, &(vp)->rc, cname));\
  78. (vp)->rc.free((vp)->rc.memory, (void *)(vp), cname);\
  79. END
  80. /* ------ Reference counting ------ */
  81. /* Increment a reference count. */
  82. #define RC_DO_INCREMENT(vp)\
  83. BEGIN\
  84. (vp)->rc.ref_count++;\
  85. IF_RC_DEBUG(rc_trace_increment(vp, &(vp)->rc));\
  86. END
  87. #define rc_increment(vp)\
  88. BEGIN\
  89. if (vp) RC_DO_INCREMENT(vp);\
  90. END
  91. /* Increment a reference count, allocating the structure if necessary. */
  92. #define rc_allocate_struct(vp, typ, pstype, mem, errstat, cname)\
  93. BEGIN\
  94. if (vp)\
  95. RC_DO_INCREMENT(vp);\
  96. else\
  97. rc_alloc_struct_1(vp, typ, pstype, mem, errstat, cname);\
  98. END
  99. /* Guarantee that a structure is allocated and is not shared. */
  100. #define RC_DO_ADJUST(vp, delta)\
  101. BEGIN\
  102. IF_RC_DEBUG(rc_trace_adjust(vp, &(vp)->rc, delta));\
  103. (vp)->rc.ref_count += (delta);\
  104. END
  105. #define rc_unshare_struct(vp, typ, pstype, mem, errstat, cname)\
  106. BEGIN\
  107. if ( (vp) == 0 || (vp)->rc.ref_count > 1 || (vp)->rc.memory != (mem) ) {\
  108. typ *new;\
  109. rc_alloc_struct_1(new, typ, pstype, mem, errstat, cname);\
  110. if ( vp ) RC_DO_ADJUST(vp, -1);\
  111. (vp) = new;\
  112. }\
  113. END
  114. /* Adjust a reference count either up or down. */
  115. #ifdef DEBUG
  116. # define rc_check_(vp)\
  117. BEGIN\
  118. if (gs_debug_c('?') && (vp)->rc.ref_count < 0)\
  119. lprintf2("0x%lx has ref_count of %ld!\n", (ulong)(vp),\
  120. (vp)->rc.ref_count);\
  121. END
  122. #else
  123. # define rc_check_(vp) DO_NOTHING
  124. #endif
  125. #define rc_adjust_(vp, delta, cname, body)\
  126. BEGIN\
  127. if (vp) {\
  128. RC_DO_ADJUST(vp, delta);\
  129. if (!(vp)->rc.ref_count) {\
  130. rc_free_struct(vp, cname);\
  131. body;\
  132. } else\
  133. rc_check_(vp);\
  134. }\
  135. END
  136. #define rc_adjust(vp, delta, cname)\
  137. rc_adjust_(vp, delta, cname, (vp) = 0)
  138. #define rc_adjust_only(vp, delta, cname)\
  139. rc_adjust_(vp, delta, cname, DO_NOTHING)
  140. #define rc_adjust_const(vp, delta, cname)\
  141. rc_adjust_only(vp, delta, cname)
  142. #define rc_decrement(vp, cname)\
  143. rc_adjust(vp, -1, cname)
  144. #define rc_decrement_only(vp, cname)\
  145. rc_adjust_only(vp, -1, cname)
  146. /*
  147. * Assign a pointer, adjusting reference counts. vpfrom might be a local
  148. * variable with a copy of the last reference to the object, and freeing
  149. * vpto might decrement the object's reference count and cause it to be
  150. * freed (incorrectly); for that reason, we do the increment first.
  151. */
  152. #define rc_assign(vpto, vpfrom, cname)\
  153. BEGIN\
  154. if ((vpto) != (vpfrom)) {\
  155. rc_increment(vpfrom);\
  156. rc_decrement_only(vpto, cname);\
  157. (vpto) = (vpfrom);\
  158. }\
  159. END
  160. /*
  161. * Adjust reference counts for assigning a pointer,
  162. * but don't do the assignment. We use this before assigning
  163. * an entire structure containing reference-counted pointers.
  164. */
  165. #define rc_pre_assign(vpto, vpfrom, cname)\
  166. BEGIN\
  167. if ((vpto) != (vpfrom)) {\
  168. rc_increment(vpfrom);\
  169. rc_decrement_only(vpto, cname);\
  170. }\
  171. END
  172. #endif /* gsrefct_INCLUDED */