gxobj.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /* Copyright (C) 1995, 1996, 1999, 2002 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: gxobj.h,v 1.7 2005/10/12 10:45:21 leonardo Exp $ */
  14. /* Memory manager implementation structures for Ghostscript */
  15. #ifndef gxobj_INCLUDED
  16. # define gxobj_INCLUDED
  17. #include "gxbitmap.h"
  18. #ifdef DEBUG
  19. #define IGC_PTR_STABILITY_CHECK 0
  20. #else
  21. #define IGC_PTR_STABILITY_CHECK 0
  22. #endif
  23. /* ================ Objects ================ */
  24. /*
  25. * Object headers have the form:
  26. -l- -mark/back-
  27. -size-
  28. -type/reloc-
  29. * l (aLone) is a single bit. Mark/back is 1 bit shorter than a uint. We
  30. * round the header size up to the next multiple of the most severe
  31. * alignment restriction (4 or 8 bytes).
  32. *
  33. * The mark/back field is used for the mark during the marking phase of
  34. * garbage collection, and for a back pointer value during the compaction
  35. * phase. Since we want to be able to collect local VM independently of
  36. * global VM, we need two different distinguished mark values:
  37. * - For local objects that have not been traced and should be freed
  38. * (compacted out), we use 1...11 in the mark field (o_unmarked).
  39. * - For global objects that have not been traced but should be kept,
  40. * we use 1...10 in the mark field (o_untraced).
  41. * Note that neither of these values is a possible real relocation value.
  42. *
  43. * The back pointer's meaning depends on whether the object is
  44. * free (unmarked) or in use (marked):
  45. * - In free objects, the back pointer is an offset from the object
  46. * header back to a chunk_head_t structure that contains the location
  47. * to which all the data in this chunk will get moved; the reloc field
  48. * contains the amount by which the following run of useful objects
  49. * will be relocated downwards.
  50. * - In useful objects, the back pointer is an offset from the object
  51. * back to the previous free object; the reloc field is not used (it
  52. * overlays the type field).
  53. * These two cases can be distinguished when scanning a chunk linearly,
  54. * but when simply examining an object via a pointer, the chunk pointer
  55. * is also needed.
  56. */
  57. #define obj_flag_bits 1
  58. #define obj_mb_bits (arch_sizeof_int * 8 - obj_flag_bits)
  59. #define o_unmarked (((uint)1 << obj_mb_bits) - 1)
  60. #define o_set_unmarked(pp)\
  61. ((pp)->o_smark = o_unmarked)
  62. #define o_is_unmarked(pp)\
  63. ((pp)->o_smark == o_unmarked)
  64. #define o_untraced (((uint)1 << obj_mb_bits) - 2)
  65. #define o_set_untraced(pp)\
  66. ((pp)->o_smark = o_untraced)
  67. #define o_is_untraced(pp)\
  68. ((pp)->o_smark == o_untraced)
  69. #define o_marked 0
  70. #define o_mark(pp)\
  71. ((pp)->o_smark = o_marked)
  72. #define obj_back_shift obj_flag_bits
  73. #define obj_back_scale (1 << obj_back_shift)
  74. typedef struct obj_header_data_s {
  75. union _f {
  76. struct _h {
  77. unsigned alone:1;
  78. } h;
  79. struct _m {
  80. unsigned _:1, smark:obj_mb_bits;
  81. } m;
  82. struct _b {
  83. unsigned _:1, back:obj_mb_bits;
  84. } b;
  85. } f;
  86. uint size;
  87. union _t {
  88. gs_memory_type_ptr_t type;
  89. uint reloc;
  90. } t;
  91. # if IGC_PTR_STABILITY_CHECK
  92. unsigned space_id:3; /* r_space_bits + 1 bit for "instability". */
  93. # endif
  94. } obj_header_data_t;
  95. /*
  96. * Define the alignment modulus for aligned objects. We assume all
  97. * alignment values are powers of 2; we can avoid nested 'max'es that way.
  98. * The final | is because back pointer values are divided by obj_back_scale,
  99. * so objects must be aligned at least 0 mod obj_back_scale.
  100. *
  101. * Note: OBJECTS ARE NOT GUARANTEED to be aligned any more strictly than
  102. * required by the hardware, regardless of the value of obj_align_mod.
  103. * See gsmemraw.h for more information about this.
  104. */
  105. #define obj_align_mod\
  106. (((arch_align_memory_mod - 1) |\
  107. (align_bitmap_mod - 1) |\
  108. (obj_back_scale - 1)) + 1)
  109. /* The only possible values for obj_align_mod are 4, 8, or 16.... */
  110. #if obj_align_mod == 4
  111. # define log2_obj_align_mod 2
  112. #else
  113. #if obj_align_mod == 8
  114. # define log2_obj_align_mod 3
  115. #else
  116. #if obj_align_mod == 16
  117. # define log2_obj_align_mod 4
  118. #endif
  119. #endif
  120. #endif
  121. #define obj_align_mask (obj_align_mod-1)
  122. #define obj_align_round(siz)\
  123. (uint)(((siz) + obj_align_mask) & -obj_align_mod)
  124. #define obj_size_round(siz)\
  125. obj_align_round((siz) + sizeof(obj_header_t))
  126. /* Define the real object header type, taking alignment into account. */
  127. struct obj_header_s { /* must be a struct because of forward reference */
  128. union _d {
  129. obj_header_data_t o;
  130. byte _pad[ROUND_UP(sizeof(obj_header_data_t), obj_align_mod)];
  131. }
  132. d;
  133. };
  134. /* Define some reasonable abbreviations for the fields. */
  135. #define o_alone d.o.f.h.alone
  136. #define o_back d.o.f.b.back
  137. #define o_smark d.o.f.m.smark
  138. #define o_size d.o.size
  139. #define o_type d.o.t.type
  140. #define o_nreloc d.o.t.reloc
  141. /*
  142. * The macros for getting the sizes of objects all take pointers to
  143. * the object header, for use when scanning storage linearly.
  144. */
  145. #define pre_obj_contents_size(pp)\
  146. ((pp)->o_size)
  147. #define pre_obj_rounded_size(pp)\
  148. obj_size_round(pre_obj_contents_size(pp))
  149. #define pre_obj_next(pp)\
  150. ((obj_header_t *)((byte *)(pp) + obj_align_round(\
  151. pre_obj_contents_size(pp) + sizeof(obj_header_t) )))
  152. /*
  153. * Define the header that free objects point back to when relocating.
  154. * Every chunk, including inner chunks, has one of these.
  155. */
  156. typedef struct chunk_head_s {
  157. byte *dest; /* destination for objects */
  158. #if obj_align_mod > arch_sizeof_ptr
  159. byte *_pad[obj_align_mod / arch_sizeof_ptr - 1];
  160. #endif
  161. obj_header_t free; /* header for a free object, */
  162. /* in case the first real object */
  163. /* is in use */
  164. } chunk_head_t;
  165. #endif /* gxobj_INCLUDED */