+int
+gc_find_freeish_pages(int *restart_page_ptr, int nbytes, int unboxed, struct alloc_region *alloc_region)
+{
+ /* if alloc_region is 0, we assume this is for a potentially large
+ object */
+ int first_page;
+ int last_page;
+ int region_size;
+ int restart_page=*restart_page_ptr;
+ int bytes_found;
+ int num_pages;
+ int large = !alloc_region && (nbytes >= large_object_size);
+
+ gc_assert(free_pages_lock);
+ /* Search for a contiguous free space of at least nbytes. If it's a
+ large object then align it on a page boundary by searching for a
+ free page. */
+
+ /* To allow the allocation of small objects without the danger of
+ using a page in the current boxed region, the search starts after
+ the current boxed free region. XX could probably keep a page
+ index ahead of the current region and bumped up here to save a
+ lot of re-scanning. */
+
+ do {
+ first_page = restart_page;
+ if (large)
+ while ((first_page < NUM_PAGES)
+ && (page_table[first_page].allocated != FREE_PAGE))
+ first_page++;
+ else
+ while (first_page < NUM_PAGES) {
+ if(page_table[first_page].allocated == FREE_PAGE)
+ break;
+ /* I don't know why we need the gen=0 test, but it
+ * breaks randomly if that's omitted -dan 2003.02.26
+ */
+ if((page_table[first_page].allocated ==
+ (unboxed ? UNBOXED_PAGE : BOXED_PAGE)) &&
+ (page_table[first_page].large_object == 0) &&
+ (gc_alloc_generation == 0) &&
+ (page_table[first_page].gen == gc_alloc_generation) &&
+ (page_table[first_page].bytes_used < (4096-32)) &&
+ (page_table[first_page].write_protected == 0) &&
+ (page_table[first_page].dont_move == 0))
+ break;
+ first_page++;
+ }
+
+ if (first_page >= NUM_PAGES) {
+ fprintf(stderr,
+ "Argh! gc_find_free_space failed (first_page), nbytes=%d.\n",
+ nbytes);
+ print_generation_stats(1);
+ lose(NULL);
+ }
+
+ gc_assert(page_table[first_page].write_protected == 0);
+
+ last_page = first_page;
+ bytes_found = 4096 - page_table[first_page].bytes_used;
+ num_pages = 1;
+ while (((bytes_found < nbytes)
+ || (alloc_region && (num_pages < 2)))
+ && (last_page < (NUM_PAGES-1))
+ && (page_table[last_page+1].allocated == FREE_PAGE)) {
+ last_page++;
+ num_pages++;
+ bytes_found += 4096;
+ gc_assert(page_table[last_page].write_protected == 0);
+ }
+
+ region_size = (4096 - page_table[first_page].bytes_used)
+ + 4096*(last_page-first_page);
+
+ gc_assert(bytes_found == region_size);
+ restart_page = last_page + 1;
+ } while ((restart_page < NUM_PAGES) && (bytes_found < nbytes));
+
+ /* Check for a failure */
+ if ((restart_page >= NUM_PAGES) && (bytes_found < nbytes)) {
+ fprintf(stderr,
+ "Argh! gc_find_freeish_pages failed (restart_page), nbytes=%d.\n",
+ nbytes);
+ print_generation_stats(1);
+ lose(NULL);
+ }
+ *restart_page_ptr=first_page;
+ return last_page;
+}
+