gsmalloc.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. /* Copyright (C) 1998, 2000, 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: gsmalloc.c,v 1.13 2004/08/18 22:24:47 stefan Exp $ */
  14. /* C heap allocator */
  15. #include "malloc_.h"
  16. #include "gdebug.h"
  17. #include "gserror.h"
  18. #include "gserrors.h"
  19. #include "gstypes.h"
  20. #include "gsmemory.h"
  21. #include "gsmdebug.h"
  22. #include "gsstruct.h" /* for st_bytes */
  23. #include "gsmalloc.h"
  24. #include "gsmemlok.h" /* locking (multithreading) wrapper */
  25. #include "gsmemret.h" /* retrying wrapper */
  26. /* ------ Heap allocator ------ */
  27. /*
  28. * An implementation of Ghostscript's memory manager interface
  29. * that works directly with the C heap. We keep track of all allocated
  30. * blocks so we can free them at cleanup time.
  31. */
  32. /* Raw memory procedures */
  33. private gs_memory_proc_alloc_bytes(gs_heap_alloc_bytes);
  34. private gs_memory_proc_resize_object(gs_heap_resize_object);
  35. private gs_memory_proc_free_object(gs_heap_free_object);
  36. private gs_memory_proc_stable(gs_heap_stable);
  37. private gs_memory_proc_status(gs_heap_status);
  38. private gs_memory_proc_free_all(gs_heap_free_all);
  39. /* Object memory procedures */
  40. private gs_memory_proc_alloc_struct(gs_heap_alloc_struct);
  41. private gs_memory_proc_alloc_byte_array(gs_heap_alloc_byte_array);
  42. private gs_memory_proc_alloc_struct_array(gs_heap_alloc_struct_array);
  43. private gs_memory_proc_object_size(gs_heap_object_size);
  44. private gs_memory_proc_object_type(gs_heap_object_type);
  45. private gs_memory_proc_alloc_string(gs_heap_alloc_string);
  46. private gs_memory_proc_resize_string(gs_heap_resize_string);
  47. private gs_memory_proc_free_string(gs_heap_free_string);
  48. private gs_memory_proc_register_root(gs_heap_register_root);
  49. private gs_memory_proc_unregister_root(gs_heap_unregister_root);
  50. private gs_memory_proc_enable_free(gs_heap_enable_free);
  51. private const gs_memory_procs_t gs_malloc_memory_procs =
  52. {
  53. /* Raw memory procedures */
  54. gs_heap_alloc_bytes,
  55. gs_heap_resize_object,
  56. gs_heap_free_object,
  57. gs_heap_stable,
  58. gs_heap_status,
  59. gs_heap_free_all,
  60. gs_ignore_consolidate_free,
  61. /* Object memory procedures */
  62. gs_heap_alloc_bytes,
  63. gs_heap_alloc_struct,
  64. gs_heap_alloc_struct,
  65. gs_heap_alloc_byte_array,
  66. gs_heap_alloc_byte_array,
  67. gs_heap_alloc_struct_array,
  68. gs_heap_alloc_struct_array,
  69. gs_heap_object_size,
  70. gs_heap_object_type,
  71. gs_heap_alloc_string,
  72. gs_heap_alloc_string,
  73. gs_heap_resize_string,
  74. gs_heap_free_string,
  75. gs_heap_register_root,
  76. gs_heap_unregister_root,
  77. gs_heap_enable_free
  78. };
  79. /* We must make sure that malloc_blocks leave the block aligned. */
  80. /*typedef struct gs_malloc_block_s gs_malloc_block_t; */
  81. #define malloc_block_data\
  82. gs_malloc_block_t *next;\
  83. gs_malloc_block_t *prev;\
  84. uint size;\
  85. gs_memory_type_ptr_t type;\
  86. client_name_t cname
  87. struct malloc_block_data_s {
  88. malloc_block_data;
  89. };
  90. struct gs_malloc_block_s {
  91. malloc_block_data;
  92. /* ANSI C does not allow zero-size arrays, so we need the following */
  93. /* unnecessary and wasteful workaround: */
  94. #define _npad (-size_of(struct malloc_block_data_s) & (arch_align_memory_mod - 1))
  95. byte _pad[(_npad == 0 ? arch_align_memory_mod : _npad)];
  96. #undef _npad
  97. };
  98. /* Initialize a malloc allocator. */
  99. private long heap_available(void);
  100. gs_malloc_memory_t *
  101. gs_malloc_memory_init(void)
  102. {
  103. gs_malloc_memory_t *mem =
  104. (gs_malloc_memory_t *)malloc(sizeof(gs_malloc_memory_t));
  105. mem->stable_memory = 0; /* just for tidyness, never referenced */
  106. mem->procs = gs_malloc_memory_procs;
  107. mem->allocated = 0;
  108. mem->limit = max_long;
  109. mem->used = 0;
  110. mem->max_used = 0;
  111. mem->gs_lib_ctx = 0;
  112. mem->non_gc_memory = (gs_memory_t *)mem;
  113. return mem;
  114. }
  115. /*
  116. * Estimate the amount of available memory by probing with mallocs.
  117. * We may under-estimate by a lot, but that's better than winding up with
  118. * a seriously inflated address space. This is quite a hack!
  119. */
  120. #define max_malloc_probes 20
  121. #define malloc_probe_size 64000
  122. private long
  123. heap_available()
  124. {
  125. long avail = 0;
  126. void *probes[max_malloc_probes];
  127. uint n;
  128. for (n = 0; n < max_malloc_probes; n++) {
  129. if ((probes[n] = malloc(malloc_probe_size)) == 0)
  130. break;
  131. if_debug2('a', "[a]heap_available probe[%d]=0x%lx\n",
  132. n, (ulong) probes[n]);
  133. avail += malloc_probe_size;
  134. }
  135. while (n)
  136. free(probes[--n]);
  137. return avail;
  138. }
  139. /* Allocate various kinds of blocks. */
  140. private byte *
  141. gs_heap_alloc_bytes(gs_memory_t * mem, uint size, client_name_t cname)
  142. {
  143. gs_malloc_memory_t *mmem = (gs_malloc_memory_t *) mem;
  144. byte *ptr = 0;
  145. #ifdef DEBUG
  146. const char *msg;
  147. static const char *const ok_msg = "OK";
  148. # define set_msg(str) (msg = (str))
  149. #else
  150. # define set_msg(str) DO_NOTHING
  151. #endif
  152. if (size > mmem->limit - sizeof(gs_malloc_block_t)) {
  153. /* Definitely too large to allocate; also avoids overflow. */
  154. set_msg("exceeded limit");
  155. } else {
  156. uint added = size + sizeof(gs_malloc_block_t);
  157. if (mmem->limit - added < mmem->used)
  158. set_msg("exceeded limit");
  159. else if ((ptr = (byte *) malloc(added)) == 0)
  160. set_msg("failed");
  161. else {
  162. gs_malloc_block_t *bp = (gs_malloc_block_t *) ptr;
  163. /*
  164. * We would like to check that malloc aligns blocks at least as
  165. * strictly as the compiler (as defined by arch_align_memory_mod).
  166. * However, Microsoft VC 6 does not satisfy this requirement.
  167. * See gsmemory.h for more explanation.
  168. */
  169. set_msg(ok_msg);
  170. if (mmem->allocated)
  171. mmem->allocated->prev = bp;
  172. bp->next = mmem->allocated;
  173. bp->prev = 0;
  174. bp->size = size;
  175. bp->type = &st_bytes;
  176. bp->cname = cname;
  177. mmem->allocated = bp;
  178. ptr = (byte *) (bp + 1);
  179. gs_alloc_fill(ptr, gs_alloc_fill_alloc, size);
  180. mmem->used += size + sizeof(gs_malloc_block_t);
  181. if (mmem->used > mmem->max_used)
  182. mmem->max_used = mmem->used;
  183. }
  184. }
  185. #ifdef DEBUG
  186. if (gs_debug_c('a') || msg != ok_msg)
  187. dlprintf4("[a+]gs_malloc(%s)(%u) = 0x%lx: %s\n",
  188. client_name_string(cname), size, (ulong) ptr, msg);
  189. #endif
  190. return ptr;
  191. #undef set_msg
  192. }
  193. private void *
  194. gs_heap_alloc_struct(gs_memory_t * mem, gs_memory_type_ptr_t pstype,
  195. client_name_t cname)
  196. {
  197. void *ptr =
  198. gs_heap_alloc_bytes(mem, gs_struct_type_size(pstype), cname);
  199. if (ptr == 0)
  200. return 0;
  201. ((gs_malloc_block_t *) ptr)[-1].type = pstype;
  202. return ptr;
  203. }
  204. private byte *
  205. gs_heap_alloc_byte_array(gs_memory_t * mem, uint num_elements, uint elt_size,
  206. client_name_t cname)
  207. {
  208. ulong lsize = (ulong) num_elements * elt_size;
  209. if (lsize != (uint) lsize)
  210. return 0;
  211. return gs_heap_alloc_bytes(mem, (uint) lsize, cname);
  212. }
  213. private void *
  214. gs_heap_alloc_struct_array(gs_memory_t * mem, uint num_elements,
  215. gs_memory_type_ptr_t pstype, client_name_t cname)
  216. {
  217. void *ptr =
  218. gs_heap_alloc_byte_array(mem, num_elements,
  219. gs_struct_type_size(pstype), cname);
  220. if (ptr == 0)
  221. return 0;
  222. ((gs_malloc_block_t *) ptr)[-1].type = pstype;
  223. return ptr;
  224. }
  225. private void *
  226. gs_heap_resize_object(gs_memory_t * mem, void *obj, uint new_num_elements,
  227. client_name_t cname)
  228. {
  229. gs_malloc_memory_t *mmem = (gs_malloc_memory_t *) mem;
  230. gs_malloc_block_t *ptr = (gs_malloc_block_t *) obj - 1;
  231. gs_memory_type_ptr_t pstype = ptr->type;
  232. uint old_size = gs_object_size(mem, obj) + sizeof(gs_malloc_block_t);
  233. uint new_size =
  234. gs_struct_type_size(pstype) * new_num_elements +
  235. sizeof(gs_malloc_block_t);
  236. gs_malloc_block_t *new_ptr;
  237. if (new_size == old_size)
  238. return obj;
  239. new_ptr = (gs_malloc_block_t *) gs_realloc(ptr, old_size, new_size);
  240. if (new_ptr == 0)
  241. return 0;
  242. if (new_ptr->prev)
  243. new_ptr->prev->next = new_ptr;
  244. else
  245. mmem->allocated = new_ptr;
  246. if (new_ptr->next)
  247. new_ptr->next->prev = new_ptr;
  248. new_ptr->size = new_size - sizeof(gs_malloc_block_t);
  249. mmem->used -= old_size;
  250. mmem->used += new_size;
  251. if (new_size > old_size)
  252. gs_alloc_fill((byte *) new_ptr + old_size,
  253. gs_alloc_fill_alloc, new_size - old_size);
  254. return new_ptr + 1;
  255. }
  256. private uint
  257. gs_heap_object_size(gs_memory_t * mem, const void *ptr)
  258. {
  259. return ((const gs_malloc_block_t *)ptr)[-1].size;
  260. }
  261. private gs_memory_type_ptr_t
  262. gs_heap_object_type(gs_memory_t * mem, const void *ptr)
  263. {
  264. return ((const gs_malloc_block_t *)ptr)[-1].type;
  265. }
  266. private void
  267. gs_heap_free_object(gs_memory_t * mem, void *ptr, client_name_t cname)
  268. {
  269. gs_malloc_memory_t *mmem = (gs_malloc_memory_t *) mem;
  270. gs_malloc_block_t *bp = mmem->allocated;
  271. gs_memory_type_ptr_t pstype;
  272. struct_proc_finalize((*finalize));
  273. if_debug3('a', "[a-]gs_free(%s) 0x%lx(%u)\n",
  274. client_name_string(cname), (ulong) ptr,
  275. (ptr == 0 ? 0 : ((gs_malloc_block_t *) ptr)[-1].size));
  276. if (ptr == 0)
  277. return;
  278. pstype = ((gs_malloc_block_t *) ptr)[-1].type;
  279. finalize = pstype->finalize;
  280. if (finalize != 0) {
  281. if_debug3('u', "[u]finalizing %s 0x%lx (%s)\n",
  282. struct_type_name_string(pstype),
  283. (ulong) ptr, client_name_string(cname));
  284. (*finalize) (ptr);
  285. }
  286. if (ptr == bp + 1) {
  287. mmem->allocated = bp->next;
  288. mmem->used -= bp->size + sizeof(gs_malloc_block_t);
  289. if (mmem->allocated)
  290. mmem->allocated->prev = 0;
  291. gs_alloc_fill(bp, gs_alloc_fill_free,
  292. bp->size + sizeof(gs_malloc_block_t));
  293. free(bp);
  294. } else {
  295. gs_malloc_block_t *np;
  296. /*
  297. * bp == 0 at this point is an error, but we'd rather have an
  298. * error message than an invalid access.
  299. */
  300. if (bp) {
  301. for (; (np = bp->next) != 0; bp = np) {
  302. if (ptr == np + 1) {
  303. bp->next = np->next;
  304. if (np->next)
  305. np->next->prev = bp;
  306. mmem->used -= np->size + sizeof(gs_malloc_block_t);
  307. gs_alloc_fill(np, gs_alloc_fill_free,
  308. np->size + sizeof(gs_malloc_block_t));
  309. free(np);
  310. return;
  311. }
  312. }
  313. }
  314. lprintf2("%s: free 0x%lx not found!\n",
  315. client_name_string(cname), (ulong) ptr);
  316. free((char *)((gs_malloc_block_t *) ptr - 1));
  317. }
  318. }
  319. private byte *
  320. gs_heap_alloc_string(gs_memory_t * mem, uint nbytes, client_name_t cname)
  321. {
  322. return gs_heap_alloc_bytes(mem, nbytes, cname);
  323. }
  324. private byte *
  325. gs_heap_resize_string(gs_memory_t * mem, byte * data, uint old_num, uint new_num,
  326. client_name_t cname)
  327. {
  328. if (gs_heap_object_type(mem, data) != &st_bytes)
  329. lprintf2("%s: resizing non-string 0x%lx!\n",
  330. client_name_string(cname), (ulong) data);
  331. return gs_heap_resize_object(mem, data, new_num, cname);
  332. }
  333. private void
  334. gs_heap_free_string(gs_memory_t * mem, byte * data, uint nbytes,
  335. client_name_t cname)
  336. {
  337. /****** SHOULD CHECK SIZE IF DEBUGGING ******/
  338. gs_heap_free_object(mem, data, cname);
  339. }
  340. private int
  341. gs_heap_register_root(gs_memory_t * mem, gs_gc_root_t * rp,
  342. gs_ptr_type_t ptype, void **up, client_name_t cname)
  343. {
  344. return 0;
  345. }
  346. private void
  347. gs_heap_unregister_root(gs_memory_t * mem, gs_gc_root_t * rp,
  348. client_name_t cname)
  349. {
  350. }
  351. private gs_memory_t *
  352. gs_heap_stable(gs_memory_t *mem)
  353. {
  354. return mem; /* heap memory is stable */
  355. }
  356. private void
  357. gs_heap_status(gs_memory_t * mem, gs_memory_status_t * pstat)
  358. {
  359. gs_malloc_memory_t *mmem = (gs_malloc_memory_t *) mem;
  360. pstat->allocated = mmem->used + heap_available();
  361. pstat->used = mmem->used;
  362. }
  363. private void
  364. gs_heap_enable_free(gs_memory_t * mem, bool enable)
  365. {
  366. if (enable)
  367. mem->procs.free_object = gs_heap_free_object,
  368. mem->procs.free_string = gs_heap_free_string;
  369. else
  370. mem->procs.free_object = gs_ignore_free_object,
  371. mem->procs.free_string = gs_ignore_free_string;
  372. }
  373. /* Release all memory acquired by this allocator. */
  374. private void
  375. gs_heap_free_all(gs_memory_t * mem, uint free_mask, client_name_t cname)
  376. {
  377. gs_malloc_memory_t *const mmem = (gs_malloc_memory_t *) mem;
  378. if (free_mask & FREE_ALL_DATA) {
  379. gs_malloc_block_t *bp = mmem->allocated;
  380. gs_malloc_block_t *np;
  381. for (; bp != 0; bp = np) {
  382. np = bp->next;
  383. if_debug3('a', "[a]gs_heap_free_all(%s) 0x%lx(%u)\n",
  384. client_name_string(bp->cname), (ulong) (bp + 1),
  385. bp->size);
  386. gs_alloc_fill(bp + 1, gs_alloc_fill_free, bp->size);
  387. free(bp);
  388. }
  389. }
  390. if (free_mask & FREE_ALL_ALLOCATOR)
  391. free(mem);
  392. }
  393. /* ------ Wrapping ------ */
  394. /* Create the retrying and the locked wrapper for the heap allocator. */
  395. int
  396. gs_malloc_wrap(gs_memory_t **wrapped, gs_malloc_memory_t *contents)
  397. {
  398. gs_memory_t *cmem = (gs_memory_t *)contents;
  399. gs_memory_locked_t *lmem = (gs_memory_locked_t *)
  400. gs_alloc_bytes_immovable(cmem, sizeof(gs_memory_locked_t),
  401. "gs_malloc_wrap(locked)");
  402. gs_memory_retrying_t *rmem;
  403. int code;
  404. if (lmem == 0)
  405. return_error(gs_error_VMerror);
  406. code = gs_memory_locked_init(lmem, cmem);
  407. if (code < 0) {
  408. gs_free_object(cmem, lmem, "gs_malloc_wrap(locked)");
  409. return code;
  410. }
  411. rmem = (gs_memory_retrying_t *)
  412. gs_alloc_bytes_immovable((gs_memory_t *)lmem,
  413. sizeof(gs_memory_retrying_t),
  414. "gs_malloc_wrap(retrying)");
  415. if (rmem == 0) {
  416. gs_memory_locked_release(lmem);
  417. gs_free_object(cmem, lmem, "gs_malloc_wrap(locked)");
  418. return_error(gs_error_VMerror);
  419. }
  420. code = gs_memory_retrying_init(rmem, (gs_memory_t *)lmem);
  421. if (code < 0) {
  422. gs_free_object((gs_memory_t *)lmem, rmem, "gs_malloc_wrap(retrying)");
  423. gs_memory_locked_release(lmem);
  424. gs_free_object(cmem, lmem, "gs_malloc_wrap(locked)");
  425. return code;
  426. }
  427. *wrapped = (gs_memory_t *)rmem;
  428. return 0;
  429. }
  430. /* Get the wrapped contents. */
  431. gs_malloc_memory_t *
  432. gs_malloc_wrapped_contents(gs_memory_t *wrapped)
  433. {
  434. gs_memory_retrying_t *rmem = (gs_memory_retrying_t *)wrapped;
  435. gs_memory_locked_t *lmem =
  436. (gs_memory_locked_t *)gs_memory_retrying_target(rmem);
  437. if (lmem)
  438. return (gs_malloc_memory_t *)gs_memory_locked_target(lmem);
  439. return (gs_malloc_memory_t *) wrapped;
  440. }
  441. /* Free the wrapper, and return the wrapped contents. */
  442. gs_malloc_memory_t *
  443. gs_malloc_unwrap(gs_memory_t *wrapped)
  444. {
  445. gs_memory_retrying_t *rmem = (gs_memory_retrying_t *)wrapped;
  446. gs_memory_locked_t *lmem =
  447. (gs_memory_locked_t *)gs_memory_retrying_target(rmem);
  448. gs_memory_t *contents = gs_memory_locked_target(lmem);
  449. gs_free_object((gs_memory_t *)lmem, rmem, "gs_malloc_unwrap(retrying)");
  450. gs_memory_locked_release(lmem);
  451. gs_free_object(contents, lmem, "gs_malloc_unwrap(locked)");
  452. return (gs_malloc_memory_t *)contents;
  453. }
  454. /* Create the default allocator, and return it. */
  455. gs_memory_t *
  456. gs_malloc_init(const gs_memory_t *parent)
  457. {
  458. gs_malloc_memory_t *malloc_memory_default = gs_malloc_memory_init();
  459. gs_memory_t *memory_t_default;
  460. if (parent)
  461. malloc_memory_default->gs_lib_ctx = parent->gs_lib_ctx;
  462. else
  463. gs_lib_ctx_init((gs_memory_t *)malloc_memory_default);
  464. gs_malloc_wrap(&memory_t_default, malloc_memory_default);
  465. memory_t_default->stable_memory = memory_t_default;
  466. return memory_t_default;
  467. }
  468. /* Release the default allocator. */
  469. void
  470. gs_malloc_release(gs_memory_t *mem)
  471. {
  472. gs_malloc_memory_t * malloc_memory_default = gs_malloc_unwrap(mem);
  473. gs_malloc_memory_release(malloc_memory_default);
  474. }