2 * GENerational Conservative Garbage Collector for SBCL x86
6 * This software is part of the SBCL system. See the README file for
9 * This software is derived from the CMU CL system, which was
10 * written at Carnegie Mellon University and released into the
11 * public domain. The software is in the public domain and is
12 * provided with absolutely no warranty. See the COPYING and CREDITS
13 * files for more information.
17 * For a review of garbage collection techniques (e.g. generational
18 * GC) and terminology (e.g. "scavenging") see Paul R. Wilson,
19 * "Uniprocessor Garbage Collection Techniques". As of 20000618, this
20 * had been accepted for _ACM Computing Surveys_ and was available
21 * as a PostScript preprint through
22 * <http://www.cs.utexas.edu/users/oops/papers.html>
24 * <ftp://ftp.cs.utexas.edu/pub/garbage/bigsurv.ps>.
28 * FIXME: GC :FULL T seems to be unable to recover a lot of unused
29 * space. After cold init is complete, GC :FULL T gets us down to
30 * about 44 Mb total used, but PURIFY gets us down to about 17 Mb
41 #include "interrupt.h"
48 /* a function defined externally in assembly language, called from
50 void do_pending_interrupt(void);
56 /* the number of actual generations. (The number of 'struct
57 * generation' objects is one more than this, because one serves as
58 * scratch when GC'ing.) */
59 #define NUM_GENERATIONS 6
61 /* Should we use page protection to help avoid the scavenging of pages
62 * that don't have pointers to younger generations? */
63 boolean enable_page_protection = 1;
65 /* Should we unmap a page and re-mmap it to have it zero filled? */
66 #if defined(__FreeBSD__) || defined(__OpenBSD__)
67 /* Note: this can waste a lot of swap on FreeBSD so don't unmap there.
69 * Presumably this behavior exists on OpenBSD too, so don't unmap
70 * there either. -- WHN 20000727 */
71 boolean gencgc_unmap_zero = 0;
73 boolean gencgc_unmap_zero = 1;
76 /* the minimum size (in bytes) for a large object*/
77 unsigned large_object_size = 4 * 4096;
79 /* Should we filter stack/register pointers? This could reduce the
80 * number of invalid pointers accepted. KLUDGE: It will probably
81 * degrades interrupt safety during object initialization. */
82 boolean enable_pointer_filter = 1;
88 #define gc_abort() lose("GC invariant lost, file \"%s\", line %d", \
91 /* FIXME: In CMU CL, this was "#if 0" with no explanation. Find out
92 * how much it costs to make it "#if 1". If it's not too expensive,
95 #define gc_assert(ex) do { \
96 if (!(ex)) gc_abort(); \
102 /* the verbosity level. All non-error messages are disabled at level 0;
103 * and only a few rare messages are printed at level 1. */
104 unsigned gencgc_verbose = (QSHOW ? 1 : 0);
106 /* FIXME: At some point enable the various error-checking things below
107 * and see what they say. */
109 /* We hunt for pointers to old-space, when GCing generations >= verify_gen.
110 * Set verify_gens to NUM_GENERATIONS to disable this kind of check. */
111 int verify_gens = NUM_GENERATIONS;
113 /* Should we do a pre-scan verify of generation 0 before it's GCed? */
114 boolean pre_verify_gen_0 = 0;
116 /* Should we check for bad pointers after gc_free_heap is called
117 * from Lisp PURIFY? */
118 boolean verify_after_free_heap = 0;
120 /* Should we print a note when code objects are found in the dynamic space
121 * during a heap verify? */
122 boolean verify_dynamic_code_check = 0;
124 /* Should we check code objects for fixup errors after they are transported? */
125 boolean check_code_fixups = 0;
127 /* Should we check that newly allocated regions are zero filled? */
128 boolean gencgc_zero_check = 0;
130 /* Should we check that the free space is zero filled? */
131 boolean gencgc_enable_verify_zero_fill = 0;
133 /* Should we check that free pages are zero filled during gc_free_heap
134 * called after Lisp PURIFY? */
135 boolean gencgc_zero_check_during_free_heap = 0;
138 * GC structures and variables
141 /* the total bytes allocated. These are seen by Lisp DYNAMIC-USAGE. */
142 unsigned long bytes_allocated = 0;
143 static unsigned long auto_gc_trigger = 0;
145 /* the source and destination generations. These are set before a GC starts
147 static int from_space;
148 static int new_space;
150 /* FIXME: It would be nice to use this symbolic constant instead of
151 * bare 4096 almost everywhere. We could also use an assertion that
152 * it's equal to getpagesize(). */
153 #define PAGE_BYTES 4096
155 /* An array of page structures is statically allocated.
156 * This helps quickly map between an address its page structure.
157 * NUM_PAGES is set from the size of the dynamic space. */
158 struct page page_table[NUM_PAGES];
160 /* To map addresses to page structures the address of the first page
162 static void *heap_base = NULL;
164 /* Calculate the start address for the given page number. */
166 *page_address(int page_num)
168 return (heap_base + (page_num * 4096));
171 /* Find the page index within the page_table for the given
172 * address. Return -1 on failure. */
174 find_page_index(void *addr)
176 int index = addr-heap_base;
179 index = ((unsigned int)index)/4096;
180 if (index < NUM_PAGES)
187 /* a structure to hold the state of a generation */
190 /* the first page that gc_alloc checks on its next call */
191 int alloc_start_page;
193 /* the first page that gc_alloc_unboxed checks on its next call */
194 int alloc_unboxed_start_page;
196 /* the first page that gc_alloc_large (boxed) considers on its next
197 * call. (Although it always allocates after the boxed_region.) */
198 int alloc_large_start_page;
200 /* the first page that gc_alloc_large (unboxed) considers on its
201 * next call. (Although it always allocates after the
202 * current_unboxed_region.) */
203 int alloc_large_unboxed_start_page;
205 /* the bytes allocated to this generation */
208 /* the number of bytes at which to trigger a GC */
211 /* to calculate a new level for gc_trigger */
212 int bytes_consed_between_gc;
214 /* the number of GCs since the last raise */
217 /* the average age after which a GC will raise objects to the
221 /* the cumulative sum of the bytes allocated to this generation. It is
222 * cleared after a GC on this generations, and update before new
223 * objects are added from a GC of a younger generation. Dividing by
224 * the bytes_allocated will give the average age of the memory in
225 * this generation since its last GC. */
226 int cum_sum_bytes_allocated;
228 /* a minimum average memory age before a GC will occur helps
229 * prevent a GC when a large number of new live objects have been
230 * added, in which case a GC could be a waste of time */
231 double min_av_mem_age;
234 /* an array of generation structures. There needs to be one more
235 * generation structure than actual generations as the oldest
236 * generation is temporarily raised then lowered. */
237 static struct generation generations[NUM_GENERATIONS+1];
239 /* the oldest generation that is will currently be GCed by default.
240 * Valid values are: 0, 1, ... (NUM_GENERATIONS-1)
242 * The default of (NUM_GENERATIONS-1) enables GC on all generations.
244 * Setting this to 0 effectively disables the generational nature of
245 * the GC. In some applications generational GC may not be useful
246 * because there are no long-lived objects.
248 * An intermediate value could be handy after moving long-lived data
249 * into an older generation so an unnecessary GC of this long-lived
250 * data can be avoided. */
251 unsigned int gencgc_oldest_gen_to_gc = NUM_GENERATIONS-1;
253 /* The maximum free page in the heap is maintained and used to update
254 * ALLOCATION_POINTER which is used by the room function to limit its
255 * search of the heap. XX Gencgc obviously needs to be better
256 * integrated with the Lisp code. */
257 static int last_free_page;
258 static int last_used_page = 0;
261 * miscellaneous heap functions
264 /* Count the number of pages which are write-protected within the
265 * given generation. */
267 count_write_protect_generation_pages(int generation)
272 for (i = 0; i < last_free_page; i++)
273 if ((page_table[i].allocated != FREE_PAGE)
274 && (page_table[i].gen == generation)
275 && (page_table[i].write_protected == 1))
280 /* Count the number of pages within the given generation */
282 count_generation_pages(int generation)
287 for (i = 0; i < last_free_page; i++)
288 if ((page_table[i].allocated != 0)
289 && (page_table[i].gen == generation))
294 /* Count the number of dont_move pages. */
296 count_dont_move_pages(void)
301 for (i = 0; i < last_free_page; i++)
302 if ((page_table[i].allocated != 0)
303 && (page_table[i].dont_move != 0))
308 /* Work through the pages and add up the number of bytes used for the
309 * given generation. */
311 generation_bytes_allocated (int gen)
316 for (i = 0; i < last_free_page; i++) {
317 if ((page_table[i].allocated != 0) && (page_table[i].gen == gen))
318 result += page_table[i].bytes_used;
323 /* Return the average age of the memory in a generation. */
325 gen_av_mem_age(int gen)
327 if (generations[gen].bytes_allocated == 0)
331 ((double)generations[gen].cum_sum_bytes_allocated)
332 / ((double)generations[gen].bytes_allocated);
335 /* The verbose argument controls how much to print: 0 for normal
336 * level of detail; 1 for debugging. */
338 print_generation_stats(int verbose) /* FIXME: should take FILE argument */
343 /* This code uses the FP instructions which may be set up for Lisp
344 * so they need to be saved and reset for C. */
347 /* number of generations to print */
349 gens = NUM_GENERATIONS+1;
351 gens = NUM_GENERATIONS;
353 /* Print the heap stats. */
355 " Generation Boxed Unboxed LB LUB Alloc Waste Trig WP GCs Mem-age\n");
357 for (i = 0; i < gens; i++) {
361 int large_boxed_cnt = 0;
362 int large_unboxed_cnt = 0;
364 for (j = 0; j < last_free_page; j++)
365 if (page_table[j].gen == i) {
367 /* Count the number of boxed pages within the given
369 if (page_table[j].allocated == BOXED_PAGE) {
370 if (page_table[j].large_object)
376 /* Count the number of unboxed pages within the given
378 if (page_table[j].allocated == UNBOXED_PAGE) {
379 if (page_table[j].large_object)
386 gc_assert(generations[i].bytes_allocated
387 == generation_bytes_allocated(i));
389 " %8d: %5d %5d %5d %5d %8d %5d %8d %4d %3d %7.4f\n",
391 boxed_cnt, unboxed_cnt, large_boxed_cnt, large_unboxed_cnt,
392 generations[i].bytes_allocated,
393 (count_generation_pages(i)*4096
394 - generations[i].bytes_allocated),
395 generations[i].gc_trigger,
396 count_write_protect_generation_pages(i),
397 generations[i].num_gc,
400 fprintf(stderr," Total bytes allocated=%ld\n", bytes_allocated);
402 fpu_restore(fpu_state);
406 * allocation routines
410 * To support quick and inline allocation, regions of memory can be
411 * allocated and then allocated from with just a free pointer and a
412 * check against an end address.
414 * Since objects can be allocated to spaces with different properties
415 * e.g. boxed/unboxed, generation, ages; there may need to be many
416 * allocation regions.
418 * Each allocation region may be start within a partly used page. Many
419 * features of memory use are noted on a page wise basis, e.g. the
420 * generation; so if a region starts within an existing allocated page
421 * it must be consistent with this page.
423 * During the scavenging of the newspace, objects will be transported
424 * into an allocation region, and pointers updated to point to this
425 * allocation region. It is possible that these pointers will be
426 * scavenged again before the allocation region is closed, e.g. due to
427 * trans_list which jumps all over the place to cleanup the list. It
428 * is important to be able to determine properties of all objects
429 * pointed to when scavenging, e.g to detect pointers to the oldspace.
430 * Thus it's important that the allocation regions have the correct
431 * properties set when allocated, and not just set when closed. The
432 * region allocation routines return regions with the specified
433 * properties, and grab all the pages, setting their properties
434 * appropriately, except that the amount used is not known.
436 * These regions are used to support quicker allocation using just a
437 * free pointer. The actual space used by the region is not reflected
438 * in the pages tables until it is closed. It can't be scavenged until
441 * When finished with the region it should be closed, which will
442 * update the page tables for the actual space used returning unused
443 * space. Further it may be noted in the new regions which is
444 * necessary when scavenging the newspace.
446 * Large objects may be allocated directly without an allocation
447 * region, the page tables are updated immediately.
449 * Unboxed objects don't contain pointers to other objects and so
450 * don't need scavenging. Further they can't contain pointers to
451 * younger generations so WP is not needed. By allocating pages to
452 * unboxed objects the whole page never needs scavenging or
453 * write-protecting. */
455 /* We are only using two regions at present. Both are for the current
456 * newspace generation. */
457 struct alloc_region boxed_region;
458 struct alloc_region unboxed_region;
460 /* XX hack. Current Lisp code uses the following. Need copying in/out. */
461 void *current_region_free_pointer;
462 void *current_region_end_addr;
464 /* The generation currently being allocated to. */
465 static int gc_alloc_generation;
467 /* Find a new region with room for at least the given number of bytes.
469 * It starts looking at the current generation's alloc_start_page. So
470 * may pick up from the previous region if there is enough space. This
471 * keeps the allocation contiguous when scavenging the newspace.
473 * The alloc_region should have been closed by a call to
474 * gc_alloc_update_page_tables, and will thus be in an empty state.
476 * To assist the scavenging functions write-protected pages are not
477 * used. Free pages should not be write-protected.
479 * It is critical to the conservative GC that the start of regions be
480 * known. To help achieve this only small regions are allocated at a
483 * During scavenging, pointers may be found to within the current
484 * region and the page generation must be set so that pointers to the
485 * from space can be recognized. Therefore the generation of pages in
486 * the region are set to gc_alloc_generation. To prevent another
487 * allocation call using the same pages, all the pages in the region
488 * are allocated, although they will initially be empty.
491 gc_alloc_new_region(int nbytes, int unboxed, struct alloc_region *alloc_region)
503 "/alloc_new_region for %d bytes from gen %d\n",
504 nbytes, gc_alloc_generation));
507 /* Check that the region is in a reset state. */
508 gc_assert((alloc_region->first_page == 0)
509 && (alloc_region->last_page == -1)
510 && (alloc_region->free_pointer == alloc_region->end_addr));
514 generations[gc_alloc_generation].alloc_unboxed_start_page;
517 generations[gc_alloc_generation].alloc_start_page;
520 /* Search for a contiguous free region of at least nbytes with the
521 * given properties: boxed/unboxed, generation. */
523 first_page = restart_page;
525 /* First search for a page with at least 32 bytes free, which is
526 * not write-protected, and which is not marked dont_move. */
527 while ((first_page < NUM_PAGES)
528 && (page_table[first_page].allocated != FREE_PAGE) /* not free page */
530 (page_table[first_page].allocated != UNBOXED_PAGE))
532 (page_table[first_page].allocated != BOXED_PAGE))
533 || (page_table[first_page].large_object != 0)
534 || (page_table[first_page].gen != gc_alloc_generation)
535 || (page_table[first_page].bytes_used >= (4096-32))
536 || (page_table[first_page].write_protected != 0)
537 || (page_table[first_page].dont_move != 0)))
539 /* Check for a failure. */
540 if (first_page >= NUM_PAGES) {
542 "Argh! gc_alloc_new_region failed on first_page, nbytes=%d.\n",
544 print_generation_stats(1);
548 gc_assert(page_table[first_page].write_protected == 0);
552 "/first_page=%d bytes_used=%d\n",
553 first_page, page_table[first_page].bytes_used));
556 /* Now search forward to calculate the available region size. It
557 * tries to keeps going until nbytes are found and the number of
558 * pages is greater than some level. This helps keep down the
559 * number of pages in a region. */
560 last_page = first_page;
561 bytes_found = 4096 - page_table[first_page].bytes_used;
563 while (((bytes_found < nbytes) || (num_pages < 2))
564 && (last_page < (NUM_PAGES-1))
565 && (page_table[last_page+1].allocated == FREE_PAGE)) {
569 gc_assert(page_table[last_page].write_protected == 0);
572 region_size = (4096 - page_table[first_page].bytes_used)
573 + 4096*(last_page-first_page);
575 gc_assert(bytes_found == region_size);
579 "/last_page=%d bytes_found=%d num_pages=%d\n",
580 last_page, bytes_found, num_pages));
583 restart_page = last_page + 1;
584 } while ((restart_page < NUM_PAGES) && (bytes_found < nbytes));
586 /* Check for a failure. */
587 if ((restart_page >= NUM_PAGES) && (bytes_found < nbytes)) {
589 "Argh! gc_alloc_new_region failed on restart_page, nbytes=%d.\n",
591 print_generation_stats(1);
597 "/gc_alloc_new_region gen %d: %d bytes: pages %d to %d: addr=%x\n",
602 page_address(first_page)));
605 /* Set up the alloc_region. */
606 alloc_region->first_page = first_page;
607 alloc_region->last_page = last_page;
608 alloc_region->start_addr = page_table[first_page].bytes_used
609 + page_address(first_page);
610 alloc_region->free_pointer = alloc_region->start_addr;
611 alloc_region->end_addr = alloc_region->start_addr + bytes_found;
613 if (gencgc_zero_check) {
615 for (p = (int *)alloc_region->start_addr;
616 p < (int *)alloc_region->end_addr; p++) {
618 /* KLUDGE: It would be nice to use %lx and explicit casts
619 * (long) in code like this, so that it is less likely to
620 * break randomly when running on a machine with different
621 * word sizes. -- WHN 19991129 */
622 lose("The new region at %x is not zero.", p);
627 /* Set up the pages. */
629 /* The first page may have already been in use. */
630 if (page_table[first_page].bytes_used == 0) {
632 page_table[first_page].allocated = UNBOXED_PAGE;
634 page_table[first_page].allocated = BOXED_PAGE;
635 page_table[first_page].gen = gc_alloc_generation;
636 page_table[first_page].large_object = 0;
637 page_table[first_page].first_object_offset = 0;
641 gc_assert(page_table[first_page].allocated == UNBOXED_PAGE);
643 gc_assert(page_table[first_page].allocated == BOXED_PAGE);
644 gc_assert(page_table[first_page].gen == gc_alloc_generation);
645 gc_assert(page_table[first_page].large_object == 0);
647 for (i = first_page+1; i <= last_page; i++) {
649 page_table[i].allocated = UNBOXED_PAGE;
651 page_table[i].allocated = BOXED_PAGE;
652 page_table[i].gen = gc_alloc_generation;
653 page_table[i].large_object = 0;
654 /* This may not be necessary for unboxed regions (think it was
656 page_table[i].first_object_offset =
657 alloc_region->start_addr - page_address(i);
660 /* Bump up last_free_page. */
661 if (last_page+1 > last_free_page) {
662 last_free_page = last_page+1;
663 SetSymbolValue(ALLOCATION_POINTER,
664 (lispobj)(((char *)heap_base) + last_free_page*4096));
665 if (last_page+1 > last_used_page)
666 last_used_page = last_page+1;
670 /* If the record_new_objects flag is 2 then all new regions created
673 * If it's 1 then then it is only recorded if the first page of the
674 * current region is <= new_areas_ignore_page. This helps avoid
675 * unnecessary recording when doing full scavenge pass.
677 * The new_object structure holds the page, byte offset, and size of
678 * new regions of objects. Each new area is placed in the array of
679 * these structures pointer to by new_areas. new_areas_index holds the
680 * offset into new_areas.
682 * If new_area overflows NUM_NEW_AREAS then it stops adding them. The
683 * later code must detect this and handle it, probably by doing a full
684 * scavenge of a generation. */
685 #define NUM_NEW_AREAS 512
686 static int record_new_objects = 0;
687 static int new_areas_ignore_page;
693 static struct new_area (*new_areas)[];
694 static int new_areas_index;
697 /* Add a new area to new_areas. */
699 add_new_area(int first_page, int offset, int size)
701 unsigned new_area_start,c;
704 /* Ignore if full. */
705 if (new_areas_index >= NUM_NEW_AREAS)
708 switch (record_new_objects) {
712 if (first_page > new_areas_ignore_page)
721 new_area_start = 4096*first_page + offset;
723 /* Search backwards for a prior area that this follows from. If
724 found this will save adding a new area. */
725 for (i = new_areas_index-1, c = 0; (i >= 0) && (c < 8); i--, c++) {
727 4096*((*new_areas)[i].page)
728 + (*new_areas)[i].offset
729 + (*new_areas)[i].size;
731 "/add_new_area S1 %d %d %d %d\n",
732 i, c, new_area_start, area_end));*/
733 if (new_area_start == area_end) {
735 "/adding to [%d] %d %d %d with %d %d %d:\n",
737 (*new_areas)[i].page,
738 (*new_areas)[i].offset,
739 (*new_areas)[i].size,
743 (*new_areas)[i].size += size;
747 /*FSHOW((stderr, "/add_new_area S1 %d %d %d\n", i, c, new_area_start));*/
749 (*new_areas)[new_areas_index].page = first_page;
750 (*new_areas)[new_areas_index].offset = offset;
751 (*new_areas)[new_areas_index].size = size;
753 "/new_area %d page %d offset %d size %d\n",
754 new_areas_index, first_page, offset, size));*/
757 /* Note the max new_areas used. */
758 if (new_areas_index > max_new_areas)
759 max_new_areas = new_areas_index;
762 /* Update the tables for the alloc_region. The region maybe added to
765 * When done the alloc_region is set up so that the next quick alloc
766 * will fail safely and thus a new region will be allocated. Further
767 * it is safe to try to re-update the page table of this reset
770 gc_alloc_update_page_tables(int unboxed, struct alloc_region *alloc_region)
776 int orig_first_page_bytes_used;
782 "/gc_alloc_update_page_tables to gen %d:\n",
783 gc_alloc_generation));
786 first_page = alloc_region->first_page;
788 /* Catch an unused alloc_region. */
789 if ((first_page == 0) && (alloc_region->last_page == -1))
792 next_page = first_page+1;
794 /* Skip if no bytes were allocated */
795 if (alloc_region->free_pointer != alloc_region->start_addr) {
796 orig_first_page_bytes_used = page_table[first_page].bytes_used;
798 gc_assert(alloc_region->start_addr == (page_address(first_page) + page_table[first_page].bytes_used));
800 /* All the pages used need to be updated */
802 /* Update the first page. */
804 /* If the page was free then set up the gen, and
805 first_object_offset. */
806 if (page_table[first_page].bytes_used == 0)
807 gc_assert(page_table[first_page].first_object_offset == 0);
810 gc_assert(page_table[first_page].allocated == UNBOXED_PAGE);
812 gc_assert(page_table[first_page].allocated == BOXED_PAGE);
813 gc_assert(page_table[first_page].gen == gc_alloc_generation);
814 gc_assert(page_table[first_page].large_object == 0);
818 /* Calc. the number of bytes used in this page. This is not always
819 the number of new bytes, unless it was free. */
821 if ((bytes_used = (alloc_region->free_pointer - page_address(first_page)))>4096) {
825 page_table[first_page].bytes_used = bytes_used;
826 byte_cnt += bytes_used;
829 /* All the rest of the pages should be free. Need to set their
830 first_object_offset pointer to the start of the region, and set
834 gc_assert(page_table[next_page].allocated == UNBOXED_PAGE);
836 gc_assert(page_table[next_page].allocated == BOXED_PAGE);
837 gc_assert(page_table[next_page].bytes_used == 0);
838 gc_assert(page_table[next_page].gen == gc_alloc_generation);
839 gc_assert(page_table[next_page].large_object == 0);
841 gc_assert(page_table[next_page].first_object_offset ==
842 alloc_region->start_addr - page_address(next_page));
844 /* Calculate the number of bytes used in this page. */
846 if ((bytes_used = (alloc_region->free_pointer
847 - page_address(next_page)))>4096) {
851 page_table[next_page].bytes_used = bytes_used;
852 byte_cnt += bytes_used;
857 region_size = alloc_region->free_pointer - alloc_region->start_addr;
858 bytes_allocated += region_size;
859 generations[gc_alloc_generation].bytes_allocated += region_size;
861 gc_assert((byte_cnt- orig_first_page_bytes_used) == region_size);
863 /* Set the generations alloc restart page to the last page of
866 generations[gc_alloc_generation].alloc_unboxed_start_page =
869 generations[gc_alloc_generation].alloc_start_page = next_page-1;
871 /* Add the region to the new_areas if requested. */
873 add_new_area(first_page,orig_first_page_bytes_used, region_size);
877 "/gc_alloc_update_page_tables update %d bytes to gen %d\n",
879 gc_alloc_generation));
883 /* No bytes allocated. Unallocate the first_page if there are 0
885 if (page_table[first_page].bytes_used == 0)
886 page_table[first_page].allocated = FREE_PAGE;
888 /* Unallocate any unused pages. */
889 while (next_page <= alloc_region->last_page) {
890 gc_assert(page_table[next_page].bytes_used == 0);
891 page_table[next_page].allocated = FREE_PAGE;
895 /* Reset the alloc_region. */
896 alloc_region->first_page = 0;
897 alloc_region->last_page = -1;
898 alloc_region->start_addr = page_address(0);
899 alloc_region->free_pointer = page_address(0);
900 alloc_region->end_addr = page_address(0);
903 static inline void *gc_quick_alloc(int nbytes);
905 /* Allocate a possibly large object. */
907 *gc_alloc_large(int nbytes, int unboxed, struct alloc_region *alloc_region)
915 int orig_first_page_bytes_used;
920 int large = (nbytes >= large_object_size);
924 FSHOW((stderr, "/alloc_large %d\n", nbytes));
929 "/gc_alloc_large for %d bytes from gen %d\n",
930 nbytes, gc_alloc_generation));
933 /* If the object is small, and there is room in the current region
934 then allocation it in the current region. */
936 && ((alloc_region->end_addr-alloc_region->free_pointer) >= nbytes))
937 return gc_quick_alloc(nbytes);
939 /* Search for a contiguous free region of at least nbytes. If it's a
940 large object then align it on a page boundary by searching for a
943 /* To allow the allocation of small objects without the danger of
944 using a page in the current boxed region, the search starts after
945 the current boxed free region. XX could probably keep a page
946 index ahead of the current region and bumped up here to save a
947 lot of re-scanning. */
949 restart_page = generations[gc_alloc_generation].alloc_large_unboxed_start_page;
951 restart_page = generations[gc_alloc_generation].alloc_large_start_page;
952 if (restart_page <= alloc_region->last_page)
953 restart_page = alloc_region->last_page+1;
956 first_page = restart_page;
959 while ((first_page < NUM_PAGES)
960 && (page_table[first_page].allocated != FREE_PAGE))
963 while ((first_page < NUM_PAGES)
964 && (page_table[first_page].allocated != FREE_PAGE)
966 (page_table[first_page].allocated != UNBOXED_PAGE))
968 (page_table[first_page].allocated != BOXED_PAGE))
969 || (page_table[first_page].large_object != 0)
970 || (page_table[first_page].gen != gc_alloc_generation)
971 || (page_table[first_page].bytes_used >= (4096-32))
972 || (page_table[first_page].write_protected != 0)
973 || (page_table[first_page].dont_move != 0)))
976 if (first_page >= NUM_PAGES) {
978 "Argh! gc_alloc_large failed (first_page), nbytes=%d.\n",
980 print_generation_stats(1);
984 gc_assert(page_table[first_page].write_protected == 0);
988 "/first_page=%d bytes_used=%d\n",
989 first_page, page_table[first_page].bytes_used));
992 last_page = first_page;
993 bytes_found = 4096 - page_table[first_page].bytes_used;
995 while ((bytes_found < nbytes)
996 && (last_page < (NUM_PAGES-1))
997 && (page_table[last_page+1].allocated == FREE_PAGE)) {
1000 bytes_found += 4096;
1001 gc_assert(page_table[last_page].write_protected == 0);
1004 region_size = (4096 - page_table[first_page].bytes_used)
1005 + 4096*(last_page-first_page);
1007 gc_assert(bytes_found == region_size);
1011 "/last_page=%d bytes_found=%d num_pages=%d\n",
1012 last_page, bytes_found, num_pages));
1015 restart_page = last_page + 1;
1016 } while ((restart_page < NUM_PAGES) && (bytes_found < nbytes));
1018 /* Check for a failure */
1019 if ((restart_page >= NUM_PAGES) && (bytes_found < nbytes)) {
1021 "Argh! gc_alloc_large failed (restart_page), nbytes=%d.\n",
1023 print_generation_stats(1);
1030 "/gc_alloc_large gen %d: %d of %d bytes: from pages %d to %d: addr=%x\n",
1031 gc_alloc_generation,
1036 page_address(first_page)));
1039 gc_assert(first_page > alloc_region->last_page);
1041 generations[gc_alloc_generation].alloc_large_unboxed_start_page =
1044 generations[gc_alloc_generation].alloc_large_start_page = last_page;
1046 /* Set up the pages. */
1047 orig_first_page_bytes_used = page_table[first_page].bytes_used;
1049 /* If the first page was free then set up the gen, and
1050 * first_object_offset. */
1051 if (page_table[first_page].bytes_used == 0) {
1053 page_table[first_page].allocated = UNBOXED_PAGE;
1055 page_table[first_page].allocated = BOXED_PAGE;
1056 page_table[first_page].gen = gc_alloc_generation;
1057 page_table[first_page].first_object_offset = 0;
1058 page_table[first_page].large_object = large;
1062 gc_assert(page_table[first_page].allocated == UNBOXED_PAGE);
1064 gc_assert(page_table[first_page].allocated == BOXED_PAGE);
1065 gc_assert(page_table[first_page].gen == gc_alloc_generation);
1066 gc_assert(page_table[first_page].large_object == large);
1070 /* Calc. the number of bytes used in this page. This is not
1071 * always the number of new bytes, unless it was free. */
1073 if ((bytes_used = nbytes+orig_first_page_bytes_used) > 4096) {
1077 page_table[first_page].bytes_used = bytes_used;
1078 byte_cnt += bytes_used;
1080 next_page = first_page+1;
1082 /* All the rest of the pages should be free. We need to set their
1083 * first_object_offset pointer to the start of the region, and
1084 * set the bytes_used. */
1086 gc_assert(page_table[next_page].allocated == FREE_PAGE);
1087 gc_assert(page_table[next_page].bytes_used == 0);
1089 page_table[next_page].allocated = UNBOXED_PAGE;
1091 page_table[next_page].allocated = BOXED_PAGE;
1092 page_table[next_page].gen = gc_alloc_generation;
1093 page_table[next_page].large_object = large;
1095 page_table[next_page].first_object_offset =
1096 orig_first_page_bytes_used - 4096*(next_page-first_page);
1098 /* Calculate the number of bytes used in this page. */
1100 if ((bytes_used=(nbytes+orig_first_page_bytes_used)-byte_cnt) > 4096) {
1104 page_table[next_page].bytes_used = bytes_used;
1105 byte_cnt += bytes_used;
1110 gc_assert((byte_cnt-orig_first_page_bytes_used) == nbytes);
1112 bytes_allocated += nbytes;
1113 generations[gc_alloc_generation].bytes_allocated += nbytes;
1115 /* Add the region to the new_areas if requested. */
1117 add_new_area(first_page,orig_first_page_bytes_used,nbytes);
1119 /* Bump up last_free_page */
1120 if (last_page+1 > last_free_page) {
1121 last_free_page = last_page+1;
1122 SetSymbolValue(ALLOCATION_POINTER,
1123 (lispobj)(((char *)heap_base) + last_free_page*4096));
1124 if (last_page+1 > last_used_page)
1125 last_used_page = last_page+1;
1128 return((void *)(page_address(first_page)+orig_first_page_bytes_used));
1131 /* Allocate bytes from the boxed_region. It first checks if there is
1132 * room, if not then it calls gc_alloc_new_region to find a new region
1133 * with enough space. A pointer to the start of the region is returned. */
1135 *gc_alloc(int nbytes)
1137 void *new_free_pointer;
1139 /* FSHOW((stderr, "/gc_alloc %d\n", nbytes)); */
1141 /* Check whether there is room in the current alloc region. */
1142 new_free_pointer = boxed_region.free_pointer + nbytes;
1144 if (new_free_pointer <= boxed_region.end_addr) {
1145 /* If so then allocate from the current alloc region. */
1146 void *new_obj = boxed_region.free_pointer;
1147 boxed_region.free_pointer = new_free_pointer;
1149 /* Check whether the alloc region is almost empty. */
1150 if ((boxed_region.end_addr - boxed_region.free_pointer) <= 32) {
1151 /* If so finished with the current region. */
1152 gc_alloc_update_page_tables(0, &boxed_region);
1153 /* Set up a new region. */
1154 gc_alloc_new_region(32, 0, &boxed_region);
1156 return((void *)new_obj);
1159 /* Else not enough free space in the current region. */
1161 /* If there some room left in the current region, enough to be worth
1162 * saving, then allocate a large object. */
1163 /* FIXME: "32" should be a named parameter. */
1164 if ((boxed_region.end_addr-boxed_region.free_pointer) > 32)
1165 return gc_alloc_large(nbytes, 0, &boxed_region);
1167 /* Else find a new region. */
1169 /* Finished with the current region. */
1170 gc_alloc_update_page_tables(0, &boxed_region);
1172 /* Set up a new region. */
1173 gc_alloc_new_region(nbytes, 0, &boxed_region);
1175 /* Should now be enough room. */
1177 /* Check whether there is room in the current region. */
1178 new_free_pointer = boxed_region.free_pointer + nbytes;
1180 if (new_free_pointer <= boxed_region.end_addr) {
1181 /* If so then allocate from the current region. */
1182 void *new_obj = boxed_region.free_pointer;
1183 boxed_region.free_pointer = new_free_pointer;
1185 /* Check whether the current region is almost empty. */
1186 if ((boxed_region.end_addr - boxed_region.free_pointer) <= 32) {
1187 /* If so find, finished with the current region. */
1188 gc_alloc_update_page_tables(0, &boxed_region);
1190 /* Set up a new region. */
1191 gc_alloc_new_region(32, 0, &boxed_region);
1194 return((void *)new_obj);
1197 /* shouldn't happen */
1199 return((void *) NIL); /* dummy value: return something ... */
1202 /* Allocate space from the boxed_region. If there is not enough free
1203 * space then call gc_alloc to do the job. A pointer to the start of
1204 * the region is returned. */
1206 *gc_quick_alloc(int nbytes)
1208 void *new_free_pointer;
1210 /* Check whether there is room in the current region. */
1211 new_free_pointer = boxed_region.free_pointer + nbytes;
1213 if (new_free_pointer <= boxed_region.end_addr) {
1214 /* If so then allocate from the current region. */
1215 void *new_obj = boxed_region.free_pointer;
1216 boxed_region.free_pointer = new_free_pointer;
1217 return((void *)new_obj);
1220 /* Else call gc_alloc */
1221 return (gc_alloc(nbytes));
1224 /* Allocate space for the boxed object. If it is a large object then
1225 * do a large alloc else allocate from the current region. If there is
1226 * not enough free space then call gc_alloc to do the job. A pointer
1227 * to the start of the region is returned. */
1229 *gc_quick_alloc_large(int nbytes)
1231 void *new_free_pointer;
1233 if (nbytes >= large_object_size)
1234 return gc_alloc_large(nbytes, 0, &boxed_region);
1236 /* Check whether there is room in the current region. */
1237 new_free_pointer = boxed_region.free_pointer + nbytes;
1239 if (new_free_pointer <= boxed_region.end_addr) {
1240 /* If so then allocate from the current region. */
1241 void *new_obj = boxed_region.free_pointer;
1242 boxed_region.free_pointer = new_free_pointer;
1243 return((void *)new_obj);
1246 /* Else call gc_alloc */
1247 return (gc_alloc(nbytes));
1251 *gc_alloc_unboxed(int nbytes)
1253 void *new_free_pointer;
1256 FSHOW((stderr, "/gc_alloc_unboxed %d\n", nbytes));
1259 /* Check whether there is room in the current region. */
1260 new_free_pointer = unboxed_region.free_pointer + nbytes;
1262 if (new_free_pointer <= unboxed_region.end_addr) {
1263 /* If so then allocate from the current region. */
1264 void *new_obj = unboxed_region.free_pointer;
1265 unboxed_region.free_pointer = new_free_pointer;
1267 /* Check whether the current region is almost empty. */
1268 if ((unboxed_region.end_addr - unboxed_region.free_pointer) <= 32) {
1269 /* If so finished with the current region. */
1270 gc_alloc_update_page_tables(1, &unboxed_region);
1272 /* Set up a new region. */
1273 gc_alloc_new_region(32, 1, &unboxed_region);
1276 return((void *)new_obj);
1279 /* Else not enough free space in the current region. */
1281 /* If there is a bit of room left in the current region then
1282 allocate a large object. */
1283 if ((unboxed_region.end_addr-unboxed_region.free_pointer) > 32)
1284 return gc_alloc_large(nbytes,1,&unboxed_region);
1286 /* Else find a new region. */
1288 /* Finished with the current region. */
1289 gc_alloc_update_page_tables(1, &unboxed_region);
1291 /* Set up a new region. */
1292 gc_alloc_new_region(nbytes, 1, &unboxed_region);
1294 /* Should now be enough room. */
1296 /* Check whether there is room in the current region. */
1297 new_free_pointer = unboxed_region.free_pointer + nbytes;
1299 if (new_free_pointer <= unboxed_region.end_addr) {
1300 /* If so then allocate from the current region. */
1301 void *new_obj = unboxed_region.free_pointer;
1302 unboxed_region.free_pointer = new_free_pointer;
1304 /* Check whether the current region is almost empty. */
1305 if ((unboxed_region.end_addr - unboxed_region.free_pointer) <= 32) {
1306 /* If so find, finished with the current region. */
1307 gc_alloc_update_page_tables(1, &unboxed_region);
1309 /* Set up a new region. */
1310 gc_alloc_new_region(32, 1, &unboxed_region);
1313 return((void *)new_obj);
1316 /* shouldn't happen? */
1318 return((void *) NIL); /* dummy value: return something ... */
1322 *gc_quick_alloc_unboxed(int nbytes)
1324 void *new_free_pointer;
1326 /* Check whether there is room in the current region. */
1327 new_free_pointer = unboxed_region.free_pointer + nbytes;
1329 if (new_free_pointer <= unboxed_region.end_addr) {
1330 /* If so then allocate from the current region. */
1331 void *new_obj = unboxed_region.free_pointer;
1332 unboxed_region.free_pointer = new_free_pointer;
1334 return((void *)new_obj);
1337 /* Else call gc_alloc */
1338 return (gc_alloc_unboxed(nbytes));
1341 /* Allocate space for the object. If it is a large object then do a
1342 * large alloc else allocate from the current region. If there is not
1343 * enough free space then call gc_alloc to do the job.
1345 * A pointer to the start of the region is returned. */
1347 *gc_quick_alloc_large_unboxed(int nbytes)
1349 void *new_free_pointer;
1351 if (nbytes >= large_object_size)
1352 return gc_alloc_large(nbytes,1,&unboxed_region);
1354 /* Check whether there is room in the current region. */
1355 new_free_pointer = unboxed_region.free_pointer + nbytes;
1357 if (new_free_pointer <= unboxed_region.end_addr) {
1358 /* If so then allocate from the current region. */
1359 void *new_obj = unboxed_region.free_pointer;
1360 unboxed_region.free_pointer = new_free_pointer;
1362 return((void *)new_obj);
1365 /* Else call gc_alloc. */
1366 return (gc_alloc_unboxed(nbytes));
1370 * scavenging/transporting routines derived from gc.c in CMU CL ca. 18b
1373 static int (*scavtab[256])(lispobj *where, lispobj object);
1374 static lispobj (*transother[256])(lispobj object);
1375 static int (*sizetab[256])(lispobj *where);
1377 static struct weak_pointer *weak_pointers;
1379 #define CEILING(x,y) (((x) + ((y) - 1)) & (~((y) - 1)))
1385 static inline boolean
1386 from_space_p(lispobj obj)
1388 int page_index=(void*)obj - heap_base;
1389 return ((page_index >= 0)
1390 && ((page_index = ((unsigned int)page_index)/4096) < NUM_PAGES)
1391 && (page_table[page_index].gen == from_space));
1394 static inline boolean
1395 new_space_p(lispobj obj)
1397 int page_index = (void*)obj - heap_base;
1398 return ((page_index >= 0)
1399 && ((page_index = ((unsigned int)page_index)/4096) < NUM_PAGES)
1400 && (page_table[page_index].gen == new_space));
1407 /* to copy a boxed object */
1408 static inline lispobj
1409 copy_object(lispobj object, int nwords)
1413 lispobj *source, *dest;
1415 gc_assert(Pointerp(object));
1416 gc_assert(from_space_p(object));
1417 gc_assert((nwords & 0x01) == 0);
1419 /* Get tag of object. */
1420 tag = LowtagOf(object);
1422 /* Allocate space. */
1423 new = gc_quick_alloc(nwords*4);
1426 source = (lispobj *) PTR(object);
1428 /* Copy the object. */
1429 while (nwords > 0) {
1430 dest[0] = source[0];
1431 dest[1] = source[1];
1437 /* Return Lisp pointer of new object. */
1438 return ((lispobj) new) | tag;
1441 /* to copy a large boxed object. If the object is in a large object
1442 * region then it is simply promoted, else it is copied. If it's large
1443 * enough then it's copied to a large object region.
1445 * Vectors may have shrunk. If the object is not copied the space
1446 * needs to be reclaimed, and the page_tables corrected. */
1448 copy_large_object(lispobj object, int nwords)
1452 lispobj *source, *dest;
1455 gc_assert(Pointerp(object));
1456 gc_assert(from_space_p(object));
1457 gc_assert((nwords & 0x01) == 0);
1459 if ((nwords > 1024*1024) && gencgc_verbose) {
1460 FSHOW((stderr, "/copy_large_object: %d bytes\n", nwords*4));
1463 /* Check whether it's a large object. */
1464 first_page = find_page_index((void *)object);
1465 gc_assert(first_page >= 0);
1467 if (page_table[first_page].large_object) {
1469 /* Promote the object. */
1471 int remaining_bytes;
1476 /* Note: Any page write-protection must be removed, else a
1477 * later scavenge_newspace may incorrectly not scavenge these
1478 * pages. This would not be necessary if they are added to the
1479 * new areas, but let's do it for them all (they'll probably
1480 * be written anyway?). */
1482 gc_assert(page_table[first_page].first_object_offset == 0);
1484 next_page = first_page;
1485 remaining_bytes = nwords*4;
1486 while (remaining_bytes > 4096) {
1487 gc_assert(page_table[next_page].gen == from_space);
1488 gc_assert(page_table[next_page].allocated == BOXED_PAGE);
1489 gc_assert(page_table[next_page].large_object);
1490 gc_assert(page_table[next_page].first_object_offset==
1491 -4096*(next_page-first_page));
1492 gc_assert(page_table[next_page].bytes_used == 4096);
1494 page_table[next_page].gen = new_space;
1496 /* Remove any write-protection. We should be able to rely
1497 * on the write-protect flag to avoid redundant calls. */
1498 if (page_table[next_page].write_protected) {
1499 os_protect(page_address(next_page), 4096, OS_VM_PROT_ALL);
1500 page_table[next_page].write_protected = 0;
1502 remaining_bytes -= 4096;
1506 /* Now only one page remains, but the object may have shrunk
1507 * so there may be more unused pages which will be freed. */
1509 /* The object may have shrunk but shouldn't have grown. */
1510 gc_assert(page_table[next_page].bytes_used >= remaining_bytes);
1512 page_table[next_page].gen = new_space;
1513 gc_assert(page_table[next_page].allocated = BOXED_PAGE);
1515 /* Adjust the bytes_used. */
1516 old_bytes_used = page_table[next_page].bytes_used;
1517 page_table[next_page].bytes_used = remaining_bytes;
1519 bytes_freed = old_bytes_used - remaining_bytes;
1521 /* Free any remaining pages; needs care. */
1523 while ((old_bytes_used == 4096) &&
1524 (page_table[next_page].gen == from_space) &&
1525 (page_table[next_page].allocated == BOXED_PAGE) &&
1526 page_table[next_page].large_object &&
1527 (page_table[next_page].first_object_offset ==
1528 -(next_page - first_page)*4096)) {
1529 /* Checks out OK, free the page. Don't need to both zeroing
1530 * pages as this should have been done before shrinking the
1531 * object. These pages shouldn't be write-protected as they
1532 * should be zero filled. */
1533 gc_assert(page_table[next_page].write_protected == 0);
1535 old_bytes_used = page_table[next_page].bytes_used;
1536 page_table[next_page].allocated = FREE_PAGE;
1537 page_table[next_page].bytes_used = 0;
1538 bytes_freed += old_bytes_used;
1542 if ((bytes_freed > 0) && gencgc_verbose)
1543 FSHOW((stderr, "/copy_large_boxed bytes_freed=%d\n", bytes_freed));
1545 generations[from_space].bytes_allocated -= 4*nwords + bytes_freed;
1546 generations[new_space].bytes_allocated += 4*nwords;
1547 bytes_allocated -= bytes_freed;
1549 /* Add the region to the new_areas if requested. */
1550 add_new_area(first_page,0,nwords*4);
1554 /* Get tag of object. */
1555 tag = LowtagOf(object);
1557 /* Allocate space. */
1558 new = gc_quick_alloc_large(nwords*4);
1561 source = (lispobj *) PTR(object);
1563 /* Copy the object. */
1564 while (nwords > 0) {
1565 dest[0] = source[0];
1566 dest[1] = source[1];
1572 /* Return Lisp pointer of new object. */
1573 return ((lispobj) new) | tag;
1577 /* to copy unboxed objects */
1578 static inline lispobj
1579 copy_unboxed_object(lispobj object, int nwords)
1583 lispobj *source, *dest;
1585 gc_assert(Pointerp(object));
1586 gc_assert(from_space_p(object));
1587 gc_assert((nwords & 0x01) == 0);
1589 /* Get tag of object. */
1590 tag = LowtagOf(object);
1592 /* Allocate space. */
1593 new = gc_quick_alloc_unboxed(nwords*4);
1596 source = (lispobj *) PTR(object);
1598 /* Copy the object. */
1599 while (nwords > 0) {
1600 dest[0] = source[0];
1601 dest[1] = source[1];
1607 /* Return Lisp pointer of new object. */
1608 return ((lispobj) new) | tag;
1611 /* to copy large unboxed objects
1613 * If the object is in a large object region then it is simply
1614 * promoted, else it is copied. If it's large enough then it's copied
1615 * to a large object region.
1617 * Bignums and vectors may have shrunk. If the object is not copied
1618 * the space needs to be reclaimed, and the page_tables corrected.
1620 * KLUDGE: There's a lot of cut-and-paste duplication between this
1621 * function and copy_large_object(..). -- WHN 20000619 */
1623 copy_large_unboxed_object(lispobj object, int nwords)
1627 lispobj *source, *dest;
1630 gc_assert(Pointerp(object));
1631 gc_assert(from_space_p(object));
1632 gc_assert((nwords & 0x01) == 0);
1634 if ((nwords > 1024*1024) && gencgc_verbose)
1635 FSHOW((stderr, "/copy_large_unboxed_object: %d bytes\n", nwords*4));
1637 /* Check whether it's a large object. */
1638 first_page = find_page_index((void *)object);
1639 gc_assert(first_page >= 0);
1641 if (page_table[first_page].large_object) {
1642 /* Promote the object. Note: Unboxed objects may have been
1643 * allocated to a BOXED region so it may be necessary to
1644 * change the region to UNBOXED. */
1645 int remaining_bytes;
1650 gc_assert(page_table[first_page].first_object_offset == 0);
1652 next_page = first_page;
1653 remaining_bytes = nwords*4;
1654 while (remaining_bytes > 4096) {
1655 gc_assert(page_table[next_page].gen == from_space);
1656 gc_assert((page_table[next_page].allocated == UNBOXED_PAGE)
1657 || (page_table[next_page].allocated == BOXED_PAGE));
1658 gc_assert(page_table[next_page].large_object);
1659 gc_assert(page_table[next_page].first_object_offset==
1660 -4096*(next_page-first_page));
1661 gc_assert(page_table[next_page].bytes_used == 4096);
1663 page_table[next_page].gen = new_space;
1664 page_table[next_page].allocated = UNBOXED_PAGE;
1665 remaining_bytes -= 4096;
1669 /* Now only one page remains, but the object may have shrunk so
1670 * there may be more unused pages which will be freed. */
1672 /* Object may have shrunk but shouldn't have grown - check. */
1673 gc_assert(page_table[next_page].bytes_used >= remaining_bytes);
1675 page_table[next_page].gen = new_space;
1676 page_table[next_page].allocated = UNBOXED_PAGE;
1678 /* Adjust the bytes_used. */
1679 old_bytes_used = page_table[next_page].bytes_used;
1680 page_table[next_page].bytes_used = remaining_bytes;
1682 bytes_freed = old_bytes_used - remaining_bytes;
1684 /* Free any remaining pages; needs care. */
1686 while ((old_bytes_used == 4096) &&
1687 (page_table[next_page].gen == from_space) &&
1688 ((page_table[next_page].allocated == UNBOXED_PAGE)
1689 || (page_table[next_page].allocated == BOXED_PAGE)) &&
1690 page_table[next_page].large_object &&
1691 (page_table[next_page].first_object_offset ==
1692 -(next_page - first_page)*4096)) {
1693 /* Checks out OK, free the page. Don't need to both zeroing
1694 * pages as this should have been done before shrinking the
1695 * object. These pages shouldn't be write-protected, even if
1696 * boxed they should be zero filled. */
1697 gc_assert(page_table[next_page].write_protected == 0);
1699 old_bytes_used = page_table[next_page].bytes_used;
1700 page_table[next_page].allocated = FREE_PAGE;
1701 page_table[next_page].bytes_used = 0;
1702 bytes_freed += old_bytes_used;
1706 if ((bytes_freed > 0) && gencgc_verbose)
1708 "/copy_large_unboxed bytes_freed=%d\n",
1711 generations[from_space].bytes_allocated -= 4*nwords + bytes_freed;
1712 generations[new_space].bytes_allocated += 4*nwords;
1713 bytes_allocated -= bytes_freed;
1718 /* Get tag of object. */
1719 tag = LowtagOf(object);
1721 /* Allocate space. */
1722 new = gc_quick_alloc_large_unboxed(nwords*4);
1725 source = (lispobj *) PTR(object);
1727 /* Copy the object. */
1728 while (nwords > 0) {
1729 dest[0] = source[0];
1730 dest[1] = source[1];
1736 /* Return Lisp pointer of new object. */
1737 return ((lispobj) new) | tag;
1745 #define DIRECT_SCAV 0
1747 /* FIXME: Most calls end up going to a little trouble to compute an
1748 * 'nwords' value. The system might be a little simpler if this
1749 * function used an 'end' parameter instead. */
1751 scavenge(lispobj *start, long nwords)
1753 while (nwords > 0) {
1758 int words_scavenged;
1762 /* FSHOW((stderr, "Scavenge: %p, %ld\n", start, nwords)); */
1764 gc_assert(object != 0x01); /* not a forwarding pointer */
1767 type = TypeOf(object);
1768 words_scavenged = (scavtab[type])(start, object);
1770 if (Pointerp(object)) {
1771 /* It's a pointer. */
1772 if (from_space_p(object)) {
1773 /* It currently points to old space. Check for a forwarding
1775 lispobj *ptr = (lispobj *)PTR(object);
1776 lispobj first_word = *ptr;
1778 if (first_word == 0x01) {
1779 /* Yes, there's a forwarding pointer. */
1781 words_scavenged = 1;
1784 /* Scavenge that pointer. */
1785 words_scavenged = (scavtab[TypeOf(object)])(start, object);
1787 /* It points somewhere other than oldspace. Leave it alone. */
1788 words_scavenged = 1;
1791 if ((object & 3) == 0) {
1792 /* It's a fixnum: really easy.. */
1793 words_scavenged = 1;
1795 /* It's some sort of header object or another. */
1796 words_scavenged = (scavtab[TypeOf(object)])(start, object);
1801 start += words_scavenged;
1802 nwords -= words_scavenged;
1804 gc_assert(nwords == 0);
1809 * code and code-related objects
1812 #define RAW_ADDR_OFFSET (6*sizeof(lispobj) - type_FunctionPointer)
1814 static lispobj trans_function_header(lispobj object);
1815 static lispobj trans_boxed(lispobj object);
1819 scav_function_pointer(lispobj *where, lispobj object)
1821 gc_assert(Pointerp(object));
1823 if (from_space_p(object)) {
1824 lispobj first, *first_pointer;
1826 /* object is a pointer into from space. Check to see whether
1827 * it has been forwarded. */
1828 first_pointer = (lispobj *) PTR(object);
1829 first = *first_pointer;
1831 if (first == 0x01) {
1833 *where = first_pointer[1];
1840 /* must transport object -- object may point to either a
1841 * function header, a closure function header, or to a
1842 * closure header. */
1844 type = TypeOf(first);
1846 case type_FunctionHeader:
1847 case type_ClosureFunctionHeader:
1848 copy = trans_function_header(object);
1851 copy = trans_boxed(object);
1855 if (copy != object) {
1856 /* Set forwarding pointer. */
1857 first_pointer[0] = 0x01;
1858 first_pointer[1] = copy;
1864 gc_assert(Pointerp(first));
1865 gc_assert(!from_space_p(first));
1873 scav_function_pointer(lispobj *where, lispobj object)
1875 lispobj *first_pointer;
1878 gc_assert(Pointerp(object));
1880 /* Object is a pointer into from space - no a FP. */
1881 first_pointer = (lispobj *) PTR(object);
1883 /* must transport object -- object may point to either a function
1884 * header, a closure function header, or to a closure header. */
1886 switch (TypeOf(*first_pointer)) {
1887 case type_FunctionHeader:
1888 case type_ClosureFunctionHeader:
1889 copy = trans_function_header(object);
1892 copy = trans_boxed(object);
1896 if (copy != object) {
1897 /* Set forwarding pointer */
1898 first_pointer[0] = 0x01;
1899 first_pointer[1] = copy;
1902 gc_assert(Pointerp(copy));
1903 gc_assert(!from_space_p(copy));
1911 /* Scan a x86 compiled code object, looking for possible fixups that
1912 * have been missed after a move.
1914 * Two types of fixups are needed:
1915 * 1. Absolute fixups to within the code object.
1916 * 2. Relative fixups to outside the code object.
1918 * Currently only absolute fixups to the constant vector, or to the
1919 * code area are checked. */
1921 sniff_code_object(struct code *code, unsigned displacement)
1923 int nheader_words, ncode_words, nwords;
1925 void *constants_start_addr, *constants_end_addr;
1926 void *code_start_addr, *code_end_addr;
1927 int fixup_found = 0;
1929 if (!check_code_fixups)
1932 /* It's ok if it's byte compiled code. The trace table offset will
1933 * be a fixnum if it's x86 compiled code - check. */
1934 if (code->trace_table_offset & 0x3) {
1935 FSHOW((stderr, "/Sniffing byte compiled code object at %x.\n", code));
1939 /* Else it's x86 machine code. */
1941 ncode_words = fixnum_value(code->code_size);
1942 nheader_words = HeaderValue(*(lispobj *)code);
1943 nwords = ncode_words + nheader_words;
1945 constants_start_addr = (void *)code + 5*4;
1946 constants_end_addr = (void *)code + nheader_words*4;
1947 code_start_addr = (void *)code + nheader_words*4;
1948 code_end_addr = (void *)code + nwords*4;
1950 /* Work through the unboxed code. */
1951 for (p = code_start_addr; p < code_end_addr; p++) {
1952 void *data = *(void **)p;
1953 unsigned d1 = *((unsigned char *)p - 1);
1954 unsigned d2 = *((unsigned char *)p - 2);
1955 unsigned d3 = *((unsigned char *)p - 3);
1956 unsigned d4 = *((unsigned char *)p - 4);
1957 unsigned d5 = *((unsigned char *)p - 5);
1958 unsigned d6 = *((unsigned char *)p - 6);
1960 /* Check for code references. */
1961 /* Check for a 32 bit word that looks like an absolute
1962 reference to within the code adea of the code object. */
1963 if ((data >= (code_start_addr-displacement))
1964 && (data < (code_end_addr-displacement))) {
1965 /* function header */
1967 && (((unsigned)p - 4 - 4*HeaderValue(*((unsigned *)p-1))) == (unsigned)code)) {
1968 /* Skip the function header */
1972 /* the case of PUSH imm32 */
1976 "/code ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1977 p, d6, d5, d4, d3, d2, d1, data));
1978 FSHOW((stderr, "/PUSH $0x%.8x\n", data));
1980 /* the case of MOV [reg-8],imm32 */
1982 && (d2==0x40 || d2==0x41 || d2==0x42 || d2==0x43
1983 || d2==0x45 || d2==0x46 || d2==0x47)
1987 "/code ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1988 p, d6, d5, d4, d3, d2, d1, data));
1989 FSHOW((stderr, "/MOV [reg-8],$0x%.8x\n", data));
1991 /* the case of LEA reg,[disp32] */
1992 if ((d2 == 0x8d) && ((d1 & 0xc7) == 5)) {
1995 "/code ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1996 p, d6, d5, d4, d3, d2, d1, data));
1997 FSHOW((stderr,"/LEA reg,[$0x%.8x]\n", data));
2001 /* Check for constant references. */
2002 /* Check for a 32 bit word that looks like an absolute
2003 reference to within the constant vector. Constant references
2005 if ((data >= (constants_start_addr-displacement))
2006 && (data < (constants_end_addr-displacement))
2007 && (((unsigned)data & 0x3) == 0)) {
2012 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
2013 p, d6, d5, d4, d3, d2, d1, data));
2014 FSHOW((stderr,"/MOV eax,0x%.8x\n", data));
2017 /* the case of MOV m32,EAX */
2021 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
2022 p, d6, d5, d4, d3, d2, d1, data));
2023 FSHOW((stderr, "/MOV 0x%.8x,eax\n", data));
2026 /* the case of CMP m32,imm32 */
2027 if ((d1 == 0x3d) && (d2 == 0x81)) {
2030 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
2031 p, d6, d5, d4, d3, d2, d1, data));
2033 FSHOW((stderr, "/CMP 0x%.8x,immed32\n", data));
2036 /* Check for a mod=00, r/m=101 byte. */
2037 if ((d1 & 0xc7) == 5) {
2042 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
2043 p, d6, d5, d4, d3, d2, d1, data));
2044 FSHOW((stderr,"/CMP 0x%.8x,reg\n", data));
2046 /* the case of CMP reg32,m32 */
2050 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
2051 p, d6, d5, d4, d3, d2, d1, data));
2052 FSHOW((stderr, "/CMP reg32,0x%.8x\n", data));
2054 /* the case of MOV m32,reg32 */
2058 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
2059 p, d6, d5, d4, d3, d2, d1, data));
2060 FSHOW((stderr, "/MOV 0x%.8x,reg32\n", data));
2062 /* the case of MOV reg32,m32 */
2066 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
2067 p, d6, d5, d4, d3, d2, d1, data));
2068 FSHOW((stderr, "/MOV reg32,0x%.8x\n", data));
2070 /* the case of LEA reg32,m32 */
2074 "abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
2075 p, d6, d5, d4, d3, d2, d1, data));
2076 FSHOW((stderr, "/LEA reg32,0x%.8x\n", data));
2082 /* If anything was found, print some information on the code
2086 "/compiled code object at %x: header words = %d, code words = %d\n",
2087 code, nheader_words, ncode_words));
2089 "/const start = %x, end = %x\n",
2090 constants_start_addr, constants_end_addr));
2092 "/code start = %x, end = %x\n",
2093 code_start_addr, code_end_addr));
2098 apply_code_fixups(struct code *old_code, struct code *new_code)
2100 int nheader_words, ncode_words, nwords;
2101 void *constants_start_addr, *constants_end_addr;
2102 void *code_start_addr, *code_end_addr;
2103 lispobj fixups = NIL;
2104 unsigned displacement = (unsigned)new_code - (unsigned)old_code;
2105 struct vector *fixups_vector;
2107 /* It's OK if it's byte compiled code. The trace table offset will
2108 * be a fixnum if it's x86 compiled code - check. */
2109 if (new_code->trace_table_offset & 0x3) {
2110 /* FSHOW((stderr, "/byte compiled code object at %x\n", new_code)); */
2114 /* Else it's x86 machine code. */
2115 ncode_words = fixnum_value(new_code->code_size);
2116 nheader_words = HeaderValue(*(lispobj *)new_code);
2117 nwords = ncode_words + nheader_words;
2119 "/compiled code object at %x: header words = %d, code words = %d\n",
2120 new_code, nheader_words, ncode_words)); */
2121 constants_start_addr = (void *)new_code + 5*4;
2122 constants_end_addr = (void *)new_code + nheader_words*4;
2123 code_start_addr = (void *)new_code + nheader_words*4;
2124 code_end_addr = (void *)new_code + nwords*4;
2127 "/const start = %x, end = %x\n",
2128 constants_start_addr,constants_end_addr));
2130 "/code start = %x; end = %x\n",
2131 code_start_addr,code_end_addr));
2134 /* The first constant should be a pointer to the fixups for this
2135 code objects. Check. */
2136 fixups = new_code->constants[0];
2138 /* It will be 0 or the unbound-marker if there are no fixups, and
2139 * will be an other pointer if it is valid. */
2140 if ((fixups == 0) || (fixups == type_UnboundMarker) || !Pointerp(fixups)) {
2141 /* Check for possible errors. */
2142 if (check_code_fixups)
2143 sniff_code_object(new_code, displacement);
2145 /*fprintf(stderr,"Fixups for code object not found!?\n");
2146 fprintf(stderr,"*** Compiled code object at %x: header_words=%d code_words=%d .\n",
2147 new_code, nheader_words, ncode_words);
2148 fprintf(stderr,"*** Const. start = %x; end= %x; Code start = %x; end = %x\n",
2149 constants_start_addr,constants_end_addr,
2150 code_start_addr,code_end_addr);*/
2154 fixups_vector = (struct vector *)PTR(fixups);
2156 /* Could be pointing to a forwarding pointer. */
2157 if (Pointerp(fixups) && (find_page_index((void*)fixups_vector) != -1)
2158 && (fixups_vector->header == 0x01)) {
2159 /* If so, then follow it. */
2160 /*SHOW("following pointer to a forwarding pointer");*/
2161 fixups_vector = (struct vector *)PTR((lispobj)fixups_vector->length);
2164 /*SHOW("got fixups");*/
2166 if (TypeOf(fixups_vector->header) == type_SimpleArrayUnsignedByte32) {
2167 /* Got the fixups for the code block. Now work through the vector,
2168 and apply a fixup at each address. */
2169 int length = fixnum_value(fixups_vector->length);
2171 for (i = 0; i < length; i++) {
2172 unsigned offset = fixups_vector->data[i];
2173 /* Now check the current value of offset. */
2174 unsigned old_value =
2175 *(unsigned *)((unsigned)code_start_addr + offset);
2177 /* If it's within the old_code object then it must be an
2178 * absolute fixup (relative ones are not saved) */
2179 if ((old_value >= (unsigned)old_code)
2180 && (old_value < ((unsigned)old_code + nwords*4)))
2181 /* So add the dispacement. */
2182 *(unsigned *)((unsigned)code_start_addr + offset) =
2183 old_value + displacement;
2185 /* It is outside the old code object so it must be a
2186 * relative fixup (absolute fixups are not saved). So
2187 * subtract the displacement. */
2188 *(unsigned *)((unsigned)code_start_addr + offset) =
2189 old_value - displacement;
2193 /* Check for possible errors. */
2194 if (check_code_fixups) {
2195 sniff_code_object(new_code,displacement);
2199 static struct code *
2200 trans_code(struct code *code)
2202 struct code *new_code;
2203 lispobj l_code, l_new_code;
2204 int nheader_words, ncode_words, nwords;
2205 unsigned long displacement;
2206 lispobj fheaderl, *prev_pointer;
2209 "\n/transporting code object located at 0x%08x\n",
2210 (unsigned long) code)); */
2212 /* If object has already been transported, just return pointer. */
2213 if (*((lispobj *)code) == 0x01)
2214 return (struct code*)(((lispobj *)code)[1]);
2216 gc_assert(TypeOf(code->header) == type_CodeHeader);
2218 /* Prepare to transport the code vector. */
2219 l_code = (lispobj) code | type_OtherPointer;
2221 ncode_words = fixnum_value(code->code_size);
2222 nheader_words = HeaderValue(code->header);
2223 nwords = ncode_words + nheader_words;
2224 nwords = CEILING(nwords, 2);
2226 l_new_code = copy_large_object(l_code, nwords);
2227 new_code = (struct code *) PTR(l_new_code);
2229 /* may not have been moved.. */
2230 if (new_code == code)
2233 displacement = l_new_code - l_code;
2237 "/old code object at 0x%08x, new code object at 0x%08x\n",
2238 (unsigned long) code,
2239 (unsigned long) new_code));
2240 FSHOW((stderr, "/Code object is %d words long.\n", nwords));
2243 /* Set forwarding pointer. */
2244 ((lispobj *)code)[0] = 0x01;
2245 ((lispobj *)code)[1] = l_new_code;
2247 /* Set forwarding pointers for all the function headers in the
2248 * code object. Also fix all self pointers. */
2250 fheaderl = code->entry_points;
2251 prev_pointer = &new_code->entry_points;
2253 while (fheaderl != NIL) {
2254 struct function *fheaderp, *nfheaderp;
2257 fheaderp = (struct function *) PTR(fheaderl);
2258 gc_assert(TypeOf(fheaderp->header) == type_FunctionHeader);
2260 /* Calculate the new function pointer and the new */
2261 /* function header. */
2262 nfheaderl = fheaderl + displacement;
2263 nfheaderp = (struct function *) PTR(nfheaderl);
2265 /* Set forwarding pointer. */
2266 ((lispobj *)fheaderp)[0] = 0x01;
2267 ((lispobj *)fheaderp)[1] = nfheaderl;
2269 /* Fix self pointer. */
2270 nfheaderp->self = nfheaderl + RAW_ADDR_OFFSET;
2272 *prev_pointer = nfheaderl;
2274 fheaderl = fheaderp->next;
2275 prev_pointer = &nfheaderp->next;
2278 /* sniff_code_object(new_code,displacement);*/
2279 apply_code_fixups(code,new_code);
2285 scav_code_header(lispobj *where, lispobj object)
2288 int nheader_words, ncode_words, nwords;
2290 struct function *fheaderp;
2292 code = (struct code *) where;
2293 ncode_words = fixnum_value(code->code_size);
2294 nheader_words = HeaderValue(object);
2295 nwords = ncode_words + nheader_words;
2296 nwords = CEILING(nwords, 2);
2298 /* Scavenge the boxed section of the code data block. */
2299 scavenge(where + 1, nheader_words - 1);
2301 /* Scavenge the boxed section of each function object in the */
2302 /* code data block. */
2303 fheaderl = code->entry_points;
2304 while (fheaderl != NIL) {
2305 fheaderp = (struct function *) PTR(fheaderl);
2306 gc_assert(TypeOf(fheaderp->header) == type_FunctionHeader);
2308 scavenge(&fheaderp->name, 1);
2309 scavenge(&fheaderp->arglist, 1);
2310 scavenge(&fheaderp->type, 1);
2312 fheaderl = fheaderp->next;
2319 trans_code_header(lispobj object)
2323 ncode = trans_code((struct code *) PTR(object));
2324 return (lispobj) ncode | type_OtherPointer;
2328 size_code_header(lispobj *where)
2331 int nheader_words, ncode_words, nwords;
2333 code = (struct code *) where;
2335 ncode_words = fixnum_value(code->code_size);
2336 nheader_words = HeaderValue(code->header);
2337 nwords = ncode_words + nheader_words;
2338 nwords = CEILING(nwords, 2);
2344 scav_return_pc_header(lispobj *where, lispobj object)
2346 lose("attempted to scavenge a return PC header where=0x%08x object=0x%08x",
2347 (unsigned long) where,
2348 (unsigned long) object);
2349 return 0; /* bogus return value to satisfy static type checking */
2353 trans_return_pc_header(lispobj object)
2355 struct function *return_pc;
2356 unsigned long offset;
2357 struct code *code, *ncode;
2359 SHOW("/trans_return_pc_header: Will this work?");
2361 return_pc = (struct function *) PTR(object);
2362 offset = HeaderValue(return_pc->header) * 4;
2364 /* Transport the whole code object. */
2365 code = (struct code *) ((unsigned long) return_pc - offset);
2366 ncode = trans_code(code);
2368 return ((lispobj) ncode + offset) | type_OtherPointer;
2371 /* On the 386, closures hold a pointer to the raw address instead of the
2372 * function object. */
2375 scav_closure_header(lispobj *where, lispobj object)
2377 struct closure *closure;
2380 closure = (struct closure *)where;
2381 fun = closure->function - RAW_ADDR_OFFSET;
2383 /* The function may have moved so update the raw address. But
2384 * don't write unnecessarily. */
2385 if (closure->function != fun + RAW_ADDR_OFFSET)
2386 closure->function = fun + RAW_ADDR_OFFSET;
2393 scav_function_header(lispobj *where, lispobj object)
2395 lose("attempted to scavenge a function header where=0x%08x object=0x%08x",
2396 (unsigned long) where,
2397 (unsigned long) object);
2398 return 0; /* bogus return value to satisfy static type checking */
2402 trans_function_header(lispobj object)
2404 struct function *fheader;
2405 unsigned long offset;
2406 struct code *code, *ncode;
2408 fheader = (struct function *) PTR(object);
2409 offset = HeaderValue(fheader->header) * 4;
2411 /* Transport the whole code object. */
2412 code = (struct code *) ((unsigned long) fheader - offset);
2413 ncode = trans_code(code);
2415 return ((lispobj) ncode + offset) | type_FunctionPointer;
2424 scav_instance_pointer(lispobj *where, lispobj object)
2426 if (from_space_p(object)) {
2427 lispobj first, *first_pointer;
2429 /* Object is a pointer into from space. Check to see */
2430 /* whether it has been forwarded. */
2431 first_pointer = (lispobj *) PTR(object);
2432 first = *first_pointer;
2434 if (first == 0x01) {
2436 first = first_pointer[1];
2438 first = trans_boxed(object);
2439 gc_assert(first != object);
2440 /* Set forwarding pointer. */
2441 first_pointer[0] = 0x01;
2442 first_pointer[1] = first;
2450 scav_instance_pointer(lispobj *where, lispobj object)
2452 lispobj copy, *first_pointer;
2454 /* Object is a pointer into from space - not a FP. */
2455 copy = trans_boxed(object);
2457 gc_assert(copy != object);
2459 first_pointer = (lispobj *) PTR(object);
2461 /* Set forwarding pointer. */
2462 first_pointer[0] = 0x01;
2463 first_pointer[1] = copy;
2474 static lispobj trans_list(lispobj object);
2478 scav_list_pointer(lispobj *where, lispobj object)
2480 /* KLUDGE: There's lots of cut-and-paste duplication between this
2481 * and scav_instance_pointer(..), scav_other_pointer(..), and
2482 * perhaps other functions too. -- WHN 20000620 */
2484 gc_assert(Pointerp(object));
2486 if (from_space_p(object)) {
2487 lispobj first, *first_pointer;
2489 /* Object is a pointer into from space. Check to see whether it has
2490 * been forwarded. */
2491 first_pointer = (lispobj *) PTR(object);
2492 first = *first_pointer;
2494 if (first == 0x01) {
2496 first = first_pointer[1];
2498 first = trans_list(object);
2500 /* Set forwarding pointer */
2501 first_pointer[0] = 0x01;
2502 first_pointer[1] = first;
2505 gc_assert(Pointerp(first));
2506 gc_assert(!from_space_p(first));
2513 scav_list_pointer(lispobj *where, lispobj object)
2515 lispobj first, *first_pointer;
2517 gc_assert(Pointerp(object));
2519 /* Object is a pointer into from space - not FP. */
2521 first = trans_list(object);
2522 gc_assert(first != object);
2524 first_pointer = (lispobj *) PTR(object);
2526 /* Set forwarding pointer */
2527 first_pointer[0] = 0x01;
2528 first_pointer[1] = first;
2530 gc_assert(Pointerp(first));
2531 gc_assert(!from_space_p(first));
2538 trans_list(lispobj object)
2540 lispobj new_list_pointer;
2541 struct cons *cons, *new_cons;
2544 gc_assert(from_space_p(object));
2546 cons = (struct cons *) PTR(object);
2548 /* Copy 'object'. */
2549 new_cons = (struct cons *) gc_quick_alloc(sizeof(struct cons));
2550 new_cons->car = cons->car;
2551 new_cons->cdr = cons->cdr; /* updated later */
2552 new_list_pointer = (lispobj)new_cons | LowtagOf(object);
2554 /* Grab the cdr before it is clobbered. */
2557 /* Set forwarding pointer (clobbers start of list). */
2559 cons->cdr = new_list_pointer;
2561 /* Try to linearize the list in the cdr direction to help reduce
2565 struct cons *cdr_cons, *new_cdr_cons;
2567 if (LowtagOf(cdr) != type_ListPointer || !from_space_p(cdr)
2568 || (*((lispobj *)PTR(cdr)) == 0x01))
2571 cdr_cons = (struct cons *) PTR(cdr);
2574 new_cdr_cons = (struct cons*) gc_quick_alloc(sizeof(struct cons));
2575 new_cdr_cons->car = cdr_cons->car;
2576 new_cdr_cons->cdr = cdr_cons->cdr;
2577 new_cdr = (lispobj)new_cdr_cons | LowtagOf(cdr);
2579 /* Grab the cdr before it is clobbered. */
2580 cdr = cdr_cons->cdr;
2582 /* Set forwarding pointer. */
2583 cdr_cons->car = 0x01;
2584 cdr_cons->cdr = new_cdr;
2586 /* Update the cdr of the last cons copied into new space to
2587 * keep the newspace scavenge from having to do it. */
2588 new_cons->cdr = new_cdr;
2590 new_cons = new_cdr_cons;
2593 return new_list_pointer;
2598 * scavenging and transporting other pointers
2603 scav_other_pointer(lispobj *where, lispobj object)
2605 gc_assert(Pointerp(object));
2607 if (from_space_p(object)) {
2608 lispobj first, *first_pointer;
2610 /* Object is a pointer into from space. Check to see */
2611 /* whether it has been forwarded. */
2612 first_pointer = (lispobj *) PTR(object);
2613 first = *first_pointer;
2615 if (first == 0x01) {
2617 first = first_pointer[1];
2620 first = (transother[TypeOf(first)])(object);
2622 if (first != object) {
2623 /* Set forwarding pointer */
2624 first_pointer[0] = 0x01;
2625 first_pointer[1] = first;
2630 gc_assert(Pointerp(first));
2631 gc_assert(!from_space_p(first));
2637 scav_other_pointer(lispobj *where, lispobj object)
2639 lispobj first, *first_pointer;
2641 gc_assert(Pointerp(object));
2643 /* Object is a pointer into from space - not FP. */
2644 first_pointer = (lispobj *) PTR(object);
2646 first = (transother[TypeOf(*first_pointer)])(object);
2648 if (first != object) {
2649 /* Set forwarding pointer. */
2650 first_pointer[0] = 0x01;
2651 first_pointer[1] = first;
2655 gc_assert(Pointerp(first));
2656 gc_assert(!from_space_p(first));
2664 * immediate, boxed, and unboxed objects
2668 size_pointer(lispobj *where)
2674 scav_immediate(lispobj *where, lispobj object)
2680 trans_immediate(lispobj object)
2682 lose("trying to transport an immediate");
2683 return NIL; /* bogus return value to satisfy static type checking */
2687 size_immediate(lispobj *where)
2694 scav_boxed(lispobj *where, lispobj object)
2700 trans_boxed(lispobj object)
2703 unsigned long length;
2705 gc_assert(Pointerp(object));
2707 header = *((lispobj *) PTR(object));
2708 length = HeaderValue(header) + 1;
2709 length = CEILING(length, 2);
2711 return copy_object(object, length);
2715 trans_boxed_large(lispobj object)
2718 unsigned long length;
2720 gc_assert(Pointerp(object));
2722 header = *((lispobj *) PTR(object));
2723 length = HeaderValue(header) + 1;
2724 length = CEILING(length, 2);
2726 return copy_large_object(object, length);
2730 size_boxed(lispobj *where)
2733 unsigned long length;
2736 length = HeaderValue(header) + 1;
2737 length = CEILING(length, 2);
2743 scav_fdefn(lispobj *where, lispobj object)
2745 struct fdefn *fdefn;
2747 fdefn = (struct fdefn *)where;
2749 /* FSHOW((stderr, "scav_fdefn, function = %p, raw_addr = %p\n",
2750 fdefn->function, fdefn->raw_addr)); */
2752 if ((char *)(fdefn->function + RAW_ADDR_OFFSET) == fdefn->raw_addr) {
2753 scavenge(where + 1, sizeof(struct fdefn)/sizeof(lispobj) - 1);
2755 /* Don't write unnecessarily. */
2756 if (fdefn->raw_addr != (char *)(fdefn->function + RAW_ADDR_OFFSET))
2757 fdefn->raw_addr = (char *)(fdefn->function + RAW_ADDR_OFFSET);
2759 return sizeof(struct fdefn) / sizeof(lispobj);
2766 scav_unboxed(lispobj *where, lispobj object)
2768 unsigned long length;
2770 length = HeaderValue(object) + 1;
2771 length = CEILING(length, 2);
2777 trans_unboxed(lispobj object)
2780 unsigned long length;
2783 gc_assert(Pointerp(object));
2785 header = *((lispobj *) PTR(object));
2786 length = HeaderValue(header) + 1;
2787 length = CEILING(length, 2);
2789 return copy_unboxed_object(object, length);
2793 trans_unboxed_large(lispobj object)
2796 unsigned long length;
2799 gc_assert(Pointerp(object));
2801 header = *((lispobj *) PTR(object));
2802 length = HeaderValue(header) + 1;
2803 length = CEILING(length, 2);
2805 return copy_large_unboxed_object(object, length);
2809 size_unboxed(lispobj *where)
2812 unsigned long length;
2815 length = HeaderValue(header) + 1;
2816 length = CEILING(length, 2);
2822 * vector-like objects
2825 #define NWORDS(x,y) (CEILING((x),(y)) / (y))
2828 scav_string(lispobj *where, lispobj object)
2830 struct vector *vector;
2833 /* NOTE: Strings contain one more byte of data than the length */
2834 /* slot indicates. */
2836 vector = (struct vector *) where;
2837 length = fixnum_value(vector->length) + 1;
2838 nwords = CEILING(NWORDS(length, 4) + 2, 2);
2844 trans_string(lispobj object)
2846 struct vector *vector;
2849 gc_assert(Pointerp(object));
2851 /* NOTE: A string contains one more byte of data (a terminating
2852 * '\0' to help when interfacing with C functions) than indicated
2853 * by the length slot. */
2855 vector = (struct vector *) PTR(object);
2856 length = fixnum_value(vector->length) + 1;
2857 nwords = CEILING(NWORDS(length, 4) + 2, 2);
2859 return copy_large_unboxed_object(object, nwords);
2863 size_string(lispobj *where)
2865 struct vector *vector;
2868 /* NOTE: A string contains one more byte of data (a terminating
2869 * '\0' to help when interfacing with C functions) than indicated
2870 * by the length slot. */
2872 vector = (struct vector *) where;
2873 length = fixnum_value(vector->length) + 1;
2874 nwords = CEILING(NWORDS(length, 4) + 2, 2);
2879 /* FIXME: What does this mean? */
2880 int gencgc_hash = 1;
2883 scav_vector(lispobj *where, lispobj object)
2885 unsigned int kv_length;
2887 unsigned int length = 0; /* (0 = dummy to stop GCC warning) */
2888 lispobj *hash_table;
2889 lispobj empty_symbol;
2890 unsigned int *index_vector = NULL; /* (NULL = dummy to stop GCC warning) */
2891 unsigned int *next_vector = NULL; /* (NULL = dummy to stop GCC warning) */
2892 unsigned int *hash_vector = NULL; /* (NULL = dummy to stop GCC warning) */
2894 unsigned next_vector_length = 0;
2896 /* FIXME: A comment explaining this would be nice. It looks as
2897 * though SB-VM:VECTOR-VALID-HASHING-SUBTYPE is set for EQ-based
2898 * hash tables in the Lisp HASH-TABLE code, and nowhere else. */
2899 if (HeaderValue(object) != subtype_VectorValidHashing)
2903 /* This is set for backward compatibility. FIXME: Do we need
2905 *where = (subtype_VectorMustRehash << type_Bits) | type_SimpleVector;
2909 kv_length = fixnum_value(where[1]);
2910 kv_vector = where + 2; /* Skip the header and length. */
2911 /*FSHOW((stderr,"/kv_length = %d\n", kv_length));*/
2913 /* Scavenge element 0, which may be a hash-table structure. */
2914 scavenge(where+2, 1);
2915 if (!Pointerp(where[2])) {
2916 lose("no pointer at %x in hash table", where[2]);
2918 hash_table = (lispobj *)PTR(where[2]);
2919 /*FSHOW((stderr,"/hash_table = %x\n", hash_table));*/
2920 if (TypeOf(hash_table[0]) != type_InstanceHeader) {
2921 lose("hash table not instance (%x at %x)", hash_table[0], hash_table);
2924 /* Scavenge element 1, which should be some internal symbol that
2925 * the hash table code reserves for marking empty slots. */
2926 scavenge(where+3, 1);
2927 if (!Pointerp(where[3])) {
2928 lose("not empty-hash-table-slot symbol pointer: %x", where[3]);
2930 empty_symbol = where[3];
2931 /* fprintf(stderr,"* empty_symbol = %x\n", empty_symbol);*/
2932 if (TypeOf(*(lispobj *)PTR(empty_symbol)) != type_SymbolHeader) {
2933 lose("not a symbol where empty-hash-table-slot symbol expected: %x",
2934 *(lispobj *)PTR(empty_symbol));
2937 /* Scavenge hash table, which will fix the positions of the other
2938 * needed objects. */
2939 scavenge(hash_table, 16);
2941 /* Cross-check the kv_vector. */
2942 if (where != (lispobj *)PTR(hash_table[9])) {
2943 lose("hash_table table!=this table %x", hash_table[9]);
2947 weak_p_obj = hash_table[10];
2951 lispobj index_vector_obj = hash_table[13];
2953 if (Pointerp(index_vector_obj) &&
2954 (TypeOf(*(lispobj *)PTR(index_vector_obj)) == type_SimpleArrayUnsignedByte32)) {
2955 index_vector = ((unsigned int *)PTR(index_vector_obj)) + 2;
2956 /*FSHOW((stderr, "/index_vector = %x\n",index_vector));*/
2957 length = fixnum_value(((unsigned int *)PTR(index_vector_obj))[1]);
2958 /*FSHOW((stderr, "/length = %d\n", length));*/
2960 lose("invalid index_vector %x", index_vector_obj);
2966 lispobj next_vector_obj = hash_table[14];
2968 if (Pointerp(next_vector_obj) &&
2969 (TypeOf(*(lispobj *)PTR(next_vector_obj)) == type_SimpleArrayUnsignedByte32)) {
2970 next_vector = ((unsigned int *)PTR(next_vector_obj)) + 2;
2971 /*FSHOW((stderr, "/next_vector = %x\n", next_vector));*/
2972 next_vector_length = fixnum_value(((unsigned int *)PTR(next_vector_obj))[1]);
2973 /*FSHOW((stderr, "/next_vector_length = %d\n", next_vector_length));*/
2975 lose("invalid next_vector %x", next_vector_obj);
2979 /* maybe hash vector */
2981 /* FIXME: This bare "15" offset should become a symbolic
2982 * expression of some sort. And all the other bare offsets
2983 * too. And the bare "16" in scavenge(hash_table, 16). And
2984 * probably other stuff too. Ugh.. */
2985 lispobj hash_vector_obj = hash_table[15];
2987 if (Pointerp(hash_vector_obj) &&
2988 (TypeOf(*(lispobj *)PTR(hash_vector_obj))
2989 == type_SimpleArrayUnsignedByte32)) {
2990 hash_vector = ((unsigned int *)PTR(hash_vector_obj)) + 2;
2991 /*FSHOW((stderr, "/hash_vector = %x\n", hash_vector));*/
2992 gc_assert(fixnum_value(((unsigned int *)PTR(hash_vector_obj))[1])
2993 == next_vector_length);
2996 /*FSHOW((stderr, "/no hash_vector: %x\n", hash_vector_obj));*/
3000 /* These lengths could be different as the index_vector can be a
3001 * different length from the others, a larger index_vector could help
3002 * reduce collisions. */
3003 gc_assert(next_vector_length*2 == kv_length);
3005 /* now all set up.. */
3007 /* Work through the KV vector. */
3010 for (i = 1; i < next_vector_length; i++) {
3011 lispobj old_key = kv_vector[2*i];
3012 unsigned int old_index = (old_key & 0x1fffffff)%length;
3014 /* Scavenge the key and value. */
3015 scavenge(&kv_vector[2*i],2);
3017 /* Check whether the key has moved and is EQ based. */
3019 lispobj new_key = kv_vector[2*i];
3020 unsigned int new_index = (new_key & 0x1fffffff)%length;
3022 if ((old_index != new_index) &&
3023 ((!hash_vector) || (hash_vector[i] == 0x80000000)) &&
3024 ((new_key != empty_symbol) ||
3025 (kv_vector[2*i] != empty_symbol))) {
3028 "* EQ key %d moved from %x to %x; index %d to %d\n",
3029 i, old_key, new_key, old_index, new_index));*/
3031 if (index_vector[old_index] != 0) {
3032 /*FSHOW((stderr, "/P1 %d\n", index_vector[old_index]));*/
3034 /* Unlink the key from the old_index chain. */
3035 if (index_vector[old_index] == i) {
3036 /*FSHOW((stderr, "/P2a %d\n", next_vector[i]));*/
3037 index_vector[old_index] = next_vector[i];
3038 /* Link it into the needing rehash chain. */
3039 next_vector[i] = fixnum_value(hash_table[11]);
3040 hash_table[11] = make_fixnum(i);
3043 unsigned prior = index_vector[old_index];
3044 unsigned next = next_vector[prior];
3046 /*FSHOW((stderr, "/P3a %d %d\n", prior, next));*/
3049 /*FSHOW((stderr, "/P3b %d %d\n", prior, next));*/
3052 next_vector[prior] = next_vector[next];
3053 /* Link it into the needing rehash
3056 fixnum_value(hash_table[11]);
3057 hash_table[11] = make_fixnum(next);
3062 next = next_vector[next];
3070 return (CEILING(kv_length + 2, 2));
3074 trans_vector(lispobj object)
3076 struct vector *vector;
3079 gc_assert(Pointerp(object));
3081 vector = (struct vector *) PTR(object);
3083 length = fixnum_value(vector->length);
3084 nwords = CEILING(length + 2, 2);
3086 return copy_large_object(object, nwords);
3090 size_vector(lispobj *where)
3092 struct vector *vector;
3095 vector = (struct vector *) where;
3096 length = fixnum_value(vector->length);
3097 nwords = CEILING(length + 2, 2);
3104 scav_vector_bit(lispobj *where, lispobj object)
3106 struct vector *vector;
3109 vector = (struct vector *) where;
3110 length = fixnum_value(vector->length);
3111 nwords = CEILING(NWORDS(length, 32) + 2, 2);
3117 trans_vector_bit(lispobj object)
3119 struct vector *vector;
3122 gc_assert(Pointerp(object));
3124 vector = (struct vector *) PTR(object);
3125 length = fixnum_value(vector->length);
3126 nwords = CEILING(NWORDS(length, 32) + 2, 2);
3128 return copy_large_unboxed_object(object, nwords);
3132 size_vector_bit(lispobj *where)
3134 struct vector *vector;
3137 vector = (struct vector *) where;
3138 length = fixnum_value(vector->length);
3139 nwords = CEILING(NWORDS(length, 32) + 2, 2);
3146 scav_vector_unsigned_byte_2(lispobj *where, lispobj object)
3148 struct vector *vector;
3151 vector = (struct vector *) where;
3152 length = fixnum_value(vector->length);
3153 nwords = CEILING(NWORDS(length, 16) + 2, 2);
3159 trans_vector_unsigned_byte_2(lispobj object)
3161 struct vector *vector;
3164 gc_assert(Pointerp(object));
3166 vector = (struct vector *) PTR(object);
3167 length = fixnum_value(vector->length);
3168 nwords = CEILING(NWORDS(length, 16) + 2, 2);
3170 return copy_large_unboxed_object(object, nwords);
3174 size_vector_unsigned_byte_2(lispobj *where)
3176 struct vector *vector;
3179 vector = (struct vector *) where;
3180 length = fixnum_value(vector->length);
3181 nwords = CEILING(NWORDS(length, 16) + 2, 2);
3188 scav_vector_unsigned_byte_4(lispobj *where, lispobj object)
3190 struct vector *vector;
3193 vector = (struct vector *) where;
3194 length = fixnum_value(vector->length);
3195 nwords = CEILING(NWORDS(length, 8) + 2, 2);
3201 trans_vector_unsigned_byte_4(lispobj object)
3203 struct vector *vector;
3206 gc_assert(Pointerp(object));
3208 vector = (struct vector *) PTR(object);
3209 length = fixnum_value(vector->length);
3210 nwords = CEILING(NWORDS(length, 8) + 2, 2);
3212 return copy_large_unboxed_object(object, nwords);
3216 size_vector_unsigned_byte_4(lispobj *where)
3218 struct vector *vector;
3221 vector = (struct vector *) where;
3222 length = fixnum_value(vector->length);
3223 nwords = CEILING(NWORDS(length, 8) + 2, 2);
3229 scav_vector_unsigned_byte_8(lispobj *where, lispobj object)
3231 struct vector *vector;
3234 vector = (struct vector *) where;
3235 length = fixnum_value(vector->length);
3236 nwords = CEILING(NWORDS(length, 4) + 2, 2);
3242 trans_vector_unsigned_byte_8(lispobj object)
3244 struct vector *vector;
3247 gc_assert(Pointerp(object));
3249 vector = (struct vector *) PTR(object);
3250 length = fixnum_value(vector->length);
3251 nwords = CEILING(NWORDS(length, 4) + 2, 2);
3253 return copy_large_unboxed_object(object, nwords);
3257 size_vector_unsigned_byte_8(lispobj *where)
3259 struct vector *vector;
3262 vector = (struct vector *) where;
3263 length = fixnum_value(vector->length);
3264 nwords = CEILING(NWORDS(length, 4) + 2, 2);
3271 scav_vector_unsigned_byte_16(lispobj *where, lispobj object)
3273 struct vector *vector;
3276 vector = (struct vector *) where;
3277 length = fixnum_value(vector->length);
3278 nwords = CEILING(NWORDS(length, 2) + 2, 2);
3284 trans_vector_unsigned_byte_16(lispobj object)
3286 struct vector *vector;
3289 gc_assert(Pointerp(object));
3291 vector = (struct vector *) PTR(object);
3292 length = fixnum_value(vector->length);
3293 nwords = CEILING(NWORDS(length, 2) + 2, 2);
3295 return copy_large_unboxed_object(object, nwords);
3299 size_vector_unsigned_byte_16(lispobj *where)
3301 struct vector *vector;
3304 vector = (struct vector *) where;
3305 length = fixnum_value(vector->length);
3306 nwords = CEILING(NWORDS(length, 2) + 2, 2);
3312 scav_vector_unsigned_byte_32(lispobj *where, lispobj object)
3314 struct vector *vector;
3317 vector = (struct vector *) where;
3318 length = fixnum_value(vector->length);
3319 nwords = CEILING(length + 2, 2);
3325 trans_vector_unsigned_byte_32(lispobj object)
3327 struct vector *vector;
3330 gc_assert(Pointerp(object));
3332 vector = (struct vector *) PTR(object);
3333 length = fixnum_value(vector->length);
3334 nwords = CEILING(length + 2, 2);
3336 return copy_large_unboxed_object(object, nwords);
3340 size_vector_unsigned_byte_32(lispobj *where)
3342 struct vector *vector;
3345 vector = (struct vector *) where;
3346 length = fixnum_value(vector->length);
3347 nwords = CEILING(length + 2, 2);
3353 scav_vector_single_float(lispobj *where, lispobj object)
3355 struct vector *vector;
3358 vector = (struct vector *) where;
3359 length = fixnum_value(vector->length);
3360 nwords = CEILING(length + 2, 2);
3366 trans_vector_single_float(lispobj object)
3368 struct vector *vector;
3371 gc_assert(Pointerp(object));
3373 vector = (struct vector *) PTR(object);
3374 length = fixnum_value(vector->length);
3375 nwords = CEILING(length + 2, 2);
3377 return copy_large_unboxed_object(object, nwords);
3381 size_vector_single_float(lispobj *where)
3383 struct vector *vector;
3386 vector = (struct vector *) where;
3387 length = fixnum_value(vector->length);
3388 nwords = CEILING(length + 2, 2);
3394 scav_vector_double_float(lispobj *where, lispobj object)
3396 struct vector *vector;
3399 vector = (struct vector *) where;
3400 length = fixnum_value(vector->length);
3401 nwords = CEILING(length * 2 + 2, 2);
3407 trans_vector_double_float(lispobj object)
3409 struct vector *vector;
3412 gc_assert(Pointerp(object));
3414 vector = (struct vector *) PTR(object);
3415 length = fixnum_value(vector->length);
3416 nwords = CEILING(length * 2 + 2, 2);
3418 return copy_large_unboxed_object(object, nwords);
3422 size_vector_double_float(lispobj *where)
3424 struct vector *vector;
3427 vector = (struct vector *) where;
3428 length = fixnum_value(vector->length);
3429 nwords = CEILING(length * 2 + 2, 2);
3434 #ifdef type_SimpleArrayLongFloat
3436 scav_vector_long_float(lispobj *where, lispobj object)
3438 struct vector *vector;
3441 vector = (struct vector *) where;
3442 length = fixnum_value(vector->length);
3443 nwords = CEILING(length * 3 + 2, 2);
3449 trans_vector_long_float(lispobj object)
3451 struct vector *vector;
3454 gc_assert(Pointerp(object));
3456 vector = (struct vector *) PTR(object);
3457 length = fixnum_value(vector->length);
3458 nwords = CEILING(length * 3 + 2, 2);
3460 return copy_large_unboxed_object(object, nwords);
3464 size_vector_long_float(lispobj *where)
3466 struct vector *vector;
3469 vector = (struct vector *) where;
3470 length = fixnum_value(vector->length);
3471 nwords = CEILING(length * 3 + 2, 2);
3478 #ifdef type_SimpleArrayComplexSingleFloat
3480 scav_vector_complex_single_float(lispobj *where, lispobj object)
3482 struct vector *vector;
3485 vector = (struct vector *) where;
3486 length = fixnum_value(vector->length);
3487 nwords = CEILING(length * 2 + 2, 2);
3493 trans_vector_complex_single_float(lispobj object)
3495 struct vector *vector;
3498 gc_assert(Pointerp(object));
3500 vector = (struct vector *) PTR(object);
3501 length = fixnum_value(vector->length);
3502 nwords = CEILING(length * 2 + 2, 2);
3504 return copy_large_unboxed_object(object, nwords);
3508 size_vector_complex_single_float(lispobj *where)
3510 struct vector *vector;
3513 vector = (struct vector *) where;
3514 length = fixnum_value(vector->length);
3515 nwords = CEILING(length * 2 + 2, 2);
3521 #ifdef type_SimpleArrayComplexDoubleFloat
3523 scav_vector_complex_double_float(lispobj *where, lispobj object)
3525 struct vector *vector;
3528 vector = (struct vector *) where;
3529 length = fixnum_value(vector->length);
3530 nwords = CEILING(length * 4 + 2, 2);
3536 trans_vector_complex_double_float(lispobj object)
3538 struct vector *vector;
3541 gc_assert(Pointerp(object));
3543 vector = (struct vector *) PTR(object);
3544 length = fixnum_value(vector->length);
3545 nwords = CEILING(length * 4 + 2, 2);
3547 return copy_large_unboxed_object(object, nwords);
3551 size_vector_complex_double_float(lispobj *where)
3553 struct vector *vector;
3556 vector = (struct vector *) where;
3557 length = fixnum_value(vector->length);
3558 nwords = CEILING(length * 4 + 2, 2);
3565 #ifdef type_SimpleArrayComplexLongFloat
3567 scav_vector_complex_long_float(lispobj *where, lispobj object)
3569 struct vector *vector;
3572 vector = (struct vector *) where;
3573 length = fixnum_value(vector->length);
3574 nwords = CEILING(length * 6 + 2, 2);
3580 trans_vector_complex_long_float(lispobj object)
3582 struct vector *vector;
3585 gc_assert(Pointerp(object));
3587 vector = (struct vector *) PTR(object);
3588 length = fixnum_value(vector->length);
3589 nwords = CEILING(length * 6 + 2, 2);
3591 return copy_large_unboxed_object(object, nwords);
3595 size_vector_complex_long_float(lispobj *where)
3597 struct vector *vector;
3600 vector = (struct vector *) where;
3601 length = fixnum_value(vector->length);
3602 nwords = CEILING(length * 6 + 2, 2);
3613 /* XX This is a hack adapted from cgc.c. These don't work too well with the
3614 * gencgc as a list of the weak pointers is maintained within the
3615 * objects which causes writes to the pages. A limited attempt is made
3616 * to avoid unnecessary writes, but this needs a re-think. */
3618 #define WEAK_POINTER_NWORDS \
3619 CEILING((sizeof(struct weak_pointer) / sizeof(lispobj)), 2)
3622 scav_weak_pointer(lispobj *where, lispobj object)
3624 struct weak_pointer *wp = weak_pointers;
3625 /* Push the weak pointer onto the list of weak pointers.
3626 * Do I have to watch for duplicates? Originally this was
3627 * part of trans_weak_pointer but that didn't work in the
3628 * case where the WP was in a promoted region.
3631 /* Check whether it's already in the list. */
3632 while (wp != NULL) {
3633 if (wp == (struct weak_pointer*)where) {
3639 /* Add it to the start of the list. */
3640 wp = (struct weak_pointer*)where;
3641 if (wp->next != weak_pointers) {
3642 wp->next = weak_pointers;
3644 /*SHOW("avoided write to weak pointer");*/
3649 /* Do not let GC scavenge the value slot of the weak pointer.
3650 * (That is why it is a weak pointer.) */
3652 return WEAK_POINTER_NWORDS;
3656 trans_weak_pointer(lispobj object)
3659 /* struct weak_pointer *wp; */
3661 gc_assert(Pointerp(object));
3663 #if defined(DEBUG_WEAK)
3664 FSHOW((stderr, "Transporting weak pointer from 0x%08x\n", object));
3667 /* Need to remember where all the weak pointers are that have */
3668 /* been transported so they can be fixed up in a post-GC pass. */
3670 copy = copy_object(object, WEAK_POINTER_NWORDS);
3671 /* wp = (struct weak_pointer *) PTR(copy);*/
3674 /* Push the weak pointer onto the list of weak pointers. */
3675 /* wp->next = weak_pointers;
3676 * weak_pointers = wp;*/
3682 size_weak_pointer(lispobj *where)
3684 return WEAK_POINTER_NWORDS;
3687 void scan_weak_pointers(void)
3689 struct weak_pointer *wp;
3690 for (wp = weak_pointers; wp != NULL; wp = wp->next) {
3691 lispobj value = wp->value;
3692 lispobj *first_pointer;
3694 first_pointer = (lispobj *)PTR(value);
3697 FSHOW((stderr, "/weak pointer at 0x%08x\n", (unsigned long) wp));
3698 FSHOW((stderr, "/value: 0x%08x\n", (unsigned long) value));
3701 if (Pointerp(value) && from_space_p(value)) {
3702 /* Now, we need to check whether the object has been forwarded. If
3703 * it has been, the weak pointer is still good and needs to be
3704 * updated. Otherwise, the weak pointer needs to be nil'ed
3706 if (first_pointer[0] == 0x01) {
3707 wp->value = first_pointer[1];
3723 scav_lose(lispobj *where, lispobj object)
3725 lose("no scavenge function for object 0x%08x", (unsigned long) object);
3726 return 0; /* bogus return value to satisfy static type checking */
3730 trans_lose(lispobj object)
3732 lose("no transport function for object 0x%08x", (unsigned long) object);
3733 return NIL; /* bogus return value to satisfy static type checking */
3737 size_lose(lispobj *where)
3739 lose("no size function for object at 0x%08x", (unsigned long) where);
3740 return 1; /* bogus return value to satisfy static type checking */
3744 gc_init_tables(void)
3748 /* Set default value in all slots of scavenge table. */
3749 for (i = 0; i < 256; i++) { /* FIXME: bare constant length, ick! */
3750 scavtab[i] = scav_lose;
3753 /* For each type which can be selected by the low 3 bits of the tag
3754 * alone, set multiple entries in our 8-bit scavenge table (one for each
3755 * possible value of the high 5 bits). */
3756 for (i = 0; i < 32; i++) { /* FIXME: bare constant length, ick! */
3757 scavtab[type_EvenFixnum|(i<<3)] = scav_immediate;
3758 scavtab[type_FunctionPointer|(i<<3)] = scav_function_pointer;
3759 /* OtherImmediate0 */
3760 scavtab[type_ListPointer|(i<<3)] = scav_list_pointer;
3761 scavtab[type_OddFixnum|(i<<3)] = scav_immediate;
3762 scavtab[type_InstancePointer|(i<<3)] = scav_instance_pointer;
3763 /* OtherImmediate1 */
3764 scavtab[type_OtherPointer|(i<<3)] = scav_other_pointer;
3767 /* Other-pointer types (those selected by all eight bits of the tag) get
3768 * one entry each in the scavenge table. */
3769 scavtab[type_Bignum] = scav_unboxed;
3770 scavtab[type_Ratio] = scav_boxed;
3771 scavtab[type_SingleFloat] = scav_unboxed;
3772 scavtab[type_DoubleFloat] = scav_unboxed;
3773 #ifdef type_LongFloat
3774 scavtab[type_LongFloat] = scav_unboxed;
3776 scavtab[type_Complex] = scav_boxed;
3777 #ifdef type_ComplexSingleFloat
3778 scavtab[type_ComplexSingleFloat] = scav_unboxed;
3780 #ifdef type_ComplexDoubleFloat
3781 scavtab[type_ComplexDoubleFloat] = scav_unboxed;
3783 #ifdef type_ComplexLongFloat
3784 scavtab[type_ComplexLongFloat] = scav_unboxed;
3786 scavtab[type_SimpleArray] = scav_boxed;
3787 scavtab[type_SimpleString] = scav_string;
3788 scavtab[type_SimpleBitVector] = scav_vector_bit;
3789 scavtab[type_SimpleVector] = scav_vector;
3790 scavtab[type_SimpleArrayUnsignedByte2] = scav_vector_unsigned_byte_2;
3791 scavtab[type_SimpleArrayUnsignedByte4] = scav_vector_unsigned_byte_4;
3792 scavtab[type_SimpleArrayUnsignedByte8] = scav_vector_unsigned_byte_8;
3793 scavtab[type_SimpleArrayUnsignedByte16] = scav_vector_unsigned_byte_16;
3794 scavtab[type_SimpleArrayUnsignedByte32] = scav_vector_unsigned_byte_32;
3795 #ifdef type_SimpleArraySignedByte8
3796 scavtab[type_SimpleArraySignedByte8] = scav_vector_unsigned_byte_8;
3798 #ifdef type_SimpleArraySignedByte16
3799 scavtab[type_SimpleArraySignedByte16] = scav_vector_unsigned_byte_16;
3801 #ifdef type_SimpleArraySignedByte30
3802 scavtab[type_SimpleArraySignedByte30] = scav_vector_unsigned_byte_32;
3804 #ifdef type_SimpleArraySignedByte32
3805 scavtab[type_SimpleArraySignedByte32] = scav_vector_unsigned_byte_32;
3807 scavtab[type_SimpleArraySingleFloat] = scav_vector_single_float;
3808 scavtab[type_SimpleArrayDoubleFloat] = scav_vector_double_float;
3809 #ifdef type_SimpleArrayLongFloat
3810 scavtab[type_SimpleArrayLongFloat] = scav_vector_long_float;
3812 #ifdef type_SimpleArrayComplexSingleFloat
3813 scavtab[type_SimpleArrayComplexSingleFloat] = scav_vector_complex_single_float;
3815 #ifdef type_SimpleArrayComplexDoubleFloat
3816 scavtab[type_SimpleArrayComplexDoubleFloat] = scav_vector_complex_double_float;
3818 #ifdef type_SimpleArrayComplexLongFloat
3819 scavtab[type_SimpleArrayComplexLongFloat] = scav_vector_complex_long_float;
3821 scavtab[type_ComplexString] = scav_boxed;
3822 scavtab[type_ComplexBitVector] = scav_boxed;
3823 scavtab[type_ComplexVector] = scav_boxed;
3824 scavtab[type_ComplexArray] = scav_boxed;
3825 scavtab[type_CodeHeader] = scav_code_header;
3826 /*scavtab[type_FunctionHeader] = scav_function_header;*/
3827 /*scavtab[type_ClosureFunctionHeader] = scav_function_header;*/
3828 /*scavtab[type_ReturnPcHeader] = scav_return_pc_header;*/
3830 scavtab[type_ClosureHeader] = scav_closure_header;
3831 scavtab[type_FuncallableInstanceHeader] = scav_closure_header;
3832 scavtab[type_ByteCodeFunction] = scav_closure_header;
3833 scavtab[type_ByteCodeClosure] = scav_closure_header;
3835 scavtab[type_ClosureHeader] = scav_boxed;
3836 scavtab[type_FuncallableInstanceHeader] = scav_boxed;
3837 scavtab[type_ByteCodeFunction] = scav_boxed;
3838 scavtab[type_ByteCodeClosure] = scav_boxed;
3840 scavtab[type_ValueCellHeader] = scav_boxed;
3841 scavtab[type_SymbolHeader] = scav_boxed;
3842 scavtab[type_BaseChar] = scav_immediate;
3843 scavtab[type_Sap] = scav_unboxed;
3844 scavtab[type_UnboundMarker] = scav_immediate;
3845 scavtab[type_WeakPointer] = scav_weak_pointer;
3846 scavtab[type_InstanceHeader] = scav_boxed;
3847 scavtab[type_Fdefn] = scav_fdefn;
3849 /* transport other table, initialized same way as scavtab */
3850 for (i = 0; i < 256; i++)
3851 transother[i] = trans_lose;
3852 transother[type_Bignum] = trans_unboxed;
3853 transother[type_Ratio] = trans_boxed;
3854 transother[type_SingleFloat] = trans_unboxed;
3855 transother[type_DoubleFloat] = trans_unboxed;
3856 #ifdef type_LongFloat
3857 transother[type_LongFloat] = trans_unboxed;
3859 transother[type_Complex] = trans_boxed;
3860 #ifdef type_ComplexSingleFloat
3861 transother[type_ComplexSingleFloat] = trans_unboxed;
3863 #ifdef type_ComplexDoubleFloat
3864 transother[type_ComplexDoubleFloat] = trans_unboxed;
3866 #ifdef type_ComplexLongFloat
3867 transother[type_ComplexLongFloat] = trans_unboxed;
3869 transother[type_SimpleArray] = trans_boxed_large;
3870 transother[type_SimpleString] = trans_string;
3871 transother[type_SimpleBitVector] = trans_vector_bit;
3872 transother[type_SimpleVector] = trans_vector;
3873 transother[type_SimpleArrayUnsignedByte2] = trans_vector_unsigned_byte_2;
3874 transother[type_SimpleArrayUnsignedByte4] = trans_vector_unsigned_byte_4;
3875 transother[type_SimpleArrayUnsignedByte8] = trans_vector_unsigned_byte_8;
3876 transother[type_SimpleArrayUnsignedByte16] = trans_vector_unsigned_byte_16;
3877 transother[type_SimpleArrayUnsignedByte32] = trans_vector_unsigned_byte_32;
3878 #ifdef type_SimpleArraySignedByte8
3879 transother[type_SimpleArraySignedByte8] = trans_vector_unsigned_byte_8;
3881 #ifdef type_SimpleArraySignedByte16
3882 transother[type_SimpleArraySignedByte16] = trans_vector_unsigned_byte_16;
3884 #ifdef type_SimpleArraySignedByte30
3885 transother[type_SimpleArraySignedByte30] = trans_vector_unsigned_byte_32;
3887 #ifdef type_SimpleArraySignedByte32
3888 transother[type_SimpleArraySignedByte32] = trans_vector_unsigned_byte_32;
3890 transother[type_SimpleArraySingleFloat] = trans_vector_single_float;
3891 transother[type_SimpleArrayDoubleFloat] = trans_vector_double_float;
3892 #ifdef type_SimpleArrayLongFloat
3893 transother[type_SimpleArrayLongFloat] = trans_vector_long_float;
3895 #ifdef type_SimpleArrayComplexSingleFloat
3896 transother[type_SimpleArrayComplexSingleFloat] = trans_vector_complex_single_float;
3898 #ifdef type_SimpleArrayComplexDoubleFloat
3899 transother[type_SimpleArrayComplexDoubleFloat] = trans_vector_complex_double_float;
3901 #ifdef type_SimpleArrayComplexLongFloat
3902 transother[type_SimpleArrayComplexLongFloat] = trans_vector_complex_long_float;
3904 transother[type_ComplexString] = trans_boxed;
3905 transother[type_ComplexBitVector] = trans_boxed;
3906 transother[type_ComplexVector] = trans_boxed;
3907 transother[type_ComplexArray] = trans_boxed;
3908 transother[type_CodeHeader] = trans_code_header;
3909 transother[type_FunctionHeader] = trans_function_header;
3910 transother[type_ClosureFunctionHeader] = trans_function_header;
3911 transother[type_ReturnPcHeader] = trans_return_pc_header;
3912 transother[type_ClosureHeader] = trans_boxed;
3913 transother[type_FuncallableInstanceHeader] = trans_boxed;
3914 transother[type_ByteCodeFunction] = trans_boxed;
3915 transother[type_ByteCodeClosure] = trans_boxed;
3916 transother[type_ValueCellHeader] = trans_boxed;
3917 transother[type_SymbolHeader] = trans_boxed;
3918 transother[type_BaseChar] = trans_immediate;
3919 transother[type_Sap] = trans_unboxed;
3920 transother[type_UnboundMarker] = trans_immediate;
3921 transother[type_WeakPointer] = trans_weak_pointer;
3922 transother[type_InstanceHeader] = trans_boxed;
3923 transother[type_Fdefn] = trans_boxed;
3925 /* size table, initialized the same way as scavtab */
3926 for (i = 0; i < 256; i++)
3927 sizetab[i] = size_lose;
3928 for (i = 0; i < 32; i++) {
3929 sizetab[type_EvenFixnum|(i<<3)] = size_immediate;
3930 sizetab[type_FunctionPointer|(i<<3)] = size_pointer;
3931 /* OtherImmediate0 */
3932 sizetab[type_ListPointer|(i<<3)] = size_pointer;
3933 sizetab[type_OddFixnum|(i<<3)] = size_immediate;
3934 sizetab[type_InstancePointer|(i<<3)] = size_pointer;
3935 /* OtherImmediate1 */
3936 sizetab[type_OtherPointer|(i<<3)] = size_pointer;
3938 sizetab[type_Bignum] = size_unboxed;
3939 sizetab[type_Ratio] = size_boxed;
3940 sizetab[type_SingleFloat] = size_unboxed;
3941 sizetab[type_DoubleFloat] = size_unboxed;
3942 #ifdef type_LongFloat
3943 sizetab[type_LongFloat] = size_unboxed;
3945 sizetab[type_Complex] = size_boxed;
3946 #ifdef type_ComplexSingleFloat
3947 sizetab[type_ComplexSingleFloat] = size_unboxed;
3949 #ifdef type_ComplexDoubleFloat
3950 sizetab[type_ComplexDoubleFloat] = size_unboxed;
3952 #ifdef type_ComplexLongFloat
3953 sizetab[type_ComplexLongFloat] = size_unboxed;
3955 sizetab[type_SimpleArray] = size_boxed;
3956 sizetab[type_SimpleString] = size_string;
3957 sizetab[type_SimpleBitVector] = size_vector_bit;
3958 sizetab[type_SimpleVector] = size_vector;
3959 sizetab[type_SimpleArrayUnsignedByte2] = size_vector_unsigned_byte_2;
3960 sizetab[type_SimpleArrayUnsignedByte4] = size_vector_unsigned_byte_4;
3961 sizetab[type_SimpleArrayUnsignedByte8] = size_vector_unsigned_byte_8;
3962 sizetab[type_SimpleArrayUnsignedByte16] = size_vector_unsigned_byte_16;
3963 sizetab[type_SimpleArrayUnsignedByte32] = size_vector_unsigned_byte_32;
3964 #ifdef type_SimpleArraySignedByte8
3965 sizetab[type_SimpleArraySignedByte8] = size_vector_unsigned_byte_8;
3967 #ifdef type_SimpleArraySignedByte16
3968 sizetab[type_SimpleArraySignedByte16] = size_vector_unsigned_byte_16;
3970 #ifdef type_SimpleArraySignedByte30
3971 sizetab[type_SimpleArraySignedByte30] = size_vector_unsigned_byte_32;
3973 #ifdef type_SimpleArraySignedByte32
3974 sizetab[type_SimpleArraySignedByte32] = size_vector_unsigned_byte_32;
3976 sizetab[type_SimpleArraySingleFloat] = size_vector_single_float;
3977 sizetab[type_SimpleArrayDoubleFloat] = size_vector_double_float;
3978 #ifdef type_SimpleArrayLongFloat
3979 sizetab[type_SimpleArrayLongFloat] = size_vector_long_float;
3981 #ifdef type_SimpleArrayComplexSingleFloat
3982 sizetab[type_SimpleArrayComplexSingleFloat] = size_vector_complex_single_float;
3984 #ifdef type_SimpleArrayComplexDoubleFloat
3985 sizetab[type_SimpleArrayComplexDoubleFloat] = size_vector_complex_double_float;
3987 #ifdef type_SimpleArrayComplexLongFloat
3988 sizetab[type_SimpleArrayComplexLongFloat] = size_vector_complex_long_float;
3990 sizetab[type_ComplexString] = size_boxed;
3991 sizetab[type_ComplexBitVector] = size_boxed;
3992 sizetab[type_ComplexVector] = size_boxed;
3993 sizetab[type_ComplexArray] = size_boxed;
3994 sizetab[type_CodeHeader] = size_code_header;
3996 /* We shouldn't see these, so just lose if it happens. */
3997 sizetab[type_FunctionHeader] = size_function_header;
3998 sizetab[type_ClosureFunctionHeader] = size_function_header;
3999 sizetab[type_ReturnPcHeader] = size_return_pc_header;
4001 sizetab[type_ClosureHeader] = size_boxed;
4002 sizetab[type_FuncallableInstanceHeader] = size_boxed;
4003 sizetab[type_ValueCellHeader] = size_boxed;
4004 sizetab[type_SymbolHeader] = size_boxed;
4005 sizetab[type_BaseChar] = size_immediate;
4006 sizetab[type_Sap] = size_unboxed;
4007 sizetab[type_UnboundMarker] = size_immediate;
4008 sizetab[type_WeakPointer] = size_weak_pointer;
4009 sizetab[type_InstanceHeader] = size_boxed;
4010 sizetab[type_Fdefn] = size_boxed;
4013 /* Scan an area looking for an object which encloses the given pointer.
4014 * Return the object start on success or NULL on failure. */
4016 search_space(lispobj *start, size_t words, lispobj *pointer)
4020 lispobj thing = *start;
4022 /* If thing is an immediate then this is a cons */
4024 || ((thing & 3) == 0) /* fixnum */
4025 || (TypeOf(thing) == type_BaseChar)
4026 || (TypeOf(thing) == type_UnboundMarker))
4029 count = (sizetab[TypeOf(thing)])(start);
4031 /* Check whether the pointer is within this object? */
4032 if ((pointer >= start) && (pointer < (start+count))) {
4034 /*FSHOW((stderr,"/found %x in %x %x\n", pointer, start, thing));*/
4038 /* Round up the count */
4039 count = CEILING(count,2);
4048 search_read_only_space(lispobj *pointer)
4050 lispobj* start = (lispobj*)READ_ONLY_SPACE_START;
4051 lispobj* end = (lispobj*)SymbolValue(READ_ONLY_SPACE_FREE_POINTER);
4052 if ((pointer < start) || (pointer >= end))
4054 return (search_space(start, (pointer+2)-start, pointer));
4058 search_static_space(lispobj *pointer)
4060 lispobj* start = (lispobj*)STATIC_SPACE_START;
4061 lispobj* end = (lispobj*)SymbolValue(STATIC_SPACE_FREE_POINTER);
4062 if ((pointer < start) || (pointer >= end))
4064 return (search_space(start, (pointer+2)-start, pointer));
4067 /* a faster version for searching the dynamic space. This will work even
4068 * if the object is in a current allocation region. */
4070 search_dynamic_space(lispobj *pointer)
4072 int page_index = find_page_index(pointer);
4075 /* Address may be invalid - do some checks. */
4076 if ((page_index == -1) || (page_table[page_index].allocated == FREE_PAGE))
4078 start = (lispobj *)((void *)page_address(page_index)
4079 + page_table[page_index].first_object_offset);
4080 return (search_space(start, (pointer+2)-start, pointer));
4083 /* FIXME: There is a strong family resemblance between this function
4084 * and the function of the same name in purify.c. Would it be possible
4085 * to implement them as exactly the same function? */
4087 valid_dynamic_space_pointer(lispobj *pointer)
4089 lispobj *start_addr;
4091 /* Find the object start address */
4092 if ((start_addr = search_dynamic_space(pointer)) == NULL) {
4096 /* We need to allow raw pointers into Code objects for return
4097 * addresses. This will also pickup pointers to functions in code
4099 if (TypeOf(*start_addr) == type_CodeHeader) {
4100 /* X Could do some further checks here. */
4104 /* If it's not a return address then it needs to be a valid Lisp
4106 if (!Pointerp((lispobj)pointer)) {
4110 /* Check that the object pointed to is consistent with the pointer
4112 switch (LowtagOf((lispobj)pointer)) {
4113 case type_FunctionPointer:
4114 /* Start_addr should be the enclosing code object, or a closure
4116 switch (TypeOf(*start_addr)) {
4117 case type_CodeHeader:
4118 /* This case is probably caught above. */
4120 case type_ClosureHeader:
4121 case type_FuncallableInstanceHeader:
4122 case type_ByteCodeFunction:
4123 case type_ByteCodeClosure:
4124 if ((unsigned)pointer !=
4125 ((unsigned)start_addr+type_FunctionPointer)) {
4129 pointer, start_addr, *start_addr));
4137 pointer, start_addr, *start_addr));
4141 case type_ListPointer:
4142 if ((unsigned)pointer !=
4143 ((unsigned)start_addr+type_ListPointer)) {
4147 pointer, start_addr, *start_addr));
4150 /* Is it plausible cons? */
4151 if ((Pointerp(start_addr[0])
4152 || ((start_addr[0] & 3) == 0) /* fixnum */
4153 || (TypeOf(start_addr[0]) == type_BaseChar)
4154 || (TypeOf(start_addr[0]) == type_UnboundMarker))
4155 && (Pointerp(start_addr[1])
4156 || ((start_addr[1] & 3) == 0) /* fixnum */
4157 || (TypeOf(start_addr[1]) == type_BaseChar)
4158 || (TypeOf(start_addr[1]) == type_UnboundMarker)))
4164 pointer, start_addr, *start_addr));
4167 case type_InstancePointer:
4168 if ((unsigned)pointer !=
4169 ((unsigned)start_addr+type_InstancePointer)) {
4173 pointer, start_addr, *start_addr));
4176 if (TypeOf(start_addr[0]) != type_InstanceHeader) {
4180 pointer, start_addr, *start_addr));
4184 case type_OtherPointer:
4185 if ((unsigned)pointer !=
4186 ((int)start_addr+type_OtherPointer)) {
4190 pointer, start_addr, *start_addr));
4193 /* Is it plausible? Not a cons. X should check the headers. */
4194 if (Pointerp(start_addr[0]) || ((start_addr[0] & 3) == 0)) {
4198 pointer, start_addr, *start_addr));
4201 switch (TypeOf(start_addr[0])) {
4202 case type_UnboundMarker:
4207 pointer, start_addr, *start_addr));
4210 /* only pointed to by function pointers? */
4211 case type_ClosureHeader:
4212 case type_FuncallableInstanceHeader:
4213 case type_ByteCodeFunction:
4214 case type_ByteCodeClosure:
4218 pointer, start_addr, *start_addr));
4221 case type_InstanceHeader:
4225 pointer, start_addr, *start_addr));
4228 /* the valid other immediate pointer objects */
4229 case type_SimpleVector:
4232 #ifdef type_ComplexSingleFloat
4233 case type_ComplexSingleFloat:
4235 #ifdef type_ComplexDoubleFloat
4236 case type_ComplexDoubleFloat:
4238 #ifdef type_ComplexLongFloat
4239 case type_ComplexLongFloat:
4241 case type_SimpleArray:
4242 case type_ComplexString:
4243 case type_ComplexBitVector:
4244 case type_ComplexVector:
4245 case type_ComplexArray:
4246 case type_ValueCellHeader:
4247 case type_SymbolHeader:
4249 case type_CodeHeader:
4251 case type_SingleFloat:
4252 case type_DoubleFloat:
4253 #ifdef type_LongFloat
4254 case type_LongFloat:
4256 case type_SimpleString:
4257 case type_SimpleBitVector:
4258 case type_SimpleArrayUnsignedByte2:
4259 case type_SimpleArrayUnsignedByte4:
4260 case type_SimpleArrayUnsignedByte8:
4261 case type_SimpleArrayUnsignedByte16:
4262 case type_SimpleArrayUnsignedByte32:
4263 #ifdef type_SimpleArraySignedByte8
4264 case type_SimpleArraySignedByte8:
4266 #ifdef type_SimpleArraySignedByte16
4267 case type_SimpleArraySignedByte16:
4269 #ifdef type_SimpleArraySignedByte30
4270 case type_SimpleArraySignedByte30:
4272 #ifdef type_SimpleArraySignedByte32
4273 case type_SimpleArraySignedByte32:
4275 case type_SimpleArraySingleFloat:
4276 case type_SimpleArrayDoubleFloat:
4277 #ifdef type_SimpleArrayLongFloat
4278 case type_SimpleArrayLongFloat:
4280 #ifdef type_SimpleArrayComplexSingleFloat
4281 case type_SimpleArrayComplexSingleFloat:
4283 #ifdef type_SimpleArrayComplexDoubleFloat
4284 case type_SimpleArrayComplexDoubleFloat:
4286 #ifdef type_SimpleArrayComplexLongFloat
4287 case type_SimpleArrayComplexLongFloat:
4290 case type_WeakPointer:
4297 pointer, start_addr, *start_addr));
4305 pointer, start_addr, *start_addr));
4313 /* Adjust large bignum and vector objects. This will adjust the allocated
4314 * region if the size has shrunk, and move unboxed objects into unboxed
4315 * pages. The pages are not promoted here, and the promoted region is not
4316 * added to the new_regions; this is really only designed to be called from
4317 * preserve_pointer. Shouldn't fail if this is missed, just may delay the
4318 * moving of objects to unboxed pages, and the freeing of pages. */
4320 maybe_adjust_large_object(lispobj *where)
4325 int remaining_bytes;
4332 /* Check whether it's a vector or bignum object. */
4333 switch (TypeOf(where[0])) {
4334 case type_SimpleVector:
4338 case type_SimpleString:
4339 case type_SimpleBitVector:
4340 case type_SimpleArrayUnsignedByte2:
4341 case type_SimpleArrayUnsignedByte4:
4342 case type_SimpleArrayUnsignedByte8:
4343 case type_SimpleArrayUnsignedByte16:
4344 case type_SimpleArrayUnsignedByte32:
4345 #ifdef type_SimpleArraySignedByte8
4346 case type_SimpleArraySignedByte8:
4348 #ifdef type_SimpleArraySignedByte16
4349 case type_SimpleArraySignedByte16:
4351 #ifdef type_SimpleArraySignedByte30
4352 case type_SimpleArraySignedByte30:
4354 #ifdef type_SimpleArraySignedByte32
4355 case type_SimpleArraySignedByte32:
4357 case type_SimpleArraySingleFloat:
4358 case type_SimpleArrayDoubleFloat:
4359 #ifdef type_SimpleArrayLongFloat
4360 case type_SimpleArrayLongFloat:
4362 #ifdef type_SimpleArrayComplexSingleFloat
4363 case type_SimpleArrayComplexSingleFloat:
4365 #ifdef type_SimpleArrayComplexDoubleFloat
4366 case type_SimpleArrayComplexDoubleFloat:
4368 #ifdef type_SimpleArrayComplexLongFloat
4369 case type_SimpleArrayComplexLongFloat:
4371 boxed = UNBOXED_PAGE;
4377 /* Find its current size. */
4378 nwords = (sizetab[TypeOf(where[0])])(where);
4380 first_page = find_page_index((void *)where);
4381 gc_assert(first_page >= 0);
4383 /* Note: Any page write-protection must be removed, else a later
4384 * scavenge_newspace may incorrectly not scavenge these pages.
4385 * This would not be necessary if they are added to the new areas,
4386 * but lets do it for them all (they'll probably be written
4389 gc_assert(page_table[first_page].first_object_offset == 0);
4391 next_page = first_page;
4392 remaining_bytes = nwords*4;
4393 while (remaining_bytes > 4096) {
4394 gc_assert(page_table[next_page].gen == from_space);
4395 gc_assert((page_table[next_page].allocated == BOXED_PAGE)
4396 || (page_table[next_page].allocated == UNBOXED_PAGE));
4397 gc_assert(page_table[next_page].large_object);
4398 gc_assert(page_table[next_page].first_object_offset ==
4399 -4096*(next_page-first_page));
4400 gc_assert(page_table[next_page].bytes_used == 4096);
4402 page_table[next_page].allocated = boxed;
4404 /* Shouldn't be write-protected at this stage. Essential that the
4406 gc_assert(!page_table[next_page].write_protected);
4407 remaining_bytes -= 4096;
4411 /* Now only one page remains, but the object may have shrunk so
4412 * there may be more unused pages which will be freed. */
4414 /* Object may have shrunk but shouldn't have grown - check. */
4415 gc_assert(page_table[next_page].bytes_used >= remaining_bytes);
4417 page_table[next_page].allocated = boxed;
4418 gc_assert(page_table[next_page].allocated ==
4419 page_table[first_page].allocated);
4421 /* Adjust the bytes_used. */
4422 old_bytes_used = page_table[next_page].bytes_used;
4423 page_table[next_page].bytes_used = remaining_bytes;
4425 bytes_freed = old_bytes_used - remaining_bytes;
4427 /* Free any remaining pages; needs care. */
4429 while ((old_bytes_used == 4096) &&
4430 (page_table[next_page].gen == from_space) &&
4431 ((page_table[next_page].allocated == UNBOXED_PAGE)
4432 || (page_table[next_page].allocated == BOXED_PAGE)) &&
4433 page_table[next_page].large_object &&
4434 (page_table[next_page].first_object_offset ==
4435 -(next_page - first_page)*4096)) {
4436 /* It checks out OK, free the page. We don't need to both zeroing
4437 * pages as this should have been done before shrinking the
4438 * object. These pages shouldn't be write protected as they
4439 * should be zero filled. */
4440 gc_assert(page_table[next_page].write_protected == 0);
4442 old_bytes_used = page_table[next_page].bytes_used;
4443 page_table[next_page].allocated = FREE_PAGE;
4444 page_table[next_page].bytes_used = 0;
4445 bytes_freed += old_bytes_used;
4449 if ((bytes_freed > 0) && gencgc_verbose)
4450 FSHOW((stderr, "/adjust_large_object freed %d\n", bytes_freed));
4452 generations[from_space].bytes_allocated -= bytes_freed;
4453 bytes_allocated -= bytes_freed;
4458 /* Take a possible pointer to a list object and mark the page_table
4459 * so that it will not need changing during a GC.
4461 * This involves locating the page it points to, then backing up to
4462 * the first page that has its first object start at offset 0, and
4463 * then marking all pages dont_move from the first until a page that ends
4464 * by being full, or having free gen.
4466 * This ensures that objects spanning pages are not broken.
4468 * It is assumed that all the page static flags have been cleared at
4469 * the start of a GC.
4471 * It is also assumed that the current gc_alloc region has been flushed and
4472 * the tables updated. */
4474 preserve_pointer(void *addr)
4476 int addr_page_index = find_page_index(addr);
4479 unsigned region_allocation;
4481 /* Address is quite likely to have been invalid - do some checks. */
4482 if ((addr_page_index == -1)
4483 || (page_table[addr_page_index].allocated == FREE_PAGE)
4484 || (page_table[addr_page_index].bytes_used == 0)
4485 || (page_table[addr_page_index].gen != from_space)
4486 /* Skip if already marked dont_move */
4487 || (page_table[addr_page_index].dont_move != 0))
4490 region_allocation = page_table[addr_page_index].allocated;
4492 /* Check the offset within the page.
4494 * FIXME: The mask should have a symbolic name, and ideally should
4495 * be derived from page size instead of hardwired to 0xfff.
4496 * (Also fix other uses of 0xfff, elsewhere.) */
4497 if (((unsigned)addr & 0xfff) > page_table[addr_page_index].bytes_used)
4500 if (enable_pointer_filter && !valid_dynamic_space_pointer(addr))
4503 /* Work backwards to find a page with a first_object_offset of 0.
4504 * The pages should be contiguous with all bytes used in the same
4505 * gen. Assumes the first_object_offset is negative or zero. */
4506 first_page = addr_page_index;
4507 while (page_table[first_page].first_object_offset != 0) {
4509 /* Do some checks. */
4510 gc_assert(page_table[first_page].bytes_used == 4096);
4511 gc_assert(page_table[first_page].gen == from_space);
4512 gc_assert(page_table[first_page].allocated == region_allocation);
4515 /* Adjust any large objects before promotion as they won't be copied
4516 * after promotion. */
4517 if (page_table[first_page].large_object) {
4518 maybe_adjust_large_object(page_address(first_page));
4519 /* If a large object has shrunk then addr may now point to a free
4520 * area in which case it's ignored here. Note it gets through the
4521 * valid pointer test above because the tail looks like conses. */
4522 if ((page_table[addr_page_index].allocated == FREE_PAGE)
4523 || (page_table[addr_page_index].bytes_used == 0)
4524 /* Check the offset within the page. */
4525 || (((unsigned)addr & 0xfff)
4526 > page_table[addr_page_index].bytes_used)) {
4528 "weird? ignore ptr 0x%x to freed area of large object\n",
4532 /* It may have moved to unboxed pages. */
4533 region_allocation = page_table[first_page].allocated;
4536 /* Now work forward until the end of this contiguous area is found,
4537 * marking all pages as dont_move. */
4538 for (i = first_page; ;i++) {
4539 gc_assert(page_table[i].allocated == region_allocation);
4541 /* Mark the page static. */
4542 page_table[i].dont_move = 1;
4544 /* Move the page to the new_space. XX I'd rather not do this but
4545 * the GC logic is not quite able to copy with the static pages
4546 * remaining in the from space. This also requires the generation
4547 * bytes_allocated counters be updated. */
4548 page_table[i].gen = new_space;
4549 generations[new_space].bytes_allocated += page_table[i].bytes_used;
4550 generations[from_space].bytes_allocated -= page_table[i].bytes_used;
4552 /* It is essential that the pages are not write protected as they
4553 * may have pointers into the old-space which need scavenging. They
4554 * shouldn't be write protected at this stage. */
4555 gc_assert(!page_table[i].write_protected);
4557 /* Check whether this is the last page in this contiguous block.. */
4558 if ((page_table[i].bytes_used < 4096)
4559 /* ..or it is 4096 and is the last in the block */
4560 || (page_table[i+1].allocated == FREE_PAGE)
4561 || (page_table[i+1].bytes_used == 0) /* next page free */
4562 || (page_table[i+1].gen != from_space) /* diff. gen */
4563 || (page_table[i+1].first_object_offset == 0))
4567 /* Check that the page is now static. */
4568 gc_assert(page_table[addr_page_index].dont_move != 0);
4573 #ifdef CONTROL_STACKS
4574 /* Scavenge the thread stack conservative roots. */
4576 scavenge_thread_stacks(void)
4578 lispobj thread_stacks = SymbolValue(CONTROL_STACKS);
4579 int type = TypeOf(thread_stacks);
4581 if (LowtagOf(thread_stacks) == type_OtherPointer) {
4582 struct vector *vector = (struct vector *) PTR(thread_stacks);
4584 if (TypeOf(vector->header) != type_SimpleVector)
4586 length = fixnum_value(vector->length);
4587 for (i = 0; i < length; i++) {
4588 lispobj stack_obj = vector->data[i];
4589 if (LowtagOf(stack_obj) == type_OtherPointer) {
4590 struct vector *stack = (struct vector *) PTR(stack_obj);
4592 if (TypeOf(stack->header) !=
4593 type_SimpleArrayUnsignedByte32) {
4596 vector_length = fixnum_value(stack->length);
4597 if ((gencgc_verbose > 1) && (vector_length <= 0))
4599 "/weird? control stack vector length %d\n",
4601 if (vector_length > 0) {
4602 lispobj *stack_pointer = (lispobj*)stack->data[0];
4603 if ((stack_pointer < (lispobj *)CONTROL_STACK_START) ||
4604 (stack_pointer > (lispobj *)CONTROL_STACK_END))
4605 lose("invalid stack pointer %x",
4606 (unsigned)stack_pointer);
4607 if ((stack_pointer > (lispobj *)CONTROL_STACK_START) &&
4608 (stack_pointer < (lispobj *)CONTROL_STACK_END)) {
4610 * (1) hardwired word length = 4; and as usual,
4611 * when fixing this, check for other places
4612 * with the same problem
4613 * (2) calling it 'length' suggests bytes;
4614 * perhaps 'size' instead? */
4615 unsigned int length = ((unsigned)CONTROL_STACK_END -
4616 (unsigned)stack_pointer) / 4;
4618 if (length >= vector_length) {
4619 lose("invalid stack size %d >= vector length %d",
4623 if (gencgc_verbose > 1) {
4625 "scavenging %d words of control stack %d of length %d words.\n",
4626 length, i, vector_length));
4628 for (j = 0; j < length; j++) {
4629 preserve_pointer((void *)stack->data[1+j]);
4640 /* If the given page is not write-protected, then scan it for pointers
4641 * to younger generations or the top temp. generation, if no
4642 * suspicious pointers are found then the page is write-protected.
4644 * Care is taken to check for pointers to the current gc_alloc region
4645 * if it is a younger generation or the temp. generation. This frees
4646 * the caller from doing a gc_alloc_update_page_tables. Actually the
4647 * gc_alloc_generation does not need to be checked as this is only
4648 * called from scavenge_generation when the gc_alloc generation is
4649 * younger, so it just checks if there is a pointer to the current
4652 * We return 1 if the page was write-protected, else 0.
4655 update_page_write_prot(int page)
4657 int gen = page_table[page].gen;
4660 void **page_addr = (void **)page_address(page);
4661 int num_words = page_table[page].bytes_used / 4;
4663 /* Shouldn't be a free page. */
4664 gc_assert(page_table[page].allocated != FREE_PAGE);
4665 gc_assert(page_table[page].bytes_used != 0);
4667 /* Skip if it's already write-protected or an unboxed page. */
4668 if (page_table[page].write_protected
4669 || (page_table[page].allocated == UNBOXED_PAGE))
4672 /* Scan the page for pointers to younger generations or the
4673 * top temp. generation. */
4675 for (j = 0; j < num_words; j++) {
4676 void *ptr = *(page_addr+j);
4677 int index = find_page_index(ptr);
4679 /* Check that it's in the dynamic space */
4681 if (/* Does it point to a younger or the temp. generation? */
4682 ((page_table[index].allocated != FREE_PAGE)
4683 && (page_table[index].bytes_used != 0)
4684 && ((page_table[index].gen < gen)
4685 || (page_table[index].gen == NUM_GENERATIONS)))
4687 /* Or does it point within a current gc_alloc region? */
4688 || ((boxed_region.start_addr <= ptr)
4689 && (ptr <= boxed_region.free_pointer))
4690 || ((unboxed_region.start_addr <= ptr)
4691 && (ptr <= unboxed_region.free_pointer))) {
4698 /* Write-protect the page. */
4699 /*FSHOW((stderr, "/write-protecting page %d gen %d\n", page, gen));*/
4701 os_protect((void *)page_addr,
4703 OS_VM_PROT_READ|OS_VM_PROT_EXECUTE);
4705 /* Note the page as protected in the page tables. */
4706 page_table[page].write_protected = 1;
4712 /* Scavenge a generation.
4714 * This will not resolve all pointers when generation is the new
4715 * space, as new objects may be added which are not check here - use
4716 * scavenge_newspace generation.
4718 * Write-protected pages should not have any pointers to the
4719 * from_space so do need scavenging; thus write-protected pages are
4720 * not always scavenged. There is some code to check that these pages
4721 * are not written; but to check fully the write-protected pages need
4722 * to be scavenged by disabling the code to skip them.
4724 * Under the current scheme when a generation is GCed the younger
4725 * generations will be empty. So, when a generation is being GCed it
4726 * is only necessary to scavenge the older generations for pointers
4727 * not the younger. So a page that does not have pointers to younger
4728 * generations does not need to be scavenged.
4730 * The write-protection can be used to note pages that don't have
4731 * pointers to younger pages. But pages can be written without having
4732 * pointers to younger generations. After the pages are scavenged here
4733 * they can be scanned for pointers to younger generations and if
4734 * there are none the page can be write-protected.
4736 * One complication is when the newspace is the top temp. generation.
4738 * Enabling SC_GEN_CK scavenges the write-protected pages and checks
4739 * that none were written, which they shouldn't be as they should have
4740 * no pointers to younger generations. This breaks down for weak
4741 * pointers as the objects contain a link to the next and are written
4742 * if a weak pointer is scavenged. Still it's a useful check. */
4744 scavenge_generation(int generation)
4751 /* Clear the write_protected_cleared flags on all pages. */
4752 for (i = 0; i < NUM_PAGES; i++)
4753 page_table[i].write_protected_cleared = 0;
4756 for (i = 0; i < last_free_page; i++) {
4757 if ((page_table[i].allocated == BOXED_PAGE)
4758 && (page_table[i].bytes_used != 0)
4759 && (page_table[i].gen == generation)) {
4762 /* This should be the start of a contiguous block. */
4763 gc_assert(page_table[i].first_object_offset == 0);
4765 /* We need to find the full extent of this contiguous
4766 * block in case objects span pages. */
4768 /* Now work forward until the end of this contiguous area
4769 * is found. A small area is preferred as there is a
4770 * better chance of its pages being write-protected. */
4771 for (last_page = i; ;last_page++)
4772 /* Check whether this is the last page in this contiguous
4774 if ((page_table[last_page].bytes_used < 4096)
4775 /* Or it is 4096 and is the last in the block */
4776 || (page_table[last_page+1].allocated != BOXED_PAGE)
4777 || (page_table[last_page+1].bytes_used == 0)
4778 || (page_table[last_page+1].gen != generation)
4779 || (page_table[last_page+1].first_object_offset == 0))
4782 /* Do a limited check for write_protected pages. If all pages
4783 * are write_protected then there is no need to scavenge. */
4786 for (j = i; j <= last_page; j++)
4787 if (page_table[j].write_protected == 0) {
4795 scavenge(page_address(i), (page_table[last_page].bytes_used
4796 + (last_page-i)*4096)/4);
4798 /* Now scan the pages and write protect those
4799 * that don't have pointers to younger
4801 if (enable_page_protection) {
4802 for (j = i; j <= last_page; j++) {
4803 num_wp += update_page_write_prot(j);
4812 if ((gencgc_verbose > 1) && (num_wp != 0)) {
4814 "/write protected %d pages within generation %d\n",
4815 num_wp, generation));
4819 /* Check that none of the write_protected pages in this generation
4820 * have been written to. */
4821 for (i = 0; i < NUM_PAGES; i++) {
4822 if ((page_table[i].allocation ! =FREE_PAGE)
4823 && (page_table[i].bytes_used != 0)
4824 && (page_table[i].gen == generation)
4825 && (page_table[i].write_protected_cleared != 0)) {
4826 FSHOW((stderr, "/scavenge_generation %d\n", generation));
4828 "/page bytes_used=%d first_object_offset=%d dont_move=%d\n",
4829 page_table[i].bytes_used,
4830 page_table[i].first_object_offset,
4831 page_table[i].dont_move));
4832 lose("write-protected page %d written to in scavenge_generation",
4840 /* Scavenge a newspace generation. As it is scavenged new objects may
4841 * be allocated to it; these will also need to be scavenged. This
4842 * repeats until there are no more objects unscavenged in the
4843 * newspace generation.
4845 * To help improve the efficiency, areas written are recorded by
4846 * gc_alloc and only these scavenged. Sometimes a little more will be
4847 * scavenged, but this causes no harm. An easy check is done that the
4848 * scavenged bytes equals the number allocated in the previous
4851 * Write-protected pages are not scanned except if they are marked
4852 * dont_move in which case they may have been promoted and still have
4853 * pointers to the from space.
4855 * Write-protected pages could potentially be written by alloc however
4856 * to avoid having to handle re-scavenging of write-protected pages
4857 * gc_alloc does not write to write-protected pages.
4859 * New areas of objects allocated are recorded alternatively in the two
4860 * new_areas arrays below. */
4861 static struct new_area new_areas_1[NUM_NEW_AREAS];
4862 static struct new_area new_areas_2[NUM_NEW_AREAS];
4864 /* Do one full scan of the new space generation. This is not enough to
4865 * complete the job as new objects may be added to the generation in
4866 * the process which are not scavenged. */
4868 scavenge_newspace_generation_one_scan(int generation)
4873 "/starting one full scan of newspace generation %d\n",
4876 for (i = 0; i < last_free_page; i++) {
4877 if ((page_table[i].allocated == BOXED_PAGE)
4878 && (page_table[i].bytes_used != 0)
4879 && (page_table[i].gen == generation)
4880 && ((page_table[i].write_protected == 0)
4881 /* (This may be redundant as write_protected is now
4882 * cleared before promotion.) */
4883 || (page_table[i].dont_move == 1))) {
4886 /* The scavenge will start at the first_object_offset of page i.
4888 * We need to find the full extent of this contiguous block in case
4889 * objects span pages.
4891 * Now work forward until the end of this contiguous area is
4892 * found. A small area is preferred as there is a better chance
4893 * of its pages being write-protected. */
4894 for (last_page = i; ;last_page++) {
4895 /* Check whether this is the last page in this contiguous
4897 if ((page_table[last_page].bytes_used < 4096)
4898 /* Or it is 4096 and is the last in the block */
4899 || (page_table[last_page+1].allocated != BOXED_PAGE)
4900 || (page_table[last_page+1].bytes_used == 0)
4901 || (page_table[last_page+1].gen != generation)
4902 || (page_table[last_page+1].first_object_offset == 0))
4906 /* Do a limited check for write_protected pages. If all pages
4907 * are write_protected then no need to scavenge. Except if the
4908 * pages are marked dont_move. */
4911 for (j = i; j <= last_page; j++)
4912 if ((page_table[j].write_protected == 0)
4913 || (page_table[j].dont_move != 0)) {
4923 /* Calculate the size. */
4925 size = (page_table[last_page].bytes_used
4926 - page_table[i].first_object_offset)/4;
4928 size = (page_table[last_page].bytes_used
4929 + (last_page-i)*4096
4930 - page_table[i].first_object_offset)/4;
4934 int a1 = bytes_allocated;
4937 "/scavenge(%x,%d)\n",
4939 + page_table[i].first_object_offset,
4942 new_areas_ignore_page = last_page;
4944 scavenge(page_address(i)+page_table[i].first_object_offset,size);
4947 /* Flush the alloc regions updating the tables. */
4948 gc_alloc_update_page_tables(0, &boxed_region);
4949 gc_alloc_update_page_tables(1, &unboxed_region);
4951 if ((all_wp != 0) && (a1 != bytes_allocated)) {
4953 "alloc'ed over %d to %d\n",
4956 "/page: bytes_used=%d first_object_offset=%d dont_move=%d wp=%d wpc=%d\n",
4957 page_table[i].bytes_used,
4958 page_table[i].first_object_offset,
4959 page_table[i].dont_move,
4960 page_table[i].write_protected,
4961 page_table[i].write_protected_cleared));
4973 /* Do a complete scavenge of the newspace generation. */
4975 scavenge_newspace_generation(int generation)
4979 /* the new_areas array currently being written to by gc_alloc */
4980 struct new_area (*current_new_areas)[] = &new_areas_1;
4981 int current_new_areas_index;
4983 /* the new_areas created but the previous scavenge cycle */
4984 struct new_area (*previous_new_areas)[] = NULL;
4985 int previous_new_areas_index;
4987 #define SC_NS_GEN_CK 0
4989 /* Clear the write_protected_cleared flags on all pages. */
4990 for (i = 0; i < NUM_PAGES; i++)
4991 page_table[i].write_protected_cleared = 0;
4994 /* Flush the current regions updating the tables. */
4995 gc_alloc_update_page_tables(0, &boxed_region);
4996 gc_alloc_update_page_tables(1, &unboxed_region);
4998 /* Turn on the recording of new areas by gc_alloc. */
4999 new_areas = current_new_areas;
5000 new_areas_index = 0;
5002 /* Don't need to record new areas that get scavenged anyway during
5003 * scavenge_newspace_generation_one_scan. */
5004 record_new_objects = 1;
5006 /* Start with a full scavenge. */
5007 scavenge_newspace_generation_one_scan(generation);
5009 /* Record all new areas now. */
5010 record_new_objects = 2;
5012 /* Flush the current regions updating the tables. */
5013 gc_alloc_update_page_tables(0, &boxed_region);
5014 gc_alloc_update_page_tables(1, &unboxed_region);
5016 /* Grab new_areas_index. */
5017 current_new_areas_index = new_areas_index;
5020 "The first scan is finished; current_new_areas_index=%d.\n",
5021 current_new_areas_index));*/
5023 while (current_new_areas_index > 0) {
5024 /* Move the current to the previous new areas */
5025 previous_new_areas = current_new_areas;
5026 previous_new_areas_index = current_new_areas_index;
5028 /* Scavenge all the areas in previous new areas. Any new areas
5029 * allocated are saved in current_new_areas. */
5031 /* Allocate an array for current_new_areas; alternating between
5032 * new_areas_1 and 2 */
5033 if (previous_new_areas == &new_areas_1)
5034 current_new_areas = &new_areas_2;
5036 current_new_areas = &new_areas_1;
5038 /* Set up for gc_alloc. */
5039 new_areas = current_new_areas;
5040 new_areas_index = 0;
5042 /* Check whether previous_new_areas had overflowed. */
5043 if (previous_new_areas_index >= NUM_NEW_AREAS) {
5044 /* New areas of objects allocated have been lost so need to do a
5045 * full scan to be sure! If this becomes a problem try
5046 * increasing NUM_NEW_AREAS. */
5048 SHOW("new_areas overflow, doing full scavenge");
5050 /* Don't need to record new areas that get scavenge anyway
5051 * during scavenge_newspace_generation_one_scan. */
5052 record_new_objects = 1;
5054 scavenge_newspace_generation_one_scan(generation);
5056 /* Record all new areas now. */
5057 record_new_objects = 2;
5059 /* Flush the current regions updating the tables. */
5060 gc_alloc_update_page_tables(0, &boxed_region);
5061 gc_alloc_update_page_tables(1, &unboxed_region);
5063 /* Work through previous_new_areas. */
5064 for (i = 0; i < previous_new_areas_index; i++) {
5065 int page = (*previous_new_areas)[i].page;
5066 int offset = (*previous_new_areas)[i].offset;
5067 int size = (*previous_new_areas)[i].size / 4;
5068 gc_assert((*previous_new_areas)[i].size % 4 == 0);
5070 /* FIXME: All these bare *4 and /4 should be something
5071 * like BYTES_PER_WORD or WBYTES. */
5074 "/S page %d offset %d size %d\n",
5075 page, offset, size*4));*/
5076 scavenge(page_address(page)+offset, size);
5079 /* Flush the current regions updating the tables. */
5080 gc_alloc_update_page_tables(0, &boxed_region);
5081 gc_alloc_update_page_tables(1, &unboxed_region);
5084 current_new_areas_index = new_areas_index;
5087 "The re-scan has finished; current_new_areas_index=%d.\n",
5088 current_new_areas_index));*/
5091 /* Turn off recording of areas allocated by gc_alloc. */
5092 record_new_objects = 0;
5095 /* Check that none of the write_protected pages in this generation
5096 * have been written to. */
5097 for (i = 0; i < NUM_PAGES; i++) {
5098 if ((page_table[i].allocation != FREE_PAGE)
5099 && (page_table[i].bytes_used != 0)
5100 && (page_table[i].gen == generation)
5101 && (page_table[i].write_protected_cleared != 0)
5102 && (page_table[i].dont_move == 0)) {
5103 lose("write protected page %d written to in scavenge_newspace_generation\ngeneration=%d dont_move=%d",
5104 i, generation, page_table[i].dont_move);
5110 /* Un-write-protect all the pages in from_space. This is done at the
5111 * start of a GC else there may be many page faults while scavenging
5112 * the newspace (I've seen drive the system time to 99%). These pages
5113 * would need to be unprotected anyway before unmapping in
5114 * free_oldspace; not sure what effect this has on paging.. */
5116 unprotect_oldspace(void)
5120 for (i = 0; i < last_free_page; i++) {
5121 if ((page_table[i].allocated != FREE_PAGE)
5122 && (page_table[i].bytes_used != 0)
5123 && (page_table[i].gen == from_space)) {
5126 page_start = (void *)page_address(i);
5128 /* Remove any write-protection. We should be able to rely
5129 * on the write-protect flag to avoid redundant calls. */
5130 if (page_table[i].write_protected) {
5131 os_protect(page_start, 4096, OS_VM_PROT_ALL);
5132 page_table[i].write_protected = 0;
5138 /* Work through all the pages and free any in from_space. This
5139 * assumes that all objects have been copied or promoted to an older
5140 * generation. Bytes_allocated and the generation bytes_allocated
5141 * counter are updated. The number of bytes freed is returned. */
5142 extern void i586_bzero(void *addr, int nbytes);
5146 int bytes_freed = 0;
5147 int first_page, last_page;
5152 /* Find a first page for the next region of pages. */
5153 while ((first_page < last_free_page)
5154 && ((page_table[first_page].allocated == FREE_PAGE)
5155 || (page_table[first_page].bytes_used == 0)
5156 || (page_table[first_page].gen != from_space)))
5159 if (first_page >= last_free_page)
5162 /* Find the last page of this region. */
5163 last_page = first_page;
5166 /* Free the page. */
5167 bytes_freed += page_table[last_page].bytes_used;
5168 generations[page_table[last_page].gen].bytes_allocated -=
5169 page_table[last_page].bytes_used;
5170 page_table[last_page].allocated = FREE_PAGE;
5171 page_table[last_page].bytes_used = 0;
5173 /* Remove any write-protection. We should be able to rely
5174 * on the write-protect flag to avoid redundant calls. */
5176 void *page_start = (void *)page_address(last_page);
5178 if (page_table[last_page].write_protected) {
5179 os_protect(page_start, 4096, OS_VM_PROT_ALL);
5180 page_table[last_page].write_protected = 0;
5185 while ((last_page < last_free_page)
5186 && (page_table[last_page].allocated != FREE_PAGE)
5187 && (page_table[last_page].bytes_used != 0)
5188 && (page_table[last_page].gen == from_space));
5190 /* Zero pages from first_page to (last_page-1).
5192 * FIXME: Why not use os_zero(..) function instead of
5193 * hand-coding this again? (Check other gencgc_unmap_zero
5195 if (gencgc_unmap_zero) {
5196 void *page_start, *addr;
5198 page_start = (void *)page_address(first_page);
5200 os_invalidate(page_start, 4096*(last_page-first_page));
5201 addr = os_validate(page_start, 4096*(last_page-first_page));
5202 if (addr == NULL || addr != page_start) {
5203 /* Is this an error condition? I couldn't really tell from
5204 * the old CMU CL code, which fprintf'ed a message with
5205 * an exclamation point at the end. But I've never seen the
5206 * message, so it must at least be unusual..
5208 * (The same condition is also tested for in gc_free_heap.)
5210 * -- WHN 19991129 */
5211 lose("i586_bzero: page moved, 0x%08x ==> 0x%08x",
5218 page_start = (int *)page_address(first_page);
5219 i586_bzero(page_start, 4096*(last_page-first_page));
5222 first_page = last_page;
5224 } while (first_page < last_free_page);
5226 bytes_allocated -= bytes_freed;
5230 /* Print some information about a pointer at the given address. */
5232 print_ptr(lispobj *addr)
5234 /* If addr is in the dynamic space then out the page information. */
5235 int pi1 = find_page_index((void*)addr);
5238 fprintf(stderr," %x: page %d alloc %d gen %d bytes_used %d offset %d dont_move %d\n",
5239 (unsigned int) addr,
5241 page_table[pi1].allocated,
5242 page_table[pi1].gen,
5243 page_table[pi1].bytes_used,
5244 page_table[pi1].first_object_offset,
5245 page_table[pi1].dont_move);
5246 fprintf(stderr," %x %x %x %x (%x) %x %x %x %x\n",
5258 extern int undefined_tramp;
5261 verify_space(lispobj *start, size_t words)
5263 int is_in_dynamic_space = (find_page_index((void*)start) != -1);
5264 int is_in_readonly_space =
5265 (READ_ONLY_SPACE_START <= (unsigned)start &&
5266 (unsigned)start < SymbolValue(READ_ONLY_SPACE_FREE_POINTER));
5270 lispobj thing = *(lispobj*)start;
5272 if (Pointerp(thing)) {
5273 int page_index = find_page_index((void*)thing);
5274 int to_readonly_space =
5275 (READ_ONLY_SPACE_START <= thing &&
5276 thing < SymbolValue(READ_ONLY_SPACE_FREE_POINTER));
5277 int to_static_space =
5278 (STATIC_SPACE_START <= thing &&
5279 thing < SymbolValue(STATIC_SPACE_FREE_POINTER));
5281 /* Does it point to the dynamic space? */
5282 if (page_index != -1) {
5283 /* If it's within the dynamic space it should point to a used
5284 * page. XX Could check the offset too. */
5285 if ((page_table[page_index].allocated != FREE_PAGE)
5286 && (page_table[page_index].bytes_used == 0))
5287 lose ("Ptr %x @ %x sees free page.", thing, start);
5288 /* Check that it doesn't point to a forwarding pointer! */
5289 if (*((lispobj *)PTR(thing)) == 0x01) {
5290 lose("Ptr %x @ %x sees forwarding ptr.", thing, start);
5292 /* Check that its not in the RO space as it would then be a
5293 * pointer from the RO to the dynamic space. */
5294 if (is_in_readonly_space) {
5295 lose("ptr to dynamic space %x from RO space %x",
5298 /* Does it point to a plausible object? This check slows
5299 * it down a lot (so it's commented out).
5301 * FIXME: Add a variable to enable this dynamically. */
5302 /* if (!valid_dynamic_space_pointer((lispobj *)thing)) {
5303 * lose("ptr %x to invalid object %x", thing, start); */
5305 /* Verify that it points to another valid space. */
5306 if (!to_readonly_space && !to_static_space
5307 && (thing != (unsigned)&undefined_tramp)) {
5308 lose("Ptr %x @ %x sees junk.", thing, start);
5312 if (thing & 0x3) { /* Skip fixnums. FIXME: There should be an
5313 * is_fixnum for this. */
5315 switch(TypeOf(*start)) {
5318 case type_SimpleVector:
5321 case type_SimpleArray:
5322 case type_ComplexString:
5323 case type_ComplexBitVector:
5324 case type_ComplexVector:
5325 case type_ComplexArray:
5326 case type_ClosureHeader:
5327 case type_FuncallableInstanceHeader:
5328 case type_ByteCodeFunction:
5329 case type_ByteCodeClosure:
5330 case type_ValueCellHeader:
5331 case type_SymbolHeader:
5333 case type_UnboundMarker:
5334 case type_InstanceHeader:
5339 case type_CodeHeader:
5341 lispobj object = *start;
5343 int nheader_words, ncode_words, nwords;
5345 struct function *fheaderp;
5347 code = (struct code *) start;
5349 /* Check that it's not in the dynamic space.
5350 * FIXME: Isn't is supposed to be OK for code
5351 * objects to be in the dynamic space these days? */
5352 if (is_in_dynamic_space
5353 /* It's ok if it's byte compiled code. The trace
5354 * table offset will be a fixnum if it's x86
5355 * compiled code - check. */
5356 && !(code->trace_table_offset & 0x3)
5357 /* Only when enabled */
5358 && verify_dynamic_code_check) {
5360 "/code object at %x in the dynamic space\n",
5364 ncode_words = fixnum_value(code->code_size);
5365 nheader_words = HeaderValue(object);
5366 nwords = ncode_words + nheader_words;
5367 nwords = CEILING(nwords, 2);
5368 /* Scavenge the boxed section of the code data block */
5369 verify_space(start + 1, nheader_words - 1);
5371 /* Scavenge the boxed section of each function object in
5372 * the code data block. */
5373 fheaderl = code->entry_points;
5374 while (fheaderl != NIL) {
5375 fheaderp = (struct function *) PTR(fheaderl);
5376 gc_assert(TypeOf(fheaderp->header) == type_FunctionHeader);
5377 verify_space(&fheaderp->name, 1);
5378 verify_space(&fheaderp->arglist, 1);
5379 verify_space(&fheaderp->type, 1);
5380 fheaderl = fheaderp->next;
5386 /* unboxed objects */
5388 case type_SingleFloat:
5389 case type_DoubleFloat:
5390 #ifdef type_ComplexLongFloat
5391 case type_LongFloat:
5393 #ifdef type_ComplexSingleFloat
5394 case type_ComplexSingleFloat:
5396 #ifdef type_ComplexDoubleFloat
5397 case type_ComplexDoubleFloat:
5399 #ifdef type_ComplexLongFloat
5400 case type_ComplexLongFloat:
5402 case type_SimpleString:
5403 case type_SimpleBitVector:
5404 case type_SimpleArrayUnsignedByte2:
5405 case type_SimpleArrayUnsignedByte4:
5406 case type_SimpleArrayUnsignedByte8:
5407 case type_SimpleArrayUnsignedByte16:
5408 case type_SimpleArrayUnsignedByte32:
5409 #ifdef type_SimpleArraySignedByte8
5410 case type_SimpleArraySignedByte8:
5412 #ifdef type_SimpleArraySignedByte16
5413 case type_SimpleArraySignedByte16:
5415 #ifdef type_SimpleArraySignedByte30
5416 case type_SimpleArraySignedByte30:
5418 #ifdef type_SimpleArraySignedByte32
5419 case type_SimpleArraySignedByte32:
5421 case type_SimpleArraySingleFloat:
5422 case type_SimpleArrayDoubleFloat:
5423 #ifdef type_SimpleArrayComplexLongFloat
5424 case type_SimpleArrayLongFloat:
5426 #ifdef type_SimpleArrayComplexSingleFloat
5427 case type_SimpleArrayComplexSingleFloat:
5429 #ifdef type_SimpleArrayComplexDoubleFloat
5430 case type_SimpleArrayComplexDoubleFloat:
5432 #ifdef type_SimpleArrayComplexLongFloat
5433 case type_SimpleArrayComplexLongFloat:
5436 case type_WeakPointer:
5437 count = (sizetab[TypeOf(*start)])(start);
5453 /* FIXME: It would be nice to make names consistent so that
5454 * foo_size meant size *in* *bytes* instead of size in some
5455 * arbitrary units. (Yes, this caused a bug, how did you guess?:-)
5456 * Some counts of lispobjs are called foo_count; it might be good
5457 * to grep for all foo_size and rename the appropriate ones to
5459 int read_only_space_size =
5460 (lispobj*)SymbolValue(READ_ONLY_SPACE_FREE_POINTER)
5461 - (lispobj*)READ_ONLY_SPACE_START;
5462 int static_space_size =
5463 (lispobj*)SymbolValue(STATIC_SPACE_FREE_POINTER)
5464 - (lispobj*)STATIC_SPACE_START;
5465 int binding_stack_size =
5466 (lispobj*)SymbolValue(BINDING_STACK_POINTER)
5467 - (lispobj*)BINDING_STACK_START;
5469 verify_space((lispobj*)READ_ONLY_SPACE_START, read_only_space_size);
5470 verify_space((lispobj*)STATIC_SPACE_START , static_space_size);
5471 verify_space((lispobj*)BINDING_STACK_START , binding_stack_size);
5475 verify_generation(int generation)
5479 for (i = 0; i < last_free_page; i++) {
5480 if ((page_table[i].allocated != FREE_PAGE)
5481 && (page_table[i].bytes_used != 0)
5482 && (page_table[i].gen == generation)) {
5484 int region_allocation = page_table[i].allocated;
5486 /* This should be the start of a contiguous block */
5487 gc_assert(page_table[i].first_object_offset == 0);
5489 /* Need to find the full extent of this contiguous block in case
5490 objects span pages. */
5492 /* Now work forward until the end of this contiguous area is
5494 for (last_page = i; ;last_page++)
5495 /* Check whether this is the last page in this contiguous
5497 if ((page_table[last_page].bytes_used < 4096)
5498 /* Or it is 4096 and is the last in the block */
5499 || (page_table[last_page+1].allocated != region_allocation)
5500 || (page_table[last_page+1].bytes_used == 0)
5501 || (page_table[last_page+1].gen != generation)
5502 || (page_table[last_page+1].first_object_offset == 0))
5505 verify_space(page_address(i), (page_table[last_page].bytes_used
5506 + (last_page-i)*4096)/4);
5512 /* Check the all the free space is zero filled. */
5514 verify_zero_fill(void)
5518 for (page = 0; page < last_free_page; page++) {
5519 if (page_table[page].allocated == FREE_PAGE) {
5520 /* The whole page should be zero filled. */
5521 int *start_addr = (int *)page_address(page);
5524 for (i = 0; i < size; i++) {
5525 if (start_addr[i] != 0) {
5526 lose("free page not zero at %x", start_addr + i);
5530 int free_bytes = 4096 - page_table[page].bytes_used;
5531 if (free_bytes > 0) {
5532 int *start_addr = (int *)((unsigned)page_address(page)
5533 + page_table[page].bytes_used);
5534 int size = free_bytes / 4;
5536 for (i = 0; i < size; i++) {
5537 if (start_addr[i] != 0) {
5538 lose("free region not zero at %x", start_addr + i);
5546 /* External entry point for verify_zero_fill */
5548 gencgc_verify_zero_fill(void)
5550 /* Flush the alloc regions updating the tables. */
5551 boxed_region.free_pointer = current_region_free_pointer;
5552 gc_alloc_update_page_tables(0, &boxed_region);
5553 gc_alloc_update_page_tables(1, &unboxed_region);
5554 SHOW("verifying zero fill");
5556 current_region_free_pointer = boxed_region.free_pointer;
5557 current_region_end_addr = boxed_region.end_addr;
5561 verify_dynamic_space(void)
5565 for (i = 0; i < NUM_GENERATIONS; i++)
5566 verify_generation(i);
5568 if (gencgc_enable_verify_zero_fill)
5572 /* Write-protect all the dynamic boxed pages in the given generation. */
5574 write_protect_generation_pages(int generation)
5578 gc_assert(generation < NUM_GENERATIONS);
5580 for (i = 0; i < last_free_page; i++)
5581 if ((page_table[i].allocated == BOXED_PAGE)
5582 && (page_table[i].bytes_used != 0)
5583 && (page_table[i].gen == generation)) {
5586 page_start = (void *)page_address(i);
5588 os_protect(page_start,
5590 OS_VM_PROT_READ | OS_VM_PROT_EXECUTE);
5592 /* Note the page as protected in the page tables. */
5593 page_table[i].write_protected = 1;
5596 if (gencgc_verbose > 1) {
5598 "/write protected %d of %d pages in generation %d\n",
5599 count_write_protect_generation_pages(generation),
5600 count_generation_pages(generation),
5605 /* Garbage collect a generation. If raise is 0 the remains of the
5606 * generation are not raised to the next generation. */
5608 garbage_collect_generation(int generation, int raise)
5610 unsigned long bytes_freed;
5612 unsigned long read_only_space_size, static_space_size;
5614 gc_assert(generation <= (NUM_GENERATIONS-1));
5616 /* The oldest generation can't be raised. */
5617 gc_assert((generation != (NUM_GENERATIONS-1)) || (raise == 0));
5619 /* Initialize the weak pointer list. */
5620 weak_pointers = NULL;
5622 /* When a generation is not being raised it is transported to a
5623 * temporary generation (NUM_GENERATIONS), and lowered when
5624 * done. Set up this new generation. There should be no pages
5625 * allocated to it yet. */
5627 gc_assert(generations[NUM_GENERATIONS].bytes_allocated == 0);
5629 /* Set the global src and dest. generations */
5630 from_space = generation;
5632 new_space = generation+1;
5634 new_space = NUM_GENERATIONS;
5636 /* Change to a new space for allocation, resetting the alloc_start_page */
5637 gc_alloc_generation = new_space;
5638 generations[new_space].alloc_start_page = 0;
5639 generations[new_space].alloc_unboxed_start_page = 0;
5640 generations[new_space].alloc_large_start_page = 0;
5641 generations[new_space].alloc_large_unboxed_start_page = 0;
5643 /* Before any pointers are preserved, the dont_move flags on the
5644 * pages need to be cleared. */
5645 for (i = 0; i < last_free_page; i++)
5646 page_table[i].dont_move = 0;
5648 /* Un-write-protect the old-space pages. This is essential for the
5649 * promoted pages as they may contain pointers into the old-space
5650 * which need to be scavenged. It also helps avoid unnecessary page
5651 * faults as forwarding pointer are written into them. They need to
5652 * be un-protected anyway before unmapping later. */
5653 unprotect_oldspace();
5655 /* Scavenge the stack's conservative roots. */
5658 for (ptr = (lispobj **)CONTROL_STACK_END - 1;
5659 ptr > (lispobj **)&raise;
5661 preserve_pointer(*ptr);
5664 #ifdef CONTROL_STACKS
5665 scavenge_thread_stacks();
5668 if (gencgc_verbose > 1) {
5669 int num_dont_move_pages = count_dont_move_pages();
5671 "/non-movable pages due to conservative pointers = %d (%d bytes)\n",
5672 num_dont_move_pages,
5673 /* FIXME: 4096 should be symbolic constant here and
5674 * prob'ly elsewhere too. */
5675 num_dont_move_pages * 4096));
5678 /* Scavenge all the rest of the roots. */
5680 /* Scavenge the Lisp functions of the interrupt handlers, taking
5681 * care to avoid SIG_DFL, SIG_IGN. */
5682 for (i = 0; i < NSIG; i++) {
5683 union interrupt_handler handler = interrupt_handlers[i];
5684 if (!ARE_SAME_HANDLER(handler.c, SIG_IGN) &&
5685 !ARE_SAME_HANDLER(handler.c, SIG_DFL)) {
5686 scavenge((lispobj *)(interrupt_handlers + i), 1);
5690 /* Scavenge the binding stack. */
5691 scavenge( (lispobj *) BINDING_STACK_START,
5692 (lispobj *)SymbolValue(BINDING_STACK_POINTER) -
5693 (lispobj *)BINDING_STACK_START);
5695 if (SymbolValue(SCAVENGE_READ_ONLY_SPACE) != NIL) {
5696 read_only_space_size =
5697 (lispobj*)SymbolValue(READ_ONLY_SPACE_FREE_POINTER) -
5698 (lispobj*)READ_ONLY_SPACE_START;
5700 "/scavenge read only space: %d bytes\n",
5701 read_only_space_size * sizeof(lispobj)));
5702 scavenge( (lispobj *) READ_ONLY_SPACE_START, read_only_space_size);
5706 (lispobj *)SymbolValue(STATIC_SPACE_FREE_POINTER) -
5707 (lispobj *)STATIC_SPACE_START;
5708 if (gencgc_verbose > 1)
5710 "/scavenge static space: %d bytes\n",
5711 static_space_size * sizeof(lispobj)));
5712 scavenge( (lispobj *) STATIC_SPACE_START, static_space_size);
5714 /* All generations but the generation being GCed need to be
5715 * scavenged. The new_space generation needs special handling as
5716 * objects may be moved in - it is handled separately below. */
5717 for (i = 0; i < NUM_GENERATIONS; i++)
5718 if ((i != generation) && (i != new_space))
5719 scavenge_generation(i);
5721 /* Finally scavenge the new_space generation. Keep going until no
5722 * more objects are moved into the new generation */
5723 scavenge_newspace_generation(new_space);
5725 #define RESCAN_CHECK 0
5727 /* As a check re-scavenge the newspace once; no new objects should
5730 int old_bytes_allocated = bytes_allocated;
5731 int bytes_allocated;
5733 /* Start with a full scavenge. */
5734 scavenge_newspace_generation_one_scan(new_space);
5736 /* Flush the current regions, updating the tables. */
5737 gc_alloc_update_page_tables(0, &boxed_region);
5738 gc_alloc_update_page_tables(1, &unboxed_region);
5740 bytes_allocated = bytes_allocated - old_bytes_allocated;
5742 if (bytes_allocated != 0) {
5743 lose("Rescan of new_space allocated %d more bytes.",
5749 scan_weak_pointers();
5751 /* Flush the current regions, updating the tables. */
5752 gc_alloc_update_page_tables(0, &boxed_region);
5753 gc_alloc_update_page_tables(1, &unboxed_region);
5755 /* Free the pages in oldspace, but not those marked dont_move. */
5756 bytes_freed = free_oldspace();
5758 /* If the GC is not raising the age then lower the generation back
5759 * to its normal generation number */
5761 for (i = 0; i < last_free_page; i++)
5762 if ((page_table[i].bytes_used != 0)
5763 && (page_table[i].gen == NUM_GENERATIONS))
5764 page_table[i].gen = generation;
5765 gc_assert(generations[generation].bytes_allocated == 0);
5766 generations[generation].bytes_allocated =
5767 generations[NUM_GENERATIONS].bytes_allocated;
5768 generations[NUM_GENERATIONS].bytes_allocated = 0;
5771 /* Reset the alloc_start_page for generation. */
5772 generations[generation].alloc_start_page = 0;
5773 generations[generation].alloc_unboxed_start_page = 0;
5774 generations[generation].alloc_large_start_page = 0;
5775 generations[generation].alloc_large_unboxed_start_page = 0;
5777 if (generation >= verify_gens) {
5781 verify_dynamic_space();
5784 /* Set the new gc trigger for the GCed generation. */
5785 generations[generation].gc_trigger =
5786 generations[generation].bytes_allocated
5787 + generations[generation].bytes_consed_between_gc;
5790 generations[generation].num_gc = 0;
5792 ++generations[generation].num_gc;
5795 /* Update last_free_page then ALLOCATION_POINTER */
5797 update_x86_dynamic_space_free_pointer(void)
5802 for (i = 0; i < NUM_PAGES; i++)
5803 if ((page_table[i].allocated != FREE_PAGE)
5804 && (page_table[i].bytes_used != 0))
5807 last_free_page = last_page+1;
5809 SetSymbolValue(ALLOCATION_POINTER,
5810 (lispobj)(((char *)heap_base) + last_free_page*4096));
5811 return 0; /* dummy value: return something ... */
5814 /* GC all generations below last_gen, raising their objects to the
5815 * next generation until all generations below last_gen are empty.
5816 * Then if last_gen is due for a GC then GC it. In the special case
5817 * that last_gen==NUM_GENERATIONS, the last generation is always
5818 * GC'ed. The valid range for last_gen is: 0,1,...,NUM_GENERATIONS.
5820 * The oldest generation to be GCed will always be
5821 * gencgc_oldest_gen_to_gc, partly ignoring last_gen if necessary. */
5823 collect_garbage(unsigned last_gen)
5830 boxed_region.free_pointer = current_region_free_pointer;
5832 FSHOW((stderr, "/entering collect_garbage(%d)\n", last_gen));
5834 if (last_gen > NUM_GENERATIONS) {
5836 "/collect_garbage: last_gen = %d, doing a level 0 GC\n",
5841 /* Flush the alloc regions updating the tables. */
5842 gc_alloc_update_page_tables(0, &boxed_region);
5843 gc_alloc_update_page_tables(1, &unboxed_region);
5845 /* Verify the new objects created by Lisp code. */
5846 if (pre_verify_gen_0) {
5847 SHOW((stderr, "pre-checking generation 0\n"));
5848 verify_generation(0);
5851 if (gencgc_verbose > 1)
5852 print_generation_stats(0);
5855 /* Collect the generation. */
5857 if (gen >= gencgc_oldest_gen_to_gc) {
5858 /* Never raise the oldest generation. */
5863 || (generations[gen].num_gc >= generations[gen].trigger_age);
5866 if (gencgc_verbose > 1) {
5868 "Starting GC of generation %d with raise=%d alloc=%d trig=%d GCs=%d\n",
5871 generations[gen].bytes_allocated,
5872 generations[gen].gc_trigger,
5873 generations[gen].num_gc));
5876 /* If an older generation is being filled then update its memory
5879 generations[gen+1].cum_sum_bytes_allocated +=
5880 generations[gen+1].bytes_allocated;
5883 garbage_collect_generation(gen, raise);
5885 /* Reset the memory age cum_sum. */
5886 generations[gen].cum_sum_bytes_allocated = 0;
5888 if (gencgc_verbose > 1) {
5889 FSHOW((stderr, "GC of generation %d finished:\n", gen));
5890 print_generation_stats(0);
5894 } while ((gen <= gencgc_oldest_gen_to_gc)
5895 && ((gen < last_gen)
5896 || ((gen <= gencgc_oldest_gen_to_gc)
5898 && (generations[gen].bytes_allocated
5899 > generations[gen].gc_trigger)
5900 && (gen_av_mem_age(gen)
5901 > generations[gen].min_av_mem_age))));
5903 /* Now if gen-1 was raised all generations before gen are empty.
5904 * If it wasn't raised then all generations before gen-1 are empty.
5906 * Now objects within this gen's pages cannot point to younger
5907 * generations unless they are written to. This can be exploited
5908 * by write-protecting the pages of gen; then when younger
5909 * generations are GCed only the pages which have been written
5914 gen_to_wp = gen - 1;
5916 /* There's not much point in WPing pages in generation 0 as it is
5917 * never scavenged (except promoted pages). */
5918 if ((gen_to_wp > 0) && enable_page_protection) {
5919 /* Check that they are all empty. */
5920 for (i = 0; i < gen_to_wp; i++) {
5921 if (generations[i].bytes_allocated)
5922 lose("trying to write-protect gen. %d when gen. %d nonempty",
5925 write_protect_generation_pages(gen_to_wp);
5928 /* Set gc_alloc back to generation 0. The current regions should
5929 * be flushed after the above GCs */
5930 gc_assert((boxed_region.free_pointer - boxed_region.start_addr) == 0);
5931 gc_alloc_generation = 0;
5933 update_x86_dynamic_space_free_pointer();
5935 /* This is now done by Lisp SCRUB-CONTROL-STACK in Lisp SUB-GC, so we
5936 * needn't do it here: */
5939 current_region_free_pointer = boxed_region.free_pointer;
5940 current_region_end_addr = boxed_region.end_addr;
5942 SHOW("returning from collect_garbage");
5945 /* This is called by Lisp PURIFY when it is finished. All live objects
5946 * will have been moved to the RO and Static heaps. The dynamic space
5947 * will need a full re-initialization. We don't bother having Lisp
5948 * PURIFY flush the current gc_alloc region, as the page_tables are
5949 * re-initialized, and every page is zeroed to be sure. */
5955 if (gencgc_verbose > 1)
5956 SHOW("entering gc_free_heap");
5958 for (page = 0; page < NUM_PAGES; page++) {
5959 /* Skip free pages which should already be zero filled. */
5960 if (page_table[page].allocated != FREE_PAGE) {
5961 void *page_start, *addr;
5963 /* Mark the page free. The other slots are assumed invalid
5964 * when it is a FREE_PAGE and bytes_used is 0 and it
5965 * should not be write-protected -- except that the
5966 * generation is used for the current region but it sets
5968 page_table[page].allocated = FREE_PAGE;
5969 page_table[page].bytes_used = 0;
5971 /* Zero the page. */
5972 page_start = (void *)page_address(page);
5974 /* First, remove any write-protection. */
5975 os_protect(page_start, 4096, OS_VM_PROT_ALL);
5976 page_table[page].write_protected = 0;
5978 os_invalidate(page_start,4096);
5979 addr = os_validate(page_start,4096);
5980 if (addr == NULL || addr != page_start) {
5981 lose("gc_free_heap: page moved, 0x%08x ==> 0x%08x",
5985 } else if (gencgc_zero_check_during_free_heap) {
5986 /* Double-check that the page is zero filled. */
5988 gc_assert(page_table[page].allocated == FREE_PAGE);
5989 gc_assert(page_table[page].bytes_used == 0);
5990 page_start = (int *)page_address(page);
5991 for (i=0; i<1024; i++) {
5992 if (page_start[i] != 0) {
5993 lose("free region not zero at %x", page_start + i);
5999 bytes_allocated = 0;
6001 /* Initialize the generations. */
6002 for (page = 0; page < NUM_GENERATIONS; page++) {
6003 generations[page].alloc_start_page = 0;
6004 generations[page].alloc_unboxed_start_page = 0;
6005 generations[page].alloc_large_start_page = 0;
6006 generations[page].alloc_large_unboxed_start_page = 0;
6007 generations[page].bytes_allocated = 0;
6008 generations[page].gc_trigger = 2000000;
6009 generations[page].num_gc = 0;
6010 generations[page].cum_sum_bytes_allocated = 0;
6013 if (gencgc_verbose > 1)
6014 print_generation_stats(0);
6016 /* Initialize gc_alloc */
6017 gc_alloc_generation = 0;
6018 boxed_region.first_page = 0;
6019 boxed_region.last_page = -1;
6020 boxed_region.start_addr = page_address(0);
6021 boxed_region.free_pointer = page_address(0);
6022 boxed_region.end_addr = page_address(0);
6024 unboxed_region.first_page = 0;
6025 unboxed_region.last_page = -1;
6026 unboxed_region.start_addr = page_address(0);
6027 unboxed_region.free_pointer = page_address(0);
6028 unboxed_region.end_addr = page_address(0);
6030 #if 0 /* Lisp PURIFY is currently running on the C stack so don't do this. */
6035 SetSymbolValue(ALLOCATION_POINTER, (lispobj)((char *)heap_base));
6037 current_region_free_pointer = boxed_region.free_pointer;
6038 current_region_end_addr = boxed_region.end_addr;
6040 if (verify_after_free_heap) {
6041 /* Check whether purify has left any bad pointers. */
6043 SHOW("checking after free_heap\n");
6055 heap_base = (void*)DYNAMIC_SPACE_START;
6057 /* Initialize each page structure. */
6058 for (i = 0; i < NUM_PAGES; i++) {
6059 /* Initialize all pages as free. */
6060 page_table[i].allocated = FREE_PAGE;
6061 page_table[i].bytes_used = 0;
6063 /* Pages are not write-protected at startup. */
6064 page_table[i].write_protected = 0;
6067 bytes_allocated = 0;
6069 /* Initialize the generations. */
6070 for (i = 0; i < NUM_GENERATIONS; i++) {
6071 generations[i].alloc_start_page = 0;
6072 generations[i].alloc_unboxed_start_page = 0;
6073 generations[i].alloc_large_start_page = 0;
6074 generations[i].alloc_large_unboxed_start_page = 0;
6075 generations[i].bytes_allocated = 0;
6076 generations[i].gc_trigger = 2000000;
6077 generations[i].num_gc = 0;
6078 generations[i].cum_sum_bytes_allocated = 0;
6079 /* the tune-able parameters */
6080 generations[i].bytes_consed_between_gc = 2000000;
6081 generations[i].trigger_age = 1;
6082 generations[i].min_av_mem_age = 0.75;
6085 /* Initialize gc_alloc. */
6086 gc_alloc_generation = 0;
6087 boxed_region.first_page = 0;
6088 boxed_region.last_page = -1;
6089 boxed_region.start_addr = page_address(0);
6090 boxed_region.free_pointer = page_address(0);
6091 boxed_region.end_addr = page_address(0);
6093 unboxed_region.first_page = 0;
6094 unboxed_region.last_page = -1;
6095 unboxed_region.start_addr = page_address(0);
6096 unboxed_region.free_pointer = page_address(0);
6097 unboxed_region.end_addr = page_address(0);
6101 current_region_free_pointer = boxed_region.free_pointer;
6102 current_region_end_addr = boxed_region.end_addr;
6105 /* Pick up the dynamic space from after a core load.
6107 * The ALLOCATION_POINTER points to the end of the dynamic space.
6109 * XX A scan is needed to identify the closest first objects for pages. */
6111 gencgc_pickup_dynamic(void)
6114 int addr = DYNAMIC_SPACE_START;
6115 int alloc_ptr = SymbolValue(ALLOCATION_POINTER);
6117 /* Initialize the first region. */
6119 page_table[page].allocated = BOXED_PAGE;
6120 page_table[page].gen = 0;
6121 page_table[page].bytes_used = 4096;
6122 page_table[page].large_object = 0;
6123 page_table[page].first_object_offset =
6124 (void *)DYNAMIC_SPACE_START - page_address(page);
6127 } while (addr < alloc_ptr);
6129 generations[0].bytes_allocated = 4096*page;
6130 bytes_allocated = 4096*page;
6132 current_region_free_pointer = boxed_region.free_pointer;
6133 current_region_end_addr = boxed_region.end_addr;
6136 /* a counter for how deep we are in alloc(..) calls */
6137 int alloc_entered = 0;
6139 /* alloc(..) is the external interface for memory allocation. It
6140 * allocates to generation 0. It is not called from within the garbage
6141 * collector as it is only external uses that need the check for heap
6142 * size (GC trigger) and to disable the interrupts (interrupts are
6143 * always disabled during a GC).
6145 * The vops that call alloc(..) assume that the returned space is zero-filled.
6146 * (E.g. the most significant word of a 2-word bignum in MOVE-FROM-UNSIGNED.)
6148 * The check for a GC trigger is only performed when the current
6149 * region is full, so in most cases it's not needed. Further MAYBE-GC
6150 * is only called once because Lisp will remember "need to collect
6151 * garbage" and get around to it when it can. */
6155 /* Check for alignment allocation problems. */
6156 gc_assert((((unsigned)current_region_free_pointer & 0x7) == 0)
6157 && ((nbytes & 0x7) == 0));
6159 if (SymbolValue(PSEUDO_ATOMIC_ATOMIC)) {/* if already in a pseudo atomic */
6161 void *new_free_pointer;
6164 if (alloc_entered) {
6165 SHOW("alloc re-entered in already-pseudo-atomic case");
6169 /* Check whether there is room in the current region. */
6170 new_free_pointer = current_region_free_pointer + nbytes;
6172 /* FIXME: Shouldn't we be doing some sort of lock here, to
6173 * keep from getting screwed if an interrupt service routine
6174 * allocates memory between the time we calculate new_free_pointer
6175 * and the time we write it back to current_region_free_pointer?
6176 * Perhaps I just don't understand pseudo-atomics..
6178 * Perhaps I don't. It looks as though what happens is if we
6179 * were interrupted any time during the pseudo-atomic
6180 * interval (which includes now) we discard the allocated
6181 * memory and try again. So, at least we don't return
6182 * a memory area that was allocated out from underneath us
6183 * by code in an ISR.
6184 * Still, that doesn't seem to prevent
6185 * current_region_free_pointer from getting corrupted:
6186 * We read current_region_free_pointer.
6187 * They read current_region_free_pointer.
6188 * They write current_region_free_pointer.
6189 * We write current_region_free_pointer, scribbling over
6190 * whatever they wrote. */
6192 if (new_free_pointer <= boxed_region.end_addr) {
6193 /* If so then allocate from the current region. */
6194 void *new_obj = current_region_free_pointer;
6195 current_region_free_pointer = new_free_pointer;
6197 return((void *)new_obj);
6200 if (auto_gc_trigger && bytes_allocated > auto_gc_trigger) {
6201 /* Double the trigger. */
6202 auto_gc_trigger *= 2;
6204 /* Exit the pseudo-atomic. */
6205 SetSymbolValue(PSEUDO_ATOMIC_ATOMIC, make_fixnum(0));
6206 if (SymbolValue(PSEUDO_ATOMIC_INTERRUPTED) != 0) {
6207 /* Handle any interrupts that occurred during
6209 do_pending_interrupt();
6211 funcall0(SymbolFunction(MAYBE_GC));
6212 /* Re-enter the pseudo-atomic. */
6213 SetSymbolValue(PSEUDO_ATOMIC_INTERRUPTED, make_fixnum(0));
6214 SetSymbolValue(PSEUDO_ATOMIC_ATOMIC, make_fixnum(1));
6217 /* Call gc_alloc. */
6218 boxed_region.free_pointer = current_region_free_pointer;
6220 void *new_obj = gc_alloc(nbytes);
6221 current_region_free_pointer = boxed_region.free_pointer;
6222 current_region_end_addr = boxed_region.end_addr;
6228 void *new_free_pointer;
6231 /* At least wrap this allocation in a pseudo atomic to prevent
6232 * gc_alloc from being re-entered. */
6233 SetSymbolValue(PSEUDO_ATOMIC_INTERRUPTED, make_fixnum(0));
6234 SetSymbolValue(PSEUDO_ATOMIC_ATOMIC, make_fixnum(1));
6237 SHOW("alloc re-entered in not-already-pseudo-atomic case");
6240 /* Check whether there is room in the current region. */
6241 new_free_pointer = current_region_free_pointer + nbytes;
6243 if (new_free_pointer <= boxed_region.end_addr) {
6244 /* If so then allocate from the current region. */
6245 void *new_obj = current_region_free_pointer;
6246 current_region_free_pointer = new_free_pointer;
6248 SetSymbolValue(PSEUDO_ATOMIC_ATOMIC, make_fixnum(0));
6249 if (SymbolValue(PSEUDO_ATOMIC_INTERRUPTED)) {
6250 /* Handle any interrupts that occurred during
6252 do_pending_interrupt();
6256 return((void *)new_obj);
6259 /* KLUDGE: There's lots of code around here shared with the
6260 * the other branch. Is there some way to factor out the
6261 * duplicate code? -- WHN 19991129 */
6262 if (auto_gc_trigger && bytes_allocated > auto_gc_trigger) {
6263 /* Double the trigger. */
6264 auto_gc_trigger *= 2;
6266 /* Exit the pseudo atomic. */
6267 SetSymbolValue(PSEUDO_ATOMIC_ATOMIC, make_fixnum(0));
6268 if (SymbolValue(PSEUDO_ATOMIC_INTERRUPTED) != 0) {
6269 /* Handle any interrupts that occurred during
6271 do_pending_interrupt();
6273 funcall0(SymbolFunction(MAYBE_GC));
6277 /* Else call gc_alloc. */
6278 boxed_region.free_pointer = current_region_free_pointer;
6279 result = gc_alloc(nbytes);
6280 current_region_free_pointer = boxed_region.free_pointer;
6281 current_region_end_addr = boxed_region.end_addr;
6284 SetSymbolValue(PSEUDO_ATOMIC_ATOMIC, make_fixnum(0));
6285 if (SymbolValue(PSEUDO_ATOMIC_INTERRUPTED) != 0) {
6286 /* Handle any interrupts that occurred during
6288 do_pending_interrupt();
6297 * noise to manipulate the gc trigger stuff
6301 set_auto_gc_trigger(os_vm_size_t dynamic_usage)
6303 auto_gc_trigger += dynamic_usage;
6307 clear_auto_gc_trigger(void)
6309 auto_gc_trigger = 0;
6312 /* Find the code object for the given pc, or return NULL on failure.
6314 * FIXME: PC shouldn't be lispobj*, should it? Maybe void*? */
6316 component_ptr_from_pc(lispobj *pc)
6318 lispobj *object = NULL;
6320 if ( (object = search_read_only_space(pc)) )
6322 else if ( (object = search_static_space(pc)) )
6325 object = search_dynamic_space(pc);
6327 if (object) /* if we found something */
6328 if (TypeOf(*object) == type_CodeHeader) /* if it's a code object */
6335 * shared support for the OS-dependent signal handlers which
6336 * catch GENCGC-related write-protect violations
6339 /* Depending on which OS we're running under, different signals might
6340 * be raised for a violation of write protection in the heap. This
6341 * function factors out the common generational GC magic which needs
6342 * to invoked in this case, and should be called from whatever signal
6343 * handler is appropriate for the OS we're running under.
6345 * Return true if this signal is a normal generational GC thing that
6346 * we were able to handle, or false if it was abnormal and control
6347 * should fall through to the general SIGSEGV/SIGBUS/whatever logic. */
6349 gencgc_handle_wp_violation(void* fault_addr)
6351 int page_index = find_page_index(fault_addr);
6353 #if defined QSHOW_SIGNALS
6354 FSHOW((stderr, "heap WP violation? fault_addr=%x, page_index=%d\n",
6355 fault_addr, page_index));
6358 /* Check whether the fault is within the dynamic space. */
6359 if (page_index == (-1)) {
6361 /* not within the dynamic space -- not our responsibility */
6366 /* The only acceptable reason for an signal like this from the
6367 * heap is that the generational GC write-protected the page. */
6368 if (page_table[page_index].write_protected != 1) {
6369 lose("access failure in heap page not marked as write-protected");
6372 /* Unprotect the page. */
6373 os_protect(page_address(page_index), 4096, OS_VM_PROT_ALL);
6374 page_table[page_index].write_protected = 0;
6375 page_table[page_index].write_protected_cleared = 1;
6377 /* Don't worry, we can handle it. */