summaryrefslogtreecommitdiff
path: root/base/gsmchunk.c
blob: 2e23272c0f543d68e97594e366f2912b0b704a44 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
/* Copyright (C) 2001-2023 Artifex Software, Inc.
   All Rights Reserved.

   This software is provided AS-IS with no warranty, either express or
   implied.

   This software is distributed under license and may not be copied,
   modified or distributed except as expressly authorized under the terms
   of the license contained in the file LICENSE in this distribution.

   Refer to licensing information at http://www.artifex.com or contact
   Artifex Software, Inc.,  39 Mesa Street, Suite 108A, San Francisco,
   CA 94129, USA, for further information.
*/


/* chunk consolidating wrapper on a base memory allocator */

/* This uses dual binary trees to handle the free list. One tree
 * holds the blocks in size order, one in location order. We use
 * a top-down semi-splaying access scheme on lookups and
 * insertions. */

#include "memory_.h"
#include "gx.h"
#include "gsstruct.h"
#include "gxobj.h"
#include "gsstype.h"
#include "gserrors.h"
#include "gsmchunk.h"
#include "gxsync.h"
#include "malloc_.h" /* For MEMENTO */
#include "assert_.h"
#include "gsmdebug.h"

/* Enable DEBUG_CHUNK to check the validity of the heap at every turn */
#undef DEBUG_CHUNK

/* Enable DEBUG_SEQ to keep sequence numbers in every block */
#undef DEBUG_SEQ

/* Enable DEBUG_CHUNK_PRINT to print the heap at every turn */
#undef DEBUG_CHUNK_PRINT

/* Enable DEBUG_CHUNK_PRINT_SLABS to list the slabs in the heap */
#undef DEBUG_CHUNK_PRINT_SLABS

#if defined(DEBUG_CHUNK_PRINT_SLABS) && !defined(DEBUG_CHUNK_PRINT)
#define DEBUG_CHUNK_PRINT
#endif

#if defined(DEBUG_CHUNK_PRINT) && !defined(DEBUG_CHUNK)
#define DEBUG_CHUNK
#endif

#if defined(DEBUG_CHUNK) && !defined(DEBUG)
#define DEBUG
#define CHUNK_FAKE_ASSERT
#endif

#ifdef DEBUG
#ifdef CHUNK_FAKE_ASSERT
#define CHUNK_ASSERT(M,A) gs_chunk_assert(M, A, #A)

static void gs_chunk_assert(gs_memory_t *m, int v, const char *s)
{
    void (*crash)(void);
    if (v)
        return;
    dmlprintf1(m, "Assert failed: %s\n", s);
    crash = NULL;
    crash();
}
#else
#define CHUNK_ASSERT(M,A) assert(A)
#endif
#endif

/* Raw memory procedures */
static gs_memory_proc_alloc_bytes(chunk_alloc_bytes_immovable);
static gs_memory_proc_resize_object(chunk_resize_object);
static gs_memory_proc_free_object(chunk_free_object);
static gs_memory_proc_stable(chunk_stable);
static gs_memory_proc_status(chunk_status);
static gs_memory_proc_free_all(chunk_free_all);
static gs_memory_proc_consolidate_free(chunk_consolidate_free);

/* Object memory procedures */
static gs_memory_proc_alloc_bytes(chunk_alloc_bytes);
static gs_memory_proc_alloc_struct(chunk_alloc_struct);
static gs_memory_proc_alloc_struct(chunk_alloc_struct_immovable);
static gs_memory_proc_alloc_byte_array(chunk_alloc_byte_array);
static gs_memory_proc_alloc_byte_array(chunk_alloc_byte_array_immovable);
static gs_memory_proc_alloc_struct_array(chunk_alloc_struct_array);
static gs_memory_proc_alloc_struct_array(chunk_alloc_struct_array_immovable);
static gs_memory_proc_object_size(chunk_object_size);
static gs_memory_proc_object_type(chunk_object_type);
static gs_memory_proc_alloc_string(chunk_alloc_string);
static gs_memory_proc_alloc_string(chunk_alloc_string_immovable);
static gs_memory_proc_resize_string(chunk_resize_string);
static gs_memory_proc_free_string(chunk_free_string);
static gs_memory_proc_register_root(chunk_register_root);
static gs_memory_proc_unregister_root(chunk_unregister_root);
static gs_memory_proc_enable_free(chunk_enable_free);
static gs_memory_proc_set_object_type(chunk_set_object_type);
static gs_memory_proc_defer_frees(chunk_defer_frees);
static const gs_memory_procs_t chunk_procs =
{
    /* Raw memory procedures */
    chunk_alloc_bytes_immovable,
    chunk_resize_object,
    chunk_free_object,
    chunk_stable,
    chunk_status,
    chunk_free_all,
    chunk_consolidate_free,
    /* Object memory procedures */
    chunk_alloc_bytes,
    chunk_alloc_struct,
    chunk_alloc_struct_immovable,
    chunk_alloc_byte_array,
    chunk_alloc_byte_array_immovable,
    chunk_alloc_struct_array,
    chunk_alloc_struct_array_immovable,
    chunk_object_size,
    chunk_object_type,
    chunk_alloc_string,
    chunk_alloc_string_immovable,
    chunk_resize_string,
    chunk_free_string,
    chunk_register_root,
    chunk_unregister_root,
    chunk_enable_free,
    chunk_set_object_type,
    chunk_defer_frees
};

typedef struct chunk_obj_node_s {
    gs_memory_type_ptr_t type;
#ifdef DEBUG_SEQ
    unsigned int sequence;
#endif
    struct chunk_obj_node_s *defer_next;
    size_t size; /* Actual size of block */
    size_t padding; /* Actual size - requested size */
} chunk_obj_node_t;

typedef struct chunk_free_node_s {
    struct chunk_free_node_s *left_loc;
    struct chunk_free_node_s *right_loc;
    struct chunk_free_node_s *left_size;
    struct chunk_free_node_s *right_size;
    size_t size;                  /* size of entire freelist block */
} chunk_free_node_t;

/*
 * Note: All objects within a chunk are 'aligned' since we round_up_to_align
 * the free list pointer when removing part of a free area.
 */
typedef struct chunk_slab_s {
    struct chunk_slab_s *next;
} chunk_slab_t;

typedef struct gs_memory_chunk_s {
    gs_memory_common;           /* interface outside world sees */
    gs_memory_t *target;        /* base allocator */
    chunk_slab_t *slabs;         /* list of slabs for freeing */
    chunk_free_node_t *free_size;/* free tree */
    chunk_free_node_t *free_loc; /* free tree */
    chunk_obj_node_t *defer_finalize_list;
    chunk_obj_node_t *defer_free_list;
    size_t used;
    size_t max_used;
    size_t total_free;
#ifdef DEBUG_SEQ
    unsigned int sequence;
#endif
    int deferring;
} gs_memory_chunk_t;

#define SIZEOF_ROUND_ALIGN(a) ROUND_UP(sizeof(a), obj_align_mod)

/* ---------- Public constructors/destructors ---------- */

/* Initialize a gs_memory_chunk_t */
int
gs_memory_chunk_wrap(gs_memory_t **wrapped,      /* chunk allocator init */
                    gs_memory_t  *target)       /* base allocator */
{
    /* Use the non-GC allocator of the target. */
    gs_memory_t       *non_gc_target = target->non_gc_memory;
    gs_memory_chunk_t *cmem = NULL;

    if (non_gc_target)
        cmem = (gs_memory_chunk_t *)gs_alloc_bytes_immovable(non_gc_target,
                                                            sizeof(gs_memory_chunk_t),
                                                            "gs_memory_chunk_wrap");
    if (cmem == NULL) {
        *wrapped = NULL;
        return_error(gs_error_VMerror);
    }
    cmem->stable_memory = (gs_memory_t *)cmem;	/* we are stable */
    cmem->procs = chunk_procs;
    cmem->gs_lib_ctx = non_gc_target->gs_lib_ctx;
    cmem->non_gc_memory = (gs_memory_t *)cmem;	/* and are not subject to GC */
    cmem->thread_safe_memory = non_gc_target->thread_safe_memory;
    cmem->target = non_gc_target;
    cmem->slabs = NULL;
    cmem->free_size = NULL;
    cmem->free_loc = NULL;
    cmem->used = 0;
    cmem->max_used = 0;
    cmem->total_free = 0;
#ifdef DEBUG_SEQ
    cmem->sequence = 0;
#endif
    cmem->deferring = 0;
    cmem->defer_finalize_list = NULL;
    cmem->defer_free_list = NULL;

#ifdef DEBUG_CHUNK_PRINT
    dmlprintf1(non_gc_target, "New chunk "PRI_INTPTR"\n", (intptr_t)cmem);
#endif

    /* Init the chunk management values */
    *wrapped = (gs_memory_t *)cmem;
    return 0;
}

/* Release a chunk memory manager. */
/* Note that this has no effect on the target. */
void
gs_memory_chunk_release(gs_memory_t *mem)
{
    gs_memory_free_all((gs_memory_t *)mem, FREE_ALL_EVERYTHING,
                       "gs_memory_chunk_release");
}

/* Release chunk memory manager, and return the target */
gs_memory_t * /* Always succeeds */
gs_memory_chunk_unwrap(gs_memory_t *mem)
{
    gs_memory_t *tmem;

    /* If this isn't a chunk, nothing to unwrap */
    if (mem->procs.status != chunk_status)
        return mem;

    tmem = ((gs_memory_chunk_t *)mem)->target;
    gs_memory_chunk_release(mem);

    return tmem;
}

/* ---------- Accessors ------------- */

/* Retrieve this allocator's target */
gs_memory_t *
gs_memory_chunk_target(const gs_memory_t *mem)
{
    gs_memory_chunk_t *cmem = (gs_memory_chunk_t *)mem;
    return cmem->target;
}

/* -------- Private members --------- */

/* Note that all of the data is 'immovable' and is opaque to the base allocator */
/* thus even if it is a GC type of allocator, no GC functions will be applied   */
/* All allocations are done in the target */

/* Procedures */

static void
chunk_mem_node_free_all_slabs(gs_memory_chunk_t *cmem)
{
    chunk_slab_t *slab, *next;
    gs_memory_t *const target = cmem->target;

    for (slab = cmem->slabs; slab != NULL; slab = next) {
        next = slab->next;
        gs_free_object(target, slab, "chunk_mem_node_free_all_slabs");
    }

    cmem->slabs = NULL;
    cmem->free_size = NULL;
    cmem->free_loc = NULL;
    cmem->total_free = 0;
    cmem->used = 0;
}

static void
chunk_free_all(gs_memory_t * mem, uint free_mask, client_name_t cname)
{
    gs_memory_chunk_t * const cmem = (gs_memory_chunk_t *)mem;
    gs_memory_t * const target = cmem->target;

    if (free_mask & FREE_ALL_DATA)
        chunk_mem_node_free_all_slabs(cmem);
    /* Only free the structures and the allocator itself. */
    if (mem->stable_memory) {
        if (mem->stable_memory != mem)
            gs_memory_free_all(mem->stable_memory, free_mask, cname);
        if (free_mask & FREE_ALL_ALLOCATOR)
            mem->stable_memory = 0;
    }
    if (free_mask & FREE_ALL_STRUCTURES) {
        cmem->target = 0;
    }
    if (free_mask & FREE_ALL_ALLOCATOR)
        gs_free_object(target, cmem, cname);
}

extern const gs_memory_struct_type_t st_bytes;

#ifdef DEBUG
static int dump_free_loc(gs_memory_t *mem, chunk_free_node_t *node, int depth, void **limit, uint *total)
{
#ifdef DEBUG_CHUNK_PRINT
    int i;
#endif
    int count;

    if (node == NULL)
        return 0;

    count = dump_free_loc(mem, node->left_loc, depth + 1 + (depth&1), limit, total);
    *total += node->size;
#ifdef DEBUG_CHUNK_PRINT
    if (depth != 0) {
        for (i = (depth-1)>>1; i != 0; i--)
            dmlprintf(mem, " ");
        if (depth & 1)
            dmlprintf(mem, "/");
        else
            dmlprintf(mem, "\\");
    }
    dmlprintf3(mem, PRI_INTPTR"+%x->"PRI_INTPTR"\n", (intptr_t)node, node->size, (intptr_t)((byte *)node)+node->size);
#endif
    CHUNK_ASSERT(mem, *limit < (void *)node);
    *limit = ((byte *)node)+node->size;
    return 1 + count + dump_free_loc(mem, node->right_loc, depth + 2 + (depth&1), limit, total);
}

static int dump_free_size(gs_memory_t *mem, chunk_free_node_t *node, int depth, uint *size, void **addr)
{
#ifdef DEBUG_CHUNK_PRINT
    int i;
#endif
    int count;

    if (node == NULL)
        return 0;

    count = dump_free_size(mem, node->left_size, depth + 1 + (depth&1), size, addr);
#ifdef DEBUG_CHUNK_PRINT
    if (depth != 0) {
        for (i = (depth-1)>>1; i != 0; i--)
            dmlprintf(mem, " ");
        if (depth & 1)
            dmlprintf(mem, "/");
        else
            dmlprintf(mem, "\\");
    }
    dmlprintf3(mem, PRI_INTPTR"+%x->"PRI_INTPTR"\n", (intptr_t)node, node->size, (intptr_t)((byte *)node)+node->size);
#endif
    CHUNK_ASSERT(mem, *size < node->size || (*size == node->size && *addr < (void *)node));
    *size = node->size;
    *addr = node;
    return 1 + count + dump_free_size(mem, node->right_size, depth + 2 + (depth&1), size, addr);
}

#ifdef DEBUG_CHUNK_PRINT
static size_t
largest_free_block(chunk_free_node_t *size)
{
    if (size == NULL)
        return 0;
    while (1) {
        if (size->right_size == NULL)
            return size->size;
        size = size->right_size;
    }
}
#endif

void
gs_memory_chunk_dump_memory(const gs_memory_t *mem)
{
    const gs_memory_chunk_t *cmem = (const gs_memory_chunk_t *)mem;
    int count1, count2;
    void *limit = NULL;
    void *addr = NULL;
    uint size = 1;
    uint total = 0;

#ifdef DEBUG_CHUNK_PRINT
    dmlprintf1(cmem->target, "Chunk "PRI_INTPTR":\n", (intptr_t)cmem);
    dmlprintf3(cmem->target, "Used=%"PRIxSIZE", Max Used=%"PRIxSIZE", Total Free=%"PRIxSIZE"\n", cmem->used, cmem->max_used, cmem->total_free);
    dmlprintf1(cmem->target, "Largest free block=%d bytes\n", largest_free_block(cmem->free_size));
#ifdef DEBUG_CHUNK_PRINT_SLABS
    {
        chunk_slab_t *slab;
        dmlprintf(cmem->target, "Slabs:\n");
        for (slab = cmem->slabs; slab != NULL; slab = slab->next)
            dmlprintf1(cmem->target, " "PRI_INTPTR"\n", (intptr_t)slab);
    }
#endif
    dmlprintf(cmem->target, "Locs:\n");
#endif
    count1 = dump_free_loc(cmem->target, cmem->free_loc, 0, &limit, &total);
#ifdef DEBUG_CHUNK_PRINT
    dmlprintf(cmem->target, "Sizes:\n");
#endif
    count2 = dump_free_size(cmem->target, cmem->free_size, 0, &size, &addr);
    if (count1 != count2) {
        void (*crash)(void) = NULL;
        dmlprintf2(cmem->target, "Tree mismatch! %d vs %d\n", count1, count2);
        crash();
    }
    if (total != cmem->total_free) {
        void (*crash)(void) = NULL;
        dmlprintf2(cmem->target, "Free size mismatch! %u vs %lu\n", total, cmem->total_free);
        crash();
    }
}
#endif

/* round up objects to make sure we have room for a header left */
inline static uint
round_up_to_align(uint size)
{
    uint num_node_headers = (size + SIZEOF_ROUND_ALIGN(chunk_obj_node_t) - 1) / SIZEOF_ROUND_ALIGN(chunk_obj_node_t);

    return num_node_headers * SIZEOF_ROUND_ALIGN(chunk_obj_node_t);
}

static inline int CMP_SIZE(const chunk_free_node_t * a, const chunk_free_node_t * b)
{
    if (a->size > b->size)
        return 1;
    if (a->size < b->size)
        return 0;
    return (a > b);
}

static void insert_free_size(gs_memory_chunk_t *cmem, chunk_free_node_t *node)
{
    chunk_free_node_t **ap;
    chunk_free_node_t *a, *b, *c;

    node->left_size  = NULL;
    node->right_size = NULL;

    /* Insert into size */
    ap = &cmem->free_size;
    while ((a = *ap) != NULL) {
        if (CMP_SIZE(a, node)) {
            b = a->left_size;
            if (b == NULL) {
                ap = &a->left_size;
                break; /* Stop searching */
            }
            if (CMP_SIZE(b, node)) {
                c = b->left_size;
                if (c == NULL) {
                    ap = &b->left_size;
                    break;
                }
                /* Splay:        a             c
                 *            b     Z   =>  W     b
                 *          c   Y               X   a
                 *         W X                     Y Z
                 */
                *ap = c;
                a->left_size  = b->right_size;
                b->left_size  = c->right_size;
                b->right_size = a;
                c->right_size = b;
                if (CMP_SIZE(c, node))
                    ap = &c->left_size;
                else
                    ap = &b->left_size;
            } else {
                c = b->right_size;
                if (c == NULL) {
                    ap = &b->right_size;
                    break;
                }
                /* Splay:         a             c
                 *            b       Z  =>   b   a
                 *          W   c            W X Y Z
                 *             X Y
                 */
                *ap = c;
                a->left_size  = c->right_size;
                b->right_size = c->left_size;
                c->left_size  = b;
                c->right_size = a;
                if (CMP_SIZE(c, node))
                    ap = &b->right_size;
                else
                    ap = &a->left_size;
            }
        } else {
            b = a->right_size;
            if (b == NULL)
            {
                ap = &a->right_size;
                break;
            }
            if (CMP_SIZE(b, node)) {
                c = b->left_size;
                if (c == NULL) {
                    ap = &b->left_size;
                    break;
                }
                /* Splay:      a                c
                 *         W       b    =>    a   b
                 *               c   Z       W X Y Z
                 *              X Y
                 */
                *ap = c;
                a->right_size = c->left_size;
                b->left_size  = c->right_size;
                c->left_size  = a;
                c->right_size = b;
                if (CMP_SIZE(c, node))
                    ap = &a->right_size;
                else
                    ap = &b->left_size;
            } else {
                c = b->right_size;
                if (c == NULL) {
                    ap = &b->right_size;
                    break;
                }
                /* Splay:    a                   c
                 *        W     b      =>     b     Z
                 *            X   c         a   Y
                 *               Y Z       W X
                 */
                *ap = c;
                a->right_size = b->left_size;
                b->right_size = c->left_size;
                b->left_size  = a;
                c->left_size  = b;
                if (CMP_SIZE(c, node))
                    ap = &b->right_size;
                else
                    ap = &c->right_size;
            }
        }
    }
    *ap = node;
}

static void insert_free_loc(gs_memory_chunk_t *cmem, chunk_free_node_t *node)
{
    chunk_free_node_t **ap;
    chunk_free_node_t *a, *b, *c;

    node->left_loc   = NULL;
    node->right_loc  = NULL;

    /* Insert into loc */
    ap = &cmem->free_loc;
    while ((a = *ap) != NULL) {
        if (a > node) {
            b = a->left_loc;
            if (b == NULL) {
                ap = &a->left_loc;
                break; /* Stop searching */
            }
            if (b > node) {
                c = b->left_loc;
                if (c == NULL) {
                    ap = &b->left_loc;
                    break;
                }
                /* Splay:        a             c
                 *            b     Z   =>  W     b
                 *          c   Y               X   a
                 *         W X                     Y Z
                 */
                *ap = c;
                a->left_loc  = b->right_loc;
                b->left_loc  = c->right_loc;
                b->right_loc = a;
                c->right_loc = b;
                if (c > node)
                    ap = &c->left_loc;
                else
                    ap = &b->left_loc;
            } else {
                c = b->right_loc;
                if (c == NULL) {
                    ap = &b->right_loc;
                    break;
                }
                /* Splay:         a             c
                 *            b       Z  =>   b   a
                 *          W   c            W X Y Z
                 *             X Y
                 */
                *ap = c;
                a->left_loc  = c->right_loc;
                b->right_loc = c->left_loc;
                c->left_loc  = b;
                c->right_loc = a;
                if (c > node)
                    ap = &b->right_loc;
                else
                    ap = &a->left_loc;
            }
        } else {
            b = a->right_loc;
            if (b == NULL)
            {
                ap = &a->right_loc;
                break;
            }
            if (b > node) {
                c = b->left_loc;
                if (c == NULL) {
                    ap = &b->left_loc;
                    break;
                }
                /* Splay:      a                c
                 *         W       b    =>    a   b
                 *               c   Z       W X Y Z
                 *              X Y
                 */
                *ap = c;
                a->right_loc = c->left_loc;
                b->left_loc  = c->right_loc;
                c->left_loc  = a;
                c->right_loc = b;
                if (c > node)
                    ap = &a->right_loc;
                else
                    ap = &b->left_loc;
            } else {
                c = b->right_loc;
                if (c == NULL) {
                    ap = &b->right_loc;
                    break;
                }
                /* Splay:    a                   c
                 *        W     b      =>     b     Z
                 *            X   c         a   Y
                 *               Y Z       W X
                 */
                *ap = c;
                a->right_loc = b->left_loc;
                b->right_loc = c->left_loc;
                b->left_loc  = a;
                c->left_loc  = b;
                if (c > node)
                    ap = &b->right_loc;
                else
                    ap = &c->right_loc;
            }
        }
    }
    *ap = node;
}

static void insert_free(gs_memory_chunk_t *cmem, chunk_free_node_t *node, uint size)
{
    node->size = size;
    insert_free_size(cmem, node);
    insert_free_loc(cmem, node);
}

static void remove_free_loc(gs_memory_chunk_t *cmem, chunk_free_node_t *node)
{
    chunk_free_node_t **ap = &cmem->free_loc;

    while (*ap != node)
    {
        if (*ap > node)
            ap = &(*ap)->left_loc;
        else
            ap = &(*ap)->right_loc;
    }

    if (node->left_loc == NULL)
        *ap = node->right_loc;
    else if (node->right_loc == NULL)
        *ap = node->left_loc;
    else {
        /* Find the in-order predecessor to node and use that to replace node */
        chunk_free_node_t **bp;
        chunk_free_node_t  *b;
        bp = &node->left_loc;
        while ((*bp)->right_loc)
            bp = &(*bp)->right_loc;
        b = (*bp);
        *bp = b->left_loc;
        b->left_loc = node->left_loc;
        b->right_loc = node->right_loc;
        *ap = b;
    }
}

static void remove_free_size(gs_memory_chunk_t *cmem, chunk_free_node_t *node)
{
    chunk_free_node_t **ap = &cmem->free_size;

    while (*ap != node)
    {
        if (CMP_SIZE(*ap, node))
            ap = &(*ap)->left_size;
        else
            ap = &(*ap)->right_size;
    }

    if (node->left_size == NULL)
        *ap = node->right_size;
    else if (node->right_size == NULL)
        *ap = node->left_size;
    else {
        /* Find the in-order predecessor to node and use that to replace node */
        chunk_free_node_t **bp;
        chunk_free_node_t  *b;
        bp = &node->left_size;
        while ((*bp)->right_size)
            bp = &(*bp)->right_size;
        b = (*bp);
        *bp = b->left_size;
        b->left_size = node->left_size;
        b->right_size = node->right_size;
        *ap = b;
    }
}

static void remove_free_size_fast(gs_memory_chunk_t *cmem, chunk_free_node_t **ap)
{
    chunk_free_node_t *node = *ap;

    if (node->left_size == NULL)
        *ap = node->right_size;
    else if (node->right_size == NULL)
        *ap = node->left_size;
    else {
        /* Find the in-order predecessor to node and use that to replace node */
        chunk_free_node_t **bp;
        chunk_free_node_t  *b;
        bp = &node->left_size;
        while ((*bp)->right_size)
            bp = &(*bp)->right_size;
        b = (*bp);
        *bp = b->left_size;
        b->left_size = node->left_size;
        b->right_size = node->right_size;
        *ap = b;
    }
}

static void remove_free(gs_memory_chunk_t *cmem, chunk_free_node_t *node)
{
    remove_free_loc(cmem, node);
    remove_free_size(cmem, node);
}

#if defined(MEMENTO) || defined(SINGLE_OBJECT_MEMORY_BLOCKS_ONLY)
#define SINGLE_OBJECT_CHUNK(size) (1)
#else
#define SINGLE_OBJECT_CHUNK(size) ((size) > (CHUNK_SIZE>>1))
#endif

/* All of the allocation routines reduce to this function */
static byte *
chunk_obj_alloc(gs_memory_t *mem, size_t size, gs_memory_type_ptr_t type, client_name_t cname)
{
    gs_memory_chunk_t  *cmem = (gs_memory_chunk_t *)mem;
    chunk_free_node_t **ap, **okp;
    chunk_free_node_t  *a, *b, *c;
    size_t newsize;
    chunk_obj_node_t *obj = NULL;

    newsize = round_up_to_align(size + SIZEOF_ROUND_ALIGN(chunk_obj_node_t));	/* space we will need */
    /* When we free this block it might have to go in free - so it had
     * better be large enough to accommodate a complete free node! */
    if (newsize < SIZEOF_ROUND_ALIGN(chunk_free_node_t))
        newsize = SIZEOF_ROUND_ALIGN(chunk_free_node_t);
    /* Protect against overflow */
    if (newsize < size)
        return NULL;

#ifdef DEBUG_SEQ
    cmem->sequence++;
#endif

#ifdef DEBUG_CHUNK_PRINT
#ifdef DEBUG_SEQ
    dmlprintf4(mem, "Event %x: malloc(chunk="PRI_INTPTR", size=%"PRIxSIZE", cname=%s)\n",
               cmem->sequence, (intptr_t)cmem, newsize, cname);
#else
    dmlprintf3(mem, "malloc(chunk="PRI_INTPTR", size=%"PRIxSIZE", cname=%s)\n",
               (intptr_t)cmem, newsize, cname);
#endif
#endif

    /* Large blocks are allocated directly */
    if (SINGLE_OBJECT_CHUNK(size)) {
        obj = (chunk_obj_node_t *)gs_alloc_bytes_immovable(cmem->target, newsize, cname);
        if (obj == NULL)
            return NULL;
    } else {
        /* Find the smallest free block that's large enough */
        /* okp points to the parent pointer to the block we pick */
        ap = &cmem->free_size;
        okp = NULL;
        while ((a = *ap) != NULL) {
            if (a->size >= newsize) {
                b = a->left_size;
                if (b == NULL) {
                    okp = ap; /* a will do */
                    break; /* Stop searching */
                }
                if (b->size >= newsize) {
                    c = b->left_size;
                    if (c == NULL) {
                        okp = &a->left_size; /* b is as good as we're going to get */
                        break;
                    }
                    /* Splay:        a             c
                     *            b     Z   =>  W     b
                     *          c   Y               X   a
                     *         W X                     Y Z
                     */
                    *ap = c;
                    a->left_size  = b->right_size;
                    b->left_size  = c->right_size;
                    b->right_size = a;
                    c->right_size = b;
                    if (c->size >= newsize) {
                        okp = ap; /* c is the best so far */
                        ap = &c->left_size;
                    } else {
                        okp = &c->right_size; /* b is the best so far */
                        ap = &b->left_size;
                    }
                } else {
                    c = b->right_size;
                    if (c == NULL) {
                        okp = ap; /* a is as good as we are going to get */
                        break;
                    }
                    /* Splay:         a             c
                     *            b       Z  =>   b   a
                     *          W   c            W X Y Z
                     *             X Y
                     */
                    *ap = c;
                    a->left_size  = c->right_size;
                    b->right_size = c->left_size;
                    c->left_size  = b;
                    c->right_size = a;
                    if (c->size >= newsize) {
                        okp = ap; /* c is the best so far */
                        ap = &b->right_size;
                    } else {
                        okp = &c->right_size; /* a is the best so far */
                        ap = &a->left_size;
                    }
                }
            } else {
                b = a->right_size;
                if (b == NULL)
                    break; /* No better match to be found */
                if (b->size >= newsize) {
                    c = b->left_size;
                    if (c == NULL) {
                        okp = &a->right_size; /* b is as good as we're going to get */
                        break;
                    }
                    /* Splay:      a                c
                     *         W       b    =>    a   b
                     *               c   Z       W X Y Z
                     *              X Y
                     */
                    *ap = c;
                    a->right_size = c->left_size;
                    b->left_size  = c->right_size;
                    c->left_size  = a;
                    c->right_size = b;
                    if (c->size >= newsize) {
                        okp = ap; /* c is the best so far */
                        ap = &a->right_size;
                    } else {
                        okp = &c->right_size; /* b is the best so far */
                        ap = &b->left_size;
                    }
                } else {
                    c = b->right_size;
                    if (c == NULL)
                        break; /* No better match to be found */
                    /* Splay:    a                   c
                     *        W     b      =>     b     Z
                     *            X   c         a   Y
                     *               Y Z       W X
                     */
                    *ap = c;
                    a->right_size = b->left_size;
                    b->right_size = c->left_size;
                    b->left_size  = a;
                    c->left_size  = b;
                    if (c->size >= newsize) {
                        okp = ap; /* c is the best so far */
                        ap = &b->right_size;
                    } else
                        ap = &c->right_size;
                }
            }
        }

        /* So *okp points to the most appropriate free tree entry. */

        if (okp == NULL) {
            /* No appropriate free space slot. We need to allocate a new slab. */
            chunk_slab_t *slab;
            uint slab_size = newsize + SIZEOF_ROUND_ALIGN(chunk_slab_t);

            if (slab_size <= (CHUNK_SIZE>>1))
                slab_size = CHUNK_SIZE;
            slab = (chunk_slab_t *)gs_alloc_bytes_immovable(cmem->target, slab_size, cname);
            if (slab == NULL)
                return NULL;
            slab->next = cmem->slabs;
            cmem->slabs = slab;

            obj = (chunk_obj_node_t *)(((byte *)slab) + SIZEOF_ROUND_ALIGN(chunk_slab_t));
            if (slab_size != newsize + SIZEOF_ROUND_ALIGN(chunk_slab_t)) {
                insert_free(cmem, (chunk_free_node_t *)(((byte *)obj)+newsize), slab_size - newsize - SIZEOF_ROUND_ALIGN(chunk_slab_t));
                cmem->total_free += slab_size - newsize - SIZEOF_ROUND_ALIGN(chunk_slab_t);
            }
        } else {
            chunk_free_node_t *ok = *okp;
            obj = (chunk_obj_node_t *)(void *)ok;
            if (ok->size >= newsize + SIZEOF_ROUND_ALIGN(chunk_free_node_t)) {
                chunk_free_node_t *tail = (chunk_free_node_t *)(((byte *)ok) + newsize);
                uint tail_size = ok->size - newsize;
                remove_free_size_fast(cmem, okp);
                remove_free_loc(cmem, ok);
                insert_free(cmem, tail, tail_size);
            } else {
                newsize = ok->size;
                remove_free_size_fast(cmem, okp);
                remove_free_loc(cmem, ok);
            }
            cmem->total_free -= newsize;
        }
    }

    if (gs_alloc_debug) {
        memset((byte *)(obj) + SIZEOF_ROUND_ALIGN(chunk_obj_node_t), 0xa1, newsize - SIZEOF_ROUND_ALIGN(chunk_obj_node_t));
        memset((byte *)(obj) + SIZEOF_ROUND_ALIGN(chunk_obj_node_t), 0xac, size);
    }

    cmem->used += newsize;
    obj->size = newsize; /* actual size */
    obj->padding = newsize - size; /* actual size - client requested size */
    obj->type = type;    /* and client desired type */
    obj->defer_next = NULL;

#ifdef DEBUG_SEQ
    obj->sequence = cmem->sequence;
#endif
    if (gs_debug_c('A'))
        dmlprintf3(mem, "[a+]chunk_obj_alloc (%s)(%"PRIuSIZE") = "PRI_INTPTR": OK.\n",
                   client_name_string(cname), size, (intptr_t) obj);
#ifdef DEBUG_CHUNK_PRINT
#ifdef DEBUG_SEQ
    dmlprintf5(mem, "Event %x: malloced(chunk="PRI_INTPTR", addr="PRI_INTPTR", size=%"PRIxSIZE", cname=%s)\n",
               obj->sequence, (intptr_t)cmem, (intptr_t)obj, obj->size, cname);
#else
    dmlprintf4(mem, "malloced(chunk="PRI_INTPTR", addr="PRI_INTPTR", size=%"PRIxSIZE", cname=%s)\n",
               (intptr_t)cmem, (intptr_t)obj, obj->size, cname);
#endif
#endif
#ifdef DEBUG_CHUNK
    gs_memory_chunk_dump_memory(cmem);
#endif

    return (byte *)Memento_label((byte *)(obj) + SIZEOF_ROUND_ALIGN(chunk_obj_node_t), cname);
}

static byte *
chunk_alloc_bytes_immovable(gs_memory_t * mem, size_t size, client_name_t cname)
{
    return chunk_obj_alloc(mem, size, &st_bytes, cname);
}

static byte *
chunk_alloc_bytes(gs_memory_t * mem, size_t size, client_name_t cname)
{
    return chunk_obj_alloc(mem, size, &st_bytes, cname);
}

static void *
chunk_alloc_struct_immovable(gs_memory_t * mem, gs_memory_type_ptr_t pstype,
                             client_name_t cname)
{
    return chunk_obj_alloc(mem, pstype->ssize, pstype, cname);
}

static void *
chunk_alloc_struct(gs_memory_t * mem, gs_memory_type_ptr_t pstype,
                   client_name_t cname)
{
    return chunk_obj_alloc(mem, pstype->ssize, pstype, cname);
}

static byte *
chunk_alloc_byte_array_immovable(gs_memory_t * mem, size_t num_elements,
                                 size_t elt_size, client_name_t cname)
{
    return chunk_alloc_bytes(mem, num_elements * elt_size, cname);
}

static byte *
chunk_alloc_byte_array(gs_memory_t * mem, size_t num_elements, size_t elt_size,
                   client_name_t cname)
{
    return chunk_alloc_bytes(mem, num_elements * elt_size, cname);
}

static void *
chunk_alloc_struct_array_immovable(gs_memory_t * mem, size_t num_elements,
                                   gs_memory_type_ptr_t pstype, client_name_t cname)
{
    return chunk_obj_alloc(mem, num_elements * pstype->ssize, pstype, cname);
}

static void *
chunk_alloc_struct_array(gs_memory_t * mem, size_t num_elements,
                         gs_memory_type_ptr_t pstype, client_name_t cname)
{
    return chunk_obj_alloc(mem, num_elements * pstype->ssize, pstype, cname);
}

static void *
chunk_resize_object(gs_memory_t * mem, void *ptr, size_t new_num_elements, client_name_t cname)
{
    void *new_ptr = NULL;

    if (ptr != NULL) {
        /* This isn't particularly efficient, but it is rarely used */
        chunk_obj_node_t *obj = (chunk_obj_node_t *)(((byte *)ptr) - SIZEOF_ROUND_ALIGN(chunk_obj_node_t));
        size_t new_size = (obj->type->ssize * new_num_elements);
        size_t old_size = obj->size - obj->padding;
        /* get the type from the old object */
        gs_memory_type_ptr_t type = obj->type;
        gs_memory_chunk_t *cmem = (gs_memory_chunk_t *)mem;
        size_t save_max_used = cmem->max_used;

        if (new_size == old_size)
            return ptr;
        if ((new_ptr = chunk_obj_alloc(mem, new_size, type, cname)) == 0)
            return NULL;
        memcpy(new_ptr, ptr, min(old_size, new_size));
        chunk_free_object(mem, ptr, cname);
        cmem->max_used = save_max_used;
        if (cmem->used > cmem->max_used)
            cmem->max_used = cmem->used;
    }

    return new_ptr;
}

static void
chunk_free_object(gs_memory_t *mem, void *ptr, client_name_t cname)
{
    gs_memory_chunk_t * const cmem = (gs_memory_chunk_t *)mem;
    size_t obj_node_size;
    chunk_obj_node_t *obj;
    struct_proc_finalize((*finalize));
    chunk_free_node_t **ap, **gtp, **ltp;
    chunk_free_node_t *a, *b, *c;

    if (ptr == NULL)
        return;

    /* back up to obj header */
    obj_node_size = SIZEOF_ROUND_ALIGN(chunk_obj_node_t);
    obj = (chunk_obj_node_t *)(((byte *)ptr) - obj_node_size);

    if (cmem->deferring) {
        if (obj->defer_next == NULL) {
            obj->defer_next = cmem->defer_finalize_list;
            cmem->defer_finalize_list = obj;
        }
        return;
    }

#ifdef DEBUG_CHUNK_PRINT
#ifdef DEBUG_SEQ
    cmem->sequence++;
    dmlprintf6(cmem->target, "Event %x: free(chunk="PRI_INTPTR", addr="PRI_INTPTR", size=%x, num=%x, cname=%s)\n",
               cmem->sequence, (intptr_t)cmem, (intptr_t)obj, obj->size, obj->sequence, cname);
#else
    dmlprintf4(cmem->target, "free(chunk="PRI_INTPTR", addr="PRI_INTPTR", size=%x, cname=%s)\n",
               (intptr_t)cmem, (intptr_t)obj, obj->size, cname);
#endif
#endif

    if (obj->type) {
        finalize = obj->type->finalize;
        if (finalize != NULL)
            finalize(mem, ptr);
    }
    /* finalize may change the head_**_chunk doing free of stuff */

    if_debug3m('A', cmem->target, "[a-]chunk_free_object(%s) "PRI_INTPTR"(%"PRIuSIZE")\n",
               client_name_string(cname), (intptr_t)ptr, obj->size);

    cmem->used -= obj->size;

    if (SINGLE_OBJECT_CHUNK(obj->size - obj->padding)) {
        gs_free_object(cmem->target, obj, "chunk_free_object(single object)");
#ifdef DEBUG_CHUNK
        gs_memory_chunk_dump_memory(cmem);
#endif
        return;
    }

    /* We want to find where to insert this free entry into our free tree. We need to know
     * both the point to the left of it, and the point to the right of it, in order to see
     * if we can merge the free entries. Accordingly, we search from the top of the tree
     * and keep pointers to the nodes that we pass that are greater than it, and less than
     * it. */
    gtp = NULL; /* gtp is set to the address of the pointer to the node where we last stepped left */
    ltp = NULL; /* ltp is set to the address of the pointer to the node where we last stepped right */
    ap = &cmem->free_loc;
    while ((a = *ap) != NULL) {
        if ((void *)a > (void *)obj) {
            b = a->left_loc; /* Try to step left from a */
            if (b == NULL) {
                gtp = ap; /* a is greater than us */
                break;
            }
            if ((void *)b > (void *)obj) {
                c = b->left_loc; /* Try to step left from b */
                if (c == NULL) {
                    gtp = &a->left_loc; /* b is greater than us */
                    break;
                }
                /* Splay:        a             c
                 *            b     Z   =>  W     b
                 *          c   Y               X   a
                 *         W X                     Y Z
                 */
                *ap = c;
                a->left_loc  = b->right_loc;
                b->left_loc  = c->right_loc;
                b->right_loc = a;
                c->right_loc = b;
                if ((void *)c > (void *)obj) { /* W */
                    gtp = ap; /* c is greater than us */
                    ap = &c->left_loc;
                } else { /* X */
                    gtp = &c->right_loc; /* b is greater than us */
                    ltp = ap; /* c is less than us */
                    ap = &b->left_loc;
                }
            } else {
                c = b->right_loc; /* Try to step right from b */
                if (c == NULL) {
                    gtp = ap; /* a is greater than us */
                    ltp = &a->left_loc; /* b is less than us */
                    break;
                }
                /* Splay:         a             c
                 *            b       Z  =>   b   a
                 *          W   c            W X Y Z
                 *             X Y
                 */
                *ap = c;
                a->left_loc  = c->right_loc;
                b->right_loc = c->left_loc;
                c->left_loc  = b;
                c->right_loc = a;
                if ((void *)c > (void *)obj) { /* X */
                    gtp = ap; /* c is greater than us */
                    ltp = &c->left_loc; /* b is less than us */
                    ap = &b->right_loc;
                } else { /* Y */
                    gtp = &c->right_loc; /* a is greater than us */
                    ltp = ap; /* c is less than us */
                    ap = &a->left_loc;
                }
            }
        } else {
            b = a->right_loc; /* Try to step right from a */
            if (b == NULL) {
                ltp = ap; /* a is less than us */
                break;
            }
            if ((void *)b > (void *)obj) {
                c = b->left_loc;
                if (c == NULL) {
                    gtp = &a->right_loc; /* b is greater than us */
                    ltp = ap; /* a is less than us */
                    break;
                }
                /* Splay:      a                c
                 *         W       b    =>    a   b
                 *               c   Z       W X Y Z
                 *              X Y
                 */
                *ap = c;
                a->right_loc = c->left_loc;
                b->left_loc  = c->right_loc;
                c->left_loc  = a;
                c->right_loc = b;
                if ((void *)c > (void *)obj) { /* X */
                    gtp = ap; /* c is greater than us */
                    ltp = &c->left_loc; /* a is less than us */
                    ap = &a->right_loc;
                } else { /* Y */
                    gtp = &c->right_loc; /* b is greater than us */
                    ltp = ap; /* c is less than than us */
                    ap = &b->left_loc;
                }
            } else {
                c = b->right_loc;
                if (c == NULL) {
                    ltp = &a->right_loc; /* b is greater than us */
                    break;
                }
                /* Splay:    a                   c
                 *        W     b      =>     b     Z
                 *            X   c         a   Y
                 *               Y Z       W X
                 */
                *ap = c;
                a->right_loc = b->left_loc;
                b->right_loc = c->left_loc;
                b->left_loc  = a;
                c->left_loc  = b;
                if ((void *)c > (void *)obj) { /* Y */
                    gtp = ap; /* c is greater than us */
                    ltp = &c->left_loc; /* b is less than us */
                    ap = &b->right_loc;
                } else { /* Z */
                    ltp = ap; /* c is less than than us */
                    ap = &c->right_loc;
                }
            }
        }
    }

    if (ltp) {
        /* There is at least 1 node smaller than us - check for merging */
        chunk_free_node_t *ltfree = (chunk_free_node_t *)(*ltp);
        if ((((byte *)ltfree) + ltfree->size) == (byte *)(void *)obj) {
            /* Merge! */
            cmem->total_free += obj->size;
            remove_free_size(cmem, ltfree);
            ltfree->size += obj->size;
            if (gtp) {
                /* There is at least 1 node greater than us - check for merging */
                chunk_free_node_t *gtfree = (chunk_free_node_t *)(*gtp);
                if ((((byte *)obj) + obj->size) == (byte *)(void *)gtfree) {
                    /* Double merge! */
                    ltfree->size += gtfree->size;
                    remove_free(cmem, gtfree);
                }
                gtp = NULL;
            }
            insert_free_size(cmem, ltfree);
            if (gs_alloc_debug)
                memset(((byte *)ltfree) + SIZEOF_ROUND_ALIGN(chunk_free_node_t), 0x69, ltfree->size - SIZEOF_ROUND_ALIGN(chunk_free_node_t));
            obj = NULL;
        }
    }
    if (gtp && obj) {
        /* There is at least 1 node greater than us - check for merging */
        chunk_free_node_t *gtfree = (chunk_free_node_t *)(*gtp);
        if ((((byte *)obj) + obj->size) == (byte *)(void *)gtfree) {
            /* Merge! */
            chunk_free_node_t *objfree = (chunk_free_node_t *)(void *)obj;
            uint obj_size = obj->size;
            cmem->total_free += obj_size;
            remove_free_size(cmem, gtfree);
            *objfree = *gtfree;
            objfree->size += obj_size;
            *gtp = objfree;
            insert_free_size(cmem, objfree);
            if (gs_alloc_debug)
                memset(((byte *)objfree) + SIZEOF_ROUND_ALIGN(chunk_free_node_t), 0x96, objfree->size - SIZEOF_ROUND_ALIGN(chunk_free_node_t));
            obj = NULL;
        }
    }

    if (obj) {
        /* Insert new one */
        chunk_free_node_t *objfree = (chunk_free_node_t *)(void *)obj;
        cmem->total_free += obj->size;
        objfree->size = obj->size;
        objfree->left_loc = NULL;
        objfree->right_loc = NULL;
        if (gtp) {
            ap = &(*gtp)->left_loc;
            while (*ap) {
                ap = &(*ap)->right_loc;
            }
        } else if (ltp) {
            ap = &(*ltp)->right_loc;
            while (*ap) {
                ap = &(*ap)->left_loc;
            }
        } else
            ap = &cmem->free_loc;
        *ap = objfree;
        insert_free_size(cmem, objfree);
        if (gs_alloc_debug)
            memset(((byte *)objfree) + SIZEOF_ROUND_ALIGN(chunk_free_node_t), 0x9b, objfree->size - SIZEOF_ROUND_ALIGN(chunk_free_node_t));
    }

#ifdef DEBUG_CHUNK
    gs_memory_chunk_dump_memory(cmem);
#endif
}

static byte *
chunk_alloc_string_immovable(gs_memory_t * mem, size_t nbytes, client_name_t cname)
{
    /* we just alloc bytes here */
    return chunk_alloc_bytes(mem, nbytes, cname);
}

static byte *
chunk_alloc_string(gs_memory_t * mem, size_t nbytes, client_name_t cname)
{
    /* we just alloc bytes here */
    return chunk_alloc_bytes(mem, nbytes, cname);
}

static byte *
chunk_resize_string(gs_memory_t * mem, byte * data, size_t old_num, size_t new_num,
                    client_name_t cname)
{
    /* just resize object - ignores old_num */
    return chunk_resize_object(mem, data, new_num, cname);
}

static void
chunk_free_string(gs_memory_t * mem, byte * data, size_t nbytes,
                  client_name_t cname)
{
    chunk_free_object(mem, data, cname);
}

static void
chunk_status(gs_memory_t * mem, gs_memory_status_t * pstat)
{
    gs_memory_chunk_t *cmem = (gs_memory_chunk_t *)mem;

    pstat->allocated = cmem->used;
    pstat->used = cmem->used - cmem->total_free;
    pstat->max_used = cmem->max_used;
    pstat->limit = (size_t)~1;      /* No limit on allocations */
    pstat->is_thread_safe = false;	/* this allocator does not have an internal mutex */
}

static gs_memory_t *
chunk_stable(gs_memory_t * mem)
{
    return mem;
}

static void
chunk_enable_free(gs_memory_t * mem, bool enable)
{
}

static void chunk_set_object_type(gs_memory_t *mem, void *ptr, gs_memory_type_ptr_t type)
{
    chunk_obj_node_t *obj = (chunk_obj_node_t *)(((byte *)ptr) - SIZEOF_ROUND_ALIGN(chunk_obj_node_t));

    if (ptr == 0)
        return;
    obj->type = type;
}

static void chunk_defer_frees(gs_memory_t *mem, int defer)
{
    gs_memory_chunk_t *cmem = (gs_memory_chunk_t *)mem;
    chunk_obj_node_t *n;

    if (defer == 0) {
        /* Run through and finalise everything on the finalize list. This
         * might cause other stuff to be put onto the finalize list. As we
         * finalize stuff we move it to the free list. */
        while (cmem->defer_finalize_list) {
            n = cmem->defer_finalize_list;
            cmem->defer_finalize_list = n->defer_next;
            if (n->type) {
                if (n->type->finalize)
                    n->type->finalize(mem, ((byte *)n) + SIZEOF_ROUND_ALIGN(chunk_obj_node_t));
                n->type = NULL;
            }
            n->defer_next = cmem->defer_free_list;
            cmem->defer_free_list = n;
        }
    }
    cmem->deferring = defer;
    if (defer == 0) {
        /* Now run through and free everything on the free list */
        while (cmem->defer_free_list) {
            n = cmem->defer_free_list;
            cmem->defer_free_list = n->defer_next;
            chunk_free_object(mem, ((byte *)n) + SIZEOF_ROUND_ALIGN(chunk_obj_node_t), "deferred free");
        }
    }
}

static void
chunk_consolidate_free(gs_memory_t *mem)
{
}

/* accessors to get size and type given the pointer returned to the client */
static size_t
chunk_object_size(gs_memory_t * mem, const void *ptr)
{
    if (ptr != NULL) {
        chunk_obj_node_t *obj = (chunk_obj_node_t *)(((byte *)ptr) - SIZEOF_ROUND_ALIGN(chunk_obj_node_t));

        return obj->size - obj->padding;
    }

    return 0;
}

static gs_memory_type_ptr_t
chunk_object_type(const gs_memory_t * mem, const void *ptr)
{
    chunk_obj_node_t *obj = (chunk_obj_node_t *)(((byte *)ptr) - SIZEOF_ROUND_ALIGN(chunk_obj_node_t));
    return obj->type;
}

static int
chunk_register_root(gs_memory_t * mem, gs_gc_root_t ** rp, gs_ptr_type_t ptype,
                 void **up, client_name_t cname)
{
    return 0;
}

static void
chunk_unregister_root(gs_memory_t * mem, gs_gc_root_t * rp, client_name_t cname)
{
}