Coverage Report

Created: 2022-04-16 11:23

/src/ghostpdl/base/gsmchunk.c
Line
Count
Source (jump to first uncovered line)
1
/* Copyright (C) 2001-2022 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
/* chunk consolidating wrapper on a base memory allocator */
18
19
/* This uses dual binary trees to handle the free list. One tree
20
 * holds the blocks in size order, one in location order. We use
21
 * a top-down semi-splaying access scheme on lookups and
22
 * insertions. */
23
24
#include "memory_.h"
25
#include "gx.h"
26
#include "gsstruct.h"
27
#include "gxobj.h"
28
#include "gsstype.h"
29
#include "gserrors.h"
30
#include "gsmchunk.h"
31
#include "gxsync.h"
32
#include "malloc_.h" /* For MEMENTO */
33
#include "assert_.h"
34
#include "gsmdebug.h"
35
36
/* Enable DEBUG_CHUNK to check the validity of the heap at every turn */
37
#undef DEBUG_CHUNK
38
39
/* Enable DEBUG_SEQ to keep sequence numbers in every block */
40
#undef DEBUG_SEQ
41
42
/* Enable DEBUG_CHUNK_PRINT to print the heap at every turn */
43
#undef DEBUG_CHUNK_PRINT
44
45
/* Enable DEBUG_CHUNK_PRINT_SLABS to list the slabs in the heap */
46
#undef DEBUG_CHUNK_PRINT_SLABS
47
48
#if defined(DEBUG_CHUNK_PRINT_SLABS) && !defined(DEBUG_CHUNK_PRINT)
49
#define DEBUG_CHUNK_PRINT
50
#endif
51
52
#if defined(DEBUG_CHUNK_PRINT) && !defined(DEBUG_CHUNK)
53
#define DEBUG_CHUNK
54
#endif
55
56
#if defined(DEBUG_CHUNK) && !defined(DEBUG)
57
#define DEBUG
58
#define CHUNK_FAKE_ASSERT
59
#endif
60
61
#ifdef DEBUG
62
#ifdef CHUNK_FAKE_ASSERT
63
#define CHUNK_ASSERT(M,A) gs_chunk_assert(M, A, #A)
64
65
static void gs_chunk_assert(gs_memory_t *m, int v, const char *s)
66
{
67
    void (*crash)(void);
68
    if (v)
69
        return;
70
    dmlprintf1(m, "Assert failed: %s\n", s);
71
    crash = NULL;
72
    crash();
73
}
74
#else
75
#define CHUNK_ASSERT(M,A) assert(A)
76
#endif
77
#endif
78
79
/* Raw memory procedures */
80
static gs_memory_proc_alloc_bytes(chunk_alloc_bytes_immovable);
81
static gs_memory_proc_resize_object(chunk_resize_object);
82
static gs_memory_proc_free_object(chunk_free_object);
83
static gs_memory_proc_stable(chunk_stable);
84
static gs_memory_proc_status(chunk_status);
85
static gs_memory_proc_free_all(chunk_free_all);
86
static gs_memory_proc_consolidate_free(chunk_consolidate_free);
87
88
/* Object memory procedures */
89
static gs_memory_proc_alloc_bytes(chunk_alloc_bytes);
90
static gs_memory_proc_alloc_struct(chunk_alloc_struct);
91
static gs_memory_proc_alloc_struct(chunk_alloc_struct_immovable);
92
static gs_memory_proc_alloc_byte_array(chunk_alloc_byte_array);
93
static gs_memory_proc_alloc_byte_array(chunk_alloc_byte_array_immovable);
94
static gs_memory_proc_alloc_struct_array(chunk_alloc_struct_array);
95
static gs_memory_proc_alloc_struct_array(chunk_alloc_struct_array_immovable);
96
static gs_memory_proc_object_size(chunk_object_size);
97
static gs_memory_proc_object_type(chunk_object_type);
98
static gs_memory_proc_alloc_string(chunk_alloc_string);
99
static gs_memory_proc_alloc_string(chunk_alloc_string_immovable);
100
static gs_memory_proc_resize_string(chunk_resize_string);
101
static gs_memory_proc_free_string(chunk_free_string);
102
static gs_memory_proc_register_root(chunk_register_root);
103
static gs_memory_proc_unregister_root(chunk_unregister_root);
104
static gs_memory_proc_enable_free(chunk_enable_free);
105
static gs_memory_proc_set_object_type(chunk_set_object_type);
106
static gs_memory_proc_defer_frees(chunk_defer_frees);
107
static const gs_memory_procs_t chunk_procs =
108
{
109
    /* Raw memory procedures */
110
    chunk_alloc_bytes_immovable,
111
    chunk_resize_object,
112
    chunk_free_object,
113
    chunk_stable,
114
    chunk_status,
115
    chunk_free_all,
116
    chunk_consolidate_free,
117
    /* Object memory procedures */
118
    chunk_alloc_bytes,
119
    chunk_alloc_struct,
120
    chunk_alloc_struct_immovable,
121
    chunk_alloc_byte_array,
122
    chunk_alloc_byte_array_immovable,
123
    chunk_alloc_struct_array,
124
    chunk_alloc_struct_array_immovable,
125
    chunk_object_size,
126
    chunk_object_type,
127
    chunk_alloc_string,
128
    chunk_alloc_string_immovable,
129
    chunk_resize_string,
130
    chunk_free_string,
131
    chunk_register_root,
132
    chunk_unregister_root,
133
    chunk_enable_free,
134
    chunk_set_object_type,
135
    chunk_defer_frees
136
};
137
138
typedef struct chunk_obj_node_s {
139
    gs_memory_type_ptr_t type;
140
#ifdef DEBUG_SEQ
141
    unsigned int sequence;
142
#endif
143
    struct chunk_obj_node_s *defer_next;
144
    size_t size; /* Actual size of block */
145
    size_t padding; /* Actual size - requested size */
146
} chunk_obj_node_t;
147
148
typedef struct chunk_free_node_s {
149
    struct chunk_free_node_s *left_loc;
150
    struct chunk_free_node_s *right_loc;
151
    struct chunk_free_node_s *left_size;
152
    struct chunk_free_node_s *right_size;
153
    size_t size;                  /* size of entire freelist block */
154
} chunk_free_node_t;
155
156
/*
157
 * Note: All objects within a chunk are 'aligned' since we round_up_to_align
158
 * the free list pointer when removing part of a free area.
159
 */
160
typedef struct chunk_slab_s {
161
    struct chunk_slab_s *next;
162
} chunk_slab_t;
163
164
typedef struct gs_memory_chunk_s {
165
    gs_memory_common;           /* interface outside world sees */
166
    gs_memory_t *target;        /* base allocator */
167
    chunk_slab_t *slabs;         /* list of slabs for freeing */
168
    chunk_free_node_t *free_size;/* free tree */
169
    chunk_free_node_t *free_loc; /* free tree */
170
    chunk_obj_node_t *defer_finalize_list;
171
    chunk_obj_node_t *defer_free_list;
172
    size_t used;
173
    size_t max_used;
174
    size_t total_free;
175
#ifdef DEBUG_SEQ
176
    unsigned int sequence;
177
#endif
178
    int deferring;
179
} gs_memory_chunk_t;
180
181
894M
#define SIZEOF_ROUND_ALIGN(a) ROUND_UP(sizeof(a), obj_align_mod)
182
183
/* ---------- Public constructors/destructors ---------- */
184
185
/* Initialize a gs_memory_chunk_t */
186
int
187
gs_memory_chunk_wrap(gs_memory_t **wrapped,      /* chunk allocator init */
188
                    gs_memory_t  *target)       /* base allocator */
189
1.50k
{
190
    /* Use the non-GC allocator of the target. */
191
1.50k
    gs_memory_t       *non_gc_target = target->non_gc_memory;
192
1.50k
    gs_memory_chunk_t *cmem = NULL;
193
194
1.50k
    if (non_gc_target)
195
1.50k
        cmem = (gs_memory_chunk_t *)gs_alloc_bytes_immovable(non_gc_target,
196
1.50k
                                                            sizeof(gs_memory_chunk_t),
197
1.50k
                                                            "gs_memory_chunk_wrap");
198
1.50k
    if (cmem == NULL) {
199
0
        *wrapped = NULL;
200
0
        return_error(gs_error_VMerror);
201
0
    }
202
1.50k
    cmem->stable_memory = (gs_memory_t *)cmem;  /* we are stable */
203
1.50k
    cmem->procs = chunk_procs;
204
1.50k
    cmem->gs_lib_ctx = non_gc_target->gs_lib_ctx;
205
1.50k
    cmem->non_gc_memory = (gs_memory_t *)cmem;  /* and are not subject to GC */
206
1.50k
    cmem->thread_safe_memory = non_gc_target->thread_safe_memory;
207
1.50k
    cmem->target = non_gc_target;
208
1.50k
    cmem->slabs = NULL;
209
1.50k
    cmem->free_size = NULL;
210
1.50k
    cmem->free_loc = NULL;
211
1.50k
    cmem->used = 0;
212
1.50k
    cmem->max_used = 0;
213
1.50k
    cmem->total_free = 0;
214
#ifdef DEBUG_SEQ
215
    cmem->sequence = 0;
216
#endif
217
1.50k
    cmem->deferring = 0;
218
1.50k
    cmem->defer_finalize_list = NULL;
219
1.50k
    cmem->defer_free_list = NULL;
220
221
#ifdef DEBUG_CHUNK_PRINT
222
    dmlprintf1(non_gc_target, "New chunk "PRI_INTPTR"\n", (intptr_t)cmem);
223
#endif
224
225
    /* Init the chunk management values */
226
1.50k
    *wrapped = (gs_memory_t *)cmem;
227
1.50k
    return 0;
228
1.50k
}
229
230
/* Release a chunk memory manager. */
231
/* Note that this has no effect on the target. */
232
void
233
gs_memory_chunk_release(gs_memory_t *mem)
234
1.50k
{
235
1.50k
    gs_memory_free_all((gs_memory_t *)mem, FREE_ALL_EVERYTHING,
236
1.50k
                       "gs_memory_chunk_release");
237
1.50k
}
238
239
/* Release chunk memory manager, and return the target */
240
gs_memory_t * /* Always succeeds */
241
gs_memory_chunk_unwrap(gs_memory_t *mem)
242
818
{
243
818
    gs_memory_t *tmem;
244
245
    /* If this isn't a chunk, nothing to unwrap */
246
818
    if (mem->procs.status != chunk_status)
247
0
        return mem;
248
249
818
    tmem = ((gs_memory_chunk_t *)mem)->target;
250
818
    gs_memory_chunk_release(mem);
251
252
818
    return tmem;
253
818
}
254
255
/* ---------- Accessors ------------- */
256
257
/* Retrieve this allocator's target */
258
gs_memory_t *
259
gs_memory_chunk_target(const gs_memory_t *mem)
260
0
{
261
0
    gs_memory_chunk_t *cmem = (gs_memory_chunk_t *)mem;
262
0
    return cmem->target;
263
0
}
264
265
/* -------- Private members --------- */
266
267
/* Note that all of the data is 'immovable' and is opaque to the base allocator */
268
/* thus even if it is a GC type of allocator, no GC functions will be applied   */
269
/* All allocations are done in the target */
270
271
/* Procedures */
272
273
static void
274
chunk_mem_node_free_all_slabs(gs_memory_chunk_t *cmem)
275
1.50k
{
276
1.50k
    chunk_slab_t *slab, *next;
277
1.50k
    gs_memory_t *const target = cmem->target;
278
279
247k
    for (slab = cmem->slabs; slab != NULL; slab = next) {
280
246k
        next = slab->next;
281
246k
        gs_free_object(target, slab, "chunk_mem_node_free_all_slabs");
282
246k
    }
283
284
1.50k
    cmem->slabs = NULL;
285
1.50k
    cmem->free_size = NULL;
286
1.50k
    cmem->free_loc = NULL;
287
1.50k
    cmem->total_free = 0;
288
1.50k
    cmem->used = 0;
289
1.50k
}
290
291
static void
292
chunk_free_all(gs_memory_t * mem, uint free_mask, client_name_t cname)
293
1.50k
{
294
1.50k
    gs_memory_chunk_t * const cmem = (gs_memory_chunk_t *)mem;
295
1.50k
    gs_memory_t * const target = cmem->target;
296
297
1.50k
    if (free_mask & FREE_ALL_DATA)
298
1.50k
        chunk_mem_node_free_all_slabs(cmem);
299
    /* Only free the structures and the allocator itself. */
300
1.50k
    if (mem->stable_memory) {
301
1.50k
        if (mem->stable_memory != mem)
302
0
            gs_memory_free_all(mem->stable_memory, free_mask, cname);
303
1.50k
        if (free_mask & FREE_ALL_ALLOCATOR)
304
1.50k
            mem->stable_memory = 0;
305
1.50k
    }
306
1.50k
    if (free_mask & FREE_ALL_STRUCTURES) {
307
1.50k
        cmem->target = 0;
308
1.50k
    }
309
1.50k
    if (free_mask & FREE_ALL_ALLOCATOR)
310
1.50k
        gs_free_object(target, cmem, cname);
311
1.50k
}
312
313
extern const gs_memory_struct_type_t st_bytes;
314
315
#ifdef DEBUG
316
static int dump_free_loc(gs_memory_t *mem, chunk_free_node_t *node, int depth, void **limit, uint *total)
317
{
318
#ifdef DEBUG_CHUNK_PRINT
319
    int i;
320
#endif
321
    int count;
322
323
    if (node == NULL)
324
        return 0;
325
326
    count = dump_free_loc(mem, node->left_loc, depth + 1 + (depth&1), limit, total);
327
    *total += node->size;
328
#ifdef DEBUG_CHUNK_PRINT
329
    if (depth != 0) {
330
        for (i = (depth-1)>>1; i != 0; i--)
331
            dmlprintf(mem, " ");
332
        if (depth & 1)
333
            dmlprintf(mem, "/");
334
        else
335
            dmlprintf(mem, "\\");
336
    }
337
    dmlprintf3(mem, PRI_INTPTR"+%x->"PRI_INTPTR"\n", (intptr_t)node, node->size, (intptr_t)((byte *)node)+node->size);
338
#endif
339
    CHUNK_ASSERT(mem, *limit < (void *)node);
340
    *limit = ((byte *)node)+node->size;
341
    return 1 + count + dump_free_loc(mem, node->right_loc, depth + 2 + (depth&1), limit, total);
342
}
343
344
static int dump_free_size(gs_memory_t *mem, chunk_free_node_t *node, int depth, uint *size, void **addr)
345
{
346
#ifdef DEBUG_CHUNK_PRINT
347
    int i;
348
#endif
349
    int count;
350
351
    if (node == NULL)
352
        return 0;
353
354
    count = dump_free_size(mem, node->left_size, depth + 1 + (depth&1), size, addr);
355
#ifdef DEBUG_CHUNK_PRINT
356
    if (depth != 0) {
357
        for (i = (depth-1)>>1; i != 0; i--)
358
            dmlprintf(mem, " ");
359
        if (depth & 1)
360
            dmlprintf(mem, "/");
361
        else
362
            dmlprintf(mem, "\\");
363
    }
364
    dmlprintf3(mem, PRI_INTPTR"+%x->"PRI_INTPTR"\n", (intptr_t)node, node->size, (intptr_t)((byte *)node)+node->size);
365
#endif
366
    CHUNK_ASSERT(mem, *size < node->size || (*size == node->size && *addr < (void *)node));
367
    *size = node->size;
368
    *addr = node;
369
    return 1 + count + dump_free_size(mem, node->right_size, depth + 2 + (depth&1), size, addr);
370
}
371
372
#ifdef DEBUG_CHUNK_PRINT
373
static size_t
374
largest_free_block(chunk_free_node_t *size)
375
{
376
    if (size == NULL)
377
        return 0;
378
    while (1) {
379
        if (size->right_size == NULL)
380
            return size->size;
381
        size = size->right_size;
382
    }
383
}
384
#endif
385
386
void
387
gs_memory_chunk_dump_memory(const gs_memory_t *mem)
388
{
389
    const gs_memory_chunk_t *cmem = (const gs_memory_chunk_t *)mem;
390
    int count1, count2;
391
    void *limit = NULL;
392
    void *addr = NULL;
393
    uint size = 1;
394
    uint total = 0;
395
396
#ifdef DEBUG_CHUNK_PRINT
397
    dmlprintf1(cmem->target, "Chunk "PRI_INTPTR":\n", (intptr_t)cmem);
398
    dmlprintf3(cmem->target, "Used=%"PRIxSIZE", Max Used=%"PRIxSIZE", Total Free=%"PRIxSIZE"\n", cmem->used, cmem->max_used, cmem->total_free);
399
    dmlprintf1(cmem->target, "Largest free block=%d bytes\n", largest_free_block(cmem->free_size));
400
#ifdef DEBUG_CHUNK_PRINT_SLABS
401
    {
402
        chunk_slab_t *slab;
403
        dmlprintf(cmem->target, "Slabs:\n");
404
        for (slab = cmem->slabs; slab != NULL; slab = slab->next)
405
            dmlprintf1(cmem->target, " "PRI_INTPTR"\n", (intptr_t)slab);
406
    }
407
#endif
408
    dmlprintf(cmem->target, "Locs:\n");
409
#endif
410
    count1 = dump_free_loc(cmem->target, cmem->free_loc, 0, &limit, &total);
411
#ifdef DEBUG_CHUNK_PRINT
412
    dmlprintf(cmem->target, "Sizes:\n");
413
#endif
414
    count2 = dump_free_size(cmem->target, cmem->free_size, 0, &size, &addr);
415
    if (count1 != count2) {
416
        void (*crash)(void) = NULL;
417
        dmlprintf2(cmem->target, "Tree mismatch! %d vs %d\n", count1, count2);
418
        crash();
419
    }
420
    if (total != cmem->total_free) {
421
        void (*crash)(void) = NULL;
422
        dmlprintf2(cmem->target, "Free size mismatch! %u vs %lu\n", total, cmem->total_free);
423
        crash();
424
    }
425
}
426
#endif
427
428
/* round up objects to make sure we have room for a header left */
429
inline static uint
430
round_up_to_align(uint size)
431
127M
{
432
127M
    uint num_node_headers = (size + SIZEOF_ROUND_ALIGN(chunk_obj_node_t) - 1) / SIZEOF_ROUND_ALIGN(chunk_obj_node_t);
433
434
127M
    return num_node_headers * SIZEOF_ROUND_ALIGN(chunk_obj_node_t);
435
127M
}
436
437
static inline int CMP_SIZE(const chunk_free_node_t * a, const chunk_free_node_t * b)
438
771M
{
439
771M
    if (a->size > b->size)
440
362M
        return 1;
441
408M
    if (a->size < b->size)
442
385M
        return 0;
443
23.1M
    return (a > b);
444
408M
}
445
446
static void insert_free_size(gs_memory_chunk_t *cmem, chunk_free_node_t *node)
447
243M
{
448
243M
    chunk_free_node_t **ap;
449
243M
    chunk_free_node_t *a, *b, *c;
450
451
243M
    node->left_size  = NULL;
452
243M
    node->right_size = NULL;
453
454
    /* Insert into size */
455
243M
    ap = &cmem->free_size;
456
283M
    while ((a = *ap) != NULL) {
457
267M
        if (CMP_SIZE(a, node)) {
458
88.9M
            b = a->left_size;
459
88.9M
            if (b == NULL) {
460
8.84M
                ap = &a->left_size;
461
8.84M
                break; /* Stop searching */
462
8.84M
            }
463
80.0M
            if (CMP_SIZE(b, node)) {
464
16.6M
                c = b->left_size;
465
16.6M
                if (c == NULL) {
466
2.58M
                    ap = &b->left_size;
467
2.58M
                    break;
468
2.58M
                }
469
                /* Splay:        a             c
470
                 *            b     Z   =>  W     b
471
                 *          c   Y               X   a
472
                 *         W X                     Y Z
473
                 */
474
14.0M
                *ap = c;
475
14.0M
                a->left_size  = b->right_size;
476
14.0M
                b->left_size  = c->right_size;
477
14.0M
                b->right_size = a;
478
14.0M
                c->right_size = b;
479
14.0M
                if (CMP_SIZE(c, node))
480
8.40M
                    ap = &c->left_size;
481
5.61M
                else
482
5.61M
                    ap = &b->left_size;
483
63.4M
            } else {
484
63.4M
                c = b->right_size;
485
63.4M
                if (c == NULL) {
486
57.4M
                    ap = &b->right_size;
487
57.4M
                    break;
488
57.4M
                }
489
                /* Splay:         a             c
490
                 *            b       Z  =>   b   a
491
                 *          W   c            W X Y Z
492
                 *             X Y
493
                 */
494
6.06M
                *ap = c;
495
6.06M
                a->left_size  = c->right_size;
496
6.06M
                b->right_size = c->left_size;
497
6.06M
                c->left_size  = b;
498
6.06M
                c->right_size = a;
499
6.06M
                if (CMP_SIZE(c, node))
500
3.86M
                    ap = &b->right_size;
501
2.19M
                else
502
2.19M
                    ap = &a->left_size;
503
6.06M
            }
504
178M
        } else {
505
178M
            b = a->right_size;
506
178M
            if (b == NULL)
507
64.1M
            {
508
64.1M
                ap = &a->right_size;
509
64.1M
                break;
510
64.1M
            }
511
114M
            if (CMP_SIZE(b, node)) {
512
101M
                c = b->left_size;
513
101M
                if (c == NULL) {
514
92.7M
                    ap = &b->left_size;
515
92.7M
                    break;
516
92.7M
                }
517
                /* Splay:      a                c
518
                 *         W       b    =>    a   b
519
                 *               c   Z       W X Y Z
520
                 *              X Y
521
                 */
522
8.76M
                *ap = c;
523
8.76M
                a->right_size = c->left_size;
524
8.76M
                b->left_size  = c->right_size;
525
8.76M
                c->left_size  = a;
526
8.76M
                c->right_size = b;
527
8.76M
                if (CMP_SIZE(c, node))
528
5.66M
                    ap = &a->right_size;
529
3.09M
                else
530
3.09M
                    ap = &b->left_size;
531
12.9M
            } else {
532
12.9M
                c = b->right_size;
533
12.9M
                if (c == NULL) {
534
2.08M
                    ap = &b->right_size;
535
2.08M
                    break;
536
2.08M
                }
537
                /* Splay:    a                   c
538
                 *        W     b      =>     b     Z
539
                 *            X   c         a   Y
540
                 *               Y Z       W X
541
                 */
542
10.8M
                *ap = c;
543
10.8M
                a->right_size = b->left_size;
544
10.8M
                b->right_size = c->left_size;
545
10.8M
                b->left_size  = a;
546
10.8M
                c->left_size  = b;
547
10.8M
                if (CMP_SIZE(c, node))
548
6.51M
                    ap = &b->right_size;
549
4.37M
                else
550
4.37M
                    ap = &c->right_size;
551
10.8M
            }
552
114M
        }
553
267M
    }
554
243M
    *ap = node;
555
243M
}
556
557
static void insert_free_loc(gs_memory_chunk_t *cmem, chunk_free_node_t *node)
558
116M
{
559
116M
    chunk_free_node_t **ap;
560
116M
    chunk_free_node_t *a, *b, *c;
561
562
116M
    node->left_loc   = NULL;
563
116M
    node->right_loc  = NULL;
564
565
    /* Insert into loc */
566
116M
    ap = &cmem->free_loc;
567
129M
    while ((a = *ap) != NULL) {
568
124M
        if (a > node) {
569
35.4M
            b = a->left_loc;
570
35.4M
            if (b == NULL) {
571
2.90M
                ap = &a->left_loc;
572
2.90M
                break; /* Stop searching */
573
2.90M
            }
574
32.5M
            if (b > node) {
575
5.07M
                c = b->left_loc;
576
5.07M
                if (c == NULL) {
577
824k
                    ap = &b->left_loc;
578
824k
                    break;
579
824k
                }
580
                /* Splay:        a             c
581
                 *            b     Z   =>  W     b
582
                 *          c   Y               X   a
583
                 *         W X                     Y Z
584
                 */
585
4.25M
                *ap = c;
586
4.25M
                a->left_loc  = b->right_loc;
587
4.25M
                b->left_loc  = c->right_loc;
588
4.25M
                b->right_loc = a;
589
4.25M
                c->right_loc = b;
590
4.25M
                if (c > node)
591
2.82M
                    ap = &c->left_loc;
592
1.42M
                else
593
1.42M
                    ap = &b->left_loc;
594
27.4M
            } else {
595
27.4M
                c = b->right_loc;
596
27.4M
                if (c == NULL) {
597
26.5M
                    ap = &b->right_loc;
598
26.5M
                    break;
599
26.5M
                }
600
                /* Splay:         a             c
601
                 *            b       Z  =>   b   a
602
                 *          W   c            W X Y Z
603
                 *             X Y
604
                 */
605
956k
                *ap = c;
606
956k
                a->left_loc  = c->right_loc;
607
956k
                b->right_loc = c->left_loc;
608
956k
                c->left_loc  = b;
609
956k
                c->right_loc = a;
610
956k
                if (c > node)
611
635k
                    ap = &b->right_loc;
612
321k
                else
613
321k
                    ap = &a->left_loc;
614
956k
            }
615
88.9M
        } else {
616
88.9M
            b = a->right_loc;
617
88.9M
            if (b == NULL)
618
26.4M
            {
619
26.4M
                ap = &a->right_loc;
620
26.4M
                break;
621
26.4M
            }
622
62.4M
            if (b > node) {
623
30.4M
                c = b->left_loc;
624
30.4M
                if (c == NULL) {
625
28.8M
                    ap = &b->left_loc;
626
28.8M
                    break;
627
28.8M
                }
628
                /* Splay:      a                c
629
                 *         W       b    =>    a   b
630
                 *               c   Z       W X Y Z
631
                 *              X Y
632
                 */
633
1.61M
                *ap = c;
634
1.61M
                a->right_loc = c->left_loc;
635
1.61M
                b->left_loc  = c->right_loc;
636
1.61M
                c->left_loc  = a;
637
1.61M
                c->right_loc = b;
638
1.61M
                if (c > node)
639
1.07M
                    ap = &a->right_loc;
640
542k
                else
641
542k
                    ap = &b->left_loc;
642
32.0M
            } else {
643
32.0M
                c = b->right_loc;
644
32.0M
                if (c == NULL) {
645
25.5M
                    ap = &b->right_loc;
646
25.5M
                    break;
647
25.5M
                }
648
                /* Splay:    a                   c
649
                 *        W     b      =>     b     Z
650
                 *            X   c         a   Y
651
                 *               Y Z       W X
652
                 */
653
6.43M
                *ap = c;
654
6.43M
                a->right_loc = b->left_loc;
655
6.43M
                b->right_loc = c->left_loc;
656
6.43M
                b->left_loc  = a;
657
6.43M
                c->left_loc  = b;
658
6.43M
                if (c > node)
659
2.32M
                    ap = &b->right_loc;
660
4.11M
                else
661
4.11M
                    ap = &c->right_loc;
662
6.43M
            }
663
62.4M
        }
664
124M
    }
665
116M
    *ap = node;
666
116M
}
667
668
static void insert_free(gs_memory_chunk_t *cmem, chunk_free_node_t *node, uint size)
669
116M
{
670
116M
    node->size = size;
671
116M
    insert_free_size(cmem, node);
672
116M
    insert_free_loc(cmem, node);
673
116M
}
674
675
static void remove_free_loc(gs_memory_chunk_t *cmem, chunk_free_node_t *node)
676
130M
{
677
130M
    chunk_free_node_t **ap = &cmem->free_loc;
678
679
387M
    while (*ap != node)
680
257M
    {
681
257M
        if (*ap > node)
682
90.5M
            ap = &(*ap)->left_loc;
683
166M
        else
684
166M
            ap = &(*ap)->right_loc;
685
257M
    }
686
687
130M
    if (node->left_loc == NULL)
688
121M
        *ap = node->right_loc;
689
8.59M
    else if (node->right_loc == NULL)
690
2.51M
        *ap = node->left_loc;
691
6.07M
    else {
692
        /* Find the in-order predecessor to node and use that to replace node */
693
6.07M
        chunk_free_node_t **bp;
694
6.07M
        chunk_free_node_t  *b;
695
6.07M
        bp = &node->left_loc;
696
7.27M
        while ((*bp)->right_loc)
697
1.20M
            bp = &(*bp)->right_loc;
698
6.07M
        b = (*bp);
699
6.07M
        *bp = b->left_loc;
700
6.07M
        b->left_loc = node->left_loc;
701
6.07M
        b->right_loc = node->right_loc;
702
6.07M
        *ap = b;
703
6.07M
    }
704
130M
}
705
706
static void remove_free_size(gs_memory_chunk_t *cmem, chunk_free_node_t *node)
707
116M
{
708
116M
    chunk_free_node_t **ap = &cmem->free_size;
709
710
386M
    while (*ap != node)
711
269M
    {
712
269M
        if (CMP_SIZE(*ap, node))
713
143M
            ap = &(*ap)->left_size;
714
126M
        else
715
126M
            ap = &(*ap)->right_size;
716
269M
    }
717
718
116M
    if (node->left_size == NULL)
719
113M
        *ap = node->right_size;
720
3.31M
    else if (node->right_size == NULL)
721
735k
        *ap = node->left_size;
722
2.58M
    else {
723
        /* Find the in-order predecessor to node and use that to replace node */
724
2.58M
        chunk_free_node_t **bp;
725
2.58M
        chunk_free_node_t  *b;
726
2.58M
        bp = &node->left_size;
727
3.77M
        while ((*bp)->right_size)
728
1.19M
            bp = &(*bp)->right_size;
729
2.58M
        b = (*bp);
730
2.58M
        *bp = b->left_size;
731
2.58M
        b->left_size = node->left_size;
732
2.58M
        b->right_size = node->right_size;
733
2.58M
        *ap = b;
734
2.58M
    }
735
116M
}
736
737
static void remove_free_size_fast(gs_memory_chunk_t *cmem, chunk_free_node_t **ap)
738
127M
{
739
127M
    chunk_free_node_t *node = *ap;
740
741
127M
    if (node->left_size == NULL)
742
75.5M
        *ap = node->right_size;
743
51.7M
    else if (node->right_size == NULL)
744
916k
        *ap = node->left_size;
745
50.8M
    else {
746
        /* Find the in-order predecessor to node and use that to replace node */
747
50.8M
        chunk_free_node_t **bp;
748
50.8M
        chunk_free_node_t  *b;
749
50.8M
        bp = &node->left_size;
750
51.0M
        while ((*bp)->right_size)
751
224k
            bp = &(*bp)->right_size;
752
50.8M
        b = (*bp);
753
50.8M
        *bp = b->left_size;
754
50.8M
        b->left_size = node->left_size;
755
50.8M
        b->right_size = node->right_size;
756
50.8M
        *ap = b;
757
50.8M
    }
758
127M
}
759
760
static void remove_free(gs_memory_chunk_t *cmem, chunk_free_node_t *node)
761
2.84M
{
762
2.84M
    remove_free_loc(cmem, node);
763
2.84M
    remove_free_size(cmem, node);
764
2.84M
}
765
766
#if defined(MEMENTO) || defined(SINGLE_OBJECT_MEMORY_BLOCKS_ONLY)
767
#define SINGLE_OBJECT_CHUNK(size) (1)
768
#else
769
255M
#define SINGLE_OBJECT_CHUNK(size) ((size) > (CHUNK_SIZE>>1))
770
#endif
771
772
/* All of the allocation routines reduce to this function */
773
static byte *
774
chunk_obj_alloc(gs_memory_t *mem, size_t size, gs_memory_type_ptr_t type, client_name_t cname)
775
127M
{
776
127M
    gs_memory_chunk_t  *cmem = (gs_memory_chunk_t *)mem;
777
127M
    chunk_free_node_t **ap, **okp;
778
127M
    chunk_free_node_t  *a, *b, *c;
779
127M
    size_t newsize;
780
127M
    chunk_obj_node_t *obj = NULL;
781
782
127M
    newsize = round_up_to_align(size + SIZEOF_ROUND_ALIGN(chunk_obj_node_t));  /* space we will need */
783
    /* When we free this block it might have to go in free - so it had
784
     * better be large enough to accommodate a complete free node! */
785
127M
    if (newsize < SIZEOF_ROUND_ALIGN(chunk_free_node_t))
786
21.3k
        newsize = SIZEOF_ROUND_ALIGN(chunk_free_node_t);
787
    /* Protect against overflow */
788
127M
    if (newsize < size)
789
0
        return NULL;
790
791
#ifdef DEBUG_SEQ
792
    cmem->sequence++;
793
#endif
794
795
#ifdef DEBUG_CHUNK_PRINT
796
#ifdef DEBUG_SEQ
797
    dmlprintf4(mem, "Event %x: malloc(chunk="PRI_INTPTR", size=%"PRIxSIZE", cname=%s)\n",
798
               cmem->sequence, (intptr_t)cmem, newsize, cname);
799
#else
800
    dmlprintf3(mem, "malloc(chunk="PRI_INTPTR", size=%"PRIxSIZE", cname=%s)\n",
801
               (intptr_t)cmem, newsize, cname);
802
#endif
803
#endif
804
805
    /* Large blocks are allocated directly */
806
127M
    if (SINGLE_OBJECT_CHUNK(newsize)) {
807
87.0k
        obj = (chunk_obj_node_t *)gs_alloc_bytes_immovable(cmem->target, newsize, cname);
808
87.0k
        if (obj == NULL)
809
1.44k
            return NULL;
810
127M
    } else {
811
        /* Find the smallest free block that's large enough */
812
        /* okp points to the parent pointer to the block we pick */
813
127M
        ap = &cmem->free_size;
814
127M
        okp = NULL;
815
192M
        while ((a = *ap) != NULL) {
816
140M
            if (a->size >= newsize) {
817
22.8M
                b = a->left_size;
818
22.8M
                if (b == NULL) {
819
4.40M
                    okp = ap; /* a will do */
820
4.40M
                    break; /* Stop searching */
821
4.40M
                }
822
18.4M
                if (b->size >= newsize) {
823
10.1M
                    c = b->left_size;
824
10.1M
                    if (c == NULL) {
825
4.47M
                        okp = &a->left_size; /* b is as good as we're going to get */
826
4.47M
                        break;
827
4.47M
                    }
828
                    /* Splay:        a             c
829
                     *            b     Z   =>  W     b
830
                     *          c   Y               X   a
831
                     *         W X                     Y Z
832
                     */
833
5.65M
                    *ap = c;
834
5.65M
                    a->left_size  = b->right_size;
835
5.65M
                    b->left_size  = c->right_size;
836
5.65M
                    b->right_size = a;
837
5.65M
                    c->right_size = b;
838
5.65M
                    if (c->size >= newsize) {
839
3.95M
                        okp = ap; /* c is the best so far */
840
3.95M
                        ap = &c->left_size;
841
3.95M
                    } else {
842
1.70M
                        okp = &c->right_size; /* b is the best so far */
843
1.70M
                        ap = &b->left_size;
844
1.70M
                    }
845
8.34M
                } else {
846
8.34M
                    c = b->right_size;
847
8.34M
                    if (c == NULL) {
848
2.01M
                        okp = ap; /* a is as good as we are going to get */
849
2.01M
                        break;
850
2.01M
                    }
851
                    /* Splay:         a             c
852
                     *            b       Z  =>   b   a
853
                     *          W   c            W X Y Z
854
                     *             X Y
855
                     */
856
6.33M
                    *ap = c;
857
6.33M
                    a->left_size  = c->right_size;
858
6.33M
                    b->right_size = c->left_size;
859
6.33M
                    c->left_size  = b;
860
6.33M
                    c->right_size = a;
861
6.33M
                    if (c->size >= newsize) {
862
5.75M
                        okp = ap; /* c is the best so far */
863
5.75M
                        ap = &b->right_size;
864
5.75M
                    } else {
865
572k
                        okp = &c->right_size; /* a is the best so far */
866
572k
                        ap = &a->left_size;
867
572k
                    }
868
6.33M
                }
869
118M
            } else {
870
118M
                b = a->right_size;
871
118M
                if (b == NULL)
872
465k
                    break; /* No better match to be found */
873
117M
                if (b->size >= newsize) {
874
108M
                    c = b->left_size;
875
108M
                    if (c == NULL) {
876
63.9M
                        okp = &a->right_size; /* b is as good as we're going to get */
877
63.9M
                        break;
878
63.9M
                    }
879
                    /* Splay:      a                c
880
                     *         W       b    =>    a   b
881
                     *               c   Z       W X Y Z
882
                     *              X Y
883
                     */
884
44.4M
                    *ap = c;
885
44.4M
                    a->right_size = c->left_size;
886
44.4M
                    b->left_size  = c->right_size;
887
44.4M
                    c->left_size  = a;
888
44.4M
                    c->right_size = b;
889
44.4M
                    if (c->size >= newsize) {
890
43.4M
                        okp = ap; /* c is the best so far */
891
43.4M
                        ap = &a->right_size;
892
43.4M
                    } else {
893
1.01M
                        okp = &c->right_size; /* b is the best so far */
894
1.01M
                        ap = &b->left_size;
895
1.01M
                    }
896
44.4M
                } else {
897
9.13M
                    c = b->right_size;
898
9.13M
                    if (c == NULL)
899
255k
                        break; /* No better match to be found */
900
                    /* Splay:    a                   c
901
                     *        W     b      =>     b     Z
902
                     *            X   c         a   Y
903
                     *               Y Z       W X
904
                     */
905
8.88M
                    *ap = c;
906
8.88M
                    a->right_size = b->left_size;
907
8.88M
                    b->right_size = c->left_size;
908
8.88M
                    b->left_size  = a;
909
8.88M
                    c->left_size  = b;
910
8.88M
                    if (c->size >= newsize) {
911
2.36M
                        okp = ap; /* c is the best so far */
912
2.36M
                        ap = &b->right_size;
913
2.36M
                    } else
914
6.51M
                        ap = &c->right_size;
915
8.88M
                }
916
117M
            }
917
140M
        }
918
919
        /* So *okp points to the most appropriate free tree entry. */
920
921
127M
        if (okp == NULL) {
922
            /* No appropriate free space slot. We need to allocate a new slab. */
923
246k
            chunk_slab_t *slab;
924
246k
            uint slab_size = newsize + SIZEOF_ROUND_ALIGN(chunk_slab_t);
925
926
246k
            if (slab_size <= (CHUNK_SIZE>>1))
927
246k
                slab_size = CHUNK_SIZE;
928
246k
            slab = (chunk_slab_t *)gs_alloc_bytes_immovable(cmem->target, slab_size, cname);
929
246k
            if (slab == NULL)
930
27
                return NULL;
931
246k
            slab->next = cmem->slabs;
932
246k
            cmem->slabs = slab;
933
934
246k
            obj = (chunk_obj_node_t *)(((byte *)slab) + SIZEOF_ROUND_ALIGN(chunk_slab_t));
935
246k
            if (slab_size != newsize + SIZEOF_ROUND_ALIGN(chunk_slab_t)) {
936
246k
                insert_free(cmem, (chunk_free_node_t *)(((byte *)obj)+newsize), slab_size - newsize - SIZEOF_ROUND_ALIGN(chunk_slab_t));
937
246k
                cmem->total_free += slab_size - newsize - SIZEOF_ROUND_ALIGN(chunk_slab_t);
938
246k
            }
939
127M
        } else {
940
127M
            chunk_free_node_t *ok = *okp;
941
127M
            obj = (chunk_obj_node_t *)(void *)ok;
942
127M
            if (ok->size >= newsize + SIZEOF_ROUND_ALIGN(chunk_free_node_t)) {
943
116M
                chunk_free_node_t *tail = (chunk_free_node_t *)(((byte *)ok) + newsize);
944
116M
                uint tail_size = ok->size - newsize;
945
116M
                remove_free_size_fast(cmem, okp);
946
116M
                remove_free_loc(cmem, ok);
947
116M
                insert_free(cmem, tail, tail_size);
948
116M
            } else {
949
11.0M
                newsize = ok->size;
950
11.0M
                remove_free_size_fast(cmem, okp);
951
11.0M
                remove_free_loc(cmem, ok);
952
11.0M
            }
953
127M
            cmem->total_free -= newsize;
954
127M
        }
955
127M
    }
956
957
127M
    if (gs_alloc_debug) {
958
0
        memset((byte *)(obj) + SIZEOF_ROUND_ALIGN(chunk_obj_node_t), 0xa1, newsize - SIZEOF_ROUND_ALIGN(chunk_obj_node_t));
959
0
        memset((byte *)(obj) + SIZEOF_ROUND_ALIGN(chunk_obj_node_t), 0xac, size);
960
0
    }
961
962
127M
    cmem->used += newsize;
963
127M
    obj->size = newsize; /* actual size */
964
127M
    obj->padding = newsize - size; /* actual size - client requested size */
965
127M
    obj->type = type;    /* and client desired type */
966
127M
    obj->defer_next = NULL;
967
968
#ifdef DEBUG_SEQ
969
    obj->sequence = cmem->sequence;
970
#endif
971
127M
    if (gs_debug_c('A'))
972
0
        dmlprintf3(mem, "[a+]chunk_obj_alloc (%s)(%"PRIuSIZE") = "PRI_INTPTR": OK.\n",
973
127M
                   client_name_string(cname), size, (intptr_t) obj);
974
#ifdef DEBUG_CHUNK_PRINT
975
#ifdef DEBUG_SEQ
976
    dmlprintf5(mem, "Event %x: malloced(chunk="PRI_INTPTR", addr="PRI_INTPTR", size=%"PRIxSIZE", cname=%s)\n",
977
               obj->sequence, (intptr_t)cmem, (intptr_t)obj, obj->size, cname);
978
#else
979
    dmlprintf4(mem, "malloced(chunk="PRI_INTPTR", addr="PRI_INTPTR", size=%"PRIxSIZE", cname=%s)\n",
980
               (intptr_t)cmem, (intptr_t)obj, obj->size, cname);
981
#endif
982
#endif
983
#ifdef DEBUG_CHUNK
984
    gs_memory_chunk_dump_memory(cmem);
985
#endif
986
987
127M
    return (byte *)Memento_label((byte *)(obj) + SIZEOF_ROUND_ALIGN(chunk_obj_node_t), cname);
988
127M
}
989
990
static byte *
991
chunk_alloc_bytes_immovable(gs_memory_t * mem, size_t size, client_name_t cname)
992
7.37k
{
993
7.37k
    return chunk_obj_alloc(mem, size, &st_bytes, cname);
994
7.37k
}
995
996
static byte *
997
chunk_alloc_bytes(gs_memory_t * mem, size_t size, client_name_t cname)
998
126M
{
999
126M
    return chunk_obj_alloc(mem, size, &st_bytes, cname);
1000
126M
}
1001
1002
static void *
1003
chunk_alloc_struct_immovable(gs_memory_t * mem, gs_memory_type_ptr_t pstype,
1004
                             client_name_t cname)
1005
26.0k
{
1006
26.0k
    return chunk_obj_alloc(mem, pstype->ssize, pstype, cname);
1007
26.0k
}
1008
1009
static void *
1010
chunk_alloc_struct(gs_memory_t * mem, gs_memory_type_ptr_t pstype,
1011
                   client_name_t cname)
1012
543k
{
1013
543k
    return chunk_obj_alloc(mem, pstype->ssize, pstype, cname);
1014
543k
}
1015
1016
static byte *
1017
chunk_alloc_byte_array_immovable(gs_memory_t * mem, size_t num_elements,
1018
                                 size_t elt_size, client_name_t cname)
1019
40.2k
{
1020
40.2k
    return chunk_alloc_bytes(mem, num_elements * elt_size, cname);
1021
40.2k
}
1022
1023
static byte *
1024
chunk_alloc_byte_array(gs_memory_t * mem, size_t num_elements, size_t elt_size,
1025
                   client_name_t cname)
1026
1.10M
{
1027
1.10M
    return chunk_alloc_bytes(mem, num_elements * elt_size, cname);
1028
1.10M
}
1029
1030
static void *
1031
chunk_alloc_struct_array_immovable(gs_memory_t * mem, size_t num_elements,
1032
                                   gs_memory_type_ptr_t pstype, client_name_t cname)
1033
319
{
1034
319
    return chunk_obj_alloc(mem, num_elements * pstype->ssize, pstype, cname);
1035
319
}
1036
1037
static void *
1038
chunk_alloc_struct_array(gs_memory_t * mem, size_t num_elements,
1039
                         gs_memory_type_ptr_t pstype, client_name_t cname)
1040
4.38k
{
1041
4.38k
    return chunk_obj_alloc(mem, num_elements * pstype->ssize, pstype, cname);
1042
4.38k
}
1043
1044
static void *
1045
chunk_resize_object(gs_memory_t * mem, void *ptr, size_t new_num_elements, client_name_t cname)
1046
50.5k
{
1047
50.5k
    void *new_ptr = NULL;
1048
1049
50.5k
    if (ptr != NULL) {
1050
        /* This isn't particularly efficient, but it is rarely used */
1051
50.5k
        chunk_obj_node_t *obj = (chunk_obj_node_t *)(((byte *)ptr) - SIZEOF_ROUND_ALIGN(chunk_obj_node_t));
1052
50.5k
        size_t new_size = (obj->type->ssize * new_num_elements);
1053
50.5k
        size_t old_size = obj->size - obj->padding;
1054
        /* get the type from the old object */
1055
50.5k
        gs_memory_type_ptr_t type = obj->type;
1056
50.5k
        gs_memory_chunk_t *cmem = (gs_memory_chunk_t *)mem;
1057
50.5k
        size_t save_max_used = cmem->max_used;
1058
1059
50.5k
        if (new_size == old_size)
1060
2.05k
            return ptr;
1061
48.4k
        if ((new_ptr = chunk_obj_alloc(mem, new_size, type, cname)) == 0)
1062
0
            return NULL;
1063
48.4k
        memcpy(new_ptr, ptr, min(old_size, new_size));
1064
48.4k
        chunk_free_object(mem, ptr, cname);
1065
48.4k
        cmem->max_used = save_max_used;
1066
48.4k
        if (cmem->used > cmem->max_used)
1067
41.8k
            cmem->max_used = cmem->used;
1068
48.4k
    }
1069
1070
48.4k
    return new_ptr;
1071
50.5k
}
1072
1073
static void
1074
chunk_free_object(gs_memory_t *mem, void *ptr, client_name_t cname)
1075
127M
{
1076
127M
    gs_memory_chunk_t * const cmem = (gs_memory_chunk_t *)mem;
1077
127M
    size_t obj_node_size;
1078
127M
    chunk_obj_node_t *obj;
1079
127M
    struct_proc_finalize((*finalize));
1080
127M
    chunk_free_node_t **ap, **gtp, **ltp;
1081
127M
    chunk_free_node_t *a, *b, *c;
1082
1083
127M
    if (ptr == NULL)
1084
389k
        return;
1085
1086
    /* back up to obj header */
1087
127M
    obj_node_size = SIZEOF_ROUND_ALIGN(chunk_obj_node_t);
1088
127M
    obj = (chunk_obj_node_t *)(((byte *)ptr) - obj_node_size);
1089
1090
127M
    if (cmem->deferring) {
1091
0
        if (obj->defer_next == NULL) {
1092
0
            obj->defer_next = cmem->defer_finalize_list;
1093
0
            cmem->defer_finalize_list = obj;
1094
0
        }
1095
0
        return;
1096
0
    }
1097
1098
#ifdef DEBUG_CHUNK_PRINT
1099
#ifdef DEBUG_SEQ
1100
    cmem->sequence++;
1101
    dmlprintf6(cmem->target, "Event %x: free(chunk="PRI_INTPTR", addr="PRI_INTPTR", size=%x, num=%x, cname=%s)\n",
1102
               cmem->sequence, (intptr_t)cmem, (intptr_t)obj, obj->size, obj->sequence, cname);
1103
#else
1104
    dmlprintf4(cmem->target, "free(chunk="PRI_INTPTR", addr="PRI_INTPTR", size=%x, cname=%s)\n",
1105
               (intptr_t)cmem, (intptr_t)obj, obj->size, cname);
1106
#endif
1107
#endif
1108
1109
127M
    if (obj->type) {
1110
127M
        finalize = obj->type->finalize;
1111
127M
        if (finalize != NULL)
1112
162k
            finalize(mem, ptr);
1113
127M
    }
1114
    /* finalize may change the head_**_chunk doing free of stuff */
1115
1116
127M
    if_debug3m('A', cmem->target, "[a-]chunk_free_object(%s) "PRI_INTPTR"(%"PRIuSIZE")\n",
1117
127M
               client_name_string(cname), (intptr_t)ptr, obj->size);
1118
1119
127M
    cmem->used -= obj->size;
1120
1121
127M
    if (SINGLE_OBJECT_CHUNK(obj->size)) {
1122
85.6k
        gs_free_object(cmem->target, obj, "chunk_free_object(single object)");
1123
#ifdef DEBUG_CHUNK
1124
        gs_memory_chunk_dump_memory(cmem);
1125
#endif
1126
85.6k
        return;
1127
85.6k
    }
1128
1129
    /* We want to find where to insert this free entry into our free tree. We need to know
1130
     * both the point to the left of it, and the point to the right of it, in order to see
1131
     * if we can merge the free entries. Accordingly, we search from the top of the tree
1132
     * and keep pointers to the nodes that we pass that are greater than it, and less than
1133
     * it. */
1134
127M
    gtp = NULL; /* gtp is set to the address of the pointer to the node where we last stepped left */
1135
127M
    ltp = NULL; /* ltp is set to the address of the pointer to the node where we last stepped right */
1136
127M
    ap = &cmem->free_loc;
1137
162M
    while ((a = *ap) != NULL) {
1138
149M
        if ((void *)a > (void *)obj) {
1139
31.0M
            b = a->left_loc; /* Try to step left from a */
1140
31.0M
            if (b == NULL) {
1141
6.36M
                gtp = ap; /* a is greater than us */
1142
6.36M
                break;
1143
6.36M
            }
1144
24.7M
            if ((void *)b > (void *)obj) {
1145
14.2M
                c = b->left_loc; /* Try to step left from b */
1146
14.2M
                if (c == NULL) {
1147
2.32M
                    gtp = &a->left_loc; /* b is greater than us */
1148
2.32M
                    break;
1149
2.32M
                }
1150
                /* Splay:        a             c
1151
                 *            b     Z   =>  W     b
1152
                 *          c   Y               X   a
1153
                 *         W X                     Y Z
1154
                 */
1155
11.9M
                *ap = c;
1156
11.9M
                a->left_loc  = b->right_loc;
1157
11.9M
                b->left_loc  = c->right_loc;
1158
11.9M
                b->right_loc = a;
1159
11.9M
                c->right_loc = b;
1160
11.9M
                if ((void *)c > (void *)obj) { /* W */
1161
6.82M
                    gtp = ap; /* c is greater than us */
1162
6.82M
                    ap = &c->left_loc;
1163
6.82M
                } else { /* X */
1164
5.13M
                    gtp = &c->right_loc; /* b is greater than us */
1165
5.13M
                    ltp = ap; /* c is less than us */
1166
5.13M
                    ap = &b->left_loc;
1167
5.13M
                }
1168
11.9M
            } else {
1169
10.4M
                c = b->right_loc; /* Try to step right from b */
1170
10.4M
                if (c == NULL) {
1171
5.01M
                    gtp = ap; /* a is greater than us */
1172
5.01M
                    ltp = &a->left_loc; /* b is less than us */
1173
5.01M
                    break;
1174
5.01M
                }
1175
                /* Splay:         a             c
1176
                 *            b       Z  =>   b   a
1177
                 *          W   c            W X Y Z
1178
                 *             X Y
1179
                 */
1180
5.40M
                *ap = c;
1181
5.40M
                a->left_loc  = c->right_loc;
1182
5.40M
                b->right_loc = c->left_loc;
1183
5.40M
                c->left_loc  = b;
1184
5.40M
                c->right_loc = a;
1185
5.40M
                if ((void *)c > (void *)obj) { /* X */
1186
3.57M
                    gtp = ap; /* c is greater than us */
1187
3.57M
                    ltp = &c->left_loc; /* b is less than us */
1188
3.57M
                    ap = &b->right_loc;
1189
3.57M
                } else { /* Y */
1190
1.82M
                    gtp = &c->right_loc; /* a is greater than us */
1191
1.82M
                    ltp = ap; /* c is less than us */
1192
1.82M
                    ap = &a->left_loc;
1193
1.82M
                }
1194
5.40M
            }
1195
118M
        } else {
1196
118M
            b = a->right_loc; /* Try to step right from a */
1197
118M
            if (b == NULL) {
1198
2.61M
                ltp = ap; /* a is less than us */
1199
2.61M
                break;
1200
2.61M
            }
1201
115M
            if ((void *)b > (void *)obj) {
1202
105M
                c = b->left_loc;
1203
105M
                if (c == NULL) {
1204
97.5M
                    gtp = &a->right_loc; /* b is greater than us */
1205
97.5M
                    ltp = ap; /* a is less than us */
1206
97.5M
                    break;
1207
97.5M
                }
1208
                /* Splay:      a                c
1209
                 *         W       b    =>    a   b
1210
                 *               c   Z       W X Y Z
1211
                 *              X Y
1212
                 */
1213
7.88M
                *ap = c;
1214
7.88M
                a->right_loc = c->left_loc;
1215
7.88M
                b->left_loc  = c->right_loc;
1216
7.88M
                c->left_loc  = a;
1217
7.88M
                c->right_loc = b;
1218
7.88M
                if ((void *)c > (void *)obj) { /* X */
1219
5.94M
                    gtp = ap; /* c is greater than us */
1220
5.94M
                    ltp = &c->left_loc; /* a is less than us */
1221
5.94M
                    ap = &a->right_loc;
1222
5.94M
                } else { /* Y */
1223
1.94M
                    gtp = &c->right_loc; /* b is greater than us */
1224
1.94M
                    ltp = ap; /* c is less than than us */
1225
1.94M
                    ap = &b->left_loc;
1226
1.94M
                }
1227
10.1M
            } else {
1228
10.1M
                c = b->right_loc;
1229
10.1M
                if (c == NULL) {
1230
664k
                    ltp = &a->right_loc; /* b is greater than us */
1231
664k
                    break;
1232
664k
                }
1233
                /* Splay:    a                   c
1234
                 *        W     b      =>     b     Z
1235
                 *            X   c         a   Y
1236
                 *               Y Z       W X
1237
                 */
1238
9.45M
                *ap = c;
1239
9.45M
                a->right_loc = b->left_loc;
1240
9.45M
                b->right_loc = c->left_loc;
1241
9.45M
                b->left_loc  = a;
1242
9.45M
                c->left_loc  = b;
1243
9.45M
                if ((void *)c > (void *)obj) { /* Y */
1244
3.90M
                    gtp = ap; /* c is greater than us */
1245
3.90M
                    ltp = &c->left_loc; /* b is less than us */
1246
3.90M
                    ap = &b->right_loc;
1247
5.54M
                } else { /* Z */
1248
5.54M
                    ltp = ap; /* c is less than than us */
1249
5.54M
                    ap = &c->right_loc;
1250
5.54M
                }
1251
9.45M
            }
1252
115M
        }
1253
149M
    }
1254
1255
127M
    if (ltp) {
1256
        /* There is at least 1 node smaller than us - check for merging */
1257
125M
        chunk_free_node_t *ltfree = (chunk_free_node_t *)(*ltp);
1258
125M
        if ((((byte *)ltfree) + ltfree->size) == (byte *)(void *)obj) {
1259
            /* Merge! */
1260
98.3M
            cmem->total_free += obj->size;
1261
98.3M
            remove_free_size(cmem, ltfree);
1262
98.3M
            ltfree->size += obj->size;
1263
98.3M
            if (gtp) {
1264
                /* There is at least 1 node greater than us - check for merging */
1265
98.1M
                chunk_free_node_t *gtfree = (chunk_free_node_t *)(*gtp);
1266
98.1M
                if ((((byte *)obj) + obj->size) == (byte *)(void *)gtfree) {
1267
                    /* Double merge! */
1268
2.84M
                    ltfree->size += gtfree->size;
1269
2.84M
                    remove_free(cmem, gtfree);
1270
2.84M
                }
1271
98.1M
                gtp = NULL;
1272
98.1M
            }
1273
98.3M
            insert_free_size(cmem, ltfree);
1274
98.3M
            if (gs_alloc_debug)
1275
0
                memset(((byte *)ltfree) + SIZEOF_ROUND_ALIGN(chunk_free_node_t), 0x69, ltfree->size - SIZEOF_ROUND_ALIGN(chunk_free_node_t));
1276
98.3M
            obj = NULL;
1277
98.3M
        }
1278
125M
    }
1279
127M
    if (gtp && obj) {
1280
        /* There is at least 1 node greater than us - check for merging */
1281
29.0M
        chunk_free_node_t *gtfree = (chunk_free_node_t *)(*gtp);
1282
29.0M
        if ((((byte *)obj) + obj->size) == (byte *)(void *)gtfree) {
1283
            /* Merge! */
1284
15.1M
            chunk_free_node_t *objfree = (chunk_free_node_t *)(void *)obj;
1285
15.1M
            uint obj_size = obj->size;
1286
15.1M
            cmem->total_free += obj_size;
1287
15.1M
            remove_free_size(cmem, gtfree);
1288
15.1M
            *objfree = *gtfree;
1289
15.1M
            objfree->size += obj_size;
1290
15.1M
            *gtp = objfree;
1291
15.1M
            insert_free_size(cmem, objfree);
1292
15.1M
            if (gs_alloc_debug)
1293
0
                memset(((byte *)objfree) + SIZEOF_ROUND_ALIGN(chunk_free_node_t), 0x96, objfree->size - SIZEOF_ROUND_ALIGN(chunk_free_node_t));
1294
15.1M
            obj = NULL;
1295
15.1M
        }
1296
29.0M
    }
1297
1298
127M
    if (obj) {
1299
        /* Insert new one */
1300
13.9M
        chunk_free_node_t *objfree = (chunk_free_node_t *)(void *)obj;
1301
13.9M
        cmem->total_free += obj->size;
1302
13.9M
        objfree->size = obj->size;
1303
13.9M
        objfree->left_loc = NULL;
1304
13.9M
        objfree->right_loc = NULL;
1305
13.9M
        if (gtp) {
1306
13.8M
            ap = &(*gtp)->left_loc;
1307
20.3M
            while (*ap) {
1308
6.52M
                ap = &(*ap)->right_loc;
1309
6.52M
            }
1310
13.8M
        } else if (ltp) {
1311
90.7k
            ap = &(*ltp)->right_loc;
1312
90.7k
            while (*ap) {
1313
0
                ap = &(*ap)->left_loc;
1314
0
            }
1315
90.7k
        } else
1316
1
            ap = &cmem->free_loc;
1317
13.9M
        *ap = objfree;
1318
13.9M
        insert_free_size(cmem, objfree);
1319
13.9M
        if (gs_alloc_debug)
1320
0
            memset(((byte *)objfree) + SIZEOF_ROUND_ALIGN(chunk_free_node_t), 0x9b, objfree->size - SIZEOF_ROUND_ALIGN(chunk_free_node_t));
1321
13.9M
    }
1322
1323
#ifdef DEBUG_CHUNK
1324
    gs_memory_chunk_dump_memory(cmem);
1325
#endif
1326
127M
}
1327
1328
static byte *
1329
chunk_alloc_string_immovable(gs_memory_t * mem, size_t nbytes, client_name_t cname)
1330
0
{
1331
    /* we just alloc bytes here */
1332
0
    return chunk_alloc_bytes(mem, nbytes, cname);
1333
0
}
1334
1335
static byte *
1336
chunk_alloc_string(gs_memory_t * mem, size_t nbytes, client_name_t cname)
1337
11.8k
{
1338
    /* we just alloc bytes here */
1339
11.8k
    return chunk_alloc_bytes(mem, nbytes, cname);
1340
11.8k
}
1341
1342
static byte *
1343
chunk_resize_string(gs_memory_t * mem, byte * data, size_t old_num, size_t new_num,
1344
                    client_name_t cname)
1345
0
{
1346
    /* just resize object - ignores old_num */
1347
0
    return chunk_resize_object(mem, data, new_num, cname);
1348
0
}
1349
1350
static void
1351
chunk_free_string(gs_memory_t * mem, byte * data, size_t nbytes,
1352
                  client_name_t cname)
1353
13.6k
{
1354
13.6k
    chunk_free_object(mem, data, cname);
1355
13.6k
}
1356
1357
static void
1358
chunk_status(gs_memory_t * mem, gs_memory_status_t * pstat)
1359
0
{
1360
0
    gs_memory_chunk_t *cmem = (gs_memory_chunk_t *)mem;
1361
1362
0
    pstat->allocated = cmem->used;
1363
0
    pstat->used = cmem->used - cmem->total_free;
1364
0
    pstat->max_used = cmem->max_used;
1365
0
    pstat->is_thread_safe = false; /* this allocator does not have an internal mutex */
1366
0
}
1367
1368
static gs_memory_t *
1369
chunk_stable(gs_memory_t * mem)
1370
109k
{
1371
109k
    return mem;
1372
109k
}
1373
1374
static void
1375
chunk_enable_free(gs_memory_t * mem, bool enable)
1376
0
{
1377
0
}
1378
1379
static void chunk_set_object_type(gs_memory_t *mem, void *ptr, gs_memory_type_ptr_t type)
1380
0
{
1381
0
    chunk_obj_node_t *obj = (chunk_obj_node_t *)(((byte *)ptr) - SIZEOF_ROUND_ALIGN(chunk_obj_node_t));
1382
1383
0
    if (ptr == 0)
1384
0
        return;
1385
0
    obj->type = type;
1386
0
}
1387
1388
static void chunk_defer_frees(gs_memory_t *mem, int defer)
1389
0
{
1390
0
    gs_memory_chunk_t *cmem = (gs_memory_chunk_t *)mem;
1391
0
    chunk_obj_node_t *n;
1392
1393
0
    if (defer == 0) {
1394
        /* Run through and finalise everything on the finalize list. This
1395
         * might cause other stuff to be put onto the finalize list. As we
1396
         * finalize stuff we move it to the free list. */
1397
0
        while (cmem->defer_finalize_list) {
1398
0
            n = cmem->defer_finalize_list;
1399
0
            cmem->defer_finalize_list = n->defer_next;
1400
0
            if (n->type) {
1401
0
                if (n->type->finalize)
1402
0
                    n->type->finalize(mem, ((byte *)n) + SIZEOF_ROUND_ALIGN(chunk_obj_node_t));
1403
0
                n->type = NULL;
1404
0
            }
1405
0
            n->defer_next = cmem->defer_free_list;
1406
0
            cmem->defer_free_list = n;
1407
0
        }
1408
0
    }
1409
0
    cmem->deferring = defer;
1410
0
    if (defer == 0) {
1411
        /* Now run through and free everything on the free list */
1412
0
        while (cmem->defer_free_list) {
1413
0
            n = cmem->defer_free_list;
1414
0
            cmem->defer_free_list = n->defer_next;
1415
0
            chunk_free_object(mem, ((byte *)n) + SIZEOF_ROUND_ALIGN(chunk_obj_node_t), "deferred free");
1416
0
        }
1417
0
    }
1418
0
}
1419
1420
static void
1421
chunk_consolidate_free(gs_memory_t *mem)
1422
0
{
1423
0
}
1424
1425
/* accessors to get size and type given the pointer returned to the client */
1426
static size_t
1427
chunk_object_size(gs_memory_t * mem, const void *ptr)
1428
0
{
1429
0
    if (ptr != NULL) {
1430
0
        chunk_obj_node_t *obj = (chunk_obj_node_t *)(((byte *)ptr) - SIZEOF_ROUND_ALIGN(chunk_obj_node_t));
1431
1432
0
        return obj->size - obj->padding;
1433
0
    }
1434
1435
0
    return 0;
1436
0
}
1437
1438
static gs_memory_type_ptr_t
1439
chunk_object_type(const gs_memory_t * mem, const void *ptr)
1440
0
{
1441
0
    chunk_obj_node_t *obj = (chunk_obj_node_t *)(((byte *)ptr) - SIZEOF_ROUND_ALIGN(chunk_obj_node_t));
1442
0
    return obj->type;
1443
0
}
1444
1445
static int
1446
chunk_register_root(gs_memory_t * mem, gs_gc_root_t ** rp, gs_ptr_type_t ptype,
1447
                 void **up, client_name_t cname)
1448
0
{
1449
0
    return 0;
1450
0
}
1451
1452
static void
1453
chunk_unregister_root(gs_memory_t * mem, gs_gc_root_t * rp, client_name_t cname)
1454
0
{
1455
0
}