/src/ghostpdl/base/gsmalloc.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* Copyright (C) 2001-2021 Artifex Software, Inc. |
2 | | All Rights Reserved. |
3 | | |
4 | | This software is provided AS-IS with no warranty, either express or |
5 | | implied. |
6 | | |
7 | | This software is distributed under license and may not be copied, |
8 | | modified or distributed except as expressly authorized under the terms |
9 | | of the license contained in the file LICENSE in this distribution. |
10 | | |
11 | | Refer to licensing information at http://www.artifex.com or contact |
12 | | Artifex Software, Inc., 1305 Grant Avenue - Suite 200, Novato, |
13 | | CA 94945, U.S.A., +1(415)492-9861, for further information. |
14 | | */ |
15 | | |
16 | | |
17 | | /* C heap allocator */ |
18 | | #include "malloc_.h" |
19 | | #include "gdebug.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 "gsmemret.h" /* retrying wrapper */ |
27 | | #include "gp.h" |
28 | | |
29 | | /* ------ Heap allocator ------ */ |
30 | | |
31 | | /* |
32 | | * An implementation of Ghostscript's memory manager interface |
33 | | * that works directly with the C heap. We keep track of all allocated |
34 | | * blocks so we can free them at cleanup time. |
35 | | */ |
36 | | /* Raw memory procedures */ |
37 | | static gs_memory_proc_alloc_bytes(gs_heap_alloc_bytes); |
38 | | static gs_memory_proc_resize_object(gs_heap_resize_object); |
39 | | static gs_memory_proc_free_object(gs_heap_free_object); |
40 | | static gs_memory_proc_stable(gs_heap_stable); |
41 | | static gs_memory_proc_status(gs_heap_status); |
42 | | static gs_memory_proc_free_all(gs_heap_free_all); |
43 | | |
44 | | /* Object memory procedures */ |
45 | | static gs_memory_proc_alloc_struct(gs_heap_alloc_struct); |
46 | | static gs_memory_proc_alloc_byte_array(gs_heap_alloc_byte_array); |
47 | | static gs_memory_proc_alloc_struct_array(gs_heap_alloc_struct_array); |
48 | | static gs_memory_proc_object_size(gs_heap_object_size); |
49 | | static gs_memory_proc_object_type(gs_heap_object_type); |
50 | | static gs_memory_proc_alloc_string(gs_heap_alloc_string); |
51 | | static gs_memory_proc_resize_string(gs_heap_resize_string); |
52 | | static gs_memory_proc_free_string(gs_heap_free_string); |
53 | | static gs_memory_proc_register_root(gs_heap_register_root); |
54 | | static gs_memory_proc_unregister_root(gs_heap_unregister_root); |
55 | | static gs_memory_proc_enable_free(gs_heap_enable_free); |
56 | | static gs_memory_proc_set_object_type(gs_heap_set_object_type); |
57 | | static gs_memory_proc_defer_frees(gs_heap_defer_frees); |
58 | | static const gs_memory_procs_t gs_malloc_memory_procs = |
59 | | { |
60 | | /* Raw memory procedures */ |
61 | | gs_heap_alloc_bytes, |
62 | | gs_heap_resize_object, |
63 | | gs_heap_free_object, |
64 | | gs_heap_stable, |
65 | | gs_heap_status, |
66 | | gs_heap_free_all, |
67 | | gs_ignore_consolidate_free, |
68 | | /* Object memory procedures */ |
69 | | gs_heap_alloc_bytes, |
70 | | gs_heap_alloc_struct, |
71 | | gs_heap_alloc_struct, |
72 | | gs_heap_alloc_byte_array, |
73 | | gs_heap_alloc_byte_array, |
74 | | gs_heap_alloc_struct_array, |
75 | | gs_heap_alloc_struct_array, |
76 | | gs_heap_object_size, |
77 | | gs_heap_object_type, |
78 | | gs_heap_alloc_string, |
79 | | gs_heap_alloc_string, |
80 | | gs_heap_resize_string, |
81 | | gs_heap_free_string, |
82 | | gs_heap_register_root, |
83 | | gs_heap_unregister_root, |
84 | | gs_heap_enable_free, |
85 | | gs_heap_set_object_type, |
86 | | gs_heap_defer_frees |
87 | | }; |
88 | | |
89 | | /* We must make sure that malloc_blocks leave the block aligned. */ |
90 | | /*typedef struct gs_malloc_block_s gs_malloc_block_t; */ |
91 | | #define malloc_block_data\ |
92 | | gs_malloc_block_t *next;\ |
93 | | gs_malloc_block_t *prev;\ |
94 | | size_t size;\ |
95 | | gs_memory_type_ptr_t type;\ |
96 | | client_name_t cname |
97 | | struct malloc_block_data_s { |
98 | | malloc_block_data; |
99 | | }; |
100 | | struct gs_malloc_block_s { |
101 | | malloc_block_data; |
102 | | /* ANSI C does not allow zero-size arrays, so we need the following */ |
103 | | /* unnecessary and wasteful workaround: */ |
104 | | #define _npad (-size_of(struct malloc_block_data_s) & (ARCH_ALIGN_MEMORY_MOD - 1)) |
105 | | byte _pad[(_npad == 0 ? ARCH_ALIGN_MEMORY_MOD : _npad)]; |
106 | | #undef _npad |
107 | | }; |
108 | | |
109 | | /* Initialize a malloc allocator. */ |
110 | | static long heap_available(void); |
111 | | gs_malloc_memory_t * |
112 | | gs_malloc_memory_init(void) |
113 | 683 | { |
114 | 683 | gs_malloc_memory_t *mem = |
115 | 683 | (gs_malloc_memory_t *)Memento_label(malloc(sizeof(gs_malloc_memory_t)), "gs_malloc_memory_t"); |
116 | | |
117 | 683 | if (mem == NULL) |
118 | 0 | return NULL; |
119 | | |
120 | 683 | mem->stable_memory = 0; /* just for tidyness, never referenced */ |
121 | 683 | mem->procs = gs_malloc_memory_procs; |
122 | 683 | mem->allocated = 0; |
123 | 683 | mem->limit = max_size_t; |
124 | 683 | mem->used = 0; |
125 | 683 | mem->max_used = 0; |
126 | 683 | mem->gs_lib_ctx = 0; |
127 | 683 | mem->non_gc_memory = (gs_memory_t *)mem; |
128 | 683 | mem->thread_safe_memory = (gs_memory_t *)mem; /* this allocator is thread safe */ |
129 | | /* Allocate a monitor to serialize access to structures within */ |
130 | 683 | mem->monitor = NULL; /* prevent use during initial allocation */ |
131 | 683 | mem->monitor = gx_monitor_label(gx_monitor_alloc((gs_memory_t *)mem), "heap"); |
132 | 683 | if (mem->monitor == NULL) { |
133 | 0 | free(mem); |
134 | 0 | return NULL; |
135 | 0 | } |
136 | | |
137 | 683 | return mem; |
138 | 683 | } |
139 | | /* |
140 | | * Estimate the amount of available memory by probing with mallocs. |
141 | | * We may under-estimate by a lot, but that's better than winding up with |
142 | | * a seriously inflated address space. This is quite a hack! |
143 | | */ |
144 | 286k | #define max_malloc_probes 20 |
145 | 546k | #define malloc_probe_size 64000 |
146 | | static long |
147 | | heap_available() |
148 | 13.6k | { |
149 | 13.6k | long avail = 0; |
150 | 13.6k | void *probes[max_malloc_probes]; |
151 | 13.6k | uint n; |
152 | | |
153 | 286k | for (n = 0; n < max_malloc_probes; n++) { |
154 | 273k | if ((probes[n] = malloc(malloc_probe_size)) == 0) |
155 | 0 | break; |
156 | 273k | if_debug2('a', "[a]heap_available probe[%d]="PRI_INTPTR"\n", |
157 | 273k | n, (intptr_t) probes[n]); |
158 | 273k | avail += malloc_probe_size; |
159 | 273k | } |
160 | 286k | while (n) |
161 | 273k | free(probes[--n]); |
162 | 13.6k | return avail; |
163 | 13.6k | } |
164 | | |
165 | | /* Allocate various kinds of blocks. */ |
166 | | static byte * |
167 | | gs_heap_alloc_bytes(gs_memory_t * mem, size_t size, client_name_t cname) |
168 | 5.38M | { |
169 | 5.38M | gs_malloc_memory_t *mmem = (gs_malloc_memory_t *) mem; |
170 | 5.38M | byte *ptr = 0; |
171 | | |
172 | | #ifdef DEBUG |
173 | | const char *msg; |
174 | | static const char *const ok_msg = "OK"; |
175 | | |
176 | | # define set_msg(str) (msg = (str)) |
177 | | #else |
178 | 16.1M | # define set_msg(str) DO_NOTHING |
179 | 5.38M | #endif |
180 | | |
181 | | /* Exclusive acces so our decisions and changes are 'atomic' */ |
182 | 5.38M | if (mmem->monitor) |
183 | 5.38M | gx_monitor_enter(mmem->monitor); |
184 | 5.38M | if (size > mmem->limit - sizeof(gs_malloc_block_t)) { |
185 | | /* Definitely too large to allocate; also avoids overflow. */ |
186 | 0 | set_msg("exceeded limit"); |
187 | 5.38M | } else { |
188 | 5.38M | size_t added = size + sizeof(gs_malloc_block_t); |
189 | | |
190 | 5.38M | if (added <= size || added > mmem->limit || mmem->limit - added < mmem->used) |
191 | 5.38M | set_msg("exceeded limit"); |
192 | 5.38M | else if ((ptr = (byte *) Memento_label(malloc(added), cname)) == 0) |
193 | 5.38M | set_msg("failed"); |
194 | 5.38M | else { |
195 | 5.38M | gs_malloc_block_t *bp = (gs_malloc_block_t *) ptr; |
196 | | |
197 | | /* |
198 | | * We would like to check that malloc aligns blocks at least as |
199 | | * strictly as the compiler (as defined by ARCH_ALIGN_MEMORY_MOD). |
200 | | * However, Microsoft VC 6 does not satisfy this requirement. |
201 | | * See gsmemory.h for more explanation. |
202 | | */ |
203 | 5.38M | set_msg(ok_msg); |
204 | 5.38M | if (mmem->allocated) |
205 | 5.38M | mmem->allocated->prev = bp; |
206 | 5.38M | bp->next = mmem->allocated; |
207 | 5.38M | bp->prev = 0; |
208 | 5.38M | bp->size = size; |
209 | 5.38M | bp->type = &st_bytes; |
210 | 5.38M | bp->cname = cname; |
211 | 5.38M | mmem->allocated = bp; |
212 | 5.38M | ptr = (byte *) (bp + 1); |
213 | 5.38M | mmem->used += size + sizeof(gs_malloc_block_t); |
214 | 5.38M | if (mmem->used > mmem->max_used) |
215 | 875k | mmem->max_used = mmem->used; |
216 | 5.38M | } |
217 | 5.38M | } |
218 | 5.38M | if (mmem->monitor) |
219 | 5.38M | gx_monitor_leave(mmem->monitor); /* Done with exclusive access */ |
220 | | /* We don't want to 'fill' under mutex to keep the window smaller */ |
221 | 5.38M | if (ptr) |
222 | 5.38M | gs_alloc_fill(ptr, gs_alloc_fill_alloc, size); |
223 | | #ifdef DEBUG |
224 | | if (gs_debug_c('a') || msg != ok_msg) |
225 | | dmlprintf6(mem, "[a+]gs_malloc(%s)(%"PRIuSIZE") = "PRI_INTPTR": %s, used=%"PRIuSIZE", max=%"PRIuSIZE"\n", |
226 | | client_name_string(cname), size, (intptr_t)ptr, msg, mmem->used, mmem->max_used); |
227 | | #endif |
228 | 5.38M | return ptr; |
229 | 5.38M | #undef set_msg |
230 | 5.38M | } |
231 | | static void * |
232 | | gs_heap_alloc_struct(gs_memory_t * mem, gs_memory_type_ptr_t pstype, |
233 | | client_name_t cname) |
234 | 26.5k | { |
235 | 26.5k | void *ptr = |
236 | 26.5k | gs_heap_alloc_bytes(mem, gs_struct_type_size(pstype), cname); |
237 | | |
238 | 26.5k | if (ptr == 0) |
239 | 0 | return 0; |
240 | 26.5k | ((gs_malloc_block_t *) ptr)[-1].type = pstype; |
241 | 26.5k | return ptr; |
242 | 26.5k | } |
243 | | static byte * |
244 | | gs_heap_alloc_byte_array(gs_memory_t * mem, size_t num_elements, size_t elt_size, |
245 | | client_name_t cname) |
246 | 3.14k | { |
247 | 3.14k | size_t lsize = (size_t) num_elements * elt_size; |
248 | | |
249 | 3.14k | if (elt_size != 0 && lsize/elt_size != num_elements) |
250 | 0 | return 0; |
251 | 3.14k | return gs_heap_alloc_bytes(mem, (size_t) lsize, cname); |
252 | 3.14k | } |
253 | | static void * |
254 | | gs_heap_alloc_struct_array(gs_memory_t * mem, size_t num_elements, |
255 | | gs_memory_type_ptr_t pstype, client_name_t cname) |
256 | 0 | { |
257 | 0 | void *ptr = |
258 | 0 | gs_heap_alloc_byte_array(mem, num_elements, |
259 | 0 | gs_struct_type_size(pstype), cname); |
260 | |
|
261 | 0 | if (ptr == 0) |
262 | 0 | return 0; |
263 | 0 | ((gs_malloc_block_t *) ptr)[-1].type = pstype; |
264 | 0 | return ptr; |
265 | 0 | } |
266 | | static void * |
267 | | gs_heap_resize_object(gs_memory_t * mem, void *obj, size_t new_num_elements, |
268 | | client_name_t cname) |
269 | 5.69k | { |
270 | 5.69k | gs_malloc_memory_t *mmem = (gs_malloc_memory_t *) mem; |
271 | 5.69k | gs_malloc_block_t *ptr = (gs_malloc_block_t *) obj - 1; |
272 | 5.69k | gs_memory_type_ptr_t pstype = ptr->type; |
273 | 5.69k | size_t old_size = gs_object_size(mem, obj) + sizeof(gs_malloc_block_t); |
274 | 5.69k | size_t new_size = |
275 | 5.69k | gs_struct_type_size(pstype) * new_num_elements + |
276 | 5.69k | sizeof(gs_malloc_block_t); |
277 | 5.69k | gs_malloc_block_t *new_ptr; |
278 | | |
279 | 5.69k | if (new_size == old_size) |
280 | 2.15k | return obj; |
281 | 3.53k | if (mmem->monitor) |
282 | 3.53k | gx_monitor_enter(mmem->monitor); /* Exclusive access */ |
283 | 3.53k | if (new_size > mmem->limit - sizeof(gs_malloc_block_t)) { |
284 | | /* too large to allocate; also avoids overflow. */ |
285 | 0 | if (mmem->monitor) |
286 | 0 | gx_monitor_leave(mmem->monitor); /* Done with exclusive access */ |
287 | 0 | return 0; |
288 | 0 | } |
289 | 3.53k | new_ptr = (gs_malloc_block_t *) gs_realloc(ptr, old_size, new_size); |
290 | 3.53k | if (new_ptr == 0) { |
291 | 0 | if (mmem->monitor) |
292 | 0 | gx_monitor_leave(mmem->monitor); /* Done with exclusive access */ |
293 | 0 | return 0; |
294 | 0 | } |
295 | 3.53k | if (new_ptr->prev) |
296 | 3.53k | new_ptr->prev->next = new_ptr; |
297 | 0 | else |
298 | 0 | mmem->allocated = new_ptr; |
299 | 3.53k | if (new_ptr->next) |
300 | 3.53k | new_ptr->next->prev = new_ptr; |
301 | 3.53k | new_ptr->size = new_size - sizeof(gs_malloc_block_t); |
302 | 3.53k | mmem->used -= old_size; |
303 | 3.53k | mmem->used += new_size; |
304 | 3.53k | if (mmem->monitor) |
305 | 3.53k | gx_monitor_leave(mmem->monitor); /* Done with exclusive access */ |
306 | 3.53k | if (new_size > old_size) |
307 | 3.53k | gs_alloc_fill((byte *) new_ptr + old_size, |
308 | 3.53k | gs_alloc_fill_alloc, new_size - old_size); |
309 | 3.53k | return new_ptr + 1; |
310 | 3.53k | } |
311 | | static size_t |
312 | | gs_heap_object_size(gs_memory_t * mem, const void *ptr) |
313 | 5.69k | { |
314 | 5.69k | return ((const gs_malloc_block_t *)ptr)[-1].size; |
315 | 5.69k | } |
316 | | static gs_memory_type_ptr_t |
317 | | gs_heap_object_type(const gs_memory_t * mem, const void *ptr) |
318 | 0 | { |
319 | 0 | return ((const gs_malloc_block_t *)ptr)[-1].type; |
320 | 0 | } |
321 | | static void |
322 | | gs_heap_free_object(gs_memory_t * mem, void *ptr, client_name_t cname) |
323 | 5.40M | { |
324 | 5.40M | gs_malloc_memory_t *mmem = (gs_malloc_memory_t *) mem; |
325 | 5.40M | gs_malloc_block_t *bp; |
326 | 5.40M | gs_memory_type_ptr_t pstype; |
327 | 5.40M | struct_proc_finalize((*finalize)); |
328 | | |
329 | 5.40M | if_debug3m('a', mem, "[a-]gs_free(%s) "PRI_INTPTR"(%"PRIuSIZE")\n", |
330 | 5.40M | client_name_string(cname), (intptr_t)ptr, |
331 | 5.40M | (ptr == 0 ? 0 : ((gs_malloc_block_t *) ptr)[-1].size)); |
332 | 5.40M | if (ptr == 0) |
333 | 14.2k | return; |
334 | 5.38M | pstype = ((gs_malloc_block_t *) ptr)[-1].type; |
335 | 5.38M | finalize = pstype->finalize; |
336 | 5.38M | if (finalize != 0) { |
337 | 4.50k | if_debug3m('u', mem, "[u]finalizing %s "PRI_INTPTR" (%s)\n", |
338 | 4.50k | struct_type_name_string(pstype), |
339 | 4.50k | (intptr_t)ptr, client_name_string(cname)); |
340 | 4.50k | (*finalize) (mem, ptr); |
341 | 4.50k | } |
342 | 5.38M | if (mmem->monitor) |
343 | 5.38M | gx_monitor_enter(mmem->monitor); /* Exclusive access */ |
344 | | /* Previously, we used to search through every allocated block to find |
345 | | * the block we are freeing. This gives us safety in that an attempt to |
346 | | * free an unallocated block will not go wrong. This does radically |
347 | | * slow down frees though, so we replace it with this simpler code; we |
348 | | * now assume that the block is valid, and hence avoid the search. |
349 | | */ |
350 | 5.38M | #if 1 |
351 | 5.38M | bp = &((gs_malloc_block_t *)ptr)[-1]; |
352 | 5.38M | if (bp->prev) |
353 | 4.04M | bp->prev->next = bp->next; |
354 | 5.38M | if (bp->next) |
355 | 5.38M | bp->next->prev = bp->prev; |
356 | 5.38M | if (bp == mmem->allocated) { |
357 | 1.34M | mmem->allocated = bp->next; |
358 | 1.34M | if (mmem->allocated) |
359 | 1.34M | mmem->allocated->prev = NULL; |
360 | 1.34M | } |
361 | 5.38M | mmem->used -= bp->size + sizeof(gs_malloc_block_t); |
362 | 5.38M | if (mmem->monitor) |
363 | 5.38M | gx_monitor_leave(mmem->monitor); /* Done with exclusive access */ |
364 | 5.38M | gs_alloc_fill(bp, gs_alloc_fill_free, |
365 | 5.38M | bp->size + sizeof(gs_malloc_block_t)); |
366 | 5.38M | free(bp); |
367 | | #else |
368 | | bp = mmem->allocated; /* If 'finalize' releases a memory, |
369 | | this function could be called recursively and |
370 | | change mmem->allocated. */ |
371 | | if (ptr == bp + 1) { |
372 | | mmem->allocated = bp->next; |
373 | | mmem->used -= bp->size + sizeof(gs_malloc_block_t); |
374 | | |
375 | | if (mmem->allocated) |
376 | | mmem->allocated->prev = 0; |
377 | | if (mmem->monitor) |
378 | | gx_monitor_leave(mmem->monitor); /* Done with exclusive access */ |
379 | | gs_alloc_fill(bp, gs_alloc_fill_free, |
380 | | bp->size + sizeof(gs_malloc_block_t)); |
381 | | free(bp); |
382 | | } else { |
383 | | gs_malloc_block_t *np; |
384 | | |
385 | | /* |
386 | | * bp == 0 at this point is an error, but we'd rather have an |
387 | | * error message than an invalid access. |
388 | | */ |
389 | | if (bp) { |
390 | | for (; (np = bp->next) != 0; bp = np) { |
391 | | if (ptr == np + 1) { |
392 | | bp->next = np->next; |
393 | | if (np->next) |
394 | | np->next->prev = bp; |
395 | | mmem->used -= np->size + sizeof(gs_malloc_block_t); |
396 | | if (mmem->monitor) |
397 | | gx_monitor_leave(mmem->monitor); /* Done with exclusive access */ |
398 | | gs_alloc_fill(np, gs_alloc_fill_free, |
399 | | np->size + sizeof(gs_malloc_block_t)); |
400 | | free(np); |
401 | | return; |
402 | | } |
403 | | } |
404 | | } |
405 | | if (mmem->monitor) |
406 | | gx_monitor_leave(mmem->monitor); /* Done with exclusive access */ |
407 | | lprintf2("%s: free "PRI_INTPTR" not found!\n", |
408 | | client_name_string(cname), (intptr_t) ptr); |
409 | | free((char *)((gs_malloc_block_t *) ptr - 1)); |
410 | | } |
411 | | #endif |
412 | 5.38M | } |
413 | | static byte * |
414 | | gs_heap_alloc_string(gs_memory_t * mem, size_t nbytes, client_name_t cname) |
415 | 71.7k | { |
416 | 71.7k | return gs_heap_alloc_bytes(mem, nbytes, cname); |
417 | 71.7k | } |
418 | | static byte * |
419 | | gs_heap_resize_string(gs_memory_t * mem, byte * data, size_t old_num, size_t new_num, |
420 | | client_name_t cname) |
421 | 0 | { |
422 | 0 | if (gs_heap_object_type(mem, data) != &st_bytes) |
423 | 0 | lprintf2("%s: resizing non-string "PRI_INTPTR"!\n", |
424 | 0 | client_name_string(cname), (intptr_t)data); |
425 | 0 | return gs_heap_resize_object(mem, data, new_num, cname); |
426 | 0 | } |
427 | | static void |
428 | | gs_heap_free_string(gs_memory_t * mem, byte * data, size_t nbytes, |
429 | | client_name_t cname) |
430 | 683 | { |
431 | | /****** SHOULD CHECK SIZE IF DEBUGGING ******/ |
432 | 683 | gs_heap_free_object(mem, data, cname); |
433 | 683 | } |
434 | | static int |
435 | | gs_heap_register_root(gs_memory_t * mem, gs_gc_root_t ** rp, |
436 | | gs_ptr_type_t ptype, void **up, client_name_t cname) |
437 | 0 | { |
438 | 0 | return 0; |
439 | 0 | } |
440 | | static void |
441 | | gs_heap_unregister_root(gs_memory_t * mem, gs_gc_root_t * rp, |
442 | | client_name_t cname) |
443 | 0 | { |
444 | 0 | } |
445 | | static gs_memory_t * |
446 | | gs_heap_stable(gs_memory_t *mem) |
447 | 0 | { |
448 | 0 | return mem; /* heap memory is stable */ |
449 | 0 | } |
450 | | |
451 | | /* |
452 | | * NB: In a multi-threaded application, this is only a 'snapshot' |
453 | | * since other threads may change the heap_status. The heap_available() |
454 | | * probe is just an approximation anyway. To pacify helgrind, we lock |
455 | | * around the modificatons to the gs_memory_status that is returned. |
456 | | */ |
457 | | static void |
458 | | gs_heap_status(gs_memory_t * mem, gs_memory_status_t * pstat) |
459 | 13.6k | { |
460 | 13.6k | gs_malloc_memory_t *mmem = (gs_malloc_memory_t *) mem; |
461 | 13.6k | long avail_snapshot = heap_available(); |
462 | | |
463 | 13.6k | if (mmem->monitor) |
464 | 13.6k | gx_monitor_enter(mmem->monitor); |
465 | 13.6k | pstat->allocated = mmem->used + avail_snapshot; |
466 | 13.6k | pstat->used = mmem->used; |
467 | 13.6k | pstat->max_used = mmem->max_used; |
468 | 13.6k | pstat->is_thread_safe = true; /* this allocator has a mutex (monitor) and IS thread safe */ |
469 | 13.6k | if (mmem->monitor) |
470 | 13.6k | gx_monitor_leave(mmem->monitor); /* Done with exclusive access */ |
471 | 13.6k | } |
472 | | static void |
473 | | gs_heap_enable_free(gs_memory_t * mem, bool enable) |
474 | 0 | { |
475 | 0 | if (enable) |
476 | 0 | mem->procs.free_object = gs_heap_free_object, |
477 | 0 | mem->procs.free_string = gs_heap_free_string; |
478 | 0 | else |
479 | 0 | mem->procs.free_object = gs_ignore_free_object, |
480 | 0 | mem->procs.free_string = gs_ignore_free_string; |
481 | 0 | } |
482 | | |
483 | | static void gs_heap_set_object_type(gs_memory_t *mem, void *ptr, gs_memory_type_ptr_t type) |
484 | 0 | { |
485 | 0 | gs_malloc_block_t *bp = (gs_malloc_block_t *) ptr; |
486 | |
|
487 | 0 | if (ptr == 0) |
488 | 0 | return; |
489 | 0 | bp[-1].type = type; |
490 | 0 | } |
491 | | |
492 | | static void gs_heap_defer_frees(gs_memory_t *mem, int defer) |
493 | 0 | { |
494 | 0 | } |
495 | | |
496 | | /* Release all memory acquired by this allocator. */ |
497 | | static void |
498 | | gs_heap_free_all(gs_memory_t * mem, uint free_mask, client_name_t cname) |
499 | 683 | { |
500 | 683 | gs_malloc_memory_t *const mmem = (gs_malloc_memory_t *) mem; |
501 | 683 | gx_monitor_t *mon = mmem->monitor; |
502 | | |
503 | | /* |
504 | | * We don't perform locking during this process since the 'monitor' |
505 | | * is contained in this allocator, and will get freed along the way. |
506 | | * It is only called at exit, and there better not be any threads |
507 | | * accessing this allocator. |
508 | | */ |
509 | 683 | mmem->monitor = NULL; /* delete reference to this monitor */ |
510 | 683 | gx_monitor_free(mon); /* free the monitor */ |
511 | 683 | #ifndef MEMENTO |
512 | | /* Normally gs calls this on closedown, and it frees every block that |
513 | | * has ever been allocated. This is not helpful for leak checking. */ |
514 | 683 | if (free_mask & FREE_ALL_DATA) { |
515 | 683 | gs_malloc_block_t *bp = mmem->allocated; |
516 | 683 | gs_malloc_block_t *np; |
517 | | |
518 | 702 | for (; bp != 0; bp = np) { |
519 | 19 | np = bp->next; |
520 | 19 | if_debug3m('a', mem, "[a]gs_heap_free_all(%s) "PRI_INTPTR"(%"PRIuSIZE")\n", |
521 | 19 | client_name_string(bp->cname), (intptr_t)(bp + 1), |
522 | 19 | bp->size); |
523 | 19 | gs_alloc_fill(bp + 1, gs_alloc_fill_free, bp->size); |
524 | 19 | free(bp); |
525 | 19 | } |
526 | 683 | } |
527 | 683 | #endif |
528 | 683 | if (free_mask & FREE_ALL_ALLOCATOR) |
529 | 683 | free(mem); |
530 | 683 | } |
531 | | |
532 | | /* ------ Wrapping ------ */ |
533 | | |
534 | | /* Create the retrying and the locked wrapper for the heap allocator. */ |
535 | | int |
536 | | gs_malloc_wrap(gs_memory_t **wrapped, gs_malloc_memory_t *contents) |
537 | 0 | { |
538 | | # ifdef USE_RETRY_MEMORY_WRAPPER |
539 | | /* |
540 | | * This is deprecated since 'retry' for clist reversion/cycling |
541 | | * will ONLY work for monochrome, simple PS or PCL, not for a |
542 | | * color device and not for PDF or XPS with transparency |
543 | | */ |
544 | | { |
545 | | gs_memory_retrying_t *rmem; |
546 | | rmem = (gs_memory_retrying_t *) |
547 | | gs_alloc_bytes_immovable((gs_memory_t *)lmem, |
548 | | sizeof(gs_memory_retrying_t), |
549 | | "gs_malloc_wrap(retrying)"); |
550 | | if (rmem == 0) { |
551 | | gs_free_object(cmem, lmem, "gs_malloc_wrap(locked)"); |
552 | | return_error(gs_error_VMerror); |
553 | | } |
554 | | code = gs_memory_retrying_init(rmem, (gs_memory_t *)lmem); |
555 | | if (code < 0) { |
556 | | gs_free_object((gs_memory_t *)lmem, rmem, "gs_malloc_wrap(retrying)"); |
557 | | gs_free_object(cmem, lmem, "gs_malloc_wrap(locked)"); |
558 | | return code; |
559 | | } |
560 | | |
561 | | *wrapped = (gs_memory_t *)rmem; |
562 | | } |
563 | | # endif /* retrying */ |
564 | 0 | return 0; |
565 | 0 | } |
566 | | |
567 | | /* Get the wrapped contents. */ |
568 | | gs_malloc_memory_t * |
569 | | gs_malloc_wrapped_contents(gs_memory_t *wrapped) |
570 | 683 | { |
571 | | #ifdef USE_RETRY_MEMORY_WRAPPER |
572 | | gs_memory_retrying_t *rmem = (gs_memory_retrying_t *)wrapped; |
573 | | |
574 | | return (gs_malloc_memory_t *)gs_memory_retrying_target(rmem); |
575 | | #else /* retrying */ |
576 | 683 | return (gs_malloc_memory_t *)wrapped; |
577 | 683 | #endif /* retrying */ |
578 | 683 | } |
579 | | |
580 | | /* Free the wrapper, and return the wrapped contents. */ |
581 | | gs_malloc_memory_t * |
582 | | gs_malloc_unwrap(gs_memory_t *wrapped) |
583 | 0 | { |
584 | | #ifdef USE_RETRY_MEMORY_WRAPPER |
585 | | gs_memory_retrying_t *rmem = (gs_memory_retrying_t *)wrapped; |
586 | | gs_memory_t *contents = gs_memory_retrying_target(rmem); |
587 | | |
588 | | gs_free_object(wrapped rmem, "gs_malloc_unwrap(retrying)"); |
589 | | return (gs_malloc_memory_t *)contents; |
590 | | #else |
591 | 0 | return (gs_malloc_memory_t *)wrapped; |
592 | 0 | #endif |
593 | 0 | } |
594 | | |
595 | | /* Create the default allocator, and return it. */ |
596 | | gs_memory_t * |
597 | | gs_malloc_init(void) |
598 | 0 | { |
599 | 0 | return gs_malloc_init_with_context(NULL); |
600 | 0 | } |
601 | | |
602 | | gs_memory_t * |
603 | | gs_malloc_init_with_context(gs_lib_ctx_t *ctx) |
604 | 683 | { |
605 | 683 | gs_malloc_memory_t *malloc_memory_default = gs_malloc_memory_init(); |
606 | 683 | gs_memory_t *memory_t_default; |
607 | | |
608 | 683 | if (malloc_memory_default == NULL) |
609 | 0 | return NULL; |
610 | | |
611 | 683 | if (gs_lib_ctx_init(ctx, (gs_memory_t *)malloc_memory_default) != 0) { |
612 | 0 | gs_malloc_release((gs_memory_t *)malloc_memory_default); |
613 | 0 | return NULL; |
614 | 0 | } |
615 | | |
616 | | #if defined(USE_RETRY_MEMORY_WRAPPER) |
617 | | gs_malloc_wrap(&memory_t_default, malloc_memory_default); |
618 | | #else |
619 | 683 | memory_t_default = (gs_memory_t *)malloc_memory_default; |
620 | 683 | #endif |
621 | 683 | memory_t_default->stable_memory = memory_t_default; |
622 | 683 | return memory_t_default; |
623 | 683 | } |
624 | | |
625 | | /* Release the default allocator. */ |
626 | | void |
627 | | gs_malloc_release(gs_memory_t *mem) |
628 | 683 | { |
629 | 683 | gs_malloc_memory_t * malloc_memory_default; |
630 | | |
631 | 683 | if (mem == NULL) |
632 | 0 | return; |
633 | | |
634 | | #ifdef USE_RETRY_MEMORY_WRAPPER |
635 | | malloc_memory_default = gs_malloc_unwrap(mem); |
636 | | #else |
637 | 683 | malloc_memory_default = (gs_malloc_memory_t *)mem; |
638 | 683 | #endif |
639 | 683 | gs_lib_ctx_fin((gs_memory_t *)malloc_memory_default); |
640 | | |
641 | 683 | gs_malloc_memory_release(malloc_memory_default); |
642 | 683 | } |