/[cmucl]/src/lisp/gencgc.c
ViewVC logotype

Contents of /src/lisp/gencgc.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.95.2.1.2.2 - (hide annotations)
Wed Apr 29 20:21:58 2009 UTC (4 years, 11 months ago) by rtoy
Branch: unicode-utf16-extfmt-branch
Changes since 1.95.2.1.2.1: +23 -1 lines
File MIME type: text/plain
Work around an issue with allocation on sparc (and probably ppc).  See
comments gc_alloc for reasons.
1 dtc 1.1 /*
2     * Generational Conservative Garbage Collector for CMUCL x86.
3     *
4     * This code was written by Douglas T. Crosher, based on Public Domain
5     * codes from Carnegie Mellon University. This code has been placed in
6     * the public domain, and is provided 'as is'.
7     *
8 dtc 1.14 * Douglas Crosher, 1996, 1997, 1998, 1999.
9 dtc 1.1 *
10 rtoy 1.95.2.1.2.2 * $Header: /tiger/var/lib/cvsroots/cmucl/src/lisp/gencgc.c,v 1.95.2.1.2.2 2009/04/29 20:21:58 rtoy Exp $
11 dtc 1.14 *
12     */
13 dtc 1.1
14 cshapiro 1.92 #include <limits.h>
15 dtc 1.1 #include <stdio.h>
16 gerd 1.32 #include <stdlib.h>
17 dtc 1.1 #include <signal.h>
18 rtoy 1.57 #include <string.h>
19 dtc 1.1 #include "lisp.h"
20 dtc 1.24 #include "arch.h"
21 dtc 1.1 #include "internals.h"
22     #include "os.h"
23     #include "globals.h"
24     #include "interrupt.h"
25     #include "validate.h"
26     #include "lispregs.h"
27 dtc 1.24 #include "interr.h"
28 dtc 1.1 #include "gencgc.h"
29    
30 rtoy 1.81 /*
31     * This value in a hash table hash-vector means that the key uses
32     * EQ-based hashing. That is, the key might be using EQ or EQL for
33     * the test. This MUST match the value used in hash-new.lisp!
34     */
35     #define EQ_BASED_HASH_VALUE 0x80000000
36 rtoy 1.67
37 dtc 1.1 #define gc_abort() lose("GC invariant lost! File \"%s\", line %d\n", \
38     __FILE__, __LINE__)
39    
40 cwang 1.55 #if (defined(i386) || defined(__x86_64))
41 gerd 1.35
42     #define set_alloc_pointer(value) \
43     SetSymbolValue (ALLOCATION_POINTER, (value))
44     #define get_alloc_pointer() \
45     SymbolValue (ALLOCATION_POINTER)
46     #define get_binding_stack_pointer() \
47     SymbolValue (BINDING_STACK_POINTER)
48     #define get_pseudo_atomic_atomic() \
49     SymbolValue (PSEUDO_ATOMIC_ATOMIC)
50     #define set_pseudo_atomic_atomic() \
51     SetSymbolValue (PSEUDO_ATOMIC_ATOMIC, make_fixnum (1))
52     #define clr_pseudo_atomic_atomic() \
53     SetSymbolValue (PSEUDO_ATOMIC_ATOMIC, make_fixnum (0))
54     #define get_pseudo_atomic_interrupted() \
55     SymbolValue (PSEUDO_ATOMIC_INTERRUPTED)
56     #define clr_pseudo_atomic_interrupted() \
57     SetSymbolValue (PSEUDO_ATOMIC_INTERRUPTED, make_fixnum (0))
58    
59 toy 1.47 #define set_current_region_free(value) \
60     SetSymbolValue(CURRENT_REGION_FREE_POINTER, (value))
61     #define set_current_region_end(value) \
62     SetSymbolValue(CURRENT_REGION_END_ADDR, (value))
63     #define get_current_region_free() \
64     SymbolValue(CURRENT_REGION_FREE_POINTER)
65    
66     #define set_current_region_end(value) \
67     SetSymbolValue(CURRENT_REGION_END_ADDR, (value))
68    
69 toy 1.33 #elif defined(sparc)
70 gerd 1.35
71 toy 1.37 /*
72     * current_dynamic_space_free_pointer contains the pseudo-atomic
73     * stuff, so we need to preserve those bits when we give it a value.
74     * This value better not have any bits set there either!
75     */
76 toy 1.50
77     /*
78     * On sparc, we don't need to set the alloc_pointer in the code here
79     * because the alloc pointer (current_dynamic_space_free_pointer) is
80     * the same as *current-region-free-pointer* and is stored in
81     * alloc-tn.
82     */
83 rtoy 1.66 #define set_alloc_pointer(value)
84 gerd 1.35 #define get_alloc_pointer() \
85 toy 1.45 ((unsigned long) current_dynamic_space_free_pointer & ~lowtag_Mask)
86 gerd 1.35 #define get_binding_stack_pointer() \
87     (current_binding_stack_pointer)
88 toy 1.33 #define get_pseudo_atomic_atomic() \
89 toy 1.41 ((unsigned long)current_dynamic_space_free_pointer & pseudo_atomic_Value)
90 toy 1.33 #define set_pseudo_atomic_atomic() \
91 gerd 1.35 (current_dynamic_space_free_pointer \
92 toy 1.41 = (lispobj*) ((unsigned long)current_dynamic_space_free_pointer | pseudo_atomic_Value))
93 toy 1.33 #define clr_pseudo_atomic_atomic() \
94 gerd 1.35 (current_dynamic_space_free_pointer \
95 toy 1.41 = (lispobj*) ((unsigned long) current_dynamic_space_free_pointer & ~pseudo_atomic_Value))
96 gerd 1.35 #define get_pseudo_atomic_interrupted() \
97 toy 1.41 ((unsigned long) current_dynamic_space_free_pointer & pseudo_atomic_InterruptedValue)
98 toy 1.33 #define clr_pseudo_atomic_interrupted() \
99 gerd 1.35 (current_dynamic_space_free_pointer \
100 toy 1.41 = (lispobj*) ((unsigned long) current_dynamic_space_free_pointer & ~pseudo_atomic_InterruptedValue))
101 gerd 1.35
102 toy 1.47 #define set_current_region_free(value) \
103     current_dynamic_space_free_pointer = (lispobj*)((value) | ((long)current_dynamic_space_free_pointer & lowtag_Mask))
104    
105     #define get_current_region_free() \
106     ((long)current_dynamic_space_free_pointer & (~(lowtag_Mask)))
107    
108     #define set_current_region_end(value) \
109     SetSymbolValue(CURRENT_REGION_END_ADDR, (value))
110    
111 cshapiro 1.87 #elif defined(DARWIN) && defined(__ppc__)
112 rtoy 1.67 #ifndef pseudo_atomic_InterruptedValue
113     #define pseudo_atomic_InterruptedValue 1
114     #endif
115     #ifndef pseudo_atomic_Value
116     #define pseudo_atomic_Value 4
117     #endif
118    
119     #define set_alloc_pointer(value)
120     #define get_alloc_pointer() \
121     ((unsigned long) current_dynamic_space_free_pointer & ~lowtag_Mask)
122     #define get_binding_stack_pointer() \
123     (current_binding_stack_pointer)
124     #define get_pseudo_atomic_atomic() \
125     ((unsigned long)current_dynamic_space_free_pointer & pseudo_atomic_Value)
126     #define set_pseudo_atomic_atomic() \
127     (current_dynamic_space_free_pointer \
128     = (lispobj*) ((unsigned long)current_dynamic_space_free_pointer | pseudo_atomic_Value))
129     #define clr_pseudo_atomic_atomic() \
130     (current_dynamic_space_free_pointer \
131     = (lispobj*) ((unsigned long) current_dynamic_space_free_pointer & ~pseudo_atomic_Value))
132     #define get_pseudo_atomic_interrupted() \
133     ((unsigned long) current_dynamic_space_free_pointer & pseudo_atomic_InterruptedValue)
134     #define clr_pseudo_atomic_interrupted() \
135     (current_dynamic_space_free_pointer \
136     = (lispobj*) ((unsigned long) current_dynamic_space_free_pointer & ~pseudo_atomic_InterruptedValue))
137    
138     #define set_current_region_free(value) \
139     current_dynamic_space_free_pointer = (lispobj*)((value) | ((long)current_dynamic_space_free_pointer & lowtag_Mask))
140    
141     #define get_current_region_free() \
142     ((long)current_dynamic_space_free_pointer & (~(lowtag_Mask)))
143    
144     #define set_current_region_end(value) \
145     SetSymbolValue(CURRENT_REGION_END_ADDR, (value))
146    
147 toy 1.33 #else
148 gerd 1.35 #error gencgc is not supported on this platform
149 toy 1.33 #endif
150    
151 gerd 1.38 /* Define for activating assertions. */
152    
153 rtoy 1.67 #if defined(DARWIN)
154 gerd 1.38 #define GC_ASSERTIONS 1
155     #endif
156    
157     /* Check for references to stack-allocated objects. */
158    
159     #ifdef GC_ASSERTIONS
160    
161     static void *invalid_stack_start, *invalid_stack_end;
162    
163     static inline void
164 rtoy 1.66 check_escaped_stack_object(lispobj * where, lispobj obj)
165 gerd 1.38 {
166 cshapiro 1.87 #if !defined(DARWIN) && !defined(__ppc__)
167 rtoy 1.66 void *p;
168    
169     if (Pointerp(obj)
170     && (p = (void *) PTR(obj),
171     (p >= (void *) CONTROL_STACK_START
172     && p < (void *) CONTROL_STACK_END))) {
173     char *space;
174    
175     if (where >= (lispobj *) DYNAMIC_0_SPACE_START
176     && where < (lispobj *) (DYNAMIC_0_SPACE_START + DYNAMIC_SPACE_SIZE))
177     space = "dynamic space";
178     else if (where >= (lispobj *) STATIC_SPACE_START
179     && where <
180     (lispobj *) (STATIC_SPACE_START + STATIC_SPACE_SIZE)) space =
181     "static space";
182     else if (where >= (lispobj *) READ_ONLY_SPACE_START
183     && where <
184     (lispobj *) (READ_ONLY_SPACE_START +
185     READ_ONLY_SPACE_SIZE)) space = "read-only space";
186     else
187     space = NULL;
188    
189     /* GC itself uses some stack, so we can't tell exactly where the
190     invalid stack area starts. Usually, it should be an error if a
191     reference to a stack-allocated object is found, although it
192     is valid to store a reference to a stack-allocated object
193     temporarily in another reachable object, as long as the
194     reference goes away at the end of a dynamic extent. */
195    
196     if (p >= invalid_stack_start && p < invalid_stack_end)
197     lose("Escaped stack-allocated object 0x%08lx at %p in %s\n",
198     (unsigned long) obj, where, space);
199 toy 1.39 #ifndef i386
200 rtoy 1.66 else if ((where >= (lispobj *) CONTROL_STACK_START
201     && where < (lispobj *) (CONTROL_STACK_END))
202     || (space == NULL)) {
203     /* Do nothing if it the reference is from the control stack,
204     because that will happen, and that's ok. Or if it's from
205     an unknown space (typically from scavenging an interrupt
206     context. */
207     }
208 toy 1.39 #endif
209    
210 rtoy 1.66 else
211     fprintf(stderr,
212     "Reference to stack-allocated object 0x%08lx at %p in %s\n",
213     (unsigned long) obj, where,
214     space ? space : "Unknown space");
215 gerd 1.38 }
216 rtoy 1.67 #endif
217 gerd 1.38 }
218    
219     #endif /* GC_ASSERTIONS */
220    
221    
222     #ifdef GC_ASSERTIONS
223     #define gc_assert(ex) \
224     do { \
225     if (!(ex)) gc_abort (); \
226     } while (0)
227 dtc 1.1 #else
228 gerd 1.32 #define gc_assert(ex) (void) 0
229 dtc 1.1 #endif
230 rtoy 1.66
231 dtc 1.1
232 dtc 1.14 /*
233     * The number of generations, an extra is added to this for use as a temp.
234     */
235 dtc 1.1 #define NUM_GENERATIONS 6
236    
237     /* Debugging variables. */
238    
239 dtc 1.14 /*
240     * The verbose level. All non-error messages are disabled at level 0;
241     * and only a few rare messages are printed at level 1.
242     */
243 dtc 1.6 unsigned gencgc_verbose = 0;
244 cracauer 1.28 unsigned counters_verbose = 0;
245 dtc 1.1
246 dtc 1.14 /*
247     * To enable the use of page protection to help avoid the scavenging
248     * of pages that don't have pointers to younger generations.
249     */
250 rtoy 1.66 boolean enable_page_protection = TRUE;
251 dtc 1.1
252 dtc 1.14 /*
253     * Hunt for pointers to old-space, when GCing generations >= verify_gen.
254     * Set to NUM_GENERATIONS to disable.
255     */
256 dtc 1.3 int verify_gens = NUM_GENERATIONS;
257 dtc 1.1
258 dtc 1.14 /*
259 toy 1.33 * Enable a pre-scan verify of generation 0 before it's GCed. (This
260     * makes GC very, very slow, so don't enable this unless you really
261     * need it!)
262 dtc 1.14 */
263 dtc 1.1 boolean pre_verify_gen_0 = FALSE;
264    
265 dtc 1.3 /*
266 dtc 1.14 * Enable checking for bad pointers after gc_free_heap called from purify.
267 dtc 1.3 */
268 rtoy 1.70 #if 0 && defined(DARWIN)
269 rtoy 1.67 boolean verify_after_free_heap = TRUE;
270     #else
271 dtc 1.3 boolean verify_after_free_heap = FALSE;
272 rtoy 1.67 #endif
273 dtc 1.3
274 dtc 1.14 /*
275     * Enable the printing of a note when code objects are found in the
276     * dynamic space during a heap verify.
277     */
278 dtc 1.1 boolean verify_dynamic_code_check = FALSE;
279    
280 dtc 1.14 /*
281     * Enable the checking of code objects for fixup errors after they are
282 toy 1.33 * transported. (Only used for x86.)
283 dtc 1.14 */
284 dtc 1.3 boolean check_code_fixups = FALSE;
285 dtc 1.1
286 dtc 1.14 /*
287     * To enable unmapping of a page and re-mmaping it to have it zero filled.
288 pmai 1.26 * Note: this can waste a lot of swap on FreeBSD and Open/NetBSD(?) so
289     * don't unmap.
290 dtc 1.14 */
291 pmai 1.26 #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
292 dtc 1.1 boolean gencgc_unmap_zero = FALSE;
293     #else
294     boolean gencgc_unmap_zero = TRUE;
295     #endif
296    
297 dtc 1.14 /*
298     * Enable checking that newly allocated regions are zero filled.
299     */
300 rtoy 1.70 #if 0 && defined(DARWIN)
301 rtoy 1.67 boolean gencgc_zero_check = TRUE;
302     boolean gencgc_enable_verify_zero_fill = TRUE;
303     #else
304 dtc 1.1 boolean gencgc_zero_check = FALSE;
305 dtc 1.10 boolean gencgc_enable_verify_zero_fill = FALSE;
306 rtoy 1.67 #endif
307 dtc 1.10
308 dtc 1.3 /*
309     * Enable checking that free pages are zero filled during gc_free_heap
310     * called after purify.
311     */
312 rtoy 1.70 #if 0 && defined(DARWIN)
313 rtoy 1.67 boolean gencgc_zero_check_during_free_heap = TRUE;
314     #else
315 dtc 1.3 boolean gencgc_zero_check_during_free_heap = FALSE;
316 rtoy 1.67 #endif
317 dtc 1.3
318 dtc 1.14 /*
319     * The minimum size for a large object.
320     */
321 dtc 1.17 unsigned large_object_size = 4 * PAGE_SIZE;
322 dtc 1.1
323 dtc 1.14 /*
324     * Enable the filtering of stack/register pointers. This could reduce
325     * the number of invalid pointers accepted. It will probably degrades
326     * interrupt safety during object initialisation.
327     */
328 dtc 1.1 boolean enable_pointer_filter = TRUE;
329 rtoy 1.66
330 dtc 1.1
331 dtc 1.14 /*
332     * The total bytes allocated. Seen by (dynamic-usage)
333     */
334 dtc 1.1 unsigned long bytes_allocated = 0;
335 dtc 1.22
336     /*
337 cracauer 1.28 * The total amount of bytes ever allocated. Not decreased by GC.
338     */
339    
340     volatile unsigned long long bytes_allocated_sum = 0;
341    
342     /*
343 dtc 1.22 * GC trigger; a value of 0xffffffff represents disabled.
344     */
345     unsigned long auto_gc_trigger = 0xffffffff;
346 dtc 1.1
347 dtc 1.14 /*
348 toy 1.46 * Number of pages to reserve for heap overflow. We want some space
349     * available on the heap when we are close to a heap overflow, so we
350     * can handle the overflow. But how much do we really need? I (rtoy)
351     * think 256 pages is probably a decent amount. (That's 1 MB for x86,
352     * 2 MB for sparc, which has 8K pages.)
353     */
354    
355     unsigned long reserved_heap_pages = 256;
356    
357     /*
358 dtc 1.14 * The src. and dest. generations. Set before a GC starts scavenging.
359     */
360 dtc 1.1 static int from_space;
361     static int new_space;
362 rtoy 1.66
363 dtc 1.1
364 dtc 1.14 /*
365     * GC structures and variables.
366     */
367 dtc 1.1
368 dtc 1.14 /*
369 dtc 1.23 * Number of pages within the dynamic heap, setup from the size of the
370     * dynamic space.
371     */
372     unsigned dynamic_space_pages;
373    
374     /*
375 dtc 1.14 * An array of page structures is statically allocated.
376 cwang 1.54 * This helps quickly map between an address and its page structure.
377 dtc 1.14 */
378 dtc 1.23 struct page *page_table;
379 dtc 1.1
380 dtc 1.14 /*
381     * Heap base, needed for mapping addresses to page structures.
382     */
383 toy 1.42 static char *heap_base = NULL;
384 dtc 1.1
385 dtc 1.14 /*
386     * Calculate the start address for the given page number.
387     */
388 rtoy 1.95.2.1.2.1 static char *
389 rtoy 1.66 page_address(int page_num)
390 dtc 1.1 {
391 rtoy 1.66 return heap_base + PAGE_SIZE * page_num;
392 dtc 1.1 }
393    
394 dtc 1.14 /*
395     * Find the page index within the page_table for the given address.
396     * Returns -1 on failure.
397     */
398 rtoy 1.95.2.1.2.1 int
399 rtoy 1.66 find_page_index(void *addr)
400 dtc 1.1 {
401 rtoy 1.66 int index = (char *) addr - heap_base;
402 dtc 1.1
403 rtoy 1.66 if (index >= 0) {
404     index = (unsigned int) index / PAGE_SIZE;
405     if (index < dynamic_space_pages)
406     return index;
407     }
408 dtc 1.1
409 rtoy 1.66 return -1;
410 dtc 1.1 }
411    
412 rtoy 1.95.2.1.2.1 /*
413     * This routine implements a write barrier used to record stores into
414     * to boxed regions outside of generation 0. When such a store occurs
415     * this routine will be automatically invoked by the page fault
416     * handler. If passed an address outside of the dynamic space, this
417     * routine will return immediately with a value of 0. Otherwise, the
418     * page belonging to the address is made writable, the protection
419     * change is recorded in the garbage collector page table, and a value
420     * of 1 is returned.
421     */
422     int
423     gc_write_barrier(void *addr)
424     {
425     int page_index = find_page_index(addr);
426    
427     /* Check if the fault is within the dynamic space. */
428     if (page_index == -1) {
429     return 0;
430     }
431    
432     /* The page should have been marked write protected */
433     if (!PAGE_WRITE_PROTECTED(page_index))
434     fprintf(stderr,
435     "*** Page fault in page not marked as write protected\n");
436    
437     /* Un-protect the page */
438     os_protect(page_address(page_index), PAGE_SIZE, OS_VM_PROT_ALL);
439     page_table[page_index].flags &= ~PAGE_WRITE_PROTECTED_MASK;
440     page_table[page_index].flags |= PAGE_WRITE_PROTECT_CLEARED_MASK;
441    
442     return 1;
443     }
444 dtc 1.1
445 dtc 1.14 /*
446     * A structure to hold the state of a generation.
447     */
448 dtc 1.1 struct generation {
449    
450 rtoy 1.66 /* The first page that gc_alloc checks on its next call. */
451     int alloc_start_page;
452    
453     /* The first page that gc_alloc_unboxed checks on its next call. */
454     int alloc_unboxed_start_page;
455    
456     /*
457     * The first page that gc_alloc_large (boxed) considers on its next call.
458     * Although it always allocates after the boxed_region.
459     */
460     int alloc_large_start_page;
461    
462     /*
463     * The first page that gc_alloc_large (unboxed) considers on its next call.
464     * Although it always allocates after the current_unboxed_region.
465     */
466     int alloc_large_unboxed_start_page;
467    
468     /* The bytes allocate to this generation. */
469     int bytes_allocated;
470    
471     /* The number of bytes at which to trigger a GC */
472     int gc_trigger;
473    
474     /* To calculate a new level for gc_trigger */
475     int bytes_consed_between_gc;
476    
477     /* The number of GCs since the last raise. */
478     int num_gc;
479 dtc 1.1
480 rtoy 1.66 /*
481     * The average age at after which a GC will raise objects to the
482     * next generation.
483     */
484     int trigger_age;
485    
486     /*
487     * The cumulative sum of the bytes allocated to this generation. It
488     * is cleared after a GC on this generation, and update before new
489     * objects are added from a GC of a younger generation. Dividing by
490     * the bytes_allocated will give the average age of the memory in
491     * this generation since its last GC.
492     */
493     int cum_sum_bytes_allocated;
494 dtc 1.1
495 rtoy 1.66 /*
496     * A minimum average memory age before a GC will occur helps prevent
497     * a GC when a large number of new live objects have been added, in
498     * which case a GC could be a waste of time.
499     */
500     double min_av_mem_age;
501 dtc 1.1 };
502    
503 dtc 1.14 /*
504     * An array of generation structures. There needs to be one more
505     * generation structure than actual generations as the oldest
506     * generations is temporarily raised then lowered.
507     */
508 dtc 1.17 static struct generation generations[NUM_GENERATIONS + 1];
509 dtc 1.1
510 moore 1.27 /* Statistics about a generation, extracted from the generations
511     array. This gets returned to Lisp.
512     */
513    
514     struct generation_stats {
515 rtoy 1.66 int bytes_allocated;
516     int gc_trigger;
517     int bytes_consed_between_gc;
518     int num_gc;
519     int trigger_age;
520     int cum_sum_bytes_allocated;
521     double min_av_mem_age;
522 moore 1.27 };
523 rtoy 1.66
524 moore 1.27
525 dtc 1.14 /*
526     * The oldest generation that will currently be GCed by default.
527     * Valid values are: 0, 1, ... (NUM_GENERATIONS - 1)
528     *
529     * The default of (NUM_GENERATIONS - 1) enables GC on all generations.
530     *
531     * Setting this to 0 effectively disables the generational nature of
532     * the GC. In some applications generational GC may not be useful
533     * because there are no long-lived objects.
534     *
535     * An intermediate value could be handy after moving long-lived data
536     * into an older generation so an unnecessary GC of this long-lived
537     * data can be avoided.
538     */
539 rtoy 1.66 unsigned int gencgc_oldest_gen_to_gc = NUM_GENERATIONS - 1;
540 dtc 1.1
541    
542 dtc 1.14 /*
543     * The maximum free page in the heap is maintained and used to update
544     * ALLOCATION_POINTER which is used by the room function to limit its
545     * search of the heap. XX Gencgc obviously needs to be better
546     * integrated with the lisp code.
547 rtoy 1.73 *
548     * Except on sparc and ppc, there's no ALLOCATION_POINTER, so it's
549     * never updated. So make this available (non-static).
550 dtc 1.14 */
551 rtoy 1.73 int last_free_page;
552 rtoy 1.66
553 dtc 1.1
554 cshapiro 1.87 static void scan_weak_tables(void);
555     static void scan_weak_objects(void);
556 dtc 1.1
557 dtc 1.14 /*
558     * Misc. heap functions.
559     */
560 dtc 1.1
561 dtc 1.14 /*
562     * Count the number of write protected pages within the given generation.
563     */
564 rtoy 1.66 static int
565     count_write_protect_generation_pages(int generation)
566 dtc 1.1 {
567 rtoy 1.66 int i;
568     int cnt = 0;
569     int mmask, mflags;
570    
571     mmask = PAGE_ALLOCATED_MASK | PAGE_WRITE_PROTECTED_MASK
572     | PAGE_GENERATION_MASK;
573     mflags = PAGE_ALLOCATED_MASK | PAGE_WRITE_PROTECTED_MASK | generation;
574    
575     for (i = 0; i < last_free_page; i++)
576     if (PAGE_FLAGS(i, mmask) == mflags)
577     cnt++;
578     return cnt;
579 dtc 1.1 }
580    
581 dtc 1.14 /*
582     * Count the number of pages within the given generation.
583     */
584 rtoy 1.66 static int
585     count_generation_pages(int generation)
586 dtc 1.1 {
587 rtoy 1.66 int i;
588     int cnt = 0;
589     int mmask, mflags;
590    
591     mmask = PAGE_ALLOCATED_MASK | PAGE_GENERATION_MASK;
592     mflags = PAGE_ALLOCATED_MASK | generation;
593    
594     for (i = 0; i < last_free_page; i++)
595     if (PAGE_FLAGS(i, mmask) == mflags)
596     cnt++;
597     return cnt;
598 dtc 1.1 }
599    
600 dtc 1.14 /*
601     * Count the number of dont_move pages.
602     */
603 rtoy 1.66 static int
604     count_dont_move_pages(void)
605 dtc 1.1 {
606 rtoy 1.66 int i;
607     int cnt = 0;
608     int mmask;
609    
610     mmask = PAGE_ALLOCATED_MASK | PAGE_DONT_MOVE_MASK;
611    
612     for (i = 0; i < last_free_page; i++)
613     if (PAGE_FLAGS(i, mmask) == mmask)
614     cnt++;
615     return cnt;
616 dtc 1.1 }
617    
618 dtc 1.14 /*
619     * Work through the pages and add up the number of bytes used for the
620     * given generation.
621     */
622 rtoy 1.58 #ifdef GC_ASSERTIONS
623 rtoy 1.66 static int
624     generation_bytes_allocated(int generation)
625 dtc 1.1 {
626 rtoy 1.66 int i;
627     int bytes_allocated = 0;
628     int mmask, mflags;
629    
630     mmask = PAGE_ALLOCATED_MASK | PAGE_GENERATION_MASK;
631     mflags = PAGE_ALLOCATED_MASK | generation;
632    
633     for (i = 0; i < last_free_page; i++) {
634     if (PAGE_FLAGS(i, mmask) == mflags)
635     bytes_allocated += page_table[i].bytes_used;
636     }
637     return bytes_allocated;
638 dtc 1.1 }
639 rtoy 1.58 #endif
640 dtc 1.1
641 dtc 1.14 /*
642     * Return the average age of the memory in a generation.
643     */
644 rtoy 1.66 static double
645     gen_av_mem_age(int gen)
646 dtc 1.1 {
647 rtoy 1.66 if (generations[gen].bytes_allocated == 0)
648     return 0.0;
649 dtc 1.14
650 rtoy 1.66 return (double) generations[gen].cum_sum_bytes_allocated /
651     (double) generations[gen].bytes_allocated;
652 dtc 1.1 }
653    
654 dtc 1.14 /*
655     * The verbose argument controls how much to print out:
656     * 0 for normal level of detail; 1 for debugging.
657     */
658 rtoy 1.66 void
659     print_generation_stats(int verbose)
660 dtc 1.1 {
661 rtoy 1.66 int i, gens;
662    
663 cwang 1.55 #if defined(i386) || defined(__x86_64)
664 toy 1.33 #define FPU_STATE_SIZE 27
665 rtoy 1.66 int fpu_state[FPU_STATE_SIZE];
666 toy 1.33 #elif defined(sparc)
667 rtoy 1.66 /*
668     * 32 (single-precision) FP registers, and the FP state register.
669     * But Sparc V9 has 32 double-precision registers (equivalent to 64
670     * single-precision, but can't be accessed), so we leave enough room
671     * for that.
672     */
673 toy 1.33 #define FPU_STATE_SIZE (((32 + 32 + 1) + 1)/2)
674 rtoy 1.66 long long fpu_state[FPU_STATE_SIZE];
675 cshapiro 1.87 #elif defined(DARWIN) && defined(__ppc__)
676 rtoy 1.67 #define FPU_STATE_SIZE 32
677     long long fpu_state[FPU_STATE_SIZE];
678 toy 1.33 #endif
679 dtc 1.11
680 rtoy 1.66 /*
681     * This code uses the FP instructions which may be setup for Lisp so
682     * they need to the saved and reset for C.
683     */
684 rtoy 1.67
685 rtoy 1.66 fpu_save(fpu_state);
686    
687 rtoy 1.67
688 rtoy 1.66 /* Number of generations to print out. */
689     if (verbose)
690     gens = NUM_GENERATIONS + 1;
691     else
692     gens = NUM_GENERATIONS;
693    
694     /* Print the heap stats */
695     fprintf(stderr, " Page count (%d KB)\n", PAGE_SIZE / 1024);
696     fprintf(stderr,
697     " Gen Boxed Unboxed LB LUB Alloc Waste Trigger WP GCs Mem-age\n");
698    
699     for (i = 0; i < gens; i++) {
700     int j;
701     int boxed_cnt = 0;
702     int unboxed_cnt = 0;
703     int large_boxed_cnt = 0;
704     int large_unboxed_cnt = 0;
705    
706     for (j = 0; j < last_free_page; j++) {
707     int flags = page_table[j].flags;
708    
709     if ((flags & PAGE_GENERATION_MASK) == i) {
710     if (flags & PAGE_ALLOCATED_MASK) {
711     /*
712     * Count the number of boxed and unboxed pages within the
713     * given generation.
714     */
715     if (flags & PAGE_UNBOXED_MASK)
716     if (flags & PAGE_LARGE_OBJECT_MASK)
717     large_unboxed_cnt++;
718     else
719     unboxed_cnt++;
720     else if (flags & PAGE_LARGE_OBJECT_MASK)
721     large_boxed_cnt++;
722     else
723     boxed_cnt++;
724     }
725     }
726 dtc 1.15 }
727 rtoy 1.66
728     gc_assert(generations[i].bytes_allocated ==
729     generation_bytes_allocated(i));
730     fprintf(stderr, " %5d: %5d %5d %5d %5d %10d %6d %10d %4d %3d %7.4f\n",
731     i, boxed_cnt, unboxed_cnt, large_boxed_cnt, large_unboxed_cnt,
732     generations[i].bytes_allocated,
733     PAGE_SIZE * count_generation_pages(i) -
734     generations[i].bytes_allocated, generations[i].gc_trigger,
735     count_write_protect_generation_pages(i), generations[i].num_gc,
736     gen_av_mem_age(i));
737 dtc 1.15 }
738 rtoy 1.66 fprintf(stderr, " Total bytes alloc=%ld\n", bytes_allocated);
739 dtc 1.14
740 rtoy 1.66 fpu_restore(fpu_state);
741 dtc 1.1 }
742    
743 moore 1.27 /* Get statistics that are kept "on the fly" out of the generation
744     array.
745     */
746 rtoy 1.66 void
747     get_generation_stats(int gen, struct generation_stats *stats)
748 moore 1.27 {
749 rtoy 1.66 if (gen <= NUM_GENERATIONS) {
750     stats->bytes_allocated = generations[gen].bytes_allocated;
751     stats->gc_trigger = generations[gen].gc_trigger;
752     stats->bytes_consed_between_gc =
753     generations[gen].bytes_consed_between_gc;
754     stats->num_gc = generations[gen].num_gc;
755     stats->trigger_age = generations[gen].trigger_age;
756     stats->cum_sum_bytes_allocated =
757     generations[gen].cum_sum_bytes_allocated;
758     stats->min_av_mem_age = generations[gen].min_av_mem_age;
759     }
760 moore 1.27 }
761    
762 rtoy 1.66 void
763     set_gc_trigger(int gen, int trigger)
764 moore 1.27 {
765 rtoy 1.66 if (gen <= NUM_GENERATIONS) {
766     generations[gen].gc_trigger = trigger;
767     }
768 moore 1.27 }
769 dtc 1.1
770 rtoy 1.66 void
771     set_trigger_age(int gen, int trigger_age)
772 moore 1.27 {
773 rtoy 1.66 if (gen <= NUM_GENERATIONS) {
774     generations[gen].trigger_age = trigger_age;
775     }
776 moore 1.27 }
777    
778 rtoy 1.66 void
779     set_min_mem_age(int gen, double min_mem_age)
780 moore 1.27 {
781 rtoy 1.66 if (gen <= NUM_GENERATIONS) {
782     generations[gen].min_av_mem_age = min_mem_age;
783     }
784 moore 1.27 }
785 dtc 1.1
786 dtc 1.14 /*
787     * Allocation routines.
788     *
789     *
790     * To support quick and inline allocation, regions of memory can be
791     * allocated and then allocated from with just a free pointer and a
792     * check against an end address.
793     *
794     * Since objects can be allocated to spaces with different properties
795     * e.g. boxed/unboxed, generation, ages; there may need to be many
796     * allocation regions.
797     *
798     * Each allocation region may be start within a partly used page.
799     * Many features of memory use are noted on a page wise basis,
800     * E.g. the generation; so if a region starts within an existing
801     * allocated page it must be consistent with this page.
802     *
803     * During the scavenging of the newspace, objects will be transported
804     * into an allocation region, and pointers updated to point to this
805     * allocation region. It is possible that these pointers will be
806     * scavenged again before the allocation region is closed, E.g. due to
807     * trans_list which jumps all over the place to cleanup the list. It
808     * is important to be able to determine properties of all objects
809     * pointed to when scavenging, E.g to detect pointers to the
810     * oldspace. Thus it's important that the allocation regions have the
811     * correct properties set when allocated, and not just set when
812     * closed. The region allocation routines return regions with the
813     * specified properties, and grab all the pages, setting there
814     * properties appropriately, except that the amount used is not known.
815     *
816     * These regions are used to support quicker allocation using just a
817     * free pointer. The actual space used by the region is not reflected
818     * in the pages tables until it is closed. It can't be scavenged until
819     * closed.
820     *
821     * When finished with the region it should be closed, which will
822     * update the page tables for the actual space used returning unused
823     * space. Further it may be noted in the new regions which is
824     * necessary when scavenging the newspace.
825     *
826     * Large objects may be allocated directly without an allocation
827     * region, the page tables are updated immediately.
828     *
829     * Unboxed objects don't contain points to other objects so don't need
830     * scavenging. Further they can't contain pointers to younger
831     * generations so WP is not needed. By allocating pages to unboxed
832     * objects the whole page never needs scavenging or write protecting.
833     */
834 dtc 1.1
835 dtc 1.14 /*
836     * Only using two regions at present, both are for the current
837     * newspace generation.
838     */
839 rtoy 1.66 struct alloc_region boxed_region;
840     struct alloc_region unboxed_region;
841 dtc 1.1
842 moore 1.27 #if 0
843 dtc 1.14 /*
844     * X hack. current lisp code uses the following. Need coping in/out.
845     */
846 dtc 1.1 void *current_region_free_pointer;
847     void *current_region_end_addr;
848 moore 1.27 #endif
849 dtc 1.1
850     /* The generation currently being allocated to. X */
851 rtoy 1.89 static int gc_alloc_generation = 0;
852 dtc 1.1
853 rtoy 1.57 extern void do_dynamic_space_overflow_warning(void);
854     extern void do_dynamic_space_overflow_error(void);
855    
856 toy 1.46 /* Handle heap overflow here, maybe. */
857     static void
858 rtoy 1.66 handle_heap_overflow(const char *msg, int size)
859 toy 1.46 {
860 rtoy 1.66 unsigned long heap_size_mb;
861    
862     if (msg) {
863     fprintf(stderr, msg, size);
864 toy 1.46 }
865     #ifndef SPARSE_BLOCK_SIZE
866     #define SPARSE_BLOCK_SIZE (0)
867 rtoy 1.66 #endif
868 toy 1.46
869 rtoy 1.66 /* Figure out how many MB of heap we have */
870     heap_size_mb = (dynamic_space_size + SPARSE_BLOCK_SIZE) >> 20;
871    
872     fprintf(stderr, " CMUCL has run out of dynamic heap space (%lu MB).\n",
873     heap_size_mb);
874     /* Try to handle heap overflow somewhat gracefully if we can. */
875 toy 1.46 #if defined(trap_DynamicSpaceOverflow) || defined(FEATURE_HEAP_OVERFLOW_CHECK)
876 rtoy 1.66 if (reserved_heap_pages == 0) {
877     fprintf(stderr, "\n Returning to top-level.\n");
878     do_dynamic_space_overflow_error();
879     } else {
880     fprintf(stderr,
881     " You can control heap size with the -dynamic-space-size commandline option.\n");
882     do_dynamic_space_overflow_warning();
883 toy 1.46 }
884     #else
885 rtoy 1.66 print_generation_stats(1);
886 toy 1.46
887 rtoy 1.66 exit(1);
888 toy 1.46 #endif
889     }
890    
891 dtc 1.14 /*
892     * Find a new region with room for at least the given number of bytes.
893     *
894     * It starts looking at the current generations alloc_start_page. So
895     * may pick up from the previous region if there is enough space. This
896     * keeps the allocation contiguous when scavenging the newspace.
897     *
898     * The alloc_region should have been closed by a call to
899     * gc_alloc_update_page_tables, and will thus be in an empty state.
900     *
901     * To assist the scavenging functions, write protected pages are not
902     * used. Free pages should not be write protected.
903     *
904     * It is critical to the conservative GC that the start of regions be
905     * known. To help achieve this only small regions are allocated at a
906     * time.
907     *
908     * During scavenging, pointers may be found that point within the
909     * current region and the page generation must be set so pointers to
910     * the from space can be recognised. So the generation of pages in
911     * the region are set to gc_alloc_generation. To prevent another
912     * allocation call using the same pages, all the pages in the region
913     * are allocated, although they will initially be empty.
914     */
915 rtoy 1.66 static void
916     gc_alloc_new_region(int nbytes, int unboxed, struct alloc_region *alloc_region)
917 dtc 1.1 {
918 rtoy 1.66 int first_page;
919     int last_page;
920     int region_size;
921     int restart_page;
922     int bytes_found;
923     int num_pages;
924     int i;
925     int mmask, mflags;
926    
927     /* Shut up some compiler warnings */
928     last_page = bytes_found = 0;
929 dtc 1.15
930 toy 1.47 #if 0
931 rtoy 1.66 fprintf(stderr, "alloc_new_region for %d bytes from gen %d\n",
932     nbytes, gc_alloc_generation);
933 toy 1.47 #endif
934 dtc 1.14
935 rtoy 1.66 /* Check that the region is in a reset state. */
936     gc_assert(alloc_region->first_page == 0
937     && alloc_region->last_page == -1
938     && alloc_region->free_pointer == alloc_region->end_addr);
939 dtc 1.14
940 rtoy 1.66 if (unboxed)
941     restart_page =
942     generations[gc_alloc_generation].alloc_unboxed_start_page;
943     else
944     restart_page = generations[gc_alloc_generation].alloc_start_page;
945 dtc 1.14
946     /*
947 rtoy 1.66 * Search for a contiguous free region of at least nbytes with the
948     * given properties: boxed/unboxed, generation. First setting up the
949     * mask and matching flags.
950 dtc 1.14 */
951    
952 rtoy 1.66 mmask = PAGE_ALLOCATED_MASK | PAGE_WRITE_PROTECTED_MASK
953     | PAGE_LARGE_OBJECT_MASK | PAGE_DONT_MOVE_MASK
954     | PAGE_UNBOXED_MASK | PAGE_GENERATION_MASK;
955     mflags = PAGE_ALLOCATED_MASK | (unboxed << PAGE_UNBOXED_SHIFT)
956     | gc_alloc_generation;
957    
958     do {
959     first_page = restart_page;
960    
961     /*
962     * First search for a page with at least 32 bytes free, that is
963     * not write protected, or marked dont_move.
964     */
965    
966     while (first_page < dynamic_space_pages) {
967     int flags = page_table[first_page].flags;
968    
969     if (!(flags & PAGE_ALLOCATED_MASK)
970     || ((flags & mmask) == mflags &&
971     page_table[first_page].bytes_used < PAGE_SIZE - 32))
972     break;
973     first_page++;
974     }
975    
976     /* Check for a failure */
977     if (first_page >= dynamic_space_pages - reserved_heap_pages) {
978     #if 0
979     handle_heap_overflow("*A2 gc_alloc_new_region failed, nbytes=%d.\n",
980     nbytes);
981     #else
982     break;
983     #endif
984     }
985 dtc 1.14
986 rtoy 1.66 gc_assert(!PAGE_WRITE_PROTECTED(first_page));
987 dtc 1.14
988     #if 0
989 rtoy 1.66 fprintf(stderr, " first_page=%d bytes_used=%d\n",
990     first_page, page_table[first_page].bytes_used);
991 dtc 1.14 #endif
992    
993 rtoy 1.66 /*
994     * Now search forward to calculate the available region size. It
995     * tries to keeps going until nbytes are found and the number of
996     * pages is greater than some level. This helps keep down the
997     * number of pages in a region.
998     */
999     last_page = first_page;
1000     bytes_found = PAGE_SIZE - page_table[first_page].bytes_used;
1001     num_pages = 1;
1002     while ((bytes_found < nbytes || num_pages < 2)
1003     && last_page < dynamic_space_pages - 1
1004     && !PAGE_ALLOCATED(last_page + 1)) {
1005     last_page++;
1006     num_pages++;
1007     bytes_found += PAGE_SIZE;
1008     gc_assert(!PAGE_WRITE_PROTECTED(last_page));
1009     }
1010    
1011     region_size = (PAGE_SIZE - page_table[first_page].bytes_used)
1012     + PAGE_SIZE * (last_page - first_page);
1013 dtc 1.14
1014 rtoy 1.66 gc_assert(bytes_found == region_size);
1015 dtc 1.14
1016     #if 0
1017 rtoy 1.66 fprintf(stderr, " last_page=%d bytes_found=%d num_pages=%d\n",
1018     last_page, bytes_found, num_pages);
1019 dtc 1.14 #endif
1020    
1021 rtoy 1.66 restart_page = last_page + 1;
1022     }
1023     while (restart_page < dynamic_space_pages && bytes_found < nbytes);
1024 dtc 1.1
1025 rtoy 1.66 if (first_page >= dynamic_space_pages - reserved_heap_pages) {
1026     handle_heap_overflow("*A2 gc_alloc_new_region failed, nbytes=%d.\n",
1027     nbytes);
1028     }
1029 dtc 1.1
1030 rtoy 1.66 /* Check for a failure */
1031     if (restart_page >= (dynamic_space_pages - reserved_heap_pages)
1032     && bytes_found < nbytes) {
1033     handle_heap_overflow("*A1 gc_alloc_new_region failed, nbytes=%d.\n",
1034     nbytes);
1035     }
1036     #if 0
1037     fprintf(stderr,
1038     "gc_alloc_new_region gen %d: %d bytes: from pages %d to %d: addr=%x\n",
1039     gc_alloc_generation, bytes_found, first_page, last_page,
1040     page_address(first_page));
1041     #endif
1042    
1043     /* Setup the alloc_region. */
1044     alloc_region->first_page = first_page;
1045     alloc_region->last_page = last_page;
1046     alloc_region->start_addr = page_table[first_page].bytes_used
1047     + page_address(first_page);
1048     alloc_region->free_pointer = alloc_region->start_addr;
1049     alloc_region->end_addr = alloc_region->start_addr + bytes_found;
1050    
1051     if (gencgc_zero_check) {
1052     int *p;
1053    
1054     for (p = (int *) alloc_region->start_addr;
1055     p < (int *) alloc_region->end_addr; p++)
1056     if (*p != 0)
1057     fprintf(stderr, "** new region not zero @ %lx\n",
1058     (unsigned long) p);
1059     }
1060    
1061     /* Setup the pages. */
1062    
1063     /* The first page may have already been in use. */
1064     if (page_table[first_page].bytes_used == 0) {
1065     PAGE_FLAGS_UPDATE(first_page, mmask, mflags);
1066     page_table[first_page].first_object_offset = 0;
1067     }
1068 dtc 1.1
1069 rtoy 1.66 gc_assert(PAGE_ALLOCATED(first_page));
1070     gc_assert(PAGE_UNBOXED_VAL(first_page) == unboxed);
1071     gc_assert(PAGE_GENERATION(first_page) == gc_alloc_generation);
1072     gc_assert(!PAGE_LARGE_OBJECT(first_page));
1073 dtc 1.14
1074 rtoy 1.66 for (i = first_page + 1; i <= last_page; i++) {
1075     PAGE_FLAGS_UPDATE(i, PAGE_ALLOCATED_MASK | PAGE_LARGE_OBJECT_MASK
1076     | PAGE_UNBOXED_MASK | PAGE_GENERATION_MASK,
1077     PAGE_ALLOCATED_MASK | (unboxed << PAGE_UNBOXED_SHIFT)
1078     | gc_alloc_generation);
1079     /*
1080     * This may not be necessary for unboxed regions (think it was
1081     * broken before!)
1082     */
1083     page_table[i].first_object_offset =
1084     alloc_region->start_addr - page_address(i);
1085     }
1086 dtc 1.15
1087 rtoy 1.66 /* Bump up the last_free_page */
1088     if (last_page + 1 > last_free_page) {
1089     last_free_page = last_page + 1;
1090     set_alloc_pointer((lispobj) ((char *) heap_base +
1091     PAGE_SIZE * last_free_page));
1092 dtc 1.1
1093 rtoy 1.66 }
1094 dtc 1.1 }
1095    
1096    
1097    
1098 dtc 1.14 /*
1099     * If the record_new_objects flag is 2 then all new regions created
1100     * are recorded.
1101     *
1102 cwang 1.54 * If it's 1 then it is only recorded if the first page of the
1103 dtc 1.14 * current region is <= new_areas_ignore_page. This helps avoid
1104     * unnecessary recording when doing full scavenge pass.
1105     *
1106     * The new_object structure holds the page, byte offset, and size of
1107     * new regions of objects. Each new area is placed in the array of
1108     * these structures pointed to by new_areas; new_areas_index holds the
1109     * offset into new_areas.
1110     *
1111     * If new_area overflows NUM_NEW_AREAS then it stops adding them. The
1112     * later code must detect this an handle it, probably by doing a full
1113     * scavenge of a generation.
1114     */
1115 dtc 1.1
1116     #define NUM_NEW_AREAS 512
1117     static int record_new_objects = 0;
1118     static int new_areas_ignore_page;
1119     struct new_area {
1120 rtoy 1.66 int page;
1121     int offset;
1122     int size;
1123 dtc 1.1 };
1124     static struct new_area (*new_areas)[];
1125 rtoy 1.89 static int new_areas_index = 0;
1126 dtc 1.1 int max_new_areas;
1127    
1128     /* Add a new area to new_areas. */
1129 rtoy 1.66 static void
1130     add_new_area(int first_page, int offset, int size)
1131 dtc 1.1 {
1132 rtoy 1.66 unsigned new_area_start, c;
1133     int i;
1134    
1135     /* Ignore if full */
1136     if (new_areas_index >= NUM_NEW_AREAS)
1137     return;
1138 dtc 1.1
1139 rtoy 1.66 switch (record_new_objects) {
1140     case 0:
1141     return;
1142     case 1:
1143     if (first_page > new_areas_ignore_page)
1144     return;
1145     break;
1146     case 2:
1147     break;
1148     default:
1149     gc_abort();
1150     }
1151 dtc 1.1
1152 rtoy 1.66 new_area_start = PAGE_SIZE * first_page + offset;
1153 dtc 1.1
1154 rtoy 1.66 /*
1155     * Search backwards for a prior area that this follows from. If
1156     * found this will save adding a new area.
1157     */
1158     for (i = new_areas_index - 1, c = 0; i >= 0 && c < 8; i--, c++) {
1159     unsigned area_end = PAGE_SIZE * (*new_areas)[i].page
1160     + (*new_areas)[i].offset + (*new_areas)[i].size;
1161 dtc 1.14
1162     #if 0
1163 rtoy 1.66 fprintf(stderr, "*S1 %d %d %d %d\n", i, c, new_area_start, area_end);
1164 dtc 1.14 #endif
1165 rtoy 1.66 if (new_area_start == area_end) {
1166 dtc 1.14 #if 0
1167 rtoy 1.66 fprintf(stderr, "-> Adding to [%d] %d %d %d with %d %d %d:\n",
1168     i, (*new_areas)[i].page, (*new_areas)[i].offset,
1169     (*new_areas)[i].size, first_page, offset, size);
1170 dtc 1.14 #endif
1171 rtoy 1.66 (*new_areas)[i].size += size;
1172     return;
1173     }
1174 dtc 1.1 }
1175 dtc 1.14 #if 0
1176 rtoy 1.66 fprintf(stderr, "*S1 %d %d %d\n", i, c, new_area_start);
1177 dtc 1.14 #endif
1178 dtc 1.1
1179 rtoy 1.66 (*new_areas)[new_areas_index].page = first_page;
1180     (*new_areas)[new_areas_index].offset = offset;
1181     (*new_areas)[new_areas_index].size = size;
1182 dtc 1.14 #if 0
1183 rtoy 1.66 fprintf(stderr, " new_area %d page %d offset %d size %d\n",
1184     new_areas_index, first_page, offset, size);
1185 dtc 1.14 #endif
1186 rtoy 1.66 new_areas_index++;
1187 dtc 1.14
1188 rtoy 1.66 /* Note the max new_areas used. */
1189     if (new_areas_index > max_new_areas)
1190     max_new_areas = new_areas_index;
1191 dtc 1.1 }
1192    
1193    
1194 dtc 1.14 /*
1195     * Update the tables for the alloc_region. The region may be added to
1196     * the new_areas.
1197     *
1198     * When done the alloc_region its setup so that the next quick alloc
1199     * will fail safely and thus a new region will be allocated. Further
1200     * it is safe to try and re-update the page table of this reset
1201     * alloc_region.
1202     */
1203 rtoy 1.66 void
1204     gc_alloc_update_page_tables(int unboxed, struct alloc_region *alloc_region)
1205 dtc 1.1 {
1206 rtoy 1.66 int more;
1207     int first_page;
1208     int next_page;
1209     int bytes_used;
1210     int orig_first_page_bytes_used;
1211     int region_size;
1212     int byte_cnt;
1213 dtc 1.1
1214 dtc 1.14 #if 0
1215 rtoy 1.66 fprintf(stderr, "gc_alloc_update_page_tables to gen %d: ",
1216     gc_alloc_generation);
1217 dtc 1.14 #endif
1218 dtc 1.1
1219 rtoy 1.66 first_page = alloc_region->first_page;
1220 dtc 1.1
1221 rtoy 1.66 /* Catch an unused alloc_region. */
1222     if (first_page == 0 && alloc_region->last_page == -1)
1223     return;
1224 dtc 1.1
1225 rtoy 1.66 next_page = first_page + 1;
1226 dtc 1.1
1227 rtoy 1.66 /* Skip if no bytes were allocated */
1228     if (alloc_region->free_pointer != alloc_region->start_addr) {
1229     orig_first_page_bytes_used = page_table[first_page].bytes_used;
1230 dtc 1.14
1231 rtoy 1.66 gc_assert(alloc_region->start_addr == page_address(first_page) +
1232     page_table[first_page].bytes_used);
1233 dtc 1.14
1234 rtoy 1.66 /* All the pages used need to be updated */
1235 dtc 1.14
1236 rtoy 1.66 /* Update the first page. */
1237 dtc 1.14
1238 rtoy 1.66 #if 0
1239     fprintf(stderr, "0");
1240 dtc 1.14 #endif
1241    
1242 rtoy 1.66 /* If the page was free then setup the gen, and first_object_offset. */
1243     if (page_table[first_page].bytes_used == 0)
1244     gc_assert(page_table[first_page].first_object_offset == 0);
1245 dtc 1.14
1246 rtoy 1.66 gc_assert(PAGE_ALLOCATED(first_page));
1247     gc_assert(PAGE_UNBOXED_VAL(first_page) == unboxed);
1248     gc_assert(PAGE_GENERATION(first_page) == gc_alloc_generation);
1249     gc_assert(!PAGE_LARGE_OBJECT(first_page));
1250 dtc 1.14
1251 rtoy 1.66 byte_cnt = 0;
1252 dtc 1.14
1253 rtoy 1.66 /*
1254     * Calc. the number of bytes used in this page. This is not always
1255     * the number of new bytes, unless it was free.
1256     */
1257     more = 0;
1258     bytes_used = alloc_region->free_pointer - page_address(first_page);
1259     if (bytes_used > PAGE_SIZE) {
1260     bytes_used = PAGE_SIZE;
1261     more = 1;
1262     }
1263     page_table[first_page].bytes_used = bytes_used;
1264     byte_cnt += bytes_used;
1265 dtc 1.14
1266 rtoy 1.66 /*
1267     * All the rest of the pages should be free. Need to set their
1268     * first_object_offset pointer to the start of the region, and set
1269     * the bytes_used.
1270     */
1271     while (more) {
1272 dtc 1.14 #if 0
1273 rtoy 1.66 fprintf(stderr, "+");
1274 dtc 1.14 #endif
1275 rtoy 1.66 gc_assert(PAGE_ALLOCATED(next_page));
1276     gc_assert(PAGE_UNBOXED_VAL(next_page) == unboxed);
1277     gc_assert(page_table[next_page].bytes_used == 0);
1278     gc_assert(PAGE_GENERATION(next_page) == gc_alloc_generation);
1279     gc_assert(!PAGE_LARGE_OBJECT(next_page));
1280    
1281     gc_assert(page_table[next_page].first_object_offset ==
1282     alloc_region->start_addr - page_address(next_page));
1283    
1284     /* Calc. the number of bytes used in this page. */
1285     more = 0;
1286     bytes_used = alloc_region->free_pointer - page_address(next_page);
1287     if (bytes_used > PAGE_SIZE) {
1288     bytes_used = PAGE_SIZE;
1289     more = 1;
1290     }
1291     page_table[next_page].bytes_used = bytes_used;
1292     byte_cnt += bytes_used;
1293 dtc 1.14
1294 rtoy 1.66 next_page++;
1295     }
1296 dtc 1.14
1297 rtoy 1.66 region_size = alloc_region->free_pointer - alloc_region->start_addr;
1298     bytes_allocated += region_size;
1299     generations[gc_alloc_generation].bytes_allocated += region_size;
1300 dtc 1.14
1301 rtoy 1.66 gc_assert(byte_cnt - orig_first_page_bytes_used == region_size);
1302 dtc 1.14
1303 rtoy 1.66 /*
1304     * Set the generations alloc restart page to the last page of
1305     * the region.
1306     */
1307     if (unboxed)
1308     generations[gc_alloc_generation].alloc_unboxed_start_page =
1309     next_page - 1;
1310     else
1311     generations[gc_alloc_generation].alloc_start_page = next_page - 1;
1312 dtc 1.14
1313 rtoy 1.66 /* Add the region to the new_areas if requested. */
1314     if (!unboxed)
1315     add_new_area(first_page, orig_first_page_bytes_used, region_size);
1316 dtc 1.14
1317     #if 0
1318 rtoy 1.66 fprintf(stderr,
1319     " gc_alloc_update_page_tables update %d bytes to gen %d\n",
1320     region_size, gc_alloc_generation);
1321 dtc 1.14 #endif
1322 rtoy 1.66 } else
1323     /*
1324     * No bytes allocated. Unallocate the first_page if there are 0 bytes_used.
1325     */
1326 dtc 1.1 if (page_table[first_page].bytes_used == 0)
1327 rtoy 1.66 page_table[first_page].flags &= ~PAGE_ALLOCATED_MASK;
1328 dtc 1.14
1329 rtoy 1.66 /* Unallocate any unused pages. */
1330     while (next_page <= alloc_region->last_page) {
1331     gc_assert(page_table[next_page].bytes_used == 0);
1332     page_table[next_page].flags &= ~PAGE_ALLOCATED_MASK;
1333     next_page++;
1334     }
1335 dtc 1.1
1336 rtoy 1.66 /* Reset the alloc_region. */
1337     alloc_region->first_page = 0;
1338     alloc_region->last_page = -1;
1339     alloc_region->start_addr = page_address(0);
1340     alloc_region->free_pointer = page_address(0);
1341     alloc_region->end_addr = page_address(0);
1342 dtc 1.1
1343 dtc 1.14 #if 0
1344 rtoy 1.66 fprintf(stderr, "\n");
1345 dtc 1.14 #endif
1346 dtc 1.1 }
1347    
1348    
1349    
1350     static inline void *gc_quick_alloc(int nbytes);
1351    
1352 dtc 1.14 /*
1353     * Allocate a possibly large object.
1354     */
1355 rtoy 1.66 static void *
1356     gc_alloc_large(int nbytes, int unboxed, struct alloc_region *alloc_region)
1357 dtc 1.1 {
1358 rtoy 1.66 int first_page;
1359     int last_page;
1360     int region_size;
1361     int restart_page;
1362     int bytes_found;
1363     int num_pages;
1364     int orig_first_page_bytes_used;
1365     int byte_cnt;
1366     int more;
1367     int bytes_used;
1368     int next_page;
1369     int large = (nbytes >= large_object_size);
1370     int mmask, mflags;
1371 dtc 1.15
1372 dtc 1.14
1373 rtoy 1.66 /* Shut up some compiler warnings */
1374     last_page = bytes_found = 0;
1375 dtc 1.14
1376 toy 1.47 #if 0
1377 rtoy 1.66 if (nbytes > 200000)
1378     fprintf(stderr, "*** alloc_large %d\n", nbytes);
1379 toy 1.47 #endif
1380 dtc 1.14
1381     #if 0
1382 rtoy 1.66 fprintf(stderr, "gc_alloc_large for %d bytes from gen %d\n",
1383     nbytes, gc_alloc_generation);
1384 dtc 1.14 #endif
1385    
1386 rtoy 1.66 /*
1387     * If the object is small, and there is room in the current region
1388     * then allocation it in the current region.
1389     */
1390     if (!large && alloc_region->end_addr - alloc_region->free_pointer >= nbytes)
1391     return gc_quick_alloc(nbytes);
1392 dtc 1.14
1393 rtoy 1.66 /*
1394     * Search for a contiguous free region of at least nbytes. If it's a
1395     * large object then align it on a page boundary by searching for a
1396     * free page.
1397     */
1398 dtc 1.14
1399 rtoy 1.66 /*
1400     * To allow the allocation of small objects without the danger of
1401     * using a page in the current boxed region, the search starts after
1402     * the current boxed free region. XX could probably keep a page
1403     * index ahead of the current region and bumped up here to save a
1404     * lot of re-scanning.
1405     */
1406     if (unboxed)
1407     restart_page =
1408     generations[gc_alloc_generation].alloc_large_unboxed_start_page;
1409     else
1410     restart_page = generations[gc_alloc_generation].alloc_large_start_page;
1411     if (restart_page <= alloc_region->last_page)
1412     restart_page = alloc_region->last_page + 1;
1413    
1414     /* Setup the mask and matching flags. */
1415    
1416     mmask = PAGE_ALLOCATED_MASK | PAGE_WRITE_PROTECTED_MASK
1417     | PAGE_LARGE_OBJECT_MASK | PAGE_DONT_MOVE_MASK
1418     | PAGE_UNBOXED_MASK | PAGE_GENERATION_MASK;
1419     mflags = PAGE_ALLOCATED_MASK | (unboxed << PAGE_UNBOXED_SHIFT)
1420     | gc_alloc_generation;
1421 dtc 1.14
1422 rtoy 1.66 do {
1423     first_page = restart_page;
1424 dtc 1.14
1425 rtoy 1.66 if (large)
1426     while (first_page < dynamic_space_pages
1427     && PAGE_ALLOCATED(first_page)) first_page++;
1428     else
1429     while (first_page < dynamic_space_pages) {
1430     int flags = page_table[first_page].flags;
1431 dtc 1.14
1432 rtoy 1.66 if (!(flags & PAGE_ALLOCATED_MASK)
1433     || ((flags & mmask) == mflags &&
1434     page_table[first_page].bytes_used < PAGE_SIZE - 32))
1435     break;
1436     first_page++;
1437     }
1438 dtc 1.14
1439 rtoy 1.66 /* Check for a failure */
1440     if (first_page >= dynamic_space_pages - reserved_heap_pages) {
1441     #if 0
1442     handle_heap_overflow("*A2 gc_alloc_large failed, nbytes=%d.\n",
1443     nbytes);
1444     #else
1445     break;
1446 dtc 1.14 #endif
1447 rtoy 1.66 }
1448     gc_assert(!PAGE_WRITE_PROTECTED(first_page));
1449 dtc 1.1
1450 rtoy 1.66 #if 0
1451     fprintf(stderr, " first_page=%d bytes_used=%d\n",
1452     first_page, page_table[first_page].bytes_used);
1453     #endif
1454 dtc 1.1
1455 rtoy 1.66 last_page = first_page;
1456     bytes_found = PAGE_SIZE - page_table[first_page].bytes_used;
1457     num_pages = 1;
1458     while (bytes_found < nbytes
1459     && last_page < dynamic_space_pages - 1
1460     && !PAGE_ALLOCATED(last_page + 1)) {
1461     last_page++;
1462     num_pages++;
1463     bytes_found += PAGE_SIZE;
1464     gc_assert(!PAGE_WRITE_PROTECTED(last_page));
1465     }
1466 dtc 1.14
1467 rtoy 1.66 region_size = (PAGE_SIZE - page_table[first_page].bytes_used)
1468     + PAGE_SIZE * (last_page - first_page);
1469 dtc 1.15
1470 rtoy 1.66 gc_assert(bytes_found == region_size);
1471 dtc 1.1
1472 rtoy 1.66 #if 0
1473     fprintf(stderr, " last_page=%d bytes_found=%d num_pages=%d\n",
1474     last_page, bytes_found, num_pages);
1475     #endif
1476 dtc 1.14
1477 rtoy 1.66 restart_page = last_page + 1;
1478     }
1479     while ((restart_page < dynamic_space_pages) && (bytes_found < nbytes));
1480 dtc 1.14
1481 rtoy 1.66 if (first_page >= dynamic_space_pages - reserved_heap_pages) {
1482     handle_heap_overflow("*A2 gc_alloc_large failed, nbytes=%d.\n", nbytes);
1483     }
1484 dtc 1.1
1485 rtoy 1.66 /* Check for a failure */
1486     if (restart_page >= (dynamic_space_pages - reserved_heap_pages)
1487     && bytes_found < nbytes) {
1488     handle_heap_overflow("*A1 gc_alloc_large failed, nbytes=%d.\n", nbytes);
1489     }
1490 dtc 1.14 #if 0
1491 rtoy 1.66 if (large)
1492     fprintf(stderr,
1493     "gc_alloc_large gen %d: %d of %d bytes: from pages %d to %d: addr=%x\n",
1494     gc_alloc_generation, nbytes, bytes_found, first_page, last_page,
1495     page_address(first_page));
1496 dtc 1.14 #endif
1497    
1498 rtoy 1.66 gc_assert(first_page > alloc_region->last_page);
1499     if (unboxed)
1500     generations[gc_alloc_generation].alloc_large_unboxed_start_page =
1501     last_page;
1502     else
1503     generations[gc_alloc_generation].alloc_large_start_page = last_page;
1504    
1505     /* Setup the pages. */
1506     orig_first_page_bytes_used = page_table[first_page].bytes_used;
1507 dtc 1.14
1508 rtoy 1.66 /*
1509     * If the first page was free then setup the gen, and
1510     * first_object_offset.
1511     */
1512 dtc 1.14
1513 rtoy 1.66 if (large)
1514     mflags |= PAGE_LARGE_OBJECT_MASK;
1515     if (page_table[first_page].bytes_used == 0) {
1516     PAGE_FLAGS_UPDATE(first_page, mmask, mflags);
1517     page_table[first_page].first_object_offset = 0;
1518 dtc 1.1 }
1519 dtc 1.14
1520 rtoy 1.66 gc_assert(PAGE_ALLOCATED(first_page));
1521     gc_assert(PAGE_UNBOXED_VAL(first_page) == unboxed);
1522     gc_assert(PAGE_GENERATION(first_page) == gc_alloc_generation);
1523     gc_assert(PAGE_LARGE_OBJECT_VAL(first_page) == large);
1524 dtc 1.14
1525 rtoy 1.66 byte_cnt = 0;
1526 dtc 1.1
1527 rtoy 1.66 /*
1528     * Calc. the number of bytes used in this page. This is not
1529     * always the number of new bytes, unless it was free.
1530     */
1531     more = 0;
1532     bytes_used = nbytes + orig_first_page_bytes_used;
1533     if (bytes_used > PAGE_SIZE) {
1534     bytes_used = PAGE_SIZE;
1535     more = 1;
1536     }
1537     page_table[first_page].bytes_used = bytes_used;
1538     byte_cnt += bytes_used;
1539    
1540     next_page = first_page + 1;
1541    
1542     /*
1543     * All the rest of the pages should be free. Need to set their
1544     * first_object_offset pointer to the start of the region, and set
1545     * the bytes_used.
1546     */
1547     while (more) {
1548     #if 0
1549     fprintf(stderr, "+");
1550     #endif
1551    
1552     gc_assert(!PAGE_ALLOCATED(next_page));
1553     gc_assert(page_table[next_page].bytes_used == 0);
1554     PAGE_FLAGS_UPDATE(next_page, mmask, mflags);
1555    
1556     page_table[next_page].first_object_offset =
1557     orig_first_page_bytes_used - PAGE_SIZE * (next_page - first_page);
1558    
1559     /* Calc. the number of bytes used in this page. */
1560     more = 0;
1561     bytes_used = nbytes + orig_first_page_bytes_used - byte_cnt;
1562     if (bytes_used > PAGE_SIZE) {
1563     bytes_used = PAGE_SIZE;
1564     more = 1;
1565     }
1566     page_table[next_page].bytes_used = bytes_used;
1567     byte_cnt += bytes_used;
1568    
1569     next_page++;
1570     }
1571    
1572     gc_assert(byte_cnt - orig_first_page_bytes_used == nbytes);
1573 dtc 1.1
1574 rtoy 1.66 bytes_allocated += nbytes;
1575     generations[gc_alloc_generation].bytes_allocated += nbytes;
1576 dtc 1.14
1577 rtoy 1.66 /* Add the region to the new_areas if requested. */
1578     if (!unboxed)
1579     add_new_area(first_page, orig_first_page_bytes_used, nbytes);
1580    
1581     /* Bump up the last_free_page */
1582     if (last_page + 1 > last_free_page) {
1583     last_free_page = last_page + 1;
1584     set_alloc_pointer((lispobj) ((char *) heap_base +
1585     PAGE_SIZE * last_free_page));
1586     }
1587 dtc 1.14
1588 rtoy 1.66 return (void *) (page_address(first_page) + orig_first_page_bytes_used);
1589 dtc 1.1 }
1590    
1591 dtc 1.14 /*
1592     * Allocate bytes from the boxed_region. It first checks if there is
1593     * room, if not then it calls gc_alloc_new_region to find a new region
1594     * with enough space. A pointer to the start of the region is returned.
1595     */
1596 rtoy 1.66 static void *
1597     gc_alloc(int nbytes)
1598 dtc 1.1 {
1599 rtoy 1.66 char *new_free_pointer;
1600 dtc 1.1
1601 dtc 1.14 #if 0
1602 rtoy 1.66 fprintf(stderr, "gc_alloc %d\n", nbytes);
1603 dtc 1.14 #endif
1604 dtc 1.1
1605 rtoy 1.66 /* Check if there is room in the current alloc region. */
1606     new_free_pointer = boxed_region.free_pointer + nbytes;
1607    
1608     if (new_free_pointer <= boxed_region.end_addr) {
1609     /* If so then allocate from the current alloc region. */
1610     char *new_obj = boxed_region.free_pointer;
1611 dtc 1.14
1612 rtoy 1.66 boxed_region.free_pointer = new_free_pointer;
1613 dtc 1.14
1614 rtoy 1.66 /* Check if the alloc region is almost empty. */
1615     if (boxed_region.end_addr - boxed_region.free_pointer <= 32) {
1616     /* If so finished with the current region. */
1617     gc_alloc_update_page_tables(0, &boxed_region);
1618     /* Setup a new region. */
1619     gc_alloc_new_region(32, 0, &boxed_region);
1620     }
1621     return (void *) new_obj;
1622 dtc 1.1 }
1623 dtc 1.14
1624 rtoy 1.66 /* Else not enough free space in the current region. */
1625    
1626     /*
1627     * If there is a bit of room left in the current region then
1628     * allocate a large object.
1629     */
1630 rtoy 1.95.2.1.2.2 #if (defined(i386) || defined(__x86_64))
1631     /*
1632     * This has potentially very bad behavior on sparc if the current
1633     * boxed region is too small for the allocation, but the free
1634     * space is greater than 32. The scenario is where we're always
1635     * allocating something that won't fit in the boxed region and we
1636     * keep calling gc_alloc_large. Since gc_alloc_large doesn't
1637     * change boxed_region, the next allocation will again be
1638     * out-of-line and we hit a kernel trap again. And so on, so we
1639     * waste all of our time doing kernel traps to allocate small
1640     * things. So disable this test on sparc. This should also be a
1641     * problem on ppc, but I didn't test it.
1642     *
1643     * X86 seems not affected or affected as badly, perhaps because
1644     * x86 doesn't do a kernel trap to handle allocation. Didn't test
1645     * this either, but we leave this in for x86.
1646     *
1647     * I don't know what the impact of this change will be except that
1648     * regions may have wasted space in them when they could have had
1649     * other thing allocated in the region.
1650     */
1651 rtoy 1.66 if (boxed_region.end_addr - boxed_region.free_pointer > 32)
1652     return gc_alloc_large(nbytes, 0, &boxed_region);
1653 rtoy 1.95.2.1.2.2 #endif
1654 dtc 1.1
1655 rtoy 1.66 /* Else find a new region. */
1656    
1657     /* Finished with the current region. */
1658     gc_alloc_update_page_tables(0, &boxed_region);
1659 dtc 1.1
1660 rtoy 1.66 /* Setup a new region. */
1661     gc_alloc_new_region(nbytes, 0, &boxed_region);
1662 dtc 1.1
1663 rtoy 1.66 /* Should now be enough room. */
1664 dtc 1.14
1665 rtoy 1.66 /* Check if there is room in the current region. */
1666     new_free_pointer = boxed_region.free_pointer + nbytes;
1667 dtc 1.14
1668 rtoy 1.66 if (new_free_pointer <= boxed_region.end_addr) {
1669     /* If so then allocate from the current region. */
1670     void *new_obj = boxed_region.free_pointer;
1671 dtc 1.14
1672 rtoy 1.66 boxed_region.free_pointer = new_free_pointer;
1673 dtc 1.14
1674 rtoy 1.66 /* Check if the current region is almost empty. */
1675     if (boxed_region.end_addr - boxed_region.free_pointer <= 32) {
1676     /* If so find, finished with the current region. */
1677     gc_alloc_update_page_tables(0, &boxed_region);
1678 dtc 1.1
1679 rtoy 1.66 /* Setup a new region. */
1680     gc_alloc_new_region(32, 0, &boxed_region);
1681     }
1682 dtc 1.14
1683 rtoy 1.66 return (void *) new_obj;
1684 dtc 1.1 }
1685 dtc 1.14
1686 rtoy 1.66 /* Shouldn't happen? */
1687     gc_assert(0);
1688     return 0;
1689 dtc 1.1 }
1690    
1691 dtc 1.14 /*
1692     * Allocate space from the boxed_region. If there is not enough free
1693     * space then call gc_alloc to do the job. A pointer to the start of
1694     * the region is returned.
1695     */
1696 rtoy 1.66 static inline void *
1697     gc_quick_alloc(int nbytes)
1698 dtc 1.1 {
1699 rtoy 1.66 char *new_free_pointer;
1700    
1701     /* Check if there is room in the current region. */
1702     new_free_pointer = boxed_region.free_pointer + nbytes;
1703 dtc 1.1
1704 rtoy 1.66 if (new_free_pointer <= boxed_region.end_addr) {
1705     /* If so then allocate from the current region. */
1706     void *new_obj = boxed_region.free_pointer;
1707 dtc 1.14
1708 rtoy 1.66 boxed_region.free_pointer = new_free_pointer;
1709     return (void *) new_obj;
1710     }
1711 dtc 1.14
1712 rtoy 1.66 /* Else call gc_alloc */
1713     return gc_alloc(nbytes);
1714 dtc 1.1 }
1715    
1716 dtc 1.14 /*
1717     * Allocate space for the boxed object. If it is a large object then
1718     * do a large alloc else allocate from the current region. If there is
1719     * not enough free space then call gc_alloc to do the job. A pointer
1720     * to the start of the region is returned.
1721     */
1722 rtoy 1.66 static inline void *
1723     gc_quick_alloc_large(int nbytes)
1724 dtc 1.1 {
1725 rtoy 1.66 char *new_free_pointer;
1726    
1727     if (nbytes >= large_object_size)
1728     return gc_alloc_large(nbytes, 0, &boxed_region);
1729 dtc 1.1
1730 rtoy 1.66 /* Check if there is room in the current region. */
1731     new_free_pointer = boxed_region.free_pointer + nbytes;
1732 dtc 1.1
1733 rtoy 1.66 if (new_free_pointer <= boxed_region.end_addr) {
1734     /* If so then allocate from the current region. */
1735     void *new_obj = boxed_region.free_pointer;
1736 dtc 1.14
1737 rtoy 1.66 boxed_region.free_pointer = new_free_pointer;
1738     return (void *) new_obj;
1739     }
1740 dtc 1.14
1741 rtoy 1.66 /* Else call gc_alloc */
1742     return gc_alloc(nbytes);
1743 dtc 1.1 }
1744    
1745    
1746    
1747    
1748 rtoy 1.66 static void *
1749     gc_alloc_unboxed(int nbytes)
1750 dtc 1.1 {
1751 rtoy 1.66 char *new_free_pointer;
1752 dtc 1.1
1753 dtc 1.14 #if 0
1754 rtoy 1.66 fprintf(stderr, "gc_alloc_unboxed %d\n", nbytes);
1755 dtc 1.14 #endif
1756 dtc 1.1
1757 rtoy 1.66 /* Check if there is room in the current region. */
1758     new_free_pointer = unboxed_region.free_pointer + nbytes;
1759    
1760     if (new_free_pointer <= unboxed_region.end_addr) {
1761     /* If so then allocate from the current region. */
1762     void *new_obj = unboxed_region.free_pointer;
1763 dtc 1.14
1764 rtoy 1.66 unboxed_region.free_pointer = new_free_pointer;
1765 dtc 1.14
1766 rtoy 1.66 /* Check if the current region is almost empty. */
1767     if ((unboxed_region.end_addr - unboxed_region.free_pointer) <= 32) {
1768     /* If so finished with the current region. */
1769     gc_alloc_update_page_tables(1, &unboxed_region);
1770 dtc 1.14
1771 rtoy 1.66 /* Setup a new region. */
1772     gc_alloc_new_region(32, 1, &unboxed_region);
1773     }
1774    
1775     return (void *) new_obj;
1776 dtc 1.1 }
1777 dtc 1.14
1778 rtoy 1.66 /* Else not enough free space in the current region. */
1779    
1780     /*
1781     * If there is a bit of room left in the current region then
1782     * allocate a large object.
1783     */
1784     if (unboxed_region.end_addr - unboxed_region.free_pointer > 32)
1785     return gc_alloc_large(nbytes, 1, &unboxed_region);
1786 dtc 1.14
1787 rtoy 1.66 /* Else find a new region. */
1788 dtc 1.14
1789 rtoy 1.66 /* Finished with the current region. */
1790     gc_alloc_update_page_tables(1, &unboxed_region);
1791 dtc 1.1
1792 rtoy 1.66 /* Setup a new region. */
1793     gc_alloc_new_region(nbytes, 1, &unboxed_region);
1794 dtc 1.1
1795 rtoy 1.66 /* Should now be enough room. */
1796 dtc 1.14
1797 rtoy 1.66 /* Check if there is room in the current region. */
1798     new_free_pointer = unboxed_region.free_pointer + nbytes;
1799 dtc 1.14
1800 rtoy 1.66 if (new_free_pointer <= unboxed_region.end_addr) {
1801     /* If so then allocate from the current region. */
1802     void *new_obj = unboxed_region.free_pointer;
1803 dtc 1.14
1804 rtoy 1.66 unboxed_region.free_pointer = new_free_pointer;
1805 dtc 1.14
1806 rtoy 1.66 /* Check if the current region is almost empty. */
1807     if ((unboxed_region.end_addr - unboxed_region.free_pointer) <= 32) {
1808     /* If so find, finished with the current region. */
1809     gc_alloc_update_page_tables(1, &unboxed_region);
1810 dtc 1.14
1811 rtoy 1.66 /* Setup a new region. */
1812     gc_alloc_new_region(32, 1, &unboxed_region);
1813     }
1814 dtc 1.14
1815 rtoy 1.66 return (void *) new_obj;
1816 dtc 1.1 }
1817 dtc 1.14
1818 rtoy 1.66 /* Shouldn't happen? */
1819     gc_assert(0);
1820     return 0;
1821 dtc 1.1 }
1822    
1823 rtoy 1.66 static inline void *
1824     gc_quick_alloc_unboxed(int nbytes)
1825 dtc 1.1 {
1826 rtoy 1.66 char *new_free_pointer;
1827    
1828     /* Check if there is room in the current region. */
1829     new_free_pointer = unboxed_region.free_pointer + nbytes;
1830 dtc 1.1
1831 rtoy 1.66 if (new_free_pointer <= unboxed_region.end_addr) {
1832     /* If so then allocate from the current region. */
1833     void *new_obj = unboxed_region.free_pointer;
1834 dtc 1.14
1835 rtoy 1.66 unboxed_region.free_pointer = new_free_pointer;
1836 dtc 1.14
1837 rtoy 1.66 return (void *) new_obj;
1838 dtc 1.1 }
1839 dtc 1.14
1840 rtoy 1.66 /* Else call gc_alloc */
1841     return gc_alloc_unboxed(nbytes);
1842 dtc 1.1 }
1843    
1844 dtc 1.14 /*
1845     * Allocate space for the object. If it is a large object then do a
1846     * large alloc else allocate from the current region. If there is not
1847     * enough free space then call gc_alloc to do the job.
1848     *
1849     * A pointer to the start of the region is returned.
1850     */
1851 rtoy 1.66 static inline void *
1852     gc_quick_alloc_large_unboxed(int nbytes)
1853 dtc 1.1 {
1854 rtoy 1.66 char *new_free_pointer;
1855 dtc 1.1
1856 rtoy 1.66 if (nbytes >= large_object_size)
1857     return gc_alloc_large(nbytes, 1, &unboxed_region);
1858 dtc 1.1
1859 rtoy 1.66 /* Check if there is room in the current region. */
1860     new_free_pointer = unboxed_region.free_pointer + nbytes;
1861 dtc 1.14
1862 rtoy 1.66 if (new_free_pointer <= unboxed_region.end_addr) {
1863     /* If so then allocate from the current region. */
1864     void *new_obj = unboxed_region.free_pointer;
1865 dtc 1.14
1866 rtoy 1.66 unboxed_region.free_pointer = new_free_pointer;
1867    
1868     return (void *) new_obj;
1869     }
1870 dtc 1.14
1871 rtoy 1.66 /* Else call gc_alloc */
1872     return gc_alloc_unboxed(nbytes);
1873 dtc 1.1 }
1874    
1875     /***************************************************************************/
1876 rtoy 1.66
1877 dtc 1.1
1878     /* Scavenging/transporting routines derived from gc.c */
1879    
1880 rtoy 1.66 static int (*scavtab[256]) (lispobj * where, lispobj object);
1881     static lispobj(*transother[256]) (lispobj object);
1882     static int (*sizetab[256]) (lispobj * where);
1883 dtc 1.1
1884     static struct weak_pointer *weak_pointers;
1885 dtc 1.14 static struct scavenger_hook *scavenger_hooks = (struct scavenger_hook *) NIL;
1886 dtc 1.1
1887 rtoy 1.95.2.1 /* Like (ceiling x y), but y is constrained to be a power of two */
1888 dtc 1.1 #define CEILING(x,y) (((x) + ((y) - 1)) & (~((y) - 1)))
1889 rtoy 1.66
1890 dtc 1.1
1891     /* Predicates */
1892    
1893 rtoy 1.66 static inline boolean
1894     from_space_p(lispobj obj)
1895 dtc 1.1 {
1896 rtoy 1.66 int page_index = (char *) obj - heap_base;
1897    
1898     return page_index >= 0
1899     && (page_index =
1900     (unsigned int) page_index / PAGE_SIZE) < dynamic_space_pages
1901     && PAGE_GENERATION(page_index) == from_space;
1902 dtc 1.14 }
1903    
1904 rtoy 1.66 static inline boolean
1905     new_space_p(lispobj obj)
1906 dtc 1.14 {
1907 rtoy 1.66 int page_index = (char *) obj - heap_base;
1908    
1909     return page_index >= 0
1910     && (page_index =
1911     (unsigned int) page_index / PAGE_SIZE) < dynamic_space_pages
1912     && PAGE_GENERATION(page_index) == new_space;
1913 dtc 1.1 }
1914 rtoy 1.66
1915 dtc 1.1
1916     /* Copying Objects */
1917    
1918    
1919     /* Copying Boxed Objects */
1920 rtoy 1.66 static inline lispobj
1921     copy_object(lispobj object, int nwords)
1922 dtc 1.1 {
1923 rtoy 1.66 int tag;
1924     lispobj *new;
1925     lispobj *source, *dest;
1926    
1927     gc_assert(Pointerp(object));
1928     gc_assert(from_space_p(object));
1929     gc_assert((nwords & 0x01) == 0);
1930 dtc 1.14
1931 rtoy 1.66 /* get tag of object */
1932     tag = LowtagOf(object);
1933    
1934     /* allocate space */
1935     new = gc_quick_alloc(nwords * sizeof(lispobj));
1936    
1937     dest = new;
1938     source = (lispobj *) PTR(object);
1939    
1940     /* copy the object */
1941     while (nwords > 0) {
1942     dest[0] = source[0];
1943     dest[1] = source[1];
1944     dest += 2;
1945     source += 2;
1946     nwords -= 2;
1947     }
1948    
1949     /* return lisp pointer of new object */
1950     return (lispobj) new | tag;
1951 dtc 1.1 }
1952    
1953 dtc 1.14 /*
1954     * Copying Large Boxed Objects. If the object is in a large object
1955     * region then it is simply promoted, else it is copied. If it's large
1956     * enough then it's copied to a large object region.
1957     *
1958     * Vectors may have shrunk. If the object is not copied the space
1959     * needs to be reclaimed, and the page_tables corrected.
1960     */
1961 rtoy 1.66 static lispobj
1962     copy_large_object(lispobj object, int nwords)
1963 dtc 1.1 {
1964 rtoy 1.66 int tag;
1965     lispobj *new;
1966     lispobj *source, *dest;
1967     int first_page;
1968    
1969     gc_assert(Pointerp(object));
1970     gc_assert(from_space_p(object));
1971     gc_assert((nwords & 0x01) == 0);
1972    
1973     if (gencgc_verbose && nwords > 1024 * 1024)
1974 agoncharov 1.90 fprintf(stderr, "** copy_large_object: %lu\n",
1975     (unsigned long) (nwords * sizeof(lispobj)));
1976 rtoy 1.66
1977     /* Check if it's a large object. */
1978     first_page = find_page_index((void *) object);
1979     gc_assert(first_page >= 0);
1980    
1981     if (PAGE_LARGE_OBJECT(first_page)) {
1982     /* Promote the object. */
1983     int remaining_bytes;
1984     int next_page;
1985     int bytes_freed;
1986     int old_bytes_used;
1987     int mmask, mflags;
1988    
1989     /*
1990     * Note: Any page write protection must be removed, else a later
1991     * scavenge_newspace may incorrectly not scavenge these pages.
1992     * This would not be necessary if they are added to the new areas,
1993     * but lets do it for them all (they'll probably be written
1994     * anyway?).
1995     */
1996    
1997     gc_assert(page_table[first_page].first_object_offset == 0);
1998    
1999     next_page = first_page;
2000     remaining_bytes = nwords * sizeof(lispobj);
2001     while (remaining_bytes > PAGE_SIZE) {
2002     gc_assert(PAGE_GENERATION(next_page) == from_space);
2003     gc_assert(PAGE_ALLOCATED(next_page));
2004     gc_assert(!PAGE_UNBOXED(next_page));
2005     gc_assert(PAGE_LARGE_OBJECT(next_page));
2006     gc_assert(page_table[next_page].first_object_offset ==
2007     PAGE_SIZE * (first_page - next_page));
2008     gc_assert(page_table[next_page].bytes_used == PAGE_SIZE);
2009 dtc 1.1
2010 rtoy 1.66 PAGE_FLAGS_UPDATE(next_page, PAGE_GENERATION_MASK, new_space);
2011    
2012     /*
2013     * Remove any write protection. Should be able to religh on the
2014     * WP flag to avoid redundant calls.
2015     */
2016     if (PAGE_WRITE_PROTECTED(next_page)) {
2017     os_protect((os_vm_address_t) page_address(next_page), PAGE_SIZE,
2018     OS_VM_PROT_ALL);
2019     page_table[next_page].flags &= ~PAGE_WRITE_PROTECTED_MASK;
2020     }
2021     remaining_bytes -= PAGE_SIZE;
2022     next_page++;
2023     }
2024 dtc 1.14
2025 rtoy 1.66 /*
2026     * Now only one page remains, but the object may have shrunk so
2027     * there may be more unused pages which will be freed.
2028     */
2029 dtc 1.1
2030 rtoy 1.66 /* Object may have shrunk but shouldn't have grown - check. */
2031     gc_assert(page_table[next_page].bytes_used >= remaining_bytes);
2032 dtc 1.1
2033 rtoy 1.66 PAGE_FLAGS_UPDATE(next_page, PAGE_GENERATION_MASK, new_space);
2034     gc_assert(PAGE_ALLOCATED(next_page));
2035     gc_assert(!PAGE_UNBOXED(next_page));
2036    
2037     /* Adjust the bytes_used. */
2038     old_bytes_used = page_table[next_page].bytes_used;
2039     page_table[next_page].bytes_used = remaining_bytes;
2040    
2041     bytes_freed = old_bytes_used - remaining_bytes;
2042    
2043     mmask = PAGE_ALLOCATED_MASK | PAGE_UNBOXED_MASK | PAGE_LARGE_OBJECT_MASK
2044     | PAGE_GENERATION_MASK;
2045     mflags = PAGE_ALLOCATED_MASK | PAGE_LARGE_OBJECT_MASK | from_space;
2046    
2047     /* Free any remaining pages; needs care. */
2048     next_page++;
2049     while (old_bytes_used == PAGE_SIZE &&
2050     PAGE_FLAGS(next_page, mmask) == mflags &&
2051     page_table[next_page].first_object_offset ==
2052     PAGE_SIZE * (first_page - next_page)) {
2053     /*
2054     * Checks out OK, free the page. Don't need to both zeroing
2055     * pages as this should have been done before shrinking the
2056     * object. These pages shouldn't be write protected as they
2057     * should be zero filled.
2058     */
2059     gc_assert(!PAGE_WRITE_PROTECTED(next_page));
2060 dtc 1.1
2061 rtoy 1.66 old_bytes_used = page_table[next_page].bytes_used;
2062     page_table[next_page].flags &= ~PAGE_ALLOCATED_MASK;
2063     page_table[next_page].bytes_used = 0;
2064     bytes_freed += old_bytes_used;
2065     next_page++;
2066     }
2067 dtc 1.14
2068 rtoy 1.66 if (gencgc_verbose && bytes_freed > 0)
2069     fprintf(stderr, "* copy_large_boxed bytes_freed %d\n", bytes_freed);
2070 dtc 1.14
2071 rtoy 1.66 generations[from_space].bytes_allocated -=
2072     sizeof(lispobj) * nwords + bytes_freed;
2073     generations[new_space].bytes_allocated += sizeof(lispobj) * nwords;
2074     bytes_allocated -= bytes_freed;
2075 dtc 1.14
2076 rtoy 1.66 /* Add the region to the new_areas if requested. */
2077     add_new_area(first_page, 0, nwords * sizeof(lispobj));
2078 dtc 1.14
2079 rtoy 1.66 return object;
2080     } else {
2081     /* get tag of object */
2082     tag = LowtagOf(object);
2083 dtc 1.14
2084 rtoy 1.66 /* allocate space */
2085     new = gc_quick_alloc_large(nwords * sizeof(lispobj));
2086 dtc 1.15
2087 rtoy 1.66 dest = new;
2088     source = (lispobj *) PTR(object);
2089 dtc 1.14
2090 rtoy 1.66 /* copy the object */
2091     while (nwords > 0) {
2092     dest[0] = source[0];
2093     dest[1] = source[1];
2094     dest += 2;
2095     source += 2;
2096     nwords -= 2;
2097     }
2098 dtc 1.14
2099 rtoy 1.66 /* return lisp pointer of new object */
2100     return (lispobj) new | tag;
2101     }
2102     }
2103 dtc 1.14
2104 rtoy 1.66 /* Copying UnBoxed Objects. */
2105     static inline lispobj
2106     copy_unboxed_object(lispobj object, int nwords)
2107     {
2108     int tag;
2109     lispobj *new;
2110     lispobj *source, *dest;
2111    
2112     gc_assert(Pointerp(object));
2113     gc_assert(from_space_p(object));
2114     gc_assert((nwords & 0x01) == 0);
2115 dtc 1.1
2116     /* get tag of object */
2117     tag = LowtagOf(object);
2118 dtc 1.14
2119 dtc 1.1 /* allocate space */
2120 rtoy 1.66 new = gc_quick_alloc_unboxed(nwords * sizeof(lispobj));
2121 dtc 1.14
2122 dtc 1.1 dest = new;
2123     source = (lispobj *) PTR(object);
2124 dtc 1.14
2125 rtoy 1.66 /* Copy the object */
2126 dtc 1.1 while (nwords > 0) {
2127 rtoy 1.66 dest[0] = source[0];
2128     dest[1] = source[1];
2129     dest += 2;
2130     source += 2;
2131     nwords -= 2;
2132 dtc 1.1 }
2133 dtc 1.14
2134 rtoy 1.66 /* Return lisp pointer of new object. */
2135 dtc 1.14 return (lispobj) new | tag;
2136 dtc 1.1 }
2137    
2138    
2139 dtc 1.14 /*
2140     * Copying Large Unboxed Objects. If the object is in a large object
2141     * region then it is simply promoted, else it is copied. If it's large
2142     * enough then it's copied to a large object region.
2143     *
2144     * Bignums and vectors may have shrunk. If the object is not copied
2145     * the space needs to be reclaimed, and the page_tables corrected.
2146     */
2147 rtoy 1.66 static lispobj
2148     copy_large_unboxed_object(lispobj object, int nwords)
2149 dtc 1.1 {
2150 rtoy 1.66 int tag;
2151     lispobj *new;
2152     lispobj *source, *dest;
2153     int first_page;
2154    
2155     gc_assert(Pointerp(object));
2156     gc_assert(from_space_p(object));
2157     gc_assert((nwords & 0x01) == 0);
2158    
2159     if (gencgc_verbose && nwords > 1024 * 1024)
2160 cshapiro 1.87 fprintf(stderr, "** copy_large_unboxed_object: %lu\n",
2161 agoncharov 1.90 (unsigned long) (nwords * sizeof(lispobj)));
2162 rtoy 1.66
2163     /* Check if it's a large object. */
2164     first_page = find_page_index((void *) object);
2165     gc_assert(first_page >= 0);
2166 dtc 1.14
2167 rtoy 1.66 if (PAGE_LARGE_OBJECT(first_page)) {
2168     /*
2169     * Promote the object. Note: Unboxed objects may have been
2170     * allocated to a BOXED region so it may be necessary to change
2171     * the region to UNBOXED.
2172     */
2173     int remaining_bytes;
2174     int next_page;
2175     int bytes_freed;
2176     int old_bytes_used;
2177     int mmask, mflags;
2178    
2179     gc_assert(page_table[first_page].first_object_offset == 0);
2180    
2181     next_page = first_page;
2182     remaining_bytes = nwords * sizeof(lispobj);
2183     while (remaining_bytes > PAGE_SIZE) {
2184     gc_assert(PAGE_GENERATION(next_page) == from_space);
2185     gc_assert(PAGE_ALLOCATED(next_page));
2186     gc_assert(PAGE_LARGE_OBJECT(next_page));
2187     gc_assert(page_table[next_page].first_object_offset ==
2188     PAGE_SIZE * (first_page - next_page));
2189     gc_assert(page_table[next_page].bytes_used == PAGE_SIZE);
2190    
2191     PAGE_FLAGS_UPDATE(next_page,
2192     PAGE_UNBOXED_MASK | PAGE_GENERATION_MASK,
2193     PAGE_UNBOXED_MASK | new_space);
2194     remaining_bytes -= PAGE_SIZE;
2195     next_page++;
2196     }
2197 dtc 1.1
2198 rtoy 1.66 /*
2199     * Now only one page remains, but the object may have shrunk so
2200     * there may be more unused pages which will be freed.
2201     */
2202 dtc 1.1
2203 rtoy 1.66 /* Object may have shrunk but shouldn't have grown - check. */
2204     gc_assert(page_table[next_page].bytes_used >= remaining_bytes);
2205 dtc 1.1
2206 rtoy 1.66 PAGE_FLAGS_UPDATE(next_page, PAGE_ALLOCATED_MASK | PAGE_UNBOXED_MASK
2207     | PAGE_GENERATION_MASK,
2208     PAGE_ALLOCATED_MASK | PAGE_UNBOXED_MASK | new_space);
2209    
2210     /* Adjust the bytes_used. */
2211     old_bytes_used = page_table[next_page].bytes_used;
2212     page_table[next_page].bytes_used = remaining_bytes;
2213    
2214     bytes_freed = old_bytes_used - remaining_bytes;
2215    
2216     mmask = PAGE_ALLOCATED_MASK | PAGE_LARGE_OBJECT_MASK
2217     | PAGE_GENERATION_MASK;
2218     mflags = PAGE_ALLOCATED_MASK | PAGE_LARGE_OBJECT_MASK | from_space;
2219    
2220     /* Free any remaining pages; needs care. */
2221     next_page++;
2222     while (old_bytes_used == PAGE_SIZE &&
2223     PAGE_FLAGS(next_page, mmask) == mflags &&
2224     page_table[next_page].first_object_offset ==
2225     PAGE_SIZE * (first_page - next_page)) {
2226     /*
2227     * Checks out OK, free the page. Don't need to both zeroing
2228     * pages as this should have been done before shrinking the
2229     * object. These pages shouldn't be write protected, even if
2230     * boxed they should be zero filled.
2231     */
2232     gc_assert(!PAGE_WRITE_PROTECTED(next_page));
2233 dtc 1.14
2234 rtoy 1.66 old_bytes_used = page_table[next_page].bytes_used;
2235     page_table[next_page].flags &= ~PAGE_ALLOCATED_MASK;
2236     page_table[next_page].bytes_used = 0;
2237     bytes_freed += old_bytes_used;
2238     next_page++;
2239     }
2240 dtc 1.14
2241 rtoy 1.66 if (gencgc_verbose && bytes_freed > 0)
2242     fprintf(stderr, "* copy_large_unboxed bytes_freed %d\n",
2243     bytes_freed);
2244    
2245     generations[from_space].bytes_allocated -=
2246     sizeof(lispobj) * nwords + bytes_freed;
2247     generations[new_space].bytes_allocated += sizeof(lispobj) * nwords;
2248     bytes_allocated -= bytes_freed;
2249 dtc 1.14
2250 rtoy 1.66 return object;
2251     } else {
2252     /* get tag of object */
2253     tag = LowtagOf(object);
2254 dtc 1.14
2255 rtoy 1.66 /* allocate space */
2256     new = gc_quick_alloc_large_unboxed(nwords * sizeof(lispobj));
2257 dtc 1.14
2258 rtoy 1.66 dest = new;
2259     source = (lispobj *) PTR(object);
2260 dtc 1.14
2261 rtoy 1.66 /* copy the object */
2262     while (nwords > 0) {
2263     dest[0] = source[0];
2264     dest[1] = source[1];
2265     dest += 2;
2266     source += 2;
2267     nwords -= 2;
2268     }
2269 dtc 1.14
2270 rtoy 1.66 /* return lisp pointer of new object */
2271     return (lispobj) new | tag;
2272 dtc 1.1 }
2273     }
2274 rtoy 1.66
2275 dtc 1.1
2276     /* Scavenging */
2277 toy 1.49
2278     /*
2279     * Douglas Crosher says:
2280     *
2281     * There were two different ways in which the scavenger dispatched,
2282     * and DIRECT_SCAV was one option. This code did work at one stage
2283     * but testing showed it to be slower. When DIRECT_SCAV is enabled
2284     * the scavenger dispatches via the scavtab for all objects, and when
2285     * disabled the scavenger firstly detects and handles some common
2286     * cases itself before dispatching.
2287     */
2288 dtc 1.1
2289     #define DIRECT_SCAV 0
2290    
2291 gerd 1.38 static void
2292 rtoy 1.66 scavenge(void *start_obj, long nwords)
2293 dtc 1.1 {
2294 rtoy 1.66 lispobj *start;
2295 toy 1.42
2296 rtoy 1.66 start = (lispobj *) start_obj;
2297    
2298     while (nwords > 0) {
2299     lispobj object;
2300     int words_scavenged;
2301 dtc 1.14
2302 rtoy 1.66 object = *start;
2303     /* Not a forwarding pointer. */
2304     gc_assert(object != 0x01);
2305 dtc 1.14
2306 dtc 1.1 #if DIRECT_SCAV
2307 rtoy 1.66 words_scavenged = scavtab[TypeOf(object)] (start, object);
2308     #else /* not DIRECT_SCAV */
2309     if (Pointerp(object)) {
2310 gerd 1.38 #ifdef GC_ASSERTIONS
2311 rtoy 1.66 check_escaped_stack_object(start, object);
2312 gerd 1.38 #endif
2313 dtc 1.14
2314 rtoy 1.66 if (from_space_p(object)) {
2315     lispobj *ptr = (lispobj *) PTR(object);
2316     lispobj first_word = *ptr;
2317    
2318     if (first_word == 0x01) {
2319     *start = ptr[1];
2320     words_scavenged = 1;
2321     } else
2322     words_scavenged = scavtab[TypeOf(object)] (start, object);
2323     } else
2324     words_scavenged = 1;
2325     } else if ((object & 3) == 0)
2326 gerd 1.38 words_scavenged = 1;
2327 rtoy 1.66 else
2328     words_scavenged = scavtab[TypeOf(object)] (start, object);
2329 gerd 1.38 #endif /* not DIRECT_SCAV */
2330 dtc 1.14
2331 rtoy 1.66 start += words_scavenged;
2332     nwords -= words_scavenged;
2333 gerd 1.38 }
2334 rtoy 1.66
2335     gc_assert(nwords == 0);
2336 dtc 1.1 }
2337 rtoy 1.66
2338 dtc 1.1
2339 cwang 1.55 #if !(defined(i386) || defined(__x86_64))
2340 toy 1.33 /* Scavenging Interrupt Contexts */
2341    
2342     static int boxed_registers[] = BOXED_REGISTERS;
2343    
2344 rtoy 1.66 static void
2345     scavenge_interrupt_context(os_context_t * context)
2346 toy 1.33 {
2347 rtoy 1.66 int i;
2348 rtoy 1.69 unsigned long pc_code_offset;
2349 rtoy 1.66
2350 toy 1.33 #ifdef reg_LIP
2351 rtoy 1.66 unsigned long lip;
2352     unsigned long lip_offset;
2353     int lip_register_pair;
2354 toy 1.33 #endif
2355 rtoy 1.68 #ifdef reg_LR
2356     unsigned long lr_code_offset;
2357     #endif
2358     #ifdef reg_CTR
2359     unsigned long ctr_code_offset;
2360     #endif
2361 toy 1.33 #ifdef SC_NPC
2362 rtoy 1.66 unsigned long npc_code_offset;
2363 toy 1.33 #endif
2364    
2365     #ifdef reg_LIP
2366 rtoy 1.66 /* Find the LIP's register pair and calculate it's offset */
2367     /* before we scavenge the context. */
2368 toy 1.33
2369 rtoy 1.66 /*
2370     * I (RLT) think this is trying to find the boxed register that is
2371     * closest to the LIP address, without going past it. Usually, it's
2372     * reg_CODE or reg_LRA. But sometimes, nothing can be found.
2373     */
2374     lip = SC_REG(context, reg_LIP);
2375     lip_offset = 0x7FFFFFFF;
2376     lip_register_pair = -1;
2377     for (i = 0; i < (sizeof(boxed_registers) / sizeof(int)); i++) {
2378     unsigned long reg;
2379     long offset;
2380     int index;
2381    
2382     index = boxed_registers[i];
2383     reg = SC_REG(context, index);
2384     if (Pointerp(reg) && PTR(reg) <= lip) {
2385     offset = lip - reg;
2386     if (offset < lip_offset) {
2387     lip_offset = offset;
2388     lip_register_pair = index;
2389     }
2390     }
2391 toy 1.33 }
2392     #endif /* reg_LIP */
2393    
2394 rtoy 1.69 /*
2395     * Compute the PC's offset from the start of the CODE
2396     * register.
2397     */
2398 rtoy 1.66 pc_code_offset = SC_PC(context) - SC_REG(context, reg_CODE);
2399 toy 1.33 #ifdef SC_NPC
2400 rtoy 1.66 npc_code_offset = SC_NPC(context) - SC_REG(context, reg_CODE);
2401 toy 1.33 #endif /* SC_NPC */
2402    
2403 rtoy 1.68 #ifdef reg_LR
2404     lr_code_offset = SC_REG(context, reg_LR) - SC_REG(context, reg_CODE);
2405     #endif
2406     #ifdef reg_CTR
2407     ctr_code_offset = SC_REG(context, reg_CTR) - SC_REG(context, reg_CODE);
2408     #endif
2409 rtoy 1.75
2410 rtoy 1.66 /* Scanvenge all boxed registers in the context. */
2411     for (i = 0; i < (sizeof(boxed_registers) / sizeof(int)); i++) {
2412     int index;
2413     lispobj foo;
2414    
2415     index = boxed_registers[i];
2416     foo = SC_REG(context, index);
2417     scavenge(&foo, 1);
2418     SC_REG(context, index) = foo;
2419    
2420     scavenge(&(SC_REG(context, index)), 1);
2421 toy 1.33 }
2422    
2423     #ifdef reg_LIP
2424 rtoy 1.66 /* Fix the LIP */
2425 toy 1.33
2426 rtoy 1.66 /*
2427     * But what happens if lip_register_pair is -1? SC_REG on Solaris
2428     * (see solaris_register_address in solaris-os.c) will return
2429     * &context->uc_mcontext.gregs[2]. But gregs[2] is REG_nPC. Is
2430     * that what we really want? My guess is that that is not what we
2431     * want, so if lip_register_pair is -1, we don't touch reg_LIP at
2432     * all. But maybe it doesn't really matter if LIP is trashed?
2433     */
2434     if (lip_register_pair >= 0) {
2435     SC_REG(context, reg_LIP) =
2436     SC_REG(context, lip_register_pair) + lip_offset;
2437 toy 1.33 }
2438     #endif /* reg_LIP */
2439 rtoy 1.66
2440     /* Fix the PC if it was in from space */
2441 rtoy 1.69 if (from_space_p(SC_PC(context))) {
2442     SC_PC(context) = SC_REG(context, reg_CODE) + pc_code_offset;
2443     }
2444 toy 1.33 #ifdef SC_NPC
2445 rtoy 1.69 if (from_space_p(SC_NPC(context))) {
2446 rtoy 1.66 SC_NPC(context) = SC_REG(context, reg_CODE) + npc_code_offset;
2447 rtoy 1.69 }
2448 toy 1.33 #endif /* SC_NPC */
2449 rtoy 1.68
2450     #ifdef reg_LR
2451 rtoy 1.69 if (from_space_p(SC_REG(context, reg_LR))) {
2452     SC_REG(context, reg_LR) = SC_REG(context, reg_CODE) + lr_code_offset;
2453 rtoy 1.68 }
2454     #endif
2455     #ifdef reg_CTR
2456 rtoy 1.69 if (from_space_p(SC_REG(context, reg_CTR))) {
2457     SC_REG(context, reg_CTR) = SC_REG(context, reg_CODE) + ctr_code_offset;
2458 rtoy 1.68 }
2459     #endif
2460 toy 1.33 }
2461    
2462 rtoy 1.66 void
2463     scavenge_interrupt_contexts(void)
2464 toy 1.33 {
2465 rtoy 1.66 int i, index;
2466     os_context_t *context;
2467 toy 1.33
2468 toy 1.40 #ifdef PRINTNOISE
2469 rtoy 1.66 printf("Scavenging interrupt contexts ...\n");
2470 toy 1.40 #endif
2471    
2472 rtoy 1.66 index = fixnum_value(SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX));
2473 toy 1.33
2474     #if defined(DEBUG_PRINT_CONTEXT_INDEX)
2475 rtoy 1.66 printf("Number of active contexts: %d\n", index);
2476 toy 1.33 #endif
2477    
2478 rtoy 1.66 for (i = 0; i < index; i++) {
2479     context = lisp_interrupt_contexts[i];
2480     scavenge_interrupt_context(context);
2481 toy 1.33 }
2482     }
2483     #endif
2484    
2485 dtc 1.1 /* Code and Code-Related Objects */
2486    
2487 toy 1.33 /*
2488     * Aargh! Why is SPARC so different here? What is the advantage of
2489     * making it different from all the other ports?
2490     */
2491 cshapiro 1.87 #if defined(sparc) || (defined(DARWIN) && defined(__ppc__))
2492 toy 1.33 #define RAW_ADDR_OFFSET 0
2493     #else
2494 dtc 1.14 #define RAW_ADDR_OFFSET (6 * sizeof(lispobj) - type_FunctionPointer)
2495 toy 1.33 #endif
2496 dtc 1.1
2497     static lispobj trans_function_header(lispobj object);
2498     static lispobj trans_boxed(lispobj object);
2499    
2500     #if DIRECT_SCAV
2501 rtoy 1.66 static int
2502     scav_function_pointer(lispobj * where, lispobj object)
2503 dtc 1.1 {
2504 rtoy 1.66 gc_assert(Pointerp(object));
2505 dtc 1.1
2506 rtoy 1.66 if (from_space_p(object)) {
2507     lispobj first, *first_pointer;
2508 dtc 1.14
2509 rtoy 1.66 /*
2510     * Object is a pointer into from space - check to see if it has
2511     * been forwarded.
2512     */
2513     first_pointer = (lispobj *) PTR(object);
2514     first = *first_pointer;
2515 dtc 1.14
2516 rtoy 1.66 if (first == 0x01) {
2517     /* Forwarded */
2518     *where = first_pointer[1];
2519     return 1;
2520     } else {
2521     int type;
2522     lispobj copy;
2523 dtc 1.14
2524 rtoy 1.66 /*
2525     * Must transport object -- object may point to either a
2526     * function header, a closure function header, or to a closure
2527     * header.
2528     */
2529 dtc 1.14
2530 rtoy 1.66 type = TypeOf(first);
2531     switch (type) {
2532     case type_FunctionHeader:
2533     case type_ClosureFunctionHeader:
2534     copy = trans_function_header(object);
2535     break;
2536     default:
2537     copy = trans_boxed(object);
2538     break;
2539     }
2540 dtc 1.14
2541 rtoy 1.66 if (copy != object) {
2542     /* Set forwarding pointer. */
2543     first_pointer[0] = 0x01;
2544     first_pointer[1] = copy;
2545     }
2546 dtc 1.14
2547 rtoy 1.66 first = copy;
2548     }
2549 dtc 1.14
2550 rtoy 1.66 gc_assert(Pointerp(first));
2551     gc_assert(!from_space_p(first));
2552 dtc 1.14
2553 rtoy 1.66 *where = first;
2554     }
2555     return 1;
2556 dtc 1.1 }
2557     #else
2558 rtoy 1.66 static int
2559     scav_function_pointer(lispobj * where, lispobj object)
2560 dtc 1.1 {
2561 rtoy 1.66 lispobj *first_pointer;
2562     lispobj copy;
2563    
2564     gc_assert(Pointerp(object));
2565    
2566     /* Object is a pointer into from space - no a FP. */
2567     first_pointer = (lispobj *) PTR(object);
2568 dtc 1.1
2569 rtoy 1.66 /*
2570     * Must transport object -- object may point to either a function
2571     * header, a closure function header, or to a closure header.
2572     */
2573 dtc 1.14
2574 rtoy 1.66 switch (TypeOf(*first_pointer)) {
2575     case type_FunctionHeader:
2576     case type_ClosureFunctionHeader:
2577     copy = trans_function_header(object);
2578     break;
2579     default:
2580     copy = trans_boxed(object);
2581     break;
2582     }
2583 dtc 1.14
2584 rtoy 1.66 if (copy != object) {
2585     /* Set forwarding pointer */
2586     first_pointer[0] = 0x01;
2587     first_pointer[1] = copy;
2588     }
2589 dtc 1.14
2590 rtoy 1.66 gc_assert(Pointerp(copy));
2591     gc_assert(!from_space_p(copy));
2592 dtc 1.1
2593 rtoy 1.66 *where = copy;
2594 dtc 1.14
2595 rtoy 1.66 return 1;
2596 dtc 1.1 }
2597     #endif
2598    
2599 cwang 1.55 #if defined(i386) || defined(__x86_64)
2600 dtc 1.14 /*
2601 cwang 1.54 * Scan an x86 compiled code object, looking for possible fixups that
2602 dtc 1.14 * have been missed after a move.
2603     *
2604     * Two types of fixups are needed:
2605     * 1. Absolution fixups to within the code object.
2606     * 2. Relative fixups to outside the code object.
2607     *
2608     * Currently only absolution fixups to the constant vector, or to the
2609     * code area are checked.
2610     */
2611 rtoy 1.66 void
2612     sniff_code_object(struct code *code, unsigned displacement)
2613 dtc 1.1 {
2614 rtoy 1.66 int nheader_words, ncode_words, nwords;
2615     void *p;
2616     void *constants_start_addr, *constants_end_addr;
2617     void *code_start_addr, *code_end_addr;
2618     int fixup_found = 0;
2619 dtc 1.14
2620 rtoy 1.66 if (!check_code_fixups)
2621     return;
2622 dtc 1.3
2623 rtoy 1.66 /*
2624     * It's ok if it's byte compiled code. The trace table offset will
2625     * be a fixnum if it's x86 compiled code - check.
2626     */
2627     if (code->trace_table_offset & 0x3) {
2628 dtc 1.14 #if 0
2629 rtoy 1.66 fprintf(stderr, "*** Sniffing byte compiled code object at %x.\n",
2630     code);
2631 dtc 1.14 #endif
2632 rtoy 1.66 return;
2633     }
2634    
2635     /* Else it's x86 machine code. */
2636    
2637     ncode_words = fixnum_value(code->code_size);
2638     nheader_words = HeaderValue(*(lispobj *) code);
2639     nwords = ncode_words + nheader_words;
2640    
2641     constants_start_addr = (void *) code + 5 * sizeof(lispobj);
2642     constants_end_addr = (void *) code + nheader_words * sizeof(lispobj);
2643     code_start_addr = (void *) code + nheader_words * sizeof(lispobj);
2644     code_end_addr = (void *) code + nwords * sizeof(lispobj);
2645    
2646     /* Work through the unboxed code. */
2647     for (p = code_start_addr; p < code_end_addr; p++) {
2648     void *data = *(void **) p;
2649     unsigned d1 = *((unsigned char *) p - 1);
2650     unsigned d2 = *((unsigned char *) p - 2);
2651     unsigned d3 = *((unsigned char *) p - 3);
2652     unsigned d4 = *((unsigned char *) p - 4);
2653     unsigned d5 = *((unsigned char *) p - 5);
2654     unsigned d6 = *((unsigned char *) p - 6);
2655    
2656     /*
2657     * Check for code references.
2658     *
2659     * Check for a 32 bit word that looks like an absolute reference
2660     * to within the code adea of the code object.
2661     */
2662     if (data >= code_start_addr - displacement
2663     && data < code_end_addr - displacement) {
2664     /* Function header */
2665     if (d4 == 0x5e
2666     && ((unsigned long) p - 4 -
2667     4 * HeaderValue(*((unsigned long *) p - 1))) ==
2668     (unsigned long) code) {
2669     /* Skip the function header */
2670     p += 6 * 4 - 4 - 1;
2671     continue;
2672     }
2673     /* Push imm32 */
2674     if (d1 == 0x68) {
2675     fixup_found = 1;
2676     fprintf(stderr,
2677     "Code ref. @ %lx: %.2x %.2x %.2x %.2x %.2x %.2x (%.8lx)\n",
2678     (unsigned long) p, d6, d5, d4, d3, d2, d1,
2679     (unsigned long) data);
2680     fprintf(stderr, "*** Push $0x%.8lx\n", (unsigned long) data);
2681     }
2682     /* Mov [reg-8],imm32 */
2683     if (d3 == 0xc7
2684     && (d2 == 0x40 || d2 == 0x41 || d2 == 0x42 || d2 == 0x43
2685     || d2 == 0x45 || d2 == 0x46 || d2 == 0x47)
2686     && d1 == 0xf8) {
2687     fixup_found = 1;
2688     fprintf(stderr,
2689     "Code ref. @ %lx: %.2x %.2x %.2x %.2x %.2x %.2x (%.8lx)\n",
2690     (unsigned long) p, d6, d5, d4, d3, d2, d1,
2691     (unsigned long) data);
2692     fprintf(stderr, "*** Mov [reg-8],$0x%.8lx\n",
2693     (unsigned long) data);
2694     }
2695     /* Lea reg, [disp32] */
2696     if (d2 == 0x8d && (d1 & 0xc7) == 5) {
2697     fixup_found = 1;
2698     fprintf(stderr,
2699     "Code ref. @ %lx: %.2x %.2x %.2x %.2x %.2x %.2x (%.8lx)\n",
2700     (unsigned long) p, d6, d5, d4, d3, d2, d1,
2701     (unsigned long) data);
2702     fprintf(stderr, "*** Lea reg,[$0x%.8lx]\n",
2703     (unsigned long) data);
2704     }
2705     }
2706    
2707     /*
2708     * Check for constant references.
2709     *
2710     * Check for a 32 bit word that looks like an absolution reference
2711     * to within the constant vector. Constant references will be
2712     * aligned.
2713     */
2714     if (data >= constants_start_addr - displacement
2715     && data < constants_end_addr - displacement
2716     && ((unsigned long) data & 0x3) == 0) {
2717     /* Mov eax,m32 */
2718     if (d1 == 0xa1) {
2719     fixup_found = 1;
2720     fprintf(stderr,
2721     "Abs. const. ref. @ %lx: %.2x %.2x %.2x %.2x %.2x %.2x (%.8lx)\n",
2722     (unsigned long) p, d6, d5, d4, d3, d2, d1,
2723     (unsigned long) data);
2724     fprintf(stderr, "*** Mov eax,0x%.8lx\n", (unsigned long) data);
2725     }
2726    
2727     /* Mov m32,eax */
2728     if (d1 == 0xa3) {
2729     fixup_found = 1;
2730     fprintf(stderr,
2731     "Abs. const. ref. @ %lx: %.2x %.2x %.2x %.2x %.2x %.2x (%.8lx)\n",
2732     (unsigned long) p, d6, d5, d4, d3, d2, d1,
2733     (unsigned long) data);
2734     fprintf(stderr, "*** Mov 0x%.8lx,eax\n", (unsigned long) data);
2735     }
2736    
2737     /* Cmp m32,imm32 */
2738     if (d1 == 0x3d && d2 == 0x81) {
2739     fixup_found = 1;
2740     fprintf(stderr,
2741     "Abs. const. ref. @ %lx: %.2x %.2x %.2x %.2x %.2x %.2x (%.8lx)\n",
2742     (unsigned long) p, d6, d5, d4, d3, d2, d1,
2743     (unsigned long) data);
2744     /* XX Check this */
2745     fprintf(stderr, "*** Cmp 0x%.8lx,immed32\n",
2746     (unsigned long) data);
2747     }
2748    
2749     /* Check for a mod=00, r/m=101 byte. */
2750     if ((d1 & 0xc7) == 5) {
2751     /* Cmp m32,reg */
2752     if (d2 == 0x39) {
2753     fixup_found = 1;
2754     fprintf(stderr,
2755     "Abs. const. ref. @ %lx: %.2x %.2x %.2x %.2x %.2x %.2x (%.8lx)\n",
2756     (unsigned long) p, d6, d5, d4, d3, d2, d1,
2757     (unsigned long) data);
2758     fprintf(stderr, "*** Cmp 0x%.8lx,reg\n",
2759     (unsigned long) data);
2760     }
2761     /* Cmp reg32,m32 */
2762     if (d2 == 0x3b) {
2763     fixup_found = 1;
2764     fprintf(stderr,
2765     "Abs. const. ref. @ %lx: %.2x %.2x %.2x %.2x %.2x %.2x (%.8lx)\n",
2766     (unsigned long) p, d6, d5, d4, d3, d2, d1,
2767     (unsigned long) data);
2768     fprintf(stderr, "*** Cmp reg32,0x%.8lx\n",
2769     (unsigned long) data);
2770     }
2771     /* Mov m32,reg32 */
2772     if (d2 == 0x89) {
2773     fixup_found = 1;
2774     fprintf(stderr,
2775     "Abs. const. ref. @ %lx: %.2x %.2x %.2x %.2x %.2x %.2x (%.8lx)\n",
2776     (unsigned long) p, d6, d5, d4, d3, d2, d1,
2777     (unsigned long) data);
2778     fprintf(stderr, "*** Mov 0x%.8lx,reg32\n",
2779     (unsigned long) data);
2780     }
2781     /* Mov reg32,m32 */
2782     if (d2 == 0x8b) {
2783     fixup_found = 1;
2784     fprintf(stderr,
2785     "Abs. const. ref. @ %lx: %.2x %.2x %.2x %.2x %.2x %.2x (%.8lx)\n",
2786     (unsigned long) p, d6, d5, d4, d3, d2, d1,
2787     (unsigned long) data);
2788     fprintf(stderr, "*** Mov reg32,0x%.8lx\n",
2789     (unsigned long) data);
2790     }
2791     /* Lea reg32,m32 */
2792     if (d2 == 0x8d) {
2793     fixup_found = 1;
2794     fprintf(stderr,
2795     "Abs. const. ref. @ %lx: %.2x %.2x %.2x %.2x %.2x %.2x (%.8lx)\n",
2796     (unsigned long) p, d6, d5, d4, d3, d2, d1,
2797     (unsigned long) data);
2798     fprintf(stderr, "*** Lea reg32,0x%.8lx\n",
2799     (unsigned long) data);
2800     }
2801     }
2802     }
2803     }
2804    
2805     /* If anything was found print out some info. on the code object. */
2806     if (fixup_found) {
2807     fprintf(stderr,
2808     "*** Compiled code object at %lx: header_words=%d code_words=%d .\n",
2809     (unsigned long) code, nheader_words, ncode_words);
2810     fprintf(stderr,
2811     "*** Const. start = %lx; end= %lx; Code start = %lx; end = %lx\n",
2812     (unsigned long) constants_start_addr,
2813     (unsigned long) constants_end_addr,
2814     (unsigned long) code_start_addr, (unsigned long) code_end_addr);
2815     }
2816     }
2817 dtc 1.1
2818 rtoy 1.66 static void
2819     apply_code_fixups(struct code *old_code, struct code *new_code)
2820     {
2821     int nheader_words, ncode_words, nwords;
2822     void *constants_start_addr, *constants_end_addr;
2823     void *code_start_addr, *code_end_addr;
2824     lispobj fixups = NIL;
2825     unsigned long displacement =
2826 dtc 1.1
2827 rtoy 1.66 (unsigned long) new_code - (unsigned long) old_code;
2828     struct vector *fixups_vector;
2829 dtc 1.14
2830     /*
2831 rtoy 1.66 * It's ok if it's byte compiled code. The trace table offset will
2832     * be a fixnum if it's x86 compiled code - check.
2833 dtc 1.14 */
2834 rtoy 1.66 if (new_code->trace_table_offset & 0x3) {
2835     #if 0
2836     fprintf(stderr, "*** Byte compiled code object at %x.\n", new_code);
2837     #endif
2838     return;
2839 dtc 1.1 }
2840    
2841 rtoy 1.66 /* Else it's x86 machine code. */
2842     ncode_words = fixnum_value(new_code->code_size);
2843     nheader_words = HeaderValue(*(lispobj *) new_code);
2844     nwords = ncode_words + nheader_words;
2845 dtc 1.14 #if 0
2846 rtoy 1.66 fprintf(stderr,
2847     "*** Compiled code object at %x: header_words=%d code_words=%d .\n",
2848     new_code, nheader_words, ncode_words);
2849     #endif
2850     constants_start_addr = (void *) new_code + 5 * sizeof(lispobj);
2851     constants_end_addr = (void *) new_code + nheader_words * sizeof(lispobj);
2852     code_start_addr = (void *) new_code + nheader_words * sizeof(lispobj);
2853     code_end_addr = (void *) new_code + nwords * sizeof(lispobj);
2854     #if 0
2855     fprintf(stderr,
2856     "*** Const. start = %x; end= %x; Code start = %x; end = %x\n",
2857     constants_start_addr, constants_end_addr, code_start_addr,
2858     code_end_addr);
2859 dtc 1.14 #endif
2860 dtc 1.1
2861 rtoy 1.66 /*
2862     * The first constant should be a pointer to the fixups for this
2863     * code objects - Check.
2864     */
2865     fixups = new_code->constants[0];
2866    
2867     /*
2868     * It will be 0 or the unbound-marker if there are no fixups, and
2869     * will be an other pointer if it is valid.
2870     */
2871     if (fixups == 0 || fixups == type_UnboundMarker || !Pointerp(fixups)) {
2872     /* Check for possible errors. */
2873     if (check_code_fixups)
2874     sniff_code_object(new_code, displacement);
2875 dtc 1.14
2876     #if 0
2877 rtoy 1.66 fprintf(stderr, "Fixups for code object not found!?\n");
2878     fprintf(stderr,
2879     "*** Compiled code object at %x: header_words=%d code_words=%d .\n",
2880     new_code, nheader_words, ncode_words);
2881     fprintf(stderr,
2882     "*** Const. start = %x; end= %x; Code start = %x; end = %x\n",
2883     constants_start_addr, constants_end_addr, code_start_addr,
2884     code_end_addr);
2885 dtc 1.14 #endif
2886 rtoy 1.66 return;
2887     }
2888 dtc 1.1
2889 rtoy 1.66 fixups_vector = (struct vector *) PTR(fixups);
2890 dtc 1.1
2891 rtoy 1.66 /* Could be pointing to a forwarding pointer. */
2892     if (Pointerp(fixups) && find_page_index((void *) fixups_vector) != -1
2893     && fixups_vector->header == 0x01) {
2894 dtc 1.19 #if 0
2895 rtoy 1.66 fprintf(stderr, "* FF\n");
2896 dtc 1.19 #endif
2897 rtoy 1.66 /* If so then follow it. */
2898     fixups_vector = (struct vector *) PTR((lispobj) fixups_vector->length);
2899     }
2900 dtc 1.14 #if 0
2901 rtoy 1.66 fprintf(stderr, "Got the fixups\n");
2902 dtc 1.14 #endif
2903 dtc 1.1
2904 rtoy 1.66 if (TypeOf(fixups_vector->header) == type_SimpleArrayUnsignedByte32) {
2905 dtc 1.14 /*
2906 rtoy 1.66 * Got the fixups for the code block. Now work through the
2907     * vector, and apply a fixup at each address.
2908 dtc 1.14 */
2909 rtoy 1.66 int length = fixnum_value(fixups_vector->length);
2910     int i;
2911    
2912     for (i = 0; i < length; i++) {
2913     unsigned offset = fixups_vector->data[i];
2914    
2915     /* Now check the current value of offset. */
2916     unsigned long old_value =
2917     *(unsigned long *) ((unsigned long) code_start_addr + offset);
2918    
2919     /*
2920     * If it's within the old_code object then it must be an
2921     * absolute fixup (relative ones are not saved).
2922     */
2923     if (old_value >= (unsigned long) old_code
2924     && old_value <
2925     (unsigned long) old_code + nwords * sizeof(lispobj))
2926     /* So add the dispacement. */
2927     *(unsigned long *) ((unsigned long) code_start_addr + offset) =
2928     old_value + displacement;
2929     else
2930     /*
2931     * It is outside the old code object so it must be a relative
2932     * fixup (absolute fixups are not saved). So subtract the
2933     * displacement.
2934     */
2935     *(unsigned long *) ((unsigned long) code_start_addr + offset) =
2936     old_value - displacement;
2937     }
2938 dtc 1.1 }
2939 dtc 1.14
2940 rtoy 1.66 /* Check for possible errors. */
2941     if (check_code_fixups)
2942     sniff_code_object(new_code, displacement);
2943 dtc 1.1 }
2944 toy 1.33 #endif
2945 dtc 1.1
2946 rtoy 1.66 static struct code *
2947     trans_code(struct code *code)
2948 dtc 1.1 {
2949 rtoy 1.66 struct code *new_code;
2950     lispobj l_code, l_new_code;
2951     int nheader_words, ncode_words, nwords;
2952     unsigned long displacement;
2953     lispobj fheaderl, *prev_pointer;
2954 dtc 1.14
2955     #if 0
2956 rtoy 1.66 fprintf(stderr, "\nTransporting code object located at 0x%08x.\n",
2957     (unsigned long) code);
2958 dtc 1.14 #endif
2959    
2960 rtoy 1.66 /* If object has already been transported, just return pointer */
2961     if (*(lispobj *) code == 0x01) {
2962     return (struct code *) (((lispobj *) code)[1]);
2963 toy 1.33 }
2964 dtc 1.14
2965    
2966 rtoy 1.66 gc_assert(TypeOf(code->header) == type_CodeHeader);
2967 dtc 1.14
2968 rtoy 1.66 /* prepare to transport the code vector */
2969     l_code = (lispobj) code | type_OtherPointer;
2970 dtc 1.14
2971 rtoy 1.66 ncode_words = fixnum_value(code->code_size);
2972     nheader_words = HeaderValue(code->header);
2973     nwords = ncode_words + nheader_words;
2974     nwords = CEILING(nwords, 2);
2975 dtc 1.1
2976 rtoy 1.66 l_new_code = copy_large_object(l_code, nwords);
2977     new_code = (struct code *) PTR(l_new_code);
2978    
2979     /* May not have been moved. */
2980     if (new_code == code)
2981     return new_code;
2982 dtc 1.14
2983 rtoy 1.66 displacement = l_new_code - l_code;
2984 dtc 1.14
2985     #if 0
2986 rtoy 1.66 fprintf(stderr, "Old code object at 0x%08x, new code object at 0x%08x.\n",
2987     (unsigned long) code, (unsigned long) new_code);
2988     fprintf(stderr, "Code object is %d words long.\n", nwords);
2989 dtc 1.14 #endif
2990    
2991 rtoy 1.66 /* set forwarding pointer */
2992     ((lispobj *) code)[0] = 0x01;
2993     ((lispobj *) code)[1] = l_new_code;
2994 dtc 1.14
2995 rtoy 1.66 /*
2996     * Set forwarding pointers for all the function headers in the code
2997     * object; also fix all self pointers.
2998     */
2999 dtc 1.14
3000 rtoy 1.66 fheaderl = code->entry_points;
3001     prev_pointer = &new_code->entry_points;
3002 dtc 1.14
3003 rtoy 1.66 while (fheaderl != NIL) {
3004     struct function *fheaderp, *nfheaderp;
3005     lispobj nfheaderl;
3006 dtc 1.14
3007 rtoy 1.66 fheaderp = (struct function *) PTR(fheaderl);
3008     gc_assert(TypeOf(fheaderp->header) == type_FunctionHeader);
3009 dtc 1.14
3010 rtoy 1.66 /*
3011     * Calcuate the new function pointer and the new function header.
3012     */
3013     nfheaderl = fheaderl + displacement;
3014     nfheaderp = (struct function *) PTR(nfheaderl);
3015 dtc 1.14
3016 rtoy 1.66 /* set forwarding pointer */
3017     ((lispobj *) fheaderp)[0] = 0x01;
3018     ((lispobj *) fheaderp)[1] = nfheaderl;
3019 dtc 1.14
3020 rtoy 1.66 /* Fix self pointer */
3021     nfheaderp->self = nfheaderl + RAW_ADDR_OFFSET;
3022 dtc 1.14
3023 rtoy 1.66 *prev_pointer = nfheaderl;
3024 dtc 1.14
3025 rtoy 1.66 fheaderl = fheaderp->next;
3026     prev_pointer = &nfheaderp->next;
3027     }
3028 dtc 1.1
3029 dtc 1.14 #if 0
3030 rtoy 1.66 sniff_code_object(new_code, displacement);
3031 dtc 1.14 #endif
3032 cwang 1.55 #if defined(i386) || defined(__x86_64)
3033 rtoy 1.66 apply_code_fixups(code, new_code);
3034 toy 1.33 #else
3035 rtoy 1.66 /* From gc.c */
3036 toy 1.33 #ifndef MACH
3037 rtoy 1.66 os_flush_icache((os_vm_address_t) (((int *) new_code) + nheader_words),
3038     ncode_words * sizeof(int));
3039 toy 1.33 #endif
3040     #endif
3041 dtc 1.14
3042 rtoy 1.66 return new_code;
3043 dtc 1.1 }
3044    
3045 rtoy 1.66 static int
3046     scav_code_header(lispobj * where, lispobj object)
3047 dtc 1.1 {
3048 rtoy 1.66 struct code *code;
3049     int nheader_words, ncode_words, nwords;
3050     lispobj fheaderl;
3051     struct function *fheaderp;
3052    
3053     code = (struct code *) where;
3054     ncode_words = fixnum_value(code->code_size);
3055     nheader_words = HeaderValue(object);
3056     nwords = ncode_words + nheader_words;
3057     nwords = CEILING(nwords, 2);
3058 dtc 1.14
3059 rtoy 1.66 /* Scavenge the boxed section of the code data block */
3060     scavenge(where + 1, nheader_words - 1);
3061 dtc 1.1
3062 rtoy 1.66 /*
3063     * Scavenge the boxed section of each function object in the code
3064     * data block
3065     */
3066     fheaderl = code->entry_points;
3067     while (fheaderl != NIL) {
3068     fheaderp = (struct function *) PTR(fheaderl);
3069     gc_assert(TypeOf(fheaderp->header) == type_FunctionHeader);
3070 dtc 1.1
3071 rtoy 1.66 scavenge(&fheaderp->name, 1);
3072     scavenge(&fheaderp->arglist, 1);
3073     scavenge(&fheaderp->type, 1);
3074 dtc 1.14
3075 rtoy 1.66 fheaderl = fheaderp->next;
3076     }
3077 dtc 1.14
3078 rtoy 1.66 return nwords;
3079 dtc 1.1 }
3080    
3081 rtoy 1.66 static lispobj
3082     trans_code_header(lispobj object)
3083 dtc 1.1 {
3084 rtoy 1.66 struct code *ncode;
3085 dtc 1.1
3086 rtoy 1.66 ncode = trans_code((struct code *) PTR(object));
3087     return (lispobj) ncode | type_OtherPointer;
3088 dtc 1.1 }
3089    
3090 rtoy 1.66 static int
3091     size_code_header(lispobj * where)
3092 dtc 1.1 {
3093 rtoy 1.66 struct code *code;
3094     int nheader_words, ncode_words, nwords;
3095 dtc 1.1
3096 rtoy 1.66 code = (struct code *) where;
3097 dtc 1.14
3098 rtoy 1.66 ncode_words = fixnum_value(code->code_size);
3099     nheader_words = HeaderValue(code->header);
3100     nwords = ncode_words + nheader_words;
3101     nwords = CEILING(nwords, 2);
3102 dtc 1.1
3103 rtoy 1.66 return nwords;
3104 dtc 1.1 }
3105    
3106 cwang 1.55 #if !(defined(i386) || defined(__x86_64))
3107 dtc 1.1
3108 rtoy 1.66 static int
3109     scav_return_pc_header(lispobj * where, lispobj object)
3110 dtc 1.1 {
3111     fprintf(stderr, "GC lossage. Should not be scavenging a ");
3112     fprintf(stderr, "Return PC Header.\n");
3113 gerd 1.32 fprintf(stderr, "where = 0x%08lx, object = 0x%08lx",
3114 dtc 1.1 (unsigned long) where, (unsigned long) object);
3115     lose(NULL);
3116     return 0;
3117     }
3118    
3119 gerd 1.38 #endif /* not i386 */
3120    
3121 rtoy 1.66 static lispobj
3122     trans_return_pc_header(lispobj object)
3123 dtc 1.1 {
3124 rtoy 1.66 struct function *return_pc;
3125     unsigned long offset;
3126     struct code *code, *ncode;
3127 dtc 1.1
3128 rtoy 1.66 return_pc = (struct function *) PTR(object);
3129     offset = HeaderValue(return_pc->header) * sizeof(lispobj);
3130 dtc 1.1
3131 rtoy 1.66 /* Transport the whole code object */
3132     code = (struct code *) ((unsigned long) return_pc - offset);
3133 dtc 1.14
3134 rtoy 1.66 ncode = trans_code(code);
3135    
3136     return ((lispobj) ncode + offset) | type_OtherPointer;
3137 dtc 1.1 }
3138    
3139 dtc 1.14 /*
3140     * On the 386, closures hold a pointer to the raw address instead of
3141     * the function object.
3142     */
3143 cwang 1.55 #if defined(i386) || defined(__x86_64)
3144 gerd 1.38
3145 rtoy 1.66 static int
3146     scav_closure_header(lispobj * where, lispobj object)
3147 dtc 1.1 {
3148 rtoy 1.66 struct closure *closure;
3149     lispobj fun;
3150 dtc 1.1
3151 rtoy 1.66 closure = (struct closure *) where;
3152     fun = closure->function - RAW_ADDR_OFFSET;
3153     scavenge(&fun, 1);
3154     /* The function may have moved so update the raw address. But don't
3155     write unnecessarily. */
3156     if (closure->function != fun + RAW_ADDR_OFFSET)
3157     closure->function = fun + RAW_ADDR_OFFSET;
3158 dtc 1.14
3159 rtoy 1.66 return 2;
3160 dtc 1.1 }
3161 gerd 1.38
3162     #endif /* i386 */
3163    
3164 cwang 1.55 #if !(defined(i386) || defined(__x86_64))
3165 dtc 1.1
3166 rtoy 1.66 static int
3167     scav_function_header(lispobj * where, lispobj object)
3168 dtc 1.1 {
3169     fprintf(stderr, "GC lossage. Should not be scavenging a ");
3170     fprintf(stderr, "Function Header.\n");
3171 gerd 1.32 fprintf(stderr, "where = 0x%08lx, object = 0x%08lx",
3172 dtc 1.1 (unsigned long) where, (unsigned long) object);
3173     lose(NULL);
3174     return 0;
3175     }
3176    
3177 gerd 1.38 #endif /* not i386 */
3178    
3179 rtoy 1.66 static lispobj
3180     trans_function_header(lispobj object)
3181 dtc 1.1 {
3182 rtoy 1.66 struct function *fheader;
3183     unsigned long offset;
3184     struct code *code, *ncode;
3185 dtc 1.14
3186 rtoy 1.66 fheader = (struct function *) PTR(object);
3187     offset = HeaderValue(fheader->header) * sizeof(lispobj);
3188 dtc 1.14
3189 rtoy 1.66 /* Transport the whole code object */
3190     code = (struct code *) ((unsigned long) fheader - offset);
3191     ncode = trans_code(code);
3192 dtc 1.14
3193 rtoy 1.66 return ((lispobj) ncode + offset) | type_FunctionPointer;
3194 dtc 1.1 }
3195 rtoy 1.66
3196 dtc 1.1
3197     /* Instances */
3198    
3199     #if DIRECT_SCAV
3200 rtoy 1.66 static int
3201     scav_instance_pointer(lispobj * where, lispobj object)
3202 dtc 1.1 {
3203 rtoy 1.66 if (from_space_p(object)) {
3204     lispobj first, *first_pointer;
3205 dtc 1.14
3206 rtoy 1.66 /*
3207     * object is a pointer into from space. check to see if it has
3208     * been forwarded
3209     */
3210     first_pointer = (lispobj *) PTR(object);
3211     first = *first_pointer;
3212 dtc 1.14
3213 rtoy 1.66 if (first == 0x01)
3214     /* Forwarded. */
3215     first = first_pointer[1];
3216     else {
3217     first = trans_boxed(object);
3218     gc_assert(first != object);
3219     /* Set forwarding pointer */
3220     first_pointer[0] = 0x01;
3221     first_pointer[1] = first;
3222     }
3223     *where = first;
3224 dtc 1.1 }
3225 rtoy 1.66 return 1;
3226 dtc 1.1 }
3227     #else
3228 rtoy 1.66 static int
3229     scav_instance_pointer(lispobj * where, lispobj object)
3230 dtc 1.1 {
3231 rtoy 1.66 lispobj copy, *first_pointer;
3232 dtc 1.14
3233 rtoy 1.66 /* Object is a pointer into from space - not a FP */
3234     copy = trans_boxed(object);
3235 dtc 1.1
3236 rtoy 1.66 gc_assert(copy != object);
3237 dtc 1.1
3238 rtoy 1.66 first_pointer = (lispobj *) PTR(object);
3239 dtc 1.14
3240 rtoy 1.66 /* Set forwarding pointer. */
3241     first_pointer[0] = 0x01;
3242     first_pointer[1] = copy;
3243     *where = copy;
3244 dtc 1.1
3245 rtoy 1.66 return 1;
3246 dtc 1.1 }
3247     #endif
3248 rtoy 1.66
3249 dtc 1.1
3250     /* Lists and Conses */
3251    
3252     static lispobj trans_list(lispobj object);
3253    
3254     #if DIRECT_SCAV
3255 rtoy 1.66 static int
3256     scav_list_pointer(lispobj * where, lispobj object)
3257 dtc 1.1 {
3258 rtoy 1.66 gc_assert(Pointerp(object));
3259 dtc 1.1
3260 rtoy 1.66 if (from_space_p(object)) {
3261     lispobj first, *first_pointer;
3262 dtc 1.14
3263 rtoy 1.66 /*
3264     * Object is a pointer into from space - check to see if it has
3265     * been forwarded.
3266     */
3267     first_pointer = (lispobj *) PTR(object);
3268     first = *first_pointer;
3269 dtc 1.14
3270 rtoy 1.66 if (first == 0x01)
3271     /* Forwarded. */
3272     first = first_pointer[1];
3273     else {
3274     first = trans_list(object);
3275    
3276     /* Set forwarding pointer */
3277     first_pointer[0] = 0x01;
3278     first_pointer[1] = first;
3279     }
3280 dtc 1.14
3281 rtoy 1.66 gc_assert(Pointerp(first));
3282     gc_assert(!from_space_p(first));
3283     *where = first;
3284 dtc 1.1 }
3285 rtoy 1.66 return 1;
3286 dtc 1.1 }
3287     #else
3288 rtoy 1.66 static int
3289     scav_list_pointer(lispobj * where, lispobj object)
3290 dtc 1.1 {
3291 rtoy 1.66 lispobj first, *first_pointer;
3292 dtc 1.1
3293 rtoy 1.66 gc_assert(Pointerp(object));
3294 dtc 1.1
3295 rtoy 1.66 /* Object is a pointer into from space - not FP */
3296 dtc 1.14
3297 rtoy 1.66 first = trans_list(object);
3298     gc_assert(first != object);
3299 dtc 1.1
3300 rtoy 1.66 first_pointer = (lispobj *) PTR(object);
3301 dtc 1.1
3302 rtoy 1.66 /* Set forwarding pointer */
3303     first_pointer[0] = 0x01;
3304     first_pointer[1] = first;
3305 dtc 1.1
3306 rtoy 1.66 gc_assert(Pointerp(first));
3307     gc_assert(!from_space_p(first));
3308     *where = first;
3309     return 1;
3310 dtc 1.1 }
3311     #endif
3312    
3313 rtoy 1.66 static lispobj
3314     trans_list(lispobj object)
3315 dtc 1.1 {
3316 rtoy 1.66 lispobj new_list_pointer;
3317     struct cons *cons, *new_cons;
3318     lispobj cdr;
3319 dtc 1.1
3320 rtoy 1.66 gc_assert(from_space_p(object));
3321 dtc 1.1
3322 rtoy 1.66 cons = (struct cons *) PTR(object);
3323 dtc 1.14
3324 rtoy 1.66 /* copy 'object' */
3325     new_cons = (struct cons *) gc_quick_alloc(sizeof(struct cons));
3326 dtc 1.1
3327 rtoy 1.66 new_cons->car = cons->car;
3328     new_cons->cdr = cons->cdr; /* updated later */
3329     new_list_pointer = (lispobj) new_cons | LowtagOf(object);
3330    
3331     /* Grab the cdr before it is clobbered */
3332     cdr = cons->cdr;
3333 dtc 1.1
3334 rtoy 1.66 /* Set forwarding pointer (clobbers start of list). */
3335     cons->car = 0x01;
3336     cons->cdr = new_list_pointer;
3337    
3338     /* Try to linearize the list in the cdr direction to help reduce paging. */
3339     while (1) {
3340     lispobj new_cdr;
3341     struct cons *cdr_cons, *new_cdr_cons;
3342 dtc 1.1
3343 rtoy 1.66 if (LowtagOf(cdr) != type_ListPointer || !from_space_p(cdr)
3344     || *((lispobj *) PTR(cdr)) == 0x01)
3345     break;
3346 dtc 1.14
3347 rtoy 1.66 cdr_cons = (struct cons *) PTR(cdr);
3348 dtc 1.14
3349 rtoy 1.66 /* copy 'cdr' */
3350     new_cdr_cons = (struct cons *) gc_quick_alloc(sizeof(struct cons));
3351 dtc 1.14
3352 rtoy 1.66 new_cdr_cons->car = cdr_cons->car;
3353     new_cdr_cons->cdr = cdr_cons->cdr;
3354     new_cdr = (lispobj) new_cdr_cons | LowtagOf(cdr);
3355 dtc 1.14
3356 rtoy 1.66 /* Grab the cdr before it is clobbered */
3357     cdr = cdr_cons->cdr;
3358 dtc 1.14
3359 rtoy 1.66 /* Set forwarding pointer */
3360     cdr_cons->car = 0x01;
3361     cdr_cons->cdr = new_cdr;
3362 dtc 1.14
3363 rtoy 1.66 /*
3364     * Update the cdr of the last cons copied into new space to keep
3365     * the newspace scavenge from having to do it.
3366     */
3367     new_cons->cdr = new_cdr;
3368 dtc 1.14
3369 rtoy 1.66 new_cons = new_cdr_cons;
3370     }
3371 dtc 1.14
3372 rtoy 1.66 return new_list_pointer;
3373 dtc 1.1 }
3374 rtoy 1.66
3375 dtc 1.1
3376     /* Scavenging and Transporting Other Pointers */
3377    
3378     #if DIRECT_SCAV
3379 rtoy 1.66 static int
3380     scav_other_pointer(lispobj * where, lispobj object)
3381 dtc 1.1 {
3382 rtoy 1.66 gc_assert(Pointerp(object));
3383 dtc 1.1
3384 rtoy 1.66 if (from_space_p(object)) {
3385     lispobj first, *first_pointer;
3386    
3387     /*
3388     * Object is a pointer into from space. check to see if it has
3389     * been forwarded.
3390     */
3391     first_pointer = (lispobj *) PTR(object);
3392     first = *first_pointer;
3393    
3394     if (first == 0x01) {
3395     /* Forwarded. */
3396     first = first_pointer[1];
3397     *where = first;
3398     } else {
3399     first = (transother[TypeOf(first)]) (object);
3400    
3401     if (first != object) {
3402     /* Set forwarding pointer */
3403     first_pointer[0] = 0x01;
3404     first_pointer[1] = first;
3405     *where = first;
3406     }
3407     }
3408    
3409     gc_assert(Pointerp(first));
3410     gc_assert(!from_space_p(first));
3411     }
3412     return 1;
3413     }
3414     #else
3415     static int
3416     scav_other_pointer(lispobj * where, lispobj object)
3417     {
3418 dtc 1.1 lispobj first, *first_pointer;
3419 dtc 1.14
3420 rtoy 1.66 gc_assert(Pointerp(object));
3421    
3422     /* Object is a pointer into from space - not FP */
3423 dtc 1.1 first_pointer = (lispobj *) PTR(object);
3424 dtc 1.14
3425 rtoy 1.66 first = (transother[TypeOf(*first_pointer)]) (object);
3426 dtc 1.14
3427 rtoy 1.66 if (first != object) {
3428 dtc 1.1 /* Set forwarding pointer */
3429     first_pointer[0] = 0x01;
3430     first_pointer[1] = first;
3431     *where = first;
3432     }
3433 dtc 1.14
3434 dtc 1.1 gc_assert(Pointerp(first));
3435     gc_assert(!from_space_p(first));
3436    
3437 rtoy 1.66 return 1;
3438 dtc 1.1 }
3439     #endif
3440 rtoy 1.66
3441 dtc 1.1
3442     /* Immediate, Boxed, and Unboxed Objects */
3443    
3444 rtoy 1.66 static int
3445     size_pointer(lispobj * where)
3446 dtc 1.1 {
3447     return 1;
3448     }
3449    
3450 rtoy 1.66 static int
3451     scav_immediate(lispobj * where, lispobj object)
3452 dtc 1.1 {
3453     return 1;
3454     }
3455    
3456 rtoy 1.66 static lispobj
3457     trans_immediate(lispobj object)
3458 dtc 1.1 {
3459     fprintf(stderr, "GC lossage. Trying to transport an immediate!?\n");
3460     lose(NULL);
3461     return NIL;
3462     }
3463    
3464 rtoy 1.66 static int
3465     size_immediate(lispobj * where)
3466 dtc 1.1 {
3467     return 1;
3468     }
3469    
3470    
3471 rtoy 1.66 static int
3472     scav_boxed(lispobj * where, lispobj object)
3473 dtc 1.1 {
3474     return 1;
3475     }
3476    
3477 rtoy 1.66 static lispobj
3478     trans_boxed(lispobj object)
3479 dtc 1.1 {
3480 rtoy 1.66 lispobj header;
3481     unsigned long length;
3482 dtc 1.1
3483 rtoy 1.66 gc_assert(Pointerp(object));
3484 dtc 1.1
3485 rtoy 1.66 header = *((lispobj *) PTR(object));
3486     length = HeaderValue(header) + 1;
3487     length = CEILING(length, 2);
3488 dtc 1.1
3489 rtoy 1.66 return copy_object(object, length);
3490 dtc 1.1 }
3491    
3492 rtoy 1.66 static lispobj
3493     trans_boxed_large(lispobj object)
3494 dtc 1.1 {
3495 rtoy 1.66 lispobj header;
3496     unsigned long length;
3497 dtc 1.1
3498 rtoy 1.66 gc_assert(Pointerp(object));
3499 dtc 1.1
3500 rtoy 1.66 header = *((lispobj *) PTR(object));
3501     length = HeaderValue(header) + 1;
3502     length = CEILING(length, 2);
3503 dtc 1.1
3504 rtoy 1.66 return copy_large_object(object, length);
3505 dtc 1.1 }
3506    
3507 rtoy 1.66 static int
3508     size_boxed(lispobj * where)
3509 dtc 1.1 {
3510 rtoy 1.66 lispobj header;
3511     unsigned long length;
3512 dtc 1.1
3513 rtoy 1.66 header = *where;
3514     length = HeaderValue(header) + 1;
3515     length = CEILING(length, 2);
3516 dtc 1.1
3517 rtoy 1.66 return length;
3518 dtc 1.1 }
3519    
3520 rtoy 1.67 /* Not needed on sparc and ppc because the raw_addr has a function lowtag */
3521 cshapiro 1.87 #if !(defined(sparc) || (defined(DARWIN) && defined(__ppc__)))
3522 rtoy 1.66 static int
3523     scav_fdefn(lispobj * where, lispobj object)
3524 dtc 1.1 {
3525 rtoy 1.66 struct fdefn *fdefn;
3526 dtc 1.14
3527 rtoy 1.66 fdefn = (struct fdefn *) where;
3528 dtc 1.14
3529 rtoy 1.66 if ((char *) (fdefn->function + RAW_ADDR_OFFSET) == fdefn->raw_addr) {
3530     scavenge(where + 1, sizeof(struct fdefn) / sizeof(lispobj) - 1);
3531 dtc 1.14
3532 rtoy 1.66 /* Don't write unnecessarily */
3533     if (fdefn->raw_addr != (char *) (fdefn->function + RAW_ADDR_OFFSET))
3534     fdefn->raw_addr = (char *) (fdefn->function + RAW_ADDR_OFFSET);
3535 dtc 1.14
3536 rtoy 1.66 return sizeof(struct fdefn) / sizeof(lispobj);
3537     } else
3538     return 1;
3539 dtc 1.1 }
3540 rtoy 1.57 #endif
3541 dtc 1.1
3542 rtoy 1.66 static int
3543     scav_unboxed(lispobj * where, lispobj object)
3544 dtc 1.1 {
3545 rtoy 1.66 unsigned long length;
3546 dtc 1.1
3547 rtoy 1.66 length = HeaderValue(object) + 1;
3548     length = CEILING(length, 2);
3549 dtc 1.1
3550 rtoy 1.66 return length;
3551 dtc 1.1 }
3552    
3553 rtoy 1.66 static lispobj
3554     trans_unboxed(lispobj object)
3555 dtc 1.1 {
3556 rtoy 1.66 lispobj header;
3557     unsigned long length;
3558 dtc 1.1
3559    
3560 rtoy 1.66 gc_assert(Pointerp(object));
3561 dtc 1.1
3562 rtoy 1.66 header = *((lispobj *) PTR(object));
3563     length = HeaderValue(header) + 1;
3564     length = CEILING(length, 2);
3565 dtc 1.1
3566 rtoy 1.66 return copy_unboxed_object(object, length);
3567 dtc 1.1 }
3568    
3569 rtoy 1.66 static lispobj
3570     trans_unboxed_large(lispobj object)
3571 dtc 1.1 {
3572 rtoy 1.66 lispobj header;
3573     unsigned long length;
3574 dtc 1.1
3575    
3576 rtoy 1.66 gc_assert(Pointerp(object));
3577 dtc 1.1
3578 rtoy 1.66 header = *((lispobj *) PTR(object));
3579     length = HeaderValue(header) + 1;
3580     length = CEILING(length, 2);
3581 dtc 1.1
3582 rtoy 1.66 return copy_large_unboxed_object(object, length);
3583 dtc 1.1 }
3584    
3585 rtoy 1.66 static int
3586     size_unboxed(lispobj * where)
3587 dtc 1.1 {
3588 rtoy 1.66 lispobj header;
3589     unsigned long length;
3590 dtc 1.1
3591 rtoy 1.66 header = *where;
3592     length = HeaderValue(header) + 1;
3593     length = CEILING(length, 2);
3594 dtc 1.1
3595 rtoy 1.66 return length;
3596 dtc 1.1 }
3597 rtoy 1.66
3598 dtc 1.1
3599     /* Vector-Like Objects */
3600    
3601     #define NWORDS(x,y) (CEILING((x),(y)) / (y))
3602    
3603 rtoy 1.66 static int
3604     size_string(lispobj * where)
3605 dtc 1.1 {
3606 rtoy 1.66 struct vector *vector;
3607     int length, nwords;
3608 dtc 1.1
3609 rtoy 1.66 /*
3610     * NOTE: Strings contain one more byte of data than the length
3611     * slot indicates.
3612     */
3613 dtc 1.1
3614 rtoy 1.66 vector = (struct vector *) where;
3615     length = fixnum_value(vector->length) + 1;
3616 rtoy 1.95.2.1 #ifndef UNICODE
3617 cwang 1.55 #ifdef __x86_64
3618 rtoy 1.66 nwords = CEILING(NWORDS(length, 8) + 2, 2);
3619 cwang 1.55 #else
3620 rtoy 1.66 nwords = CEILING(NWORDS(length, 4) + 2, 2);
3621 cwang 1.55 #endif
3622 rtoy 1.95.2.1 #else
3623     /*
3624     * Strings are just like arrays with 16-bit elements, and contain
3625     * one more element than the slot length indicates.
3626     */
3627     nwords = CEILING(NWORDS(length, 2) + 2, 2);
3628     #endif
3629 rtoy 1.66 return nwords;
3630 dtc 1.1 }
3631    
3632 rtoy 1.66 static int
3633     scav_string(lispobj * where, lispobj object)
3634 dtc 1.1 {
3635 rtoy 1.66 return size_string(where);
3636 dtc 1.1 }
3637    
3638 rtoy 1.66 static lispobj
3639     trans_string(lispobj object)
3640 dtc 1.1 {
3641 rtoy 1.66 gc_assert(Pointerp(object));
3642     return copy_large_unboxed_object(object,
3643     size_string((lispobj *) PTR(object)));
3644 dtc 1.1 }
3645 rtoy 1.66
3646 dtc 1.1
3647 gerd 1.32 /************************************************************************
3648     Hash Tables
3649     ************************************************************************/
3650    
3651     /* This struct corresponds to the Lisp HASH-TABLE structure defined in
3652     hash-new.lisp. */
3653    
3654 rtoy 1.66 struct hash_table {
3655     lispobj instance_header; /* 0 */
3656     lispobj dummy2;
3657     lispobj test;
3658     lispobj test_fun;
3659     lispobj hash_fun;
3660     lispobj rehash_size; /* 5 */
3661     lispobj rehash_threshold;
3662     lispobj rehash_trigger;
3663