Newer
Older
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
/*
* Generational Conservative Garbage Collector for CMUCL x86.
*
* This code was written by Douglas T. Crosher, based on Public Domain
* codes from Carnegie Mellon University. This code has been placed in
* the public domain, and is provided 'as is'.
*
* Douglas Crosher, 1996, 1997.
*
* $Header: /Volumes/share2/src/cmucl/cvs2git/cvsroot/src/lisp/gencgc.c,v 1.1 1997/11/25 17:59:18 dtc Exp $
* */
#include <stdio.h>
#include <signal.h>
#include "lisp.h"
#include "internals.h"
#include "os.h"
#include "globals.h"
#include "interrupt.h"
#include "validate.h"
#include "lispregs.h"
#include "gencgc.h"
#define gc_abort() lose("GC invariant lost! File \"%s\", line %d\n", \
__FILE__, __LINE__)
#if 1
#define gc_assert(ex) do { \
if (!(ex)) gc_abort(); \
} while (0)
#else
#define gc_assert(ex)
#endif
/* The number of generations, an extra is added to this for use as a
temp. */
#define NUM_GENERATIONS 6
/* Debugging variables. */
boolean gencgc_verbose = FALSE;
/* To enable the use of page protection to help avoid the scavenging
of pages that don't have pointers to younger generations. */
boolean enable_page_protection = TRUE;
/* Hunt for pointers to old-space, when GCing generations >=
verify_gen. Set to NUM_GENERATIONS to disable. */
int verify_gens = NUM_GENERATIONS-1;
/* Enable a pre-scan verify of generation 0 before it's GCed */
boolean pre_verify_gen_0 = FALSE;
/* Enable the printing of a note when code objects are found in the
dynamic space during a heap verify. */
boolean verify_dynamic_code_check = FALSE;
/* Enable the checking of code objects for fixup errors after they are
transported. */
boolean check_code_fixups = TRUE;
/* To enable unmapping of a page and re-mmaping it to have it zero
filled. */
#if defined(__FreeBSD__)
/* Note: this can waste a lot of swap on FreeBSD so don't unmap. */
boolean gencgc_unmap_zero = FALSE;
#else
boolean gencgc_unmap_zero = TRUE;
#endif
/* Enable checking that newly allocated regions are zero filled. */
boolean gencgc_zero_check = FALSE;
/* The minimum size for a large object. */
unsigned large_object_size = 4*4096;
/* Enable the filtering of stack/register pointers. This could reduce
the number of invalid pointers accepted. It will probably degrades
interrupt safety during object initialisation. */
boolean enable_pointer_filter = TRUE;
/* The total bytes allocated. Seen by (dynamic-usage) */
unsigned long bytes_allocated = 0;
static unsigned long auto_gc_trigger = 0;
/* The src. and dest. generations. Set before a GC starts scavenging */
static int from_space;
static int new_space;
/* GC structures and variables.*/
#define PAGE_BYTES 4096
/* An array of page structures is statically allocated.
This helps quickly map between an address its page structure.
NUM_PAGES is set from the size of the dynamic space. */
struct page page_table[NUM_PAGES];
/* To map addresses to page structures the address of the first page
is needed. */
static void *heap_base = NULL;
/* Calculate the start address for the given page number. */
inline void
*page_address(int page_num)
{
return (heap_base + (page_num * 4096));
}
/* Find the page index within the page_table for the given
address. Returns -1 on failure. */
inline int
find_page_index(void *addr)
{
int index = addr-heap_base;
if (index >= 0) {
index = ((unsigned int)index)/4096;
if (index < NUM_PAGES)
return (index);
}
return (-1);
}
/* A structure to hold the state of a generation */
struct generation {
/* The first page that gc_alloc checks on its next call. */
int alloc_start_page;
/* The first page that gc_alloc_unboxed checks on its next call. */
int alloc_unboxed_start_page;
/* The first page that gc_alloc_large (boxed) considers on its next
call. Although it always allocates after the boxed_region. */
int alloc_large_start_page;
/* The first page that gc_alloc_large (unboxed) considers on its
next call. Although it always allocates after the
current_unboxed_region. */
int alloc_large_unboxed_start_page;
/* The bytes allocate to this generation. */
int bytes_allocated;
/* The number of bytes at which to trigger a GC */
int gc_trigger;
/* To calculate a new level for gc_trigger */
int bytes_consed_between_gc;
/* The number of GCs since the last raise. */
int num_gc;
/* The average age at after which a GC will raise objects to the
next generation. */
int trigger_age;
/* The cumulative sum of the bytes allocated to this generation. It is
cleared after a GC on this generations, and update before new
objects are added from a GC of a younger generation. Dividing by
the bytes_allocated will give the average age of the memory in
this generation since its last GC. */
int cum_sum_bytes_allocated;
/* A minimum average memory age before a GC will occur helps
prevent a GC when a large number of new live objects have been
added, in which case a GC could be a waste of time. */
double min_av_mem_age;
};
/* An array of generation structures. There needs to be one more
generation structure than actual generations as the oldest
generations is temporarily raised then lowered. */
static struct generation generations[NUM_GENERATIONS+1];
/* The oldest generation that is will currently be GCed by default.
Valid values are: 0, 1, ... (NUM_GENERATIONS-1)
The default of (NUM_GENERATIONS-1) enables GC on all generations.
Setting this to 0 effectively disables the generational nature of
the GC. In some applications generational GC may not be useful
because there are no long-lived objects.
An intermediate value could be handy after moving long-lived data
into an older generation so an unnecessary GC of this long-lived
data can be avoided. */
unsigned int gencgc_oldest_gen_to_gc = NUM_GENERATIONS-1;
/* The maximum free page in the heap is maintained and used to update
ALLOCATION_POINTER which is used by the room function
to limit its search of the heap. XX Gencgc obviously needs to be
better integrated with the lisp code. */
static int last_free_page;
static int last_used_page = 0;
/* Misc. heap functions. */
/* Count the number of pages write protected within the given
generation */
static int
count_write_protect_generation_pages(int generation)
{
int i;
int cnt = 0;
for (i = 0; i < last_free_page; i++)
if ((page_table[i].allocated != FREE_PAGE)
&& (page_table[i].gen == generation)
&& (page_table[i].write_protected == 1))
cnt++;
return(cnt);
}
/* Count the number of pages within the given generation */
static int
count_generation_pages(int generation)
{
int i;
int cnt = 0;
for (i = 0; i < last_free_page; i++)
if ((page_table[i].allocated != 0)
&& (page_table[i].gen == generation))
cnt++;
return(cnt);
}
/* Count the number of dont_move pages. */
static int
count_dont_move_pages(void)
{
int i;
int cnt = 0;
for (i = 0; i < last_free_page; i++)
if ((page_table[i].allocated != 0)
&& (page_table[i].dont_move != 0))
cnt++;
return(cnt);
}
/* Work through the pages and add up the number of bytes used for the
given generation. */
static int
generation_bytes_allocated (int gen)
{
int i;
int bytes_allocated = 0;
for (i = 0; i < last_free_page; i++) {
if ((page_table[i].allocated != 0) && (page_table[i].gen == gen))
bytes_allocated += page_table[i].bytes_used;
}
return (bytes_allocated);
}
/* Return the average age of the memory in a generation. */
static double
gen_av_mem_age(int gen)
{
if (generations[gen].bytes_allocated == 0)
return (0.0);
return (((double)generations[gen].cum_sum_bytes_allocated)/((double)generations[gen].bytes_allocated));
}
/* The verbose argument controls how much to print out: 0 for normal
level of detail; 1 for debugging. */
static void
print_generation_stats(int verbose)
{
int i, gens;
/* Number of generations to print out. */
if (verbose)
gens = NUM_GENERATIONS+1;
else
gens = NUM_GENERATIONS;
/* Print the heap stats */
fprintf(stderr," Generation Boxed Unboxed LB LUB Alloc Waste Trig WP GCs Mem-age\n");
for (i = 0; i < gens; i++) {
int j;
int boxed_cnt = 0;
int unboxed_cnt = 0;
int large_boxed_cnt = 0;
int large_unboxed_cnt = 0;
for (j = 0; j < last_free_page; j++)
if (page_table[j].gen == i) {
/* Count the number of boxed pages within the given generation */
if (page_table[j].allocated == BOXED_PAGE)
if (page_table[j].large_object)
large_boxed_cnt++;
else
boxed_cnt++;
/* Count the number of unboxed pages within the given generation */
if (page_table[j].allocated == UNBOXED_PAGE)
if (page_table[j].large_object)
large_unboxed_cnt++;
else
unboxed_cnt++;
}
gc_assert(generations[i].bytes_allocated == generation_bytes_allocated(i));
fprintf(stderr," %8d: %5d %5d %5d %5d %8d %5d %8d %4d %3d %7.4lf\n",
i,
boxed_cnt, unboxed_cnt, large_boxed_cnt, large_unboxed_cnt,
generations[i].bytes_allocated,
(count_generation_pages(i)*4096 - generations[i].bytes_allocated),
generations[i].gc_trigger,
count_write_protect_generation_pages(i),
generations[i].num_gc,
gen_av_mem_age(i));
}
fprintf(stderr," Total bytes alloc=%d\n", bytes_allocated);
}
/* Allocation routines */
/* To support quick and inline allocation, regions of memory can be
allocated and then allocated from with just a free pointer and a
check against an end address.
Since objects can be allocated to spaces with different properties
e.g. boxed/unboxed, generation, ages; there may need to be many
allocation regions.
Each allocation region may be start within a partly used page.
Many features of memory use are noted on a page wise basis,
E.g. the generation; so if a region starts within an existing
allocated page it must be consistent with this page.
During the scavenging of the newspace, objects will be transported
into an allocation region, and pointers updated to point to this
allocation region. It is possible that these pointers will be
scavenged again before the allocation region is closed, E.g. due to
trans_list which jumps all over the place to cleanup the list. It
is important to be able to determine properties of all objects
pointed to when scavenging, E.g to detect pointers to the
oldspace. Thus it's important that the allocation regions have the
correct properties set when allocated, and not just set when
closed. The region allocation routines return regions with the
specified properties, and grab all the pages, setting there
properties appropriately, except that the amount used is not known.
These regions are used to support quicker allocation using just a
free pointer. The actual space used by the region is not reflected
in the pages tables until it is closed. It can't be scavenged until
closed.
When finished with the region it should be closed, which will
update the page tables for the actual space used returning unused
space. Further it may be noted in the new regions which is
necessary when scavenging the newspace.
Large objects may be allocated directly without an allocation
region, the page tables are updated immediately.
Unboxed objects don't contain points to other objects so don't need
scavenging. Further they can't contain pointers to younger
generations so WP is not needed. By allocating pages to unboxed
objects the whole page never needs scavenging or write protecting.
*/
/* Only using two regions at present, both are for the current
newspace generation. */
struct alloc_region boxed_region;
struct alloc_region unboxed_region;
/* X hack. current lisp code uses the following. Need coping
in/out. */
void *current_region_free_pointer;
void *current_region_end_addr;
/* The generation currently being allocated to. X */
static int gc_alloc_generation;
/* Find a new region with room for at least the given number of bytes.
It starts looking at the current generations alloc_start_page. So
may pick up from the previous region if there is enough space. This
keeps the allocation contiguous when scavenging the newspace.
The alloc_region is should have been closed by a call to
gc_alloc_update_page_tables, and will thus be in an empty state.
To assist the scavenging functions write protected pages are not
used. Free pages should not be write protected.
It is critical to the conservative GC that the start of regions be
known. To help achieve this only small regions are allocated at a
time.
During scavenging pointers may be found to within the current
region and the page generation must be set so pointers to the from
space can be recognised. So the generation of pages in the region
are set to gc_alloc_generation. To prevent another allocation call
using the same pages, all the pages in the region are allocated,
although they will initially be empty.
*/
static void
gc_alloc_new_region(int nbytes, int unboxed, struct alloc_region *alloc_region)
{
int first_page;
int last_page;
int region_size;
int restart_page;
int bytes_found;
int num_pages;
int i;
/* fprintf(stderr,"alloc_new_region for %d bytes from gen %d\n",
nbytes, gc_alloc_generation);*/
/* Check that the region is in a reset state. */
gc_assert((alloc_region->first_page == 0)
&& (alloc_region->last_page == -1)
&& (alloc_region->free_pointer == alloc_region->end_addr));
if (unboxed)
restart_page = generations[gc_alloc_generation].alloc_unboxed_start_page;
else
restart_page = generations[gc_alloc_generation].alloc_start_page;
/* Search for a contiguous free region of at least nbytes with the
given properties: boxed/unboxed, generation. */
do {
first_page = restart_page;
/* First search for a page with at least 32 bytes free, that is
not write protected, or marked dont_move. */
while ((first_page < NUM_PAGES)
&& (page_table[first_page].allocated != FREE_PAGE) /* Not free page */
&& ((unboxed && (page_table[first_page].allocated != UNBOXED_PAGE))
|| (!unboxed &&
(page_table[first_page].allocated != BOXED_PAGE))
|| (page_table[first_page].large_object != 0)
|| (page_table[first_page].gen != gc_alloc_generation)
|| (page_table[first_page].bytes_used >= (4096-32))
|| (page_table[first_page].write_protected != 0)
|| (page_table[first_page].dont_move != 0)))
first_page++;
/* Check for a failure */
if (first_page >= NUM_PAGES) {
fprintf(stderr,"*A2 gc_alloc_new_region failed, nbytes=%d.\n", nbytes);
print_generation_stats(1);
exit(1);
}
gc_assert(page_table[first_page].write_protected == 0);
/* fprintf(stderr," first_page=%d bytes_used=%d\n",first_page, page_table[first_page].bytes_used);*/
/* Now search forward to calculate the available region size. It
tries to keeps going until nbytes are found and the number of
pages is greater than some level. This helps keep down the
number of pages in a region. */
last_page = first_page;
bytes_found = 4096 - page_table[first_page].bytes_used;
num_pages = 1;
while (((bytes_found < nbytes) || (num_pages < 2))
&& (last_page < (NUM_PAGES-1))
&& (page_table[last_page+1].allocated == FREE_PAGE)) {
last_page++;
num_pages++;
bytes_found += 4096;
gc_assert(page_table[last_page].write_protected == 0);
}
region_size = (4096 - page_table[first_page].bytes_used)
+ 4096*(last_page-first_page);
gc_assert(bytes_found == region_size);
/* fprintf(stderr," last_page=%d bytes_found=%d num_pages=%d\n",last_page, bytes_found, num_pages);*/
restart_page = last_page + 1;
}
while ((restart_page < NUM_PAGES) && (bytes_found < nbytes));
/* Check for a failure */
if ((restart_page >= NUM_PAGES) && (bytes_found < nbytes)) {
fprintf(stderr,"*A1 gc_alloc_new_region failed, nbytes=%d.\n", nbytes);
print_generation_stats(1);
exit(1);
}
/*fprintf(stderr,"gc_alloc_new_region gen %d: %d bytes: from pages %d to %d: addr=%x\n", gc_alloc_generation, bytes_found, first_page, last_page, page_address(first_page));*/
/* Setup the alloc_region. */
alloc_region->first_page = first_page;
alloc_region->last_page = last_page;
alloc_region->start_addr = page_table[first_page].bytes_used
+ page_address(first_page);
alloc_region->free_pointer = alloc_region->start_addr;
alloc_region->end_addr = alloc_region->start_addr + bytes_found;
if (gencgc_zero_check) {
int *p;
for(p = (int *)alloc_region->start_addr;
p < (int *)alloc_region->end_addr; p++)
if (*p != 0)
fprintf(stderr,"** new region not zero @ %x\n",p);
}
/* Setup the pages. */
/* The first page may have already been in use. */
if (page_table[first_page].bytes_used == 0) {
if (unboxed)
page_table[first_page].allocated = UNBOXED_PAGE;
else
page_table[first_page].allocated = BOXED_PAGE;
page_table[first_page].gen = gc_alloc_generation;
page_table[first_page].large_object = 0;
page_table[first_page].first_object_offset = 0;
}
if (unboxed)
gc_assert(page_table[first_page].allocated == UNBOXED_PAGE);
else
gc_assert(page_table[first_page].allocated == BOXED_PAGE);
gc_assert(page_table[first_page].gen == gc_alloc_generation);
gc_assert(page_table[first_page].large_object == 0);
for (i = first_page+1; i <= last_page; i++) {
if (unboxed)
page_table[i].allocated = UNBOXED_PAGE;
else
page_table[i].allocated = BOXED_PAGE;
page_table[i].gen = gc_alloc_generation;
page_table[i].large_object = 0;
/* This may not be necessary for unboxed regions (think it was
broken before!) */
page_table[i].first_object_offset =
alloc_region->start_addr - page_address(i);
}
/* Bump up last_free_page */
if (last_page+1 > last_free_page) {
last_free_page = last_page+1;
SetSymbolValue(ALLOCATION_POINTER,
(lispobj)(((char *)heap_base) + last_free_page*4096));
if (last_page+1 > last_used_page)
last_used_page = last_page+1;
}
}
/* If the record_new_objects flag is 2 then all new regions created
are recorded.
If it's 1 then then it is only recorded if the first page of the
current region is <= new_areas_ignore_page. This helps avoid
unnecessary recording when doing full scavenge pass.
The new_object structure holds the page, byte offset, and size of
new regions of objects. Each new area is placed in the array of
these structures pointer to by new_areas. new_areas_index holds the
offset into new_areas.
If new_area overflows NUM_NEW_AREAS then it stops adding them. The
later code must detect this an handle it, probably by doing a full
scavenge of a generation. */
#define NUM_NEW_AREAS 512
static int record_new_objects = 0;
static int new_areas_ignore_page;
struct new_area {
int page;
int offset;
int size;
};
static struct new_area (*new_areas)[];
static new_areas_index;
int max_new_areas;
/* Add a new area to new_areas. */
static void
add_new_area(int first_page, int offset, int size)
{
unsigned new_area_start,c;
int i;
/* Ignore if full */
if (new_areas_index >= NUM_NEW_AREAS)
return;
switch (record_new_objects) {
case 0:
return;
case 1:
if (first_page > new_areas_ignore_page)
return;
break;
case 2:
break;
default:
gc_abort();
}
new_area_start = 4096*first_page + offset;
/* Search backwards for a prior area that this follows from. If
found this will save adding a new area. */
for (i = new_areas_index-1, c = 0; (i >= 0) && (c < 8); i--, c++) {
unsigned area_end = 4096*((*new_areas)[i].page)
+ (*new_areas)[i].offset + (*new_areas)[i].size;
/*fprintf(stderr,"*S1 %d %d %d %d\n",i,c,new_area_start,area_end);*/
if (new_area_start == area_end) {
/*fprintf(stderr,"-> Adding to [%d] %d %d %d with %d %d %d:\n",
i, (*new_areas)[i].page, (*new_areas)[i].offset ,
(*new_areas)[i].size, first_page, offset, size);*/
(*new_areas)[i].size += size;
return;
}
}
/*fprintf(stderr,"*S1 %d %d %d\n",i,c,new_area_start);*/
(*new_areas)[new_areas_index].page = first_page;
(*new_areas)[new_areas_index].offset = offset;
(*new_areas)[new_areas_index].size = size;
/*fprintf(stderr," new_area %d page %d offset %d size %d\n",
new_areas_index, first_page, offset, size);*/
new_areas_index++;
/* Note the max new_areas used. */
if (new_areas_index > max_new_areas)
max_new_areas = new_areas_index;
}
/* Update the tables for the alloc_region. The region maybe added to
the new_areas.
When done the alloc_region its setup so that the next quick alloc
will fail safely and thus a new regions will be allocated. Further
it is safe to try and re-update the page table of this reset
alloc_region.
*/
void
gc_alloc_update_page_tables(int unboxed, struct alloc_region *alloc_region)
{
int more;
int first_page;
int next_page;
int bytes_used;
int orig_first_page_bytes_used;
int region_size;
int byte_cnt;
/*fprintf(stderr,"gc_alloc_update_page_tables to gen %d: ",
gc_alloc_generation);*/
first_page = alloc_region->first_page;
/* Catch an unused alloc_region. */
if ((first_page == 0) && (alloc_region->last_page == -1))
return;
next_page = first_page+1;
/* Skip if no bytes were allocated */
if (alloc_region->free_pointer != alloc_region->start_addr) {
orig_first_page_bytes_used = page_table[first_page].bytes_used;
gc_assert(alloc_region->start_addr == (page_address(first_page) + page_table[first_page].bytes_used));
/* All the pages used need to be updated */
/* Update the first page. */
/* fprintf(stderr,"0");*/
/* If the page was free then setup the gen, and
first_object_offset. */
if (page_table[first_page].bytes_used == 0)
gc_assert(page_table[first_page].first_object_offset == 0);
if (unboxed)
gc_assert(page_table[first_page].allocated == UNBOXED_PAGE);
else
gc_assert(page_table[first_page].allocated == BOXED_PAGE);
gc_assert(page_table[first_page].gen == gc_alloc_generation);
gc_assert(page_table[first_page].large_object == 0);
byte_cnt = 0;
/* Calc. the number of bytes used in this page. This is not always
the number of new bytes, unless it was free. */
more = 0;
if ((bytes_used = (alloc_region->free_pointer - page_address(first_page)))>4096) {
bytes_used = 4096;
more = 1;
}
page_table[first_page].bytes_used = bytes_used;
byte_cnt += bytes_used;
/* All the rest of the pages should be free. Need to set their
first_object_offset pointer to the start of the region, and set
the bytes_used. */
while (more) {
/* fprintf(stderr,"+")*/
if (unboxed)
gc_assert(page_table[next_page].allocated == UNBOXED_PAGE);
else
gc_assert(page_table[next_page].allocated == BOXED_PAGE);
gc_assert(page_table[next_page].bytes_used == 0);
gc_assert(page_table[next_page].gen == gc_alloc_generation);
gc_assert(page_table[next_page].large_object == 0);
gc_assert(page_table[next_page].first_object_offset ==
alloc_region->start_addr - page_address(next_page));
/* Calc. the number of bytes used in this page. */
more = 0;
if ((bytes_used = (alloc_region->free_pointer
- page_address(next_page)))>4096) {
bytes_used = 4096;
more = 1;
}
page_table[next_page].bytes_used = bytes_used;
byte_cnt += bytes_used;
next_page++;
}
region_size = alloc_region->free_pointer - alloc_region->start_addr;
bytes_allocated += region_size;
generations[gc_alloc_generation].bytes_allocated += region_size;
gc_assert((byte_cnt- orig_first_page_bytes_used) == region_size);
/* Set the generations alloc restart page to the last page of
the region. */
if (unboxed)
generations[gc_alloc_generation].alloc_unboxed_start_page =
next_page-1;
else
generations[gc_alloc_generation].alloc_start_page = next_page-1;
/* Add the region to the new_areas if requested. */
if (!unboxed)
add_new_area(first_page,orig_first_page_bytes_used, region_size);
/* fprintf(stderr," gc_alloc_update_page_tables update %d bytes to gen %d\n",region_size,gc_alloc_generation);*/
}
else
/* No bytes allocated. Unallocate the first_page if there are 0
bytes_used. */
if (page_table[first_page].bytes_used == 0)
page_table[first_page].allocated = FREE_PAGE;
/* Unallocate any unused pages. */
while (next_page <= alloc_region->last_page) {
gc_assert(page_table[next_page].bytes_used == 0);
page_table[next_page].allocated = FREE_PAGE;
next_page++;
}
/* Reset the alloc_region. */
alloc_region->first_page = 0;
alloc_region->last_page = -1;
alloc_region->start_addr = page_address(0);
alloc_region->free_pointer = page_address(0);
alloc_region->end_addr = page_address(0);
/* fprintf(stderr,"\n");*/
}
static inline void *gc_quick_alloc(int nbytes);
/* Allocate a possibly large object. */
static void
*gc_alloc_large(int nbytes, int unboxed, struct alloc_region *alloc_region)
{
int first_page;
int last_page;
int region_size;
int restart_page;
int bytes_found;
int num_pages;
int orig_first_page_bytes_used;
int byte_cnt;
int more;
int bytes_used;
int next_page;
int large = (nbytes >= large_object_size);
/* if (nbytes > 200000)
fprintf(stderr,"*** alloc_large %d\n",nbytes);*/
/* fprintf(stderr,"gc_alloc_large for %d bytes from gen %d\n",
nbytes, gc_alloc_generation);*/
/* If the object is small, and there is room in the current region
then allocation it in the current region. */
if (!large
&& ((alloc_region->end_addr-alloc_region->free_pointer) >= nbytes))
return gc_quick_alloc(nbytes);
/* Search for a contiguous free region of at least nbytes. If it's a
large object then align it on a page boundary by searching for a
free page. */
/* To allow the allocation of small objects without the danger of
using a page in the current boxed region, the search starts after
the current boxed free region. XX could probably keep a page
index ahead of the current region and bumped up here to save a
lot of re-scanning. */
if (unboxed)
restart_page = generations[gc_alloc_generation].alloc_large_unboxed_start_page;
else
restart_page = generations[gc_alloc_generation].alloc_large_start_page;
if (restart_page <= alloc_region->last_page)
restart_page = alloc_region->last_page+1;
do {
first_page = restart_page;
if (large)
while ((first_page < NUM_PAGES)
&& (page_table[first_page].allocated != FREE_PAGE))
first_page++;
else
while ((first_page < NUM_PAGES)
&& (page_table[first_page].allocated != FREE_PAGE)
&& ((unboxed &&
(page_table[first_page].allocated != UNBOXED_PAGE))
|| (!unboxed &&
(page_table[first_page].allocated != BOXED_PAGE))
|| (page_table[first_page].large_object != 0)
|| (page_table[first_page].gen != gc_alloc_generation)
|| (page_table[first_page].bytes_used >= (4096-32))
|| (page_table[first_page].write_protected != 0)
|| (page_table[first_page].dont_move != 0)))
first_page++;
/* Check for a failure */
if (first_page >= NUM_PAGES) {
fprintf(stderr,"*A2 gc_alloc_large failed, nbytes=%d.\n", nbytes);
print_generation_stats(1);
exit(1);
}
gc_assert(page_table[first_page].write_protected == 0);
/* fprintf(stderr," first_page=%d bytes_used=%d\n",first_page, page_table[first_page].bytes_used);*/
last_page = first_page;
bytes_found = 4096 - page_table[first_page].bytes_used;
num_pages = 1;
while ((bytes_found < nbytes)
&& (last_page < (NUM_PAGES-1))
&& (page_table[last_page+1].allocated == FREE_PAGE)) {
last_page++;
num_pages++;
bytes_found += 4096;
gc_assert(page_table[last_page].write_protected == 0);
}
region_size = (4096 - page_table[first_page].bytes_used)
+ 4096*(last_page-first_page);
gc_assert(bytes_found == region_size);
/* fprintf(stderr," last_page=%d bytes_found=%d num_pages=%d\n",last_page, bytes_found, num_pages);*/
restart_page = last_page + 1;
}
while ((restart_page < NUM_PAGES) && (bytes_found < nbytes));
/* Check for a failure */
if ((restart_page >= NUM_PAGES) && (bytes_found < nbytes)) {
fprintf(stderr,"*A1 gc_alloc_large failed, nbytes=%d.\n", nbytes);
print_generation_stats(1);
exit(1);
}
/* if (large)
fprintf(stderr,"gc_alloc_large gen %d: %d of %d bytes: from pages %d to %d: addr=%x\n",
gc_alloc_generation, nbytes, bytes_found,
first_page, last_page, page_address(first_page));*/
gc_assert(first_page > alloc_region->last_page);
if (unboxed)
generations[gc_alloc_generation].alloc_large_unboxed_start_page =
last_page;
else
generations[gc_alloc_generation].alloc_large_start_page = last_page;
/* Setup the pages. */
orig_first_page_bytes_used = page_table[first_page].bytes_used;
/* If the first page was free then setup the gen, and
first_object_offset. */
if (page_table[first_page].bytes_used == 0) {
if (unboxed)
page_table[first_page].allocated = UNBOXED_PAGE;
else
page_table[first_page].allocated = BOXED_PAGE;
page_table[first_page].gen = gc_alloc_generation;
page_table[first_page].first_object_offset = 0;
page_table[first_page].large_object = large;
}
if (unboxed)
gc_assert(page_table[first_page].allocated == UNBOXED_PAGE);
else
gc_assert(page_table[first_page].allocated == BOXED_PAGE);
gc_assert(page_table[first_page].gen == gc_alloc_generation);
gc_assert(page_table[first_page].large_object == large);
byte_cnt = 0;
/* Calc. the number of bytes used in this page. This is not
always the number of new bytes, unless it was free. */
more = 0;
if ((bytes_used = nbytes+orig_first_page_bytes_used) > 4096) {
bytes_used = 4096;
more = 1;
}
page_table[first_page].bytes_used = bytes_used;
byte_cnt += bytes_used;
next_page = first_page+1;
/* All the rest of the pages should be free. Need to set their
first_object_offset pointer to the start of the region, and
set the bytes_used. */
while (more) {
/* fprintf(stderr,"+");*/
gc_assert(page_table[next_page].allocated == FREE_PAGE);
gc_assert(page_table[next_page].bytes_used == 0);
if (unboxed)
page_table[next_page].allocated = UNBOXED_PAGE;
else
page_table[next_page].allocated = BOXED_PAGE;
page_table[next_page].gen = gc_alloc_generation;
page_table[next_page].large_object = large;
page_table[next_page].first_object_offset =
orig_first_page_bytes_used - 4096*(next_page-first_page);
/* Calc. the number of bytes used in this page. */
more = 0;
if ((bytes_used=(nbytes+orig_first_page_bytes_used)-byte_cnt) > 4096) {
bytes_used = 4096;
more = 1;
}
page_table[next_page].bytes_used = bytes_used;
byte_cnt += bytes_used;
next_page++;
}
gc_assert((byte_cnt-orig_first_page_bytes_used) == nbytes);
bytes_allocated += nbytes;
generations[gc_alloc_generation].bytes_allocated += nbytes;
/* Add the region to the new_areas if requested. */
if (!unboxed)
add_new_area(first_page,orig_first_page_bytes_used,nbytes);
/* Bump up last_free_page */
if (last_page+1 > last_free_page) {
last_free_page = last_page+1;
SetSymbolValue(ALLOCATION_POINTER,
(lispobj)(((char *)heap_base) + last_free_page*4096));
if (last_page+1 > last_used_page)
last_used_page = last_page+1;
}
return((void *)(page_address(first_page)+orig_first_page_bytes_used));
}