forked from ocaml-flambda/flambda-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfreelist.c
1859 lines (1697 loc) · 55 KB
/
freelist.c
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
/**************************************************************************/
/* */
/* OCaml */
/* */
/* Damien Doligez, projet Para, INRIA Rocquencourt */
/* */
/* Copyright 1996 Institut National de Recherche en Informatique et */
/* en Automatique. */
/* */
/* All rights reserved. This file is distributed under the terms of */
/* the GNU Lesser General Public License version 2.1, with the */
/* special exception on linking described in the file LICENSE. */
/* */
/**************************************************************************/
#define CAML_INTERNALS
#define FREELIST_DEBUG 0
#if FREELIST_DEBUG
#include <stdio.h>
#endif
#include <string.h>
#include "caml/config.h"
#include "caml/custom.h"
#include "caml/freelist.h"
#include "caml/gc.h"
#include "caml/gc_ctrl.h"
#include "caml/memory.h"
#include "caml/major_gc.h"
#include "caml/misc.h"
#include "caml/mlvalues.h"
#include "caml/eventlog.h"
/*************** declarations common to all policies ******************/
/* A block in a small free list is a [value] (integer representing a
pointer to the first word after the block's header). The end of the
list is NULL.
*/
#define Val_NULL ((value) NULL)
asize_t caml_fl_cur_wsz = 0; /* Number of words in the free set,
including headers but not fragments. */
value caml_fl_merge = Val_NULL; /* Current insertion pointer. Managed
jointly with [sweep_slice]. */
/* Next in list */
#define Next_small(v) Field ((v), 0)
/* Next in memory order */
Caml_inline value Next_in_mem (value v) {
return (value) &Field ((v), Whsize_val (v));
}
#ifdef CAML_INSTR
/* number of pointers followed to allocate from the free set */
uintnat caml_instr_alloc_jump = 0;
#define EV_ALLOC_JUMP(n) (caml_instr_alloc_jump += (n))
#endif /*CAML_INSTR*/
/********************* next-fit allocation policy *********************/
/* The free-list is kept sorted by increasing addresses.
This makes the merging of adjacent free blocks possible.
(See [nf_merge_block].)
*/
/* The sentinel can be located anywhere in memory, but it must not be
adjacent to any heap object. */
static struct {
value filler1; /* Make sure the sentinel is never adjacent to any block. */
header_t h;
value first_field;
value filler2; /* Make sure the sentinel is never adjacent to any block. */
} nf_sentinel = {0, Make_header (0, 0, Caml_blue), Val_NULL, 0};
#define Nf_head (Val_bp (&(nf_sentinel.first_field)))
static value nf_prev = Nf_head; /* Current allocation pointer. */
static value nf_last = Val_NULL; /* Last block in the list. Only valid
just after [nf_allocate] returns NULL. */
#if defined (DEBUG) || FREELIST_DEBUG
static void nf_check (void)
{
value cur;
int prev_found = 0, merge_found = 0;
uintnat size_found = 0;
cur = Next_small (Nf_head);
while (cur != Val_NULL){
size_found += Whsize_bp (cur);
CAMLassert (Is_in_heap (cur));
if (cur == nf_prev) prev_found = 1;
if (cur == caml_fl_merge) merge_found = 1;
cur = Next_small (cur);
}
CAMLassert (prev_found || nf_prev == Nf_head);
CAMLassert (merge_found || caml_fl_merge == Nf_head);
CAMLassert (size_found == caml_fl_cur_wsz);
}
#endif /* DEBUG || FREELIST_DEBUG */
/* [nf_allocate_block] is called by [nf_allocate]. Given a suitable free
block and the requested size, it allocates a new block from the free
block. There are three cases:
0. The free block has the requested size. Detach the block from the
free-list and return it.
1. The free block is 1 word longer than the requested size. Detach
the block from the free list. The remaining word cannot be linked:
turn it into an empty block (header only), and return the rest.
2. The free block is large enough. Split it in two and return the right
block.
In all cases, the allocated block is right-justified in the free block:
it is located in the high-address words of the free block, so that
the linking of the free-list does not change in case 2.
*/
static header_t *nf_allocate_block (mlsize_t wh_sz, value prev, value cur)
{
header_t h = Hd_bp (cur);
CAMLassert (Whsize_hd (h) >= wh_sz);
if (Wosize_hd (h) < wh_sz + 1){ /* Cases 0 and 1. */
caml_fl_cur_wsz -= Whsize_hd (h);
Next_small (prev) = Next_small (cur);
CAMLassert (Is_in_heap (Next_small (prev))
|| Next_small (prev) == Val_NULL);
if (caml_fl_merge == cur) caml_fl_merge = prev;
#ifdef DEBUG
nf_last = Val_NULL;
#endif
/* In case 1, the following creates the empty block correctly.
In case 0, it gives an invalid header to the block. The function
calling [nf_allocate] will overwrite it. */
Hd_op (cur) = Make_header (0, 0, Caml_white);
}else{ /* Case 2. */
caml_fl_cur_wsz -= wh_sz;
Hd_op (cur) = Make_header (Wosize_hd (h) - wh_sz, 0, Caml_blue);
}
nf_prev = prev;
return (header_t *) &Field (cur, Wosize_hd (h) - wh_sz);
}
static header_t *nf_allocate (mlsize_t wo_sz)
{
value cur = Val_NULL, prev;
CAMLassert (sizeof (char *) == sizeof (value));
CAMLassert (wo_sz >= 1);
CAMLassert (nf_prev != Val_NULL);
/* Search from [nf_prev] to the end of the list. */
prev = nf_prev;
cur = Next_small (prev);
while (cur != Val_NULL){
CAMLassert (Is_in_heap (cur));
if (Wosize_bp (cur) >= wo_sz){
return nf_allocate_block (Whsize_wosize (wo_sz), prev, cur);
}
prev = cur;
cur = Next_small (prev);
CAML_EVENTLOG_DO(EV_ALLOC_JUMP (1));
}
nf_last = prev;
/* Search from the start of the list to [nf_prev]. */
prev = Nf_head;
cur = Next_small (prev);
while (prev != nf_prev){
if (Wosize_bp (cur) >= wo_sz){
return nf_allocate_block (Whsize_wosize (wo_sz), prev, cur);
}
prev = cur;
cur = Next_small (prev);
CAML_EVENTLOG_DO(EV_ALLOC_JUMP (1));
}
/* No suitable block was found. */
return NULL;
}
/* Location of the last fragment seen by the sweeping code.
This is a pointer to the first word after the fragment, which is
the header of the next block.
Note that [last_fragment] doesn't point to the fragment itself,
but to the block after it.
*/
static header_t *nf_last_fragment;
static void nf_init_merge (void)
{
CAML_EV_ALLOC_FLUSH();
nf_last_fragment = NULL;
caml_fl_merge = Nf_head;
#ifdef DEBUG
nf_check ();
#endif
}
static void nf_init (void)
{
Next_small (Nf_head) = Val_NULL;
nf_prev = Nf_head;
caml_fl_cur_wsz = 0;
}
static void nf_reset (void)
{
nf_init ();
}
/* Note: the [limit] parameter is unused because we merge blocks one by one. */
static header_t *nf_merge_block (value bp, char *limit)
{
value prev, cur, adj;
header_t hd = Hd_val (bp);
mlsize_t prev_wosz;
caml_fl_cur_wsz += Whsize_hd (hd);
/* [merge_block] is now responsible for calling the finalization function. */
if (Tag_hd (hd) == Custom_tag){
void (*final_fun)(value) = Custom_ops_val(bp)->finalize;
if (final_fun != NULL) final_fun(bp);
}
#ifdef DEBUG
caml_set_fields (bp, 0, Debug_free_major);
#endif
prev = caml_fl_merge;
cur = Next_small (prev);
/* The sweep code makes sure that this is the right place to insert
this block: */
CAMLassert (Bp_val (prev) < Bp_val (bp) || prev == Nf_head);
CAMLassert (Bp_val (cur) > Bp_val (bp) || cur == Val_NULL);
/* If [last_fragment] and [bp] are adjacent, merge them. */
if (nf_last_fragment == Hp_val (bp)){
mlsize_t bp_whsz = Whsize_val (bp);
if (bp_whsz <= Max_wosize){
hd = Make_header (bp_whsz, 0, Caml_white);
bp = (value) nf_last_fragment;
Hd_val (bp) = hd;
caml_fl_cur_wsz += Whsize_wosize (0);
}
}
/* If [bp] and [cur] are adjacent, remove [cur] from the free-list
and merge them. */
adj = Next_in_mem (bp);
if (adj == cur){
value next_cur = Next_small (cur);
mlsize_t cur_whsz = Whsize_val (cur);
if (Wosize_hd (hd) + cur_whsz <= Max_wosize){
Next_small (prev) = next_cur;
if (nf_prev == cur) nf_prev = prev;
hd = Make_header (Wosize_hd (hd) + cur_whsz, 0, Caml_blue);
Hd_val (bp) = hd;
adj = Next_in_mem (bp);
#ifdef DEBUG
nf_last = Val_NULL;
Next_small (cur) = (value) Debug_free_major;
Hd_val (cur) = Debug_free_major;
#endif
cur = next_cur;
}
}
/* If [prev] and [bp] are adjacent merge them, else insert [bp] into
the free-list if it is big enough. */
prev_wosz = Wosize_val (prev);
if (Next_in_mem (prev) == bp && prev_wosz + Whsize_hd (hd) < Max_wosize){
Hd_val (prev) = Make_header (prev_wosz + Whsize_hd (hd), 0, Caml_blue);
#ifdef DEBUG
Hd_val (bp) = Debug_free_major;
#endif
CAMLassert (caml_fl_merge == prev);
}else if (Wosize_hd (hd) != 0){
Hd_val (bp) = Bluehd_hd (hd);
Next_small (bp) = cur;
Next_small (prev) = bp;
caml_fl_merge = bp;
}else{
/* This is a fragment. Leave it in white but remember it for eventual
merging with the next block. */
nf_last_fragment = (header_t *) bp;
caml_fl_cur_wsz -= Whsize_wosize (0);
}
return Hp_val (adj);
}
/* This is a heap extension. We have to insert it in the right place
in the free-list.
[nf_add_blocks] can only be called right after a call to
[nf_allocate] that returned Val_NULL.
Most of the heap extensions are expected to be at the end of the
free list. (This depends on the implementation of [malloc].)
[bp] must point to a list of blocks chained by their field 0,
terminated by Val_NULL, and field 1 of the first block must point to
the last block.
*/
static void nf_add_blocks (value bp)
{
value cur = bp;
CAMLassert (nf_last != Val_NULL);
CAMLassert (Next_small (nf_last) == Val_NULL);
do {
caml_fl_cur_wsz += Whsize_bp (cur);
cur = Field(cur, 0);
} while (cur != Val_NULL);
if (Bp_val (bp) > Bp_val (nf_last)){
Next_small (nf_last) = bp;
if (nf_last == caml_fl_merge && (char *) bp < caml_gc_sweep_hp){
caml_fl_merge = Field (bp, 1);
}
}else{
value prev;
prev = Nf_head;
cur = Next_small (prev);
while (cur != Val_NULL && Bp_val (cur) < Bp_val (bp)){
CAMLassert (Bp_val (prev) < Bp_val (bp) || prev == Nf_head);
prev = cur;
cur = Next_small (prev);
}
CAMLassert (Bp_val (prev) < Bp_val (bp) || prev == Nf_head);
CAMLassert (Bp_val (cur) > Bp_val (bp) || cur == Val_NULL);
Next_small (Field (bp, 1)) = cur;
Next_small (prev) = bp;
/* When inserting blocks between [caml_fl_merge] and [caml_gc_sweep_hp],
we must advance [caml_fl_merge] to the new block, so that [caml_fl_merge]
is always the last free-list block before [caml_gc_sweep_hp]. */
if (prev == caml_fl_merge && (char *) bp < caml_gc_sweep_hp){
caml_fl_merge = Field (bp, 1);
}
}
}
static void nf_make_free_blocks
(value *p, mlsize_t size, int do_merge, int color)
{
mlsize_t sz;
while (size > 0){
if (size > Whsize_wosize (Max_wosize)){
sz = Whsize_wosize (Max_wosize);
}else{
sz = size;
}
*(header_t *)p = Make_header (Wosize_whsize (sz), 0, color);
if (do_merge) nf_merge_block (Val_hp (p), NULL);
size -= sz;
p += sz;
}
}
/******************** first-fit allocation policy *********************/
#define FLP_MAX 1000
static value flp [FLP_MAX];
static int flp_size = 0;
static value beyond = Val_NULL;
/* The sentinel can be located anywhere in memory, but it must not be
adjacent to any heap object. */
static struct {
value filler1; /* Make sure the sentinel is never adjacent to any block. */
header_t h;
value first_field;
value filler2; /* Make sure the sentinel is never adjacent to any block. */
} ff_sentinel = {0, Make_header (0, 0, Caml_blue), Val_NULL, 0};
#define Ff_head (Val_bp (&(ff_sentinel.first_field)))
static value ff_last = Val_NULL; /* Last block in the list. Only valid
just after [ff_allocate] returns NULL. */
#if defined (DEBUG) || FREELIST_DEBUG
static void ff_check (void)
{
value cur;
int flp_found = 0, merge_found = 0;
uintnat size_found = 0;
int sz = 0;
cur = Next_small (Ff_head);
while (cur != Val_NULL){
size_found += Whsize_bp (cur);
CAMLassert (Is_in_heap (cur));
if (Wosize_bp (cur) > sz){
sz = Wosize_bp (cur);
if (flp_found < flp_size){
CAMLassert (Next_small (flp[flp_found]) == cur);
++ flp_found;
}else{
CAMLassert (beyond == Val_NULL
|| Bp_val (cur) >= Bp_val (Next_small (beyond)));
}
}
if (cur == caml_fl_merge) merge_found = 1;
cur = Next_small (cur);
}
CAMLassert (flp_found == flp_size);
CAMLassert (merge_found || caml_fl_merge == Ff_head);
CAMLassert (size_found == caml_fl_cur_wsz);
}
#endif /* DEBUG || FREELIST_DEBUG */
/* [ff_allocate_block] is called by [ff_allocate]. Given a suitable free
block and the requested size, it allocates a new block from the free
block. There are three cases:
0. The free block has the requested size. Detach the block from the
free-list and return it.
1. The free block is 1 word longer than the requested size. Detach
the block from the free list. The remaining word cannot be linked:
turn it into an empty block (header only), and return the rest.
2. The free block is large enough. Split it in two and return the right
block.
In all cases, the allocated block is right-justified in the free block:
it is located in the high-address words of the free block, so that
the linking of the free-list does not change in case 2.
*/
static header_t *ff_allocate_block (mlsize_t wh_sz, int flpi, value prev,
value cur)
{
header_t h = Hd_bp (cur);
CAMLassert (Whsize_hd (h) >= wh_sz);
if (Wosize_hd (h) < wh_sz + 1){ /* Cases 0 and 1. */
caml_fl_cur_wsz -= Whsize_hd (h);
Next_small (prev) = Next_small (cur);
CAMLassert (Is_in_heap (Next_small (prev))
|| Next_small (prev) == Val_NULL);
if (caml_fl_merge == cur) caml_fl_merge = prev;
#ifdef DEBUG
ff_last = Val_NULL;
#endif
/* In case 1, the following creates the empty block correctly.
In case 0, it gives an invalid header to the block. The function
calling [ff_allocate] will overwrite it. */
Hd_op (cur) = Make_header (0, 0, Caml_white);
if (flpi + 1 < flp_size && flp[flpi + 1] == cur){
flp[flpi + 1] = prev;
}else if (flpi == flp_size - 1){
beyond = (prev == Ff_head) ? Val_NULL : prev;
-- flp_size;
}
}else{ /* Case 2. */
caml_fl_cur_wsz -= wh_sz;
Hd_op (cur) = Make_header (Wosize_hd (h) - wh_sz, 0, Caml_blue);
}
return (header_t *) &Field (cur, Wosize_hd (h) - wh_sz);
}
static header_t *ff_allocate (mlsize_t wo_sz)
{
value cur = Val_NULL, prev;
header_t *result;
int i;
mlsize_t sz, prevsz;
CAMLassert (sizeof (char *) == sizeof (value));
CAMLassert (wo_sz >= 1);
/* Search in the flp array. */
for (i = 0; i < flp_size; i++){
sz = Wosize_bp (Next_small (flp[i]));
if (sz >= wo_sz){
#if FREELIST_DEBUG
if (i > 5) fprintf (stderr, "FLP: found at %d size=%d\n", i, wo_sz);
#endif
result = ff_allocate_block (Whsize_wosize (wo_sz), i, flp[i],
Next_small (flp[i]));
goto update_flp;
}
}
/* Extend the flp array. */
if (flp_size == 0){
prev = Ff_head;
prevsz = 0;
}else{
prev = Next_small (flp[flp_size - 1]);
prevsz = Wosize_bp (prev);
if (beyond != Val_NULL) prev = beyond;
}
while (flp_size < FLP_MAX){
cur = Next_small (prev);
if (cur == Val_NULL){
ff_last = prev;
beyond = (prev == Ff_head) ? Val_NULL : prev;
return NULL;
}else{
sz = Wosize_bp (cur);
if (sz > prevsz){
flp[flp_size] = prev;
++ flp_size;
if (sz >= wo_sz){
beyond = cur;
i = flp_size - 1;
#if FREELIST_DEBUG
if (flp_size > 5){
fprintf (stderr, "FLP: extended to %d\n", flp_size);
}
#endif
result = ff_allocate_block (Whsize_wosize (wo_sz), flp_size - 1,
prev, cur);
goto update_flp;
}
prevsz = sz;
}
}
prev = cur;
}
beyond = cur;
/* The flp table is full. Do a slow first-fit search. */
#if FREELIST_DEBUG
fprintf (stderr, "FLP: table is full -- slow first-fit\n");
#endif
if (beyond != Val_NULL){
prev = beyond;
}else{
prev = flp[flp_size - 1];
}
prevsz = Wosize_bp (Next_small (flp[FLP_MAX-1]));
CAMLassert (prevsz < wo_sz);
cur = Next_small (prev);
while (cur != Val_NULL){
CAMLassert (Is_in_heap (cur));
sz = Wosize_bp (cur);
if (sz < prevsz){
beyond = cur;
}else if (sz >= wo_sz){
return ff_allocate_block (Whsize_wosize (wo_sz), flp_size, prev, cur);
}
prev = cur;
cur = Next_small (prev);
}
ff_last = prev;
return NULL;
update_flp: /* (i, sz) */
/* The block at [i] was removed or reduced. Update the table. */
CAMLassert (0 <= i && i < flp_size + 1);
if (i < flp_size){
if (i > 0){
prevsz = Wosize_bp (Next_small (flp[i-1]));
}else{
prevsz = 0;
}
if (i == flp_size - 1){
if (Wosize_bp (Next_small (flp[i])) <= prevsz){
beyond = Next_small (flp[i]);
-- flp_size;
}else{
beyond = Val_NULL;
}
}else{
value buf [FLP_MAX];
int j = 0;
mlsize_t oldsz = sz;
prev = flp[i];
while (prev != flp[i+1] && j < FLP_MAX - i){
cur = Next_small (prev);
sz = Wosize_bp (cur);
if (sz > prevsz){
buf[j++] = prev;
prevsz = sz;
if (sz >= oldsz){
CAMLassert (sz == oldsz);
break;
}
}
prev = cur;
}
#if FREELIST_DEBUG
if (j > 2) fprintf (stderr, "FLP: update; buf size = %d\n", j);
#endif
if (FLP_MAX >= flp_size + j - 1){
if (j != 1){
memmove (&flp[i+j], &flp[i+1], sizeof (value) * (flp_size-i-1));
}
if (j > 0) memmove (&flp[i], &buf[0], sizeof (value) * j);
flp_size += j - 1;
}else{
if (FLP_MAX > i + j){
if (j != 1){
memmove (&flp[i+j], &flp[i+1], sizeof (value) * (FLP_MAX-i-j));
}
if (j > 0) memmove (&flp[i], &buf[0], sizeof (value) * j);
}else{
if (i != FLP_MAX){
memmove (&flp[i], &buf[0], sizeof (value) * (FLP_MAX - i));
}
}
flp_size = FLP_MAX - 1;
beyond = Next_small (flp[FLP_MAX - 1]);
}
}
}
return result;
}
/* Location of the last fragment seen by the sweeping code.
This is a pointer to the first word after the fragment, which is
the header of the next block.
Note that [ff_last_fragment] doesn't point to the fragment itself,
but to the block after it.
*/
static header_t *ff_last_fragment;
static void ff_init_merge (void)
{
CAML_EV_ALLOC_FLUSH();
ff_last_fragment = NULL;
caml_fl_merge = Ff_head;
#ifdef DEBUG
ff_check ();
#endif
}
static void ff_truncate_flp (value changed)
{
if (changed == Ff_head){
flp_size = 0;
beyond = Val_NULL;
}else{
while (flp_size > 0 &&
Bp_val (Next_small (flp[flp_size - 1])) >= Bp_val (changed))
-- flp_size;
if (Bp_val (beyond) >= Bp_val (changed)) beyond = Val_NULL;
}
}
static void ff_init (void)
{
Next_small (Ff_head) = Val_NULL;
ff_truncate_flp (Ff_head);
caml_fl_cur_wsz = 0;
}
static void ff_reset (void)
{
ff_init ();
}
/* Note: the [limit] parameter is unused because we merge blocks one by one. */
static header_t *ff_merge_block (value bp, char *limit)
{
value prev, cur, adj;
header_t hd = Hd_val (bp);
mlsize_t prev_wosz;
caml_fl_cur_wsz += Whsize_hd (hd);
/* [merge_block] is now responsible for calling the finalization function. */
if (Tag_hd (hd) == Custom_tag){
void (*final_fun)(value) = Custom_ops_val(bp)->finalize;
if (final_fun != NULL) final_fun(bp);
}
#ifdef DEBUG
caml_set_fields (bp, 0, Debug_free_major);
#endif
prev = caml_fl_merge;
cur = Next_small (prev);
/* The sweep code makes sure that this is the right place to insert
this block: */
CAMLassert (Bp_val (prev) < Bp_val (bp) || prev == Ff_head);
CAMLassert (Bp_val (cur) > Bp_val (bp) || cur == Val_NULL);
ff_truncate_flp (prev);
/* If [ff_last_fragment] and [bp] are adjacent, merge them. */
if (ff_last_fragment == Hp_bp (bp)){
mlsize_t bp_whsz = Whsize_val (bp);
if (bp_whsz <= Max_wosize){
hd = Make_header (bp_whsz, 0, Caml_white);
bp = (value) ff_last_fragment;
Hd_val (bp) = hd;
caml_fl_cur_wsz += Whsize_wosize (0);
}
}
/* If [bp] and [cur] are adjacent, remove [cur] from the free-list
and merge them. */
adj = Next_in_mem (bp);
if (adj == cur){
value next_cur = Next_small (cur);
mlsize_t cur_whsz = Whsize_val (cur);
if (Wosize_hd (hd) + cur_whsz <= Max_wosize){
Next_small (prev) = next_cur;
hd = Make_header (Wosize_hd (hd) + cur_whsz, 0, Caml_blue);
Hd_val (bp) = hd;
adj = Next_in_mem (bp);
#ifdef DEBUG
ff_last = Val_NULL;
Next_small (cur) = (value) Debug_free_major;
Hd_val (cur) = Debug_free_major;
#endif
cur = next_cur;
}
}
/* If [prev] and [bp] are adjacent merge them, else insert [bp] into
the free-list if it is big enough. */
prev_wosz = Wosize_val (prev);
if (Next_in_mem (prev) == bp && prev_wosz + Whsize_hd (hd) < Max_wosize){
Hd_val (prev) = Make_header (prev_wosz + Whsize_hd (hd), 0, Caml_blue);
#ifdef DEBUG
Hd_val (bp) = Debug_free_major;
#endif
CAMLassert (caml_fl_merge == prev);
}else if (Wosize_hd (hd) != 0){
Hd_val (bp) = Bluehd_hd (hd);
Next_small (bp) = cur;
Next_small (prev) = bp;
caml_fl_merge = bp;
}else{
/* This is a fragment. Leave it in white but remember it for eventual
merging with the next block. */
ff_last_fragment = (header_t *) bp;
caml_fl_cur_wsz -= Whsize_wosize (0);
}
return Hp_val (adj);
}
/* This is a heap extension. We have to insert it in the right place
in the free-list.
[ff_add_blocks] can only be called right after a call to
[ff_allocate] that returned Val_NULL.
Most of the heap extensions are expected to be at the end of the
free list. (This depends on the implementation of [malloc].)
[bp] must point to a list of blocks chained by their field 0,
terminated by Val_NULL, and field 1 of the first block must point to
the last block.
*/
static void ff_add_blocks (value bp)
{
value cur = bp;
CAMLassert (ff_last != Val_NULL);
CAMLassert (Next_small (ff_last) == Val_NULL);
do {
caml_fl_cur_wsz += Whsize_bp (cur);
cur = Field(cur, 0);
} while (cur != Val_NULL);
if (Bp_val (bp) > Bp_val (ff_last)){
Next_small (ff_last) = bp;
if (ff_last == caml_fl_merge && (char *) bp < caml_gc_sweep_hp){
caml_fl_merge = Field (bp, 1);
}
if (flp_size < FLP_MAX){
flp [flp_size++] = ff_last;
}
}else{
value prev;
prev = Ff_head;
cur = Next_small (prev);
while (cur != Val_NULL && Bp_val (cur) < Bp_val (bp)){
CAMLassert (Bp_val (prev) < Bp_val (bp) || prev == Ff_head);
/* XXX TODO: extend flp on the fly */
prev = cur;
cur = Next_small (prev);
}
CAMLassert (Bp_val (prev) < Bp_val (bp) || prev == Ff_head);
CAMLassert (Bp_val (cur) > Bp_val (bp) || cur == Val_NULL);
Next_small (Field (bp, 1)) = cur;
Next_small (prev) = bp;
/* When inserting blocks between [caml_fl_merge] and [caml_gc_sweep_hp],
we must advance [caml_fl_merge] to the new block, so that [caml_fl_merge]
is always the last free-list block before [caml_gc_sweep_hp]. */
if (prev == caml_fl_merge && (char *) bp < caml_gc_sweep_hp){
caml_fl_merge = Field (bp, 1);
}
ff_truncate_flp (bp);
}
}
static void ff_make_free_blocks
(value *p, mlsize_t size, int do_merge, int color)
{
mlsize_t sz;
while (size > 0){
if (size > Whsize_wosize (Max_wosize)){
sz = Whsize_wosize (Max_wosize);
}else{
sz = size;
}
*(header_t *)p = Make_header (Wosize_whsize (sz), 0, color);
if (do_merge) ff_merge_block (Val_hp (p), NULL);
size -= sz;
p += sz;
}
}
/********************* best-fit allocation policy *********************/
/* quick-fit + FIFO-ordered best fit (Wilson's nomenclature)
We use Standish's data structure (a tree of doubly-linked lists)
with a splay tree (Sleator & Tarjan).
*/
/* [BF_NUM_SMALL] must be at least 4 for this code to work
and at least 5 for good performance on typical OCaml programs.
For portability reasons, BF_NUM_SMALL cannot be more than 32.
*/
#define BF_NUM_SMALL 16
/* Note that indexing into [bf_small_fl] starts at 1, so the first entry
in this array is unused.
*/
static struct {
value free;
value *merge;
} bf_small_fl [BF_NUM_SMALL + 1];
static int bf_small_map = 0;
/* Small free blocks have only one pointer to the next block.
Large free blocks have 5 fields:
tree fields:
- isnode flag
- left child
- right child
list fields:
- next
- prev
*/
typedef struct large_free_block {
int isnode;
struct large_free_block *left;
struct large_free_block *right;
struct large_free_block *prev;
struct large_free_block *next;
} large_free_block;
Caml_inline mlsize_t bf_large_wosize (struct large_free_block *n) {
return Wosize_val((value)(n));
}
static struct large_free_block *bf_large_tree;
static struct large_free_block *bf_large_least;
/* [bf_large_least] is either NULL or a pointer to the smallest (leftmost)
block in the tree. In this latter case, the block must be alone in its
doubly-linked list (i.e. have [isnode] true and [prev] and [next]
both pointing back to this block)
*/
/* Auxiliary functions for bitmap */
/* Find first (i.e. least significant) bit set in a word. */
#ifdef HAS_FFS
#include <strings.h>
#elif defined(HAS_BITSCANFORWARD)
#include <intrin.h>
Caml_inline int ffs (int x)
{
unsigned long index;
unsigned char result;
result = _BitScanForward (&index, (unsigned long) x);
return result ? (int) index + 1 : 0;
}
#else
Caml_inline int ffs (int x)
{
/* adapted from Hacker's Delight */
int bnz, b0, b1, b2, b3, b4;
CAMLassert ((x & 0xFFFFFFFF) == x);
x = x & -x;
bnz = x != 0;
b4 = !!(x & 0xFFFF0000) << 4;
b3 = !!(x & 0xFF00FF00) << 3;
b2 = !!(x & 0xF0F0F0F0) << 2;
b1 = !!(x & 0xCCCCCCCC) << 1;
b0 = !!(x & 0xAAAAAAAA);
return bnz + b0 + b1 + b2 + b3 + b4;
}
#endif /* HAS_FFS or HAS_BITSCANFORWARD */
/* Indexing starts at 1 because that's the minimum block size. */
Caml_inline void set_map (int index)
{
bf_small_map |= (1 << (index - 1));
}
Caml_inline void unset_map (int index)
{
bf_small_map &= ~(1 << (index - 1));
}
/* debug functions for checking the data structures */
#if defined (DEBUG) || FREELIST_DEBUG
static mlsize_t bf_check_cur_size = 0;
static asize_t bf_check_subtree (large_free_block *p)
{
mlsize_t wosz;
large_free_block *cur, *next;
asize_t total_size = 0;
if (p == NULL) return 0;
wosz = bf_large_wosize(p);
CAMLassert (p->isnode == 1);
total_size += bf_check_subtree (p->left);
CAMLassert (wosz > BF_NUM_SMALL);
CAMLassert (wosz > bf_check_cur_size);
bf_check_cur_size = wosz;
cur = p;
while (1){
CAMLassert (bf_large_wosize (cur) == wosz);
CAMLassert (Color_val ((value) cur) == Caml_blue);
CAMLassert ((cur == p && cur->isnode == 1) || cur->isnode == 0);
total_size += Whsize_wosize (wosz);
next = cur->next;
CAMLassert (next->prev == cur);
if (next == p) break;
cur = next;
}
total_size += bf_check_subtree (p->right);
return total_size;
}
static void bf_check (void)
{
mlsize_t i;
asize_t total_size = 0;
int map = 0;
/* check free lists */
CAMLassert (BF_NUM_SMALL <= 8 * sizeof (int));
for (i = 1; i <= BF_NUM_SMALL; i++){
value b;
int col = 0;
int merge_found = 0;
if (bf_small_fl[i].merge == &bf_small_fl[i].free){
merge_found = 1;
}else{
CAMLassert (caml_gc_phase != Phase_sweep
|| caml_fl_merge == Val_NULL
|| bf_small_fl[i].merge < &Next_small(caml_fl_merge));
}
CAMLassert (*bf_small_fl[i].merge == Val_NULL
|| Color_val (*bf_small_fl[i].merge) == Caml_blue);
if (bf_small_fl[i].free != Val_NULL) map |= 1 << (i-1);
for (b = bf_small_fl[i].free; b != Val_NULL; b = Next_small (b)){
if (bf_small_fl[i].merge == &Next_small (b)) merge_found = 1;
CAMLassert (Wosize_val (b) == i);
total_size += Whsize_wosize (i);
if (Color_val (b) == Caml_blue){
col = 1;
CAMLassert (Next_small (b) == Val_NULL
|| Bp_val (Next_small (b)) > Bp_val (b));
}else{
CAMLassert (col == 0);
CAMLassert (Color_val (b) == Caml_white);
}
}
if (caml_gc_phase == Phase_sweep) CAMLassert (merge_found);
}
CAMLassert (map == bf_small_map);
/* check [caml_fl_merge] */
CAMLassert (caml_gc_phase != Phase_sweep
|| caml_fl_merge == Val_NULL
|| Hp_val (caml_fl_merge) < (header_t *) caml_gc_sweep_hp);
/* check the tree */
bf_check_cur_size = 0;
total_size += bf_check_subtree (bf_large_tree);
/* check the total free set size */
CAMLassert (total_size == caml_fl_cur_wsz);
/* check the smallest-block pointer */
if (bf_large_least != NULL){
large_free_block *x = bf_large_tree;
while (x->left != NULL) x = x->left;
CAMLassert (x == bf_large_least);
CAMLassert (x->isnode == 1);
CAMLassert (x->prev == x);
CAMLassert (x->next == x);
}
}
#endif /* DEBUG || FREELIST_DEBUG */
#if FREELIST_DEBUG
#define FREELIST_DEBUG_bf_check() bf_check ()
#else
#define FREELIST_DEBUG_bf_check()
#endif