gsmalloc.c 15 KB

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