gsmemory.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /* Copyright (C) 1993, 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: gsmemory.c,v 1.4 2001/08/25 06:46:21 lpd Exp $ */
  16. /* Generic allocator support */
  17. #include "memory_.h"
  18. #include "gdebug.h"
  19. #include "gstypes.h"
  20. #include "gsmemory.h"
  21. #include "gsmdebug.h"
  22. #include "gsrefct.h" /* to check prototype */
  23. #include "gsstruct.h" /* ditto */
  24. /* Define the fill patterns for unallocated memory. */
  25. const byte gs_alloc_fill_alloc = 0xa1;
  26. const byte gs_alloc_fill_block = 0xb1;
  27. const byte gs_alloc_fill_collected = 0xc1;
  28. const byte gs_alloc_fill_deleted = 0xd1;
  29. const byte gs_alloc_fill_free = 0xf1;
  30. /* A 'structure' type descriptor for free blocks. */
  31. gs_public_st_simple(st_free, byte, "(free)");
  32. /* The 'structure' type descriptor for bytes. */
  33. gs_public_st_simple(st_bytes, byte, "bytes");
  34. /* The structure type descriptor for GC roots. */
  35. public_st_gc_root_t();
  36. /* The descriptors for elements and arrays of const strings. */
  37. private_st_const_string();
  38. public_st_const_string_element();
  39. /* GC procedures for bytestrings */
  40. gs_ptr_type_t
  41. enum_bytestring(enum_ptr_t *pep, const gs_bytestring *pbs)
  42. {
  43. return (pbs->bytes ? ENUM_OBJ(pbs->bytes) : ENUM_STRING(pbs));
  44. }
  45. gs_ptr_type_t
  46. enum_const_bytestring(enum_ptr_t *pep, const gs_const_bytestring *pbs)
  47. {
  48. return (pbs->bytes ? ENUM_OBJ(pbs->bytes) : ENUM_CONST_STRING(pbs));
  49. }
  50. void
  51. reloc_bytestring(gs_bytestring *pbs, gc_state_t *gcst)
  52. {
  53. if (pbs->bytes) {
  54. byte *bytes = pbs->bytes;
  55. long offset = pbs->data - bytes;
  56. pbs->bytes = bytes = RELOC_OBJ(bytes);
  57. pbs->data = bytes + offset;
  58. } else
  59. RELOC_STRING_VAR(*(gs_string *)pbs);
  60. }
  61. void
  62. reloc_const_bytestring(gs_const_bytestring *pbs, gc_state_t *gcst)
  63. {
  64. if (pbs->bytes) {
  65. const byte *bytes = pbs->bytes;
  66. long offset = pbs->data - bytes;
  67. pbs->bytes = bytes = RELOC_OBJ(bytes);
  68. pbs->data = bytes + offset;
  69. } else
  70. RELOC_CONST_STRING_VAR(*(gs_const_string *)pbs);
  71. }
  72. /* Fill an unoccupied block with a pattern. */
  73. /* Note that the block size may be too large for a single memset. */
  74. void
  75. gs_alloc_memset(void *ptr, int /*byte */ fill, ulong lsize)
  76. {
  77. ulong msize = lsize;
  78. char *p = ptr;
  79. int isize;
  80. for (; msize; msize -= isize, p += isize) {
  81. isize = min(msize, max_int);
  82. memset(p, fill, isize);
  83. }
  84. }
  85. /*
  86. * Either allocate (if obj == 0) or resize (if obj != 0) a structure array.
  87. * If obj != 0, pstype is used only for checking (in DEBUG configurations).
  88. */
  89. void *
  90. gs_resize_struct_array(gs_memory_t *mem, void *obj, uint num_elements,
  91. gs_memory_type_ptr_t pstype, client_name_t cname)
  92. {
  93. if (obj == 0)
  94. return gs_alloc_struct_array(mem, num_elements, void, pstype, cname);
  95. #ifdef DEBUG
  96. if (gs_object_type(mem, obj) != pstype) {
  97. lprintf3("resize_struct_array 0x%lx, type was 0x%lx, expected 0x%lx!\n",
  98. (ulong)obj, (ulong)gs_object_type(mem, obj), (ulong)pstype);
  99. return 0;
  100. }
  101. #endif
  102. return gs_resize_object(mem, obj, num_elements, cname);
  103. }
  104. /* Allocate a structure using a "raw memory" allocator. */
  105. void *
  106. gs_raw_alloc_struct_immovable(gs_raw_memory_t * rmem,
  107. gs_memory_type_ptr_t pstype,
  108. client_name_t cname)
  109. {
  110. return gs_alloc_bytes_immovable(rmem, gs_struct_type_size(pstype), cname);
  111. }
  112. /* No-op freeing procedures */
  113. void
  114. gs_ignore_free_object(gs_memory_t * mem, void *data, client_name_t cname)
  115. {
  116. }
  117. void
  118. gs_ignore_free_string(gs_memory_t * mem, byte * data, uint nbytes,
  119. client_name_t cname)
  120. {
  121. }
  122. /* Deconstifying freeing procedures. */
  123. /* These procedures rely on a severely deprecated pun. */
  124. void
  125. gs_free_const_object(gs_memory_t * mem, const void *data, client_name_t cname)
  126. {
  127. union { const void *r; void *w; } u;
  128. u.r = data;
  129. gs_free_object(mem, u.w, cname);
  130. }
  131. void
  132. gs_free_const_string(gs_memory_t * mem, const byte * data, uint nbytes,
  133. client_name_t cname)
  134. {
  135. union { const byte *r; byte *w; } u;
  136. u.r = data;
  137. gs_free_string(mem, u.w, nbytes, cname);
  138. }
  139. /* Free a [const] bytestring. */
  140. void
  141. gs_free_bytestring(gs_memory_t *mem, gs_bytestring *pbs, client_name_t cname)
  142. {
  143. if (pbs->bytes)
  144. gs_free_object(mem, pbs->bytes, cname);
  145. else
  146. gs_free_string(mem, pbs->data, pbs->size, cname);
  147. }
  148. void
  149. gs_free_const_bytestring(gs_memory_t *mem, gs_const_bytestring *pbs,
  150. client_name_t cname)
  151. {
  152. if (pbs->bytes)
  153. gs_free_const_object(mem, pbs->bytes, cname);
  154. else
  155. gs_free_const_string(mem, pbs->data, pbs->size, cname);
  156. }
  157. /* No-op consolidation procedure */
  158. void
  159. gs_ignore_consolidate_free(gs_memory_t *mem)
  160. {
  161. }
  162. /* No-op pointer enumeration procedure */
  163. ENUM_PTRS_BEGIN_PROC(gs_no_struct_enum_ptrs)
  164. {
  165. return 0;
  166. ENUM_PTRS_END_PROC
  167. }
  168. /* No-op pointer relocation procedure */
  169. RELOC_PTRS_BEGIN(gs_no_struct_reloc_ptrs)
  170. {
  171. }
  172. RELOC_PTRS_END
  173. /* Get the size of a structure from the descriptor. */
  174. uint
  175. gs_struct_type_size(gs_memory_type_ptr_t pstype)
  176. {
  177. return pstype->ssize;
  178. }
  179. /* Get the name of a structure from the descriptor. */
  180. struct_name_t
  181. gs_struct_type_name(gs_memory_type_ptr_t pstype)
  182. {
  183. return pstype->sname;
  184. }
  185. /* Register a structure root. */
  186. int
  187. gs_register_struct_root(gs_memory_t *mem, gs_gc_root_t *root,
  188. void **pp, client_name_t cname)
  189. {
  190. return gs_register_root(mem, root, ptr_struct_type, pp, cname);
  191. }
  192. /* ---------------- Reference counting ---------------- */
  193. #ifdef DEBUG
  194. private const char *
  195. rc_object_type_name(const void *vp, const rc_header *prc)
  196. {
  197. gs_memory_type_ptr_t pstype;
  198. if (prc->memory == 0)
  199. return "(unknown)";
  200. pstype = gs_object_type(prc->memory, vp);
  201. if (prc->free != rc_free_struct_only) {
  202. /*
  203. * This object might be stack-allocated or have other unusual memory
  204. * management properties. Make some reasonableness checks.
  205. * ****** THIS IS A HACK. ******
  206. */
  207. long dist;
  208. dist = (const char *)&dist - (const char *)vp;
  209. if (dist < 10000 && dist > -10000)
  210. return "(on stack)";
  211. if ((ulong)pstype < 0x10000 || (long)pstype < 0)
  212. return "(anomalous)";
  213. }
  214. return client_name_string(gs_struct_type_name(pstype));
  215. }
  216. /* Trace reference count operations. */
  217. void
  218. rc_trace_init_free(const void *vp, const rc_header *prc)
  219. {
  220. dprintf3("[^]%s 0x%lx init = %ld\n",
  221. rc_object_type_name(vp, prc), (ulong)vp, (long)prc->ref_count);
  222. }
  223. void
  224. rc_trace_free_struct(const void *vp, const rc_header *prc, client_name_t cname)
  225. {
  226. dprintf3("[^]%s 0x%lx => free (%s)\n",
  227. rc_object_type_name(vp, prc),
  228. (ulong)vp, client_name_string(cname));
  229. }
  230. void
  231. rc_trace_increment(const void *vp, const rc_header *prc)
  232. {
  233. dprintf3("[^]%s 0x%lx ++ => %ld\n",
  234. rc_object_type_name(vp, prc),
  235. (ulong)vp, (long)prc->ref_count);
  236. }
  237. void
  238. rc_trace_adjust(const void *vp, const rc_header *prc, int delta)
  239. {
  240. dprintf4("[^]%s 0x%lx %+d => %ld\n",
  241. rc_object_type_name(vp, prc),
  242. (ulong)vp, delta, (long)(prc->ref_count + delta));
  243. }
  244. #endif /* DEBUG */
  245. /* Normal freeing routine for reference-counted structures. */
  246. void
  247. rc_free_struct_only(gs_memory_t * mem, void *data, client_name_t cname)
  248. {
  249. if (mem != 0)
  250. gs_free_object(mem, data, cname);
  251. }
  252. /* ---------------- Basic-structure GC procedures ---------------- */
  253. /* Enumerate pointers */
  254. ENUM_PTRS_BEGIN_PROC(basic_enum_ptrs)
  255. {
  256. const gc_struct_data_t *psd = pstype->proc_data;
  257. if (index < psd->num_ptrs) {
  258. const gc_ptr_element_t *ppe = &psd->ptrs[index];
  259. EV_CONST char *pptr = (EV_CONST char *)vptr + ppe->offset;
  260. switch ((gc_ptr_type_index_t)ppe->type) {
  261. case GC_ELT_OBJ:
  262. return ENUM_OBJ(*(const void *EV_CONST *)pptr);
  263. case GC_ELT_STRING:
  264. return ENUM_STRING((const gs_string *)pptr);
  265. case GC_ELT_CONST_STRING:
  266. return ENUM_CONST_STRING((const gs_const_string *)pptr);
  267. }
  268. }
  269. if (!psd->super_type)
  270. return 0;
  271. return ENUM_USING(*(psd->super_type),
  272. (EV_CONST void *)
  273. ((EV_CONST char *)vptr + psd->super_offset),
  274. pstype->ssize, index - psd->num_ptrs);
  275. }
  276. ENUM_PTRS_END_PROC
  277. /* Relocate pointers */
  278. RELOC_PTRS_BEGIN(basic_reloc_ptrs)
  279. {
  280. const gc_struct_data_t *psd = pstype->proc_data;
  281. uint i;
  282. for (i = 0; i < psd->num_ptrs; ++i) {
  283. const gc_ptr_element_t *ppe = &psd->ptrs[i];
  284. char *pptr = (char *)vptr + ppe->offset;
  285. switch ((gc_ptr_type_index_t) ppe->type) {
  286. case GC_ELT_OBJ:
  287. RELOC_OBJ_VAR(*(void **)pptr);
  288. break;
  289. case GC_ELT_STRING:
  290. RELOC_STRING_VAR(*(gs_string *)pptr);
  291. break;
  292. case GC_ELT_CONST_STRING:
  293. RELOC_CONST_STRING_VAR(*(gs_const_string *)pptr);
  294. break;
  295. }
  296. }
  297. if (psd->super_type)
  298. RELOC_USING(*(psd->super_type),
  299. (void *)((char *)vptr + psd->super_offset),
  300. pstype->ssize);
  301. } RELOC_PTRS_END