gsmemory.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. /* Copyright (C) 1993, 1996, 1997, 1998, 1999, 2001 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: gsmemory.h,v 1.8 2004/08/04 19:36:12 stefan Exp $ */
  14. /* Client interface for memory allocation */
  15. /*
  16. * The allocator knows about two basic kinds of memory: objects, which are
  17. * aligned and cannot have pointers to their interior, and strings, which
  18. * are not aligned and which can have interior references.
  19. *
  20. * Note: OBJECTS ARE NOT GUARANTEED to be aligned any more strictly than
  21. * required by the hardware, regardless of the value of obj_align_mod. In
  22. * other words, whether ALIGNMENT_MOD(ptr, obj_align_mod) will be zero
  23. * depends on the alignment provided by the underlying allocator.
  24. * Most systems ensure this, but Microsoft VC 6 in particular does not.
  25. * See gsmemraw.h for more information about this.
  26. *
  27. * The standard allocator is designed to interface to a garbage collector,
  28. * although it does not include or call one. The allocator API recognizes
  29. * that the garbage collector may move objects, relocating pointers to them;
  30. * the API provides for allocating both movable (the default) and immovable
  31. * objects. Clients must not attempt to resize immovable objects, and must
  32. * not create references to substrings of immovable strings.
  33. */
  34. #ifndef gsmemory_INCLUDED
  35. # define gsmemory_INCLUDED
  36. #include "gstypes.h" /* for gs_bytestring */
  37. #include "gslibctx.h"
  38. /* Define the opaque type for a structure descriptor. */
  39. typedef struct gs_memory_struct_type_s gs_memory_struct_type_t;
  40. typedef const gs_memory_struct_type_t *gs_memory_type_ptr_t;
  41. /* Define the opaque type for an allocator. */
  42. /* (The actual structure is defined later in this file.) */
  43. #ifndef gs_memory_DEFINED
  44. # define gs_memory_DEFINED
  45. typedef struct gs_memory_s gs_memory_t;
  46. #endif
  47. /* Define the opaque type for a pointer type. */
  48. typedef struct gs_ptr_procs_s gs_ptr_procs_t;
  49. typedef const gs_ptr_procs_t *gs_ptr_type_t;
  50. /* Define the opaque type for a GC root. */
  51. typedef struct gs_gc_root_s gs_gc_root_t;
  52. /* Accessors for structure types. */
  53. typedef client_name_t struct_name_t;
  54. /* Get the size of a structure from the descriptor. */
  55. uint gs_struct_type_size(gs_memory_type_ptr_t);
  56. /* Get the name of a structure from the descriptor. */
  57. struct_name_t gs_struct_type_name(gs_memory_type_ptr_t);
  58. #define gs_struct_type_name_string(styp)\
  59. ((const char *)gs_struct_type_name(styp))
  60. /*
  61. * Define the structure for reporting memory manager statistics.
  62. */
  63. typedef struct gs_memory_status_s {
  64. /*
  65. * "Allocated" space is the total amount of space acquired from
  66. * the parent of the memory manager. It includes space used for
  67. * allocated data, space available for allocation, and overhead.
  68. */
  69. ulong allocated;
  70. /*
  71. * "Used" space is the amount of space used by allocated data
  72. * plus overhead.
  73. */
  74. ulong used;
  75. } gs_memory_status_t;
  76. /*
  77. * Allocate bytes. The bytes are always aligned maximally
  78. * if the processor requires alignment.
  79. *
  80. * Note that the object memory level can allocate bytes as
  81. * either movable or immovable: raw memory blocks are
  82. * always immovable.
  83. */
  84. #define gs_memory_t_proc_alloc_bytes(proc, mem_t)\
  85. byte *proc(mem_t *mem, uint nbytes, client_name_t cname)
  86. #define gs_alloc_bytes_immovable(mem, nbytes, cname)\
  87. ((mem)->procs.alloc_bytes_immovable(mem, nbytes, cname))
  88. /*
  89. * Resize an object to a new number of elements. At the raw
  90. * memory level, the "element" is a byte; for object memory
  91. * (gsmemory.h), the object may be an an array of either
  92. * bytes or structures. The new size may be larger than,
  93. * the same as, or smaller than the old. If the new size is
  94. * the same as the old, resize_object returns the same
  95. * object; otherwise, it preserves the first min(old_size,
  96. * new_size) bytes of the object's contents.
  97. */
  98. #define gs_memory_t_proc_resize_object(proc, mem_t)\
  99. void *proc(mem_t *mem, void *obj, uint new_num_elements,\
  100. client_name_t cname)
  101. #define gs_resize_object(mem, obj, newn, cname)\
  102. ((mem)->procs.resize_object(mem, obj, newn, cname))
  103. /*
  104. * Free an object (at the object memory level, this includes
  105. * everything except strings). Note: data == 0 must be
  106. * allowed, and must be a no-op.
  107. */
  108. #define gs_memory_t_proc_free_object(proc, mem_t)\
  109. void proc(mem_t *mem, void *data, client_name_t cname)
  110. #define gs_free_object(mem, data, cname)\
  111. ((mem)->procs.free_object(mem, data, cname))
  112. /*
  113. * Report status (assigned, used).
  114. */
  115. #define gs_memory_t_proc_status(proc, mem_t)\
  116. void proc(mem_t *mem, gs_memory_status_t *status)
  117. #define gs_memory_status(mem, pst)\
  118. ((mem)->procs.status(mem, pst))
  119. /*
  120. * Return the stable allocator for this allocator. The
  121. * stable allocator allocates from the same heap and in
  122. * the same VM space, but is not subject to save and restore.
  123. * (It is the client's responsibility to avoid creating
  124. * dangling pointers.)
  125. *
  126. * Note that the stable allocator may be the same allocator
  127. * as this one.
  128. */
  129. #define gs_memory_t_proc_stable(proc, mem_t)\
  130. mem_t *proc(mem_t *mem)
  131. #define gs_memory_stable(mem)\
  132. ((mem)->procs.stable(mem))
  133. /*
  134. * Free one or more of: data memory acquired by the allocator
  135. * (FREE_ALL_DATA), overhead structures other than the
  136. * allocator itself (FREE_ALL_STRUCTURES), and the allocator
  137. * itself (FREE_ALL_ALLOCATOR). Note that this requires
  138. * allocators to keep track of all the memory they have ever
  139. * acquired, and where they acquired it. Note that this
  140. * operation propagates to the stable allocator (if
  141. * different).
  142. */
  143. #define FREE_ALL_DATA 1
  144. #define FREE_ALL_STRUCTURES 2
  145. #define FREE_ALL_ALLOCATOR 4
  146. #define FREE_ALL_EVERYTHING\
  147. (FREE_ALL_DATA | FREE_ALL_STRUCTURES | FREE_ALL_ALLOCATOR)
  148. #define gs_memory_t_proc_free_all(proc, mem_t)\
  149. void proc(mem_t *mem, uint free_mask, client_name_t cname)
  150. #define gs_memory_free_all(mem, free_mask, cname)\
  151. ((mem)->procs.free_all(mem, free_mask, cname))
  152. /* Backward compatibility */
  153. #define gs_free_all(mem)\
  154. gs_memory_free_all(mem, FREE_ALL_DATA, "(free_all)")
  155. /*
  156. * Consolidate free space. This may be used as part of (or
  157. * as an alternative to) garbage collection, or before
  158. * giving up on an attempt to allocate.
  159. */
  160. #define gs_memory_t_proc_consolidate_free(proc, mem_t)\
  161. void proc(mem_t *mem)
  162. #define gs_consolidate_free(mem)\
  163. ((mem)->procs.consolidate_free(mem))
  164. /* Define the members of the procedure structure. */
  165. #define gs_raw_memory_procs(mem_t)\
  166. gs_memory_t_proc_alloc_bytes((*alloc_bytes_immovable), mem_t);\
  167. gs_memory_t_proc_resize_object((*resize_object), mem_t);\
  168. gs_memory_t_proc_free_object((*free_object), mem_t);\
  169. gs_memory_t_proc_stable((*stable), mem_t);\
  170. gs_memory_t_proc_status((*status), mem_t);\
  171. gs_memory_t_proc_free_all((*free_all), mem_t);\
  172. gs_memory_t_proc_consolidate_free((*consolidate_free), mem_t)
  173. /*
  174. * Define the memory manager procedural interface.
  175. */
  176. typedef struct gs_memory_procs_s {
  177. gs_raw_memory_procs(gs_memory_t);
  178. /* Redefine inherited procedures with the new allocator type. */
  179. #define gs_memory_proc_alloc_bytes(proc)\
  180. gs_memory_t_proc_alloc_bytes(proc, gs_memory_t)
  181. #define gs_memory_proc_resize_object(proc)\
  182. gs_memory_t_proc_resize_object(proc, gs_memory_t)
  183. #define gs_memory_proc_free_object(proc)\
  184. gs_memory_t_proc_free_object(proc, gs_memory_t)
  185. #define gs_memory_proc_stable(proc)\
  186. gs_memory_t_proc_stable(proc, gs_memory_t)
  187. #define gs_memory_proc_status(proc)\
  188. gs_memory_t_proc_status(proc, gs_memory_t)
  189. #define gs_memory_proc_free_all(proc)\
  190. gs_memory_t_proc_free_all(proc, gs_memory_t)
  191. #define gs_memory_proc_consolidate_free(proc)\
  192. gs_memory_t_proc_consolidate_free(proc, gs_memory_t)
  193. /*
  194. * Allocate possibly movable bytes. (We inherit allocating immovable
  195. * bytes from the raw memory allocator.)
  196. */
  197. #define gs_alloc_bytes(mem, nbytes, cname)\
  198. (*(mem)->procs.alloc_bytes)(mem, nbytes, cname)
  199. gs_memory_proc_alloc_bytes((*alloc_bytes));
  200. /*
  201. * Allocate a structure.
  202. */
  203. #define gs_memory_proc_alloc_struct(proc)\
  204. void *proc(gs_memory_t *mem, gs_memory_type_ptr_t pstype,\
  205. client_name_t cname)
  206. #define gs_alloc_struct(mem, typ, pstype, cname)\
  207. (typ *)(*(mem)->procs.alloc_struct)(mem, pstype, cname)
  208. gs_memory_proc_alloc_struct((*alloc_struct));
  209. #define gs_alloc_struct_immovable(mem, typ, pstype, cname)\
  210. (typ *)(*(mem)->procs.alloc_struct_immovable)(mem, pstype, cname)
  211. gs_memory_proc_alloc_struct((*alloc_struct_immovable));
  212. /*
  213. * Allocate an array of bytes.
  214. */
  215. #define gs_memory_proc_alloc_byte_array(proc)\
  216. byte *proc(gs_memory_t *mem, uint num_elements, uint elt_size,\
  217. client_name_t cname)
  218. #define gs_alloc_byte_array(mem, nelts, esize, cname)\
  219. (*(mem)->procs.alloc_byte_array)(mem, nelts, esize, cname)
  220. gs_memory_proc_alloc_byte_array((*alloc_byte_array));
  221. #define gs_alloc_byte_array_immovable(mem, nelts, esize, cname)\
  222. (*(mem)->procs.alloc_byte_array_immovable)(mem, nelts, esize, cname)
  223. gs_memory_proc_alloc_byte_array((*alloc_byte_array_immovable));
  224. /*
  225. * Allocate an array of structures.
  226. */
  227. #define gs_memory_proc_alloc_struct_array(proc)\
  228. void *proc(gs_memory_t *mem, uint num_elements,\
  229. gs_memory_type_ptr_t pstype, client_name_t cname)
  230. #define gs_alloc_struct_array(mem, nelts, typ, pstype, cname)\
  231. (typ *)(*(mem)->procs.alloc_struct_array)(mem, nelts, pstype, cname)
  232. gs_memory_proc_alloc_struct_array((*alloc_struct_array));
  233. #define gs_alloc_struct_array_immovable(mem, nelts, typ, pstype, cname)\
  234. (typ *)(*(mem)->procs.alloc_struct_array_immovable)(mem, nelts, pstype, cname)
  235. gs_memory_proc_alloc_struct_array((*alloc_struct_array_immovable));
  236. /*
  237. * Get the size of an object (anything except a string).
  238. */
  239. #define gs_memory_proc_object_size(proc)\
  240. uint proc(gs_memory_t *mem, const void *obj)
  241. #define gs_object_size(mem, obj)\
  242. (*(mem)->procs.object_size)(mem, obj)
  243. gs_memory_proc_object_size((*object_size));
  244. /*
  245. * Get the type of an object (anything except a string).
  246. * The value returned for byte objects is useful only for
  247. * printing.
  248. */
  249. #define gs_memory_proc_object_type(proc)\
  250. gs_memory_type_ptr_t proc(gs_memory_t *mem, const void *obj)
  251. #define gs_object_type(mem, obj)\
  252. (*(mem)->procs.object_type)(mem, obj)
  253. gs_memory_proc_object_type((*object_type));
  254. /*
  255. * Allocate a string (unaligned bytes).
  256. */
  257. #define gs_memory_proc_alloc_string(proc)\
  258. byte *proc(gs_memory_t *mem, uint nbytes, client_name_t cname)
  259. #define gs_alloc_string(mem, nbytes, cname)\
  260. (*(mem)->procs.alloc_string)(mem, nbytes, cname)
  261. gs_memory_proc_alloc_string((*alloc_string));
  262. #define gs_alloc_string_immovable(mem, nbytes, cname)\
  263. (*(mem)->procs.alloc_string_immovable)(mem, nbytes, cname)
  264. gs_memory_proc_alloc_string((*alloc_string_immovable));
  265. /*
  266. * Resize a string. The specification is the same as resize_object
  267. * except that the element size is always a byte.
  268. */
  269. #define gs_memory_proc_resize_string(proc)\
  270. byte *proc(gs_memory_t *mem, byte *data, uint old_num, uint new_num,\
  271. client_name_t cname)
  272. #define gs_resize_string(mem, data, oldn, newn, cname)\
  273. (*(mem)->procs.resize_string)(mem, data, oldn, newn, cname)
  274. gs_memory_proc_resize_string((*resize_string));
  275. /*
  276. * Free a string.
  277. */
  278. #define gs_memory_proc_free_string(proc)\
  279. void proc(gs_memory_t *mem, byte *data, uint nbytes,\
  280. client_name_t cname)
  281. #define gs_free_string(mem, data, nbytes, cname)\
  282. (*(mem)->procs.free_string)(mem, data, nbytes, cname)
  283. gs_memory_proc_free_string((*free_string));
  284. /*
  285. * Register a root for the garbage collector. root = NULL
  286. * asks the memory manager to allocate the root object
  287. * itself (immovable, in the manager's parent): this is the usual
  288. * way to call this procedure.
  289. */
  290. #define gs_memory_proc_register_root(proc)\
  291. int proc(gs_memory_t *mem, gs_gc_root_t *root, gs_ptr_type_t ptype,\
  292. void **pp, client_name_t cname)
  293. #define gs_register_root(mem, root, ptype, pp, cname)\
  294. (*(mem)->procs.register_root)(mem, root, ptype, pp, cname)
  295. gs_memory_proc_register_root((*register_root));
  296. /*
  297. * Unregister a root. The root object itself will be freed iff
  298. * it was allocated by gs_register_root.
  299. */
  300. #define gs_memory_proc_unregister_root(proc)\
  301. void proc(gs_memory_t *mem, gs_gc_root_t *root, client_name_t cname)
  302. #define gs_unregister_root(mem, root, cname)\
  303. (*(mem)->procs.unregister_root)(mem, root, cname)
  304. gs_memory_proc_unregister_root((*unregister_root));
  305. /*
  306. * Enable or disable the freeing operations: when disabled,
  307. * these operations return normally but do nothing. The
  308. * garbage collector and the PostScript interpreter
  309. * 'restore' operator need to temporarily disable the
  310. * freeing functions of (an) allocator(s) while running
  311. * finalization procedures.
  312. */
  313. #define gs_memory_proc_enable_free(proc)\
  314. void proc(gs_memory_t *mem, bool enable)
  315. #define gs_enable_free(mem, enable)\
  316. (*(mem)->procs.enable_free)(mem, enable)
  317. gs_memory_proc_enable_free((*enable_free));
  318. } gs_memory_procs_t;
  319. /*
  320. * Define versions of the freeing procedures that are applicable even if the
  321. * pointer is declared as const T *. These are intended for use where a
  322. * structure contains a pointer member whose referent is declared as const
  323. * because it is const for all ordinary clients.
  324. */
  325. void gs_free_const_object(gs_memory_t *mem, const void *data,
  326. client_name_t cname);
  327. void gs_free_const_string(gs_memory_t *mem, const byte *data, uint nbytes,
  328. client_name_t cname);
  329. /*
  330. * Free a [const] bytestring. Note that this is *not* a member procedure of
  331. * the allocator: it calls the free_object or free_string procedure.
  332. */
  333. void gs_free_bytestring(gs_memory_t *mem, gs_bytestring *pbs,
  334. client_name_t cname);
  335. void gs_free_const_bytestring(gs_memory_t *mem, gs_const_bytestring *pbs,
  336. client_name_t cname);
  337. /*
  338. * Either allocate (if obj == 0) or resize (if obj != 0) a structure array.
  339. * If obj != 0, pstype is used only for checking (in DEBUG configurations).
  340. */
  341. void *gs_resize_struct_array(gs_memory_t *mem, void *obj, uint num_elements,
  342. gs_memory_type_ptr_t pstype,
  343. client_name_t cname);
  344. /* Register a structure root. This just calls gs_register_root. */
  345. int gs_register_struct_root(gs_memory_t *mem, gs_gc_root_t *root,
  346. void **pp, client_name_t cname);
  347. /* Define no-op freeing procedures for use by enable_free. */
  348. gs_memory_proc_free_object(gs_ignore_free_object);
  349. gs_memory_proc_free_string(gs_ignore_free_string);
  350. /* Define a no-op consolidation procedure. */
  351. gs_memory_proc_consolidate_free(gs_ignore_consolidate_free);
  352. /*
  353. * Allocate a structure using a "raw memory" allocator. Note that this does
  354. * not retain the identity of the structure. Note also that it returns a
  355. * void *, and does not take the type of the returned pointer as a
  356. * parameter.
  357. */
  358. void *gs_raw_alloc_struct_immovable(gs_memory_t * rmem,
  359. gs_memory_type_ptr_t pstype,
  360. client_name_t cname);
  361. typedef struct pl_mem_node_s pl_mem_node_t;
  362. /*
  363. * Define an abstract allocator instance.
  364. * Subclasses may have state as well
  365. *
  366. * stable_memory: no save or restore, maybe gc-ed
  367. * non-gc allocators stable_memory == this
  368. *
  369. * gs_lib_ctx: pointer to a library context
  370. *
  371. * head: is only used by pl_alloc in a pcl/pxl only system to track memory
  372. *
  373. * non_gc_memory: a garabge collecting allocator requires a "parent" who doesn't gc
  374. * non-gc allocators non_gc_memory == this
  375. */
  376. #define gs_memory_common\
  377. gs_memory_t *stable_memory;\
  378. gs_memory_procs_t procs;\
  379. gs_lib_ctx_t *gs_lib_ctx;\
  380. pl_mem_node_t *head;\
  381. gs_memory_t *non_gc_memory
  382. struct gs_memory_s {
  383. gs_memory_common;
  384. };
  385. #endif /* gsmemory_INCLUDED */