/[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.2 - (hide annotations)
Sat Nov 1 22:40:36 2008 UTC (5 years, 5 months ago) by rtoy
Branch: unicode-utf16-branch
CVS Tags: unicode-utf16-sync-2008-12, unicode-utf16-sync-label-2009-03-16, unicode-utf16-char-support-2009-03-26, unicode-utf16-char-support-2009-03-25, unicode-utf16-sync-2008-11
Changes since 1.95.2.1: +35 -7 lines
File MIME type: text/plain
Sync to snapshot 2008-11.
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.2 * $Header: /tiger/var/lib/cvsroots/cmucl/src/lisp/gencgc.c,v 1.95.2.2 2008/11/01 22:40:36 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.2 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.2 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.2 /*
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     if (boxed_region.end_addr - boxed_region.free_pointer > 32)
1631     return gc_alloc_large(nbytes, 0, &boxed_region);
1632 dtc 1.1
1633 rtoy 1.66 /* Else find a new region. */
1634    
1635     /* Finished with the current region. */
1636     gc_alloc_update_page_tables(0, &boxed_region);
1637 dtc 1.1
1638 rtoy 1.66 /* Setup a new region. */
1639     gc_alloc_new_region(nbytes, 0, &boxed_region);
1640 dtc 1.1
1641 rtoy 1.66 /* Should now be enough room. */
1642 dtc 1.14
1643 rtoy 1.66 /* Check if there is room in the current region. */
1644     new_free_pointer = boxed_region.free_pointer + nbytes;
1645 dtc 1.14
1646 rtoy 1.66 if (new_free_pointer <= boxed_region.end_addr) {
1647     /* If so then allocate from the current region. */
1648     void *new_obj = boxed_region.free_pointer;
1649 dtc 1.14
1650 rtoy 1.66 boxed_region.free_pointer = new_free_pointer;
1651 dtc 1.14
1652 rtoy 1.66 /* Check if the current region is almost empty. */
1653     if (boxed_region.end_addr - boxed_region.free_pointer <= 32) {
1654     /* If so find, finished with the current region. */
1655     gc_alloc_update_page_tables(0, &boxed_region);
1656 dtc 1.1
1657 rtoy 1.66 /* Setup a new region. */
1658     gc_alloc_new_region(32, 0, &boxed_region);
1659     }
1660 dtc 1.14
1661 rtoy 1.66 return (void *) new_obj;
1662 dtc 1.1 }
1663 dtc 1.14
1664 rtoy 1.66 /* Shouldn't happen? */
1665     gc_assert(0);
1666     return 0;
1667 dtc 1.1 }
1668    
1669 dtc 1.14 /*
1670     * Allocate space from the boxed_region. If there is not enough free
1671     * space then call gc_alloc to do the job. A pointer to the start of
1672     * the region is returned.
1673     */
1674 rtoy 1.66 static inline void *
1675     gc_quick_alloc(int nbytes)
1676 dtc 1.1 {
1677 rtoy 1.66 char *new_free_pointer;
1678    
1679     /* Check if there is room in the current region. */
1680     new_free_pointer = boxed_region.free_pointer + nbytes;
1681 dtc 1.1
1682 rtoy 1.66 if (new_free_pointer <= boxed_region.end_addr) {
1683     /* If so then allocate from the current region. */
1684     void *new_obj = boxed_region.free_pointer;
1685 dtc 1.14
1686 rtoy 1.66 boxed_region.free_pointer = new_free_pointer;
1687     return (void *) new_obj;
1688     }
1689 dtc 1.14
1690 rtoy 1.66 /* Else call gc_alloc */
1691     return gc_alloc(nbytes);
1692 dtc 1.1 }
1693    
1694 dtc 1.14 /*
1695     * Allocate space for the boxed object. If it is a large object then
1696     * do a large alloc else allocate from the current region. If there is
1697     * not enough free space then call gc_alloc to do the job. A pointer
1698     * to the start of the region is returned.
1699     */
1700 rtoy 1.66 static inline void *
1701     gc_quick_alloc_large(int nbytes)
1702 dtc 1.1 {
1703 rtoy 1.66 char *new_free_pointer;
1704    
1705     if (nbytes >= large_object_size)
1706     return gc_alloc_large(nbytes, 0, &boxed_region);
1707 dtc 1.1
1708 rtoy 1.66 /* Check if there is room in the current region. */
1709     new_free_pointer = boxed_region.free_pointer + nbytes;
1710 dtc 1.1
1711 rtoy 1.66 if (new_free_pointer <= boxed_region.end_addr) {
1712     /* If so then allocate from the current region. */
1713     void *new_obj = boxed_region.free_pointer;
1714 dtc 1.14
1715 rtoy 1.66 boxed_region.free_pointer = new_free_pointer;
1716     return (void *) new_obj;
1717     }
1718 dtc 1.14
1719 rtoy 1.66 /* Else call gc_alloc */
1720     return gc_alloc(nbytes);
1721 dtc 1.1 }
1722    
1723    
1724    
1725    
1726 rtoy 1.66 static void *
1727     gc_alloc_unboxed(int nbytes)
1728 dtc 1.1 {
1729 rtoy 1.66 char *new_free_pointer;
1730 dtc 1.1
1731 dtc 1.14 #if 0
1732 rtoy 1.66 fprintf(stderr, "gc_alloc_unboxed %d\n", nbytes);
1733 dtc 1.14 #endif
1734 dtc 1.1
1735 rtoy 1.66 /* Check if there is room in the current region. */
1736     new_free_pointer = unboxed_region.free_pointer + nbytes;
1737    
1738     if (new_free_pointer <= unboxed_region.end_addr) {
1739     /* If so then allocate from the current region. */
1740     void *new_obj = unboxed_region.free_pointer;
1741 dtc 1.14
1742 rtoy 1.66 unboxed_region.free_pointer = new_free_pointer;
1743 dtc 1.14
1744 rtoy 1.66 /* Check if the current region is almost empty. */
1745     if ((unboxed_region.end_addr - unboxed_region.free_pointer) <= 32) {
1746     /* If so finished with the current region. */
1747     gc_alloc_update_page_tables(1, &unboxed_region);
1748 dtc 1.14
1749 rtoy 1.66 /* Setup a new region. */
1750     gc_alloc_new_region(32, 1, &unboxed_region);
1751     }
1752    
1753     return (void *) new_obj;
1754 dtc 1.1 }
1755 dtc 1.14
1756 rtoy 1.66 /* Else not enough free space in the current region. */
1757    
1758     /*
1759     * If there is a bit of room left in the current region then
1760     * allocate a large object.
1761     */
1762     if (unboxed_region.end_addr - unboxed_region.free_pointer > 32)
1763     return gc_alloc_large(nbytes, 1, &unboxed_region);
1764 dtc 1.14
1765 rtoy 1.66 /* Else find a new region. */
1766 dtc 1.14
1767 rtoy 1.66 /* Finished with the current region. */
1768     gc_alloc_update_page_tables(1, &unboxed_region);
1769 dtc 1.1
1770 rtoy 1.66 /* Setup a new region. */
1771     gc_alloc_new_region(nbytes, 1, &unboxed_region);
1772 dtc 1.1
1773 rtoy 1.66 /* Should now be enough room. */
1774 dtc 1.14
1775 rtoy 1.66 /* Check if there is room in the current region. */
1776     new_free_pointer = unboxed_region.free_pointer + nbytes;
1777 dtc 1.14
1778 rtoy 1.66 if (new_free_pointer <= unboxed_region.end_addr) {
1779     /* If so then allocate from the current region. */
1780     void *new_obj = unboxed_region.free_pointer;
1781 dtc 1.14
1782 rtoy 1.66 unboxed_region.free_pointer = new_free_pointer;
1783 dtc 1.14
1784 rtoy 1.66 /* Check if the current region is almost empty. */
1785     if ((unboxed_region.end_addr - unboxed_region.free_pointer) <= 32) {
1786     /* If so find, finished with the current region. */
1787     gc_alloc_update_page_tables(1, &unboxed_region);
1788 dtc 1.14
1789 rtoy 1.66 /* Setup a new region. */
1790     gc_alloc_new_region(32, 1, &unboxed_region);
1791     }
1792 dtc 1.14
1793 rtoy 1.66 return (void *) new_obj;
1794 dtc 1.1 }
1795 dtc 1.14
1796 rtoy 1.66 /* Shouldn't happen? */
1797     gc_assert(0);
1798     return 0;
1799 dtc 1.1 }
1800    
1801 rtoy 1.66 static inline void *
1802     gc_quick_alloc_unboxed(int nbytes)
1803 dtc 1.1 {
1804 rtoy 1.66 char *new_free_pointer;
1805    
1806     /* Check if there is room in the current region. */
1807     new_free_pointer = unboxed_region.free_pointer + nbytes;
1808 dtc 1.1
1809 rtoy 1.66 if (new_free_pointer <= unboxed_region.end_addr) {
1810     /* If so then allocate from the current region. */
1811     void *new_obj = unboxed_region.free_pointer;
1812 dtc 1.14
1813 rtoy 1.66 unboxed_region.free_pointer = new_free_pointer;
1814 dtc 1.14
1815 rtoy 1.66 return (void *) new_obj;
1816 dtc 1.1 }
1817 dtc 1.14
1818 rtoy 1.66 /* Else call gc_alloc */
1819     return gc_alloc_unboxed(nbytes);
1820 dtc 1.1 }
1821    
1822 dtc 1.14 /*
1823     * Allocate space for the object. If it is a large object then do a
1824     * large alloc else allocate from the current region. If there is not
1825     * enough free space then call gc_alloc to do the job.
1826     *
1827     * A pointer to the start of the region is returned.
1828     */
1829 rtoy 1.66 static inline void *
1830     gc_quick_alloc_large_unboxed(int nbytes)
1831 dtc 1.1 {
1832 rtoy 1.66 char *new_free_pointer;
1833 dtc 1.1
1834 rtoy 1.66 if (nbytes >= large_object_size)
1835     return gc_alloc_large(nbytes, 1, &unboxed_region);
1836 dtc 1.1
1837 rtoy 1.66 /* Check if there is room in the current region. */
1838     new_free_pointer = unboxed_region.free_pointer + nbytes;
1839 dtc 1.14
1840 rtoy 1.66 if (new_free_pointer <= unboxed_region.end_addr) {
1841     /* If so then allocate from the current region. */
1842     void *new_obj = unboxed_region.free_pointer;
1843 dtc 1.14
1844 rtoy 1.66 unboxed_region.free_pointer = new_free_pointer;
1845    
1846     return (void *) new_obj;
1847     }
1848 dtc 1.14
1849 rtoy 1.66 /* Else call gc_alloc */
1850     return gc_alloc_unboxed(nbytes);
1851 dtc 1.1 }
1852    
1853     /***************************************************************************/
1854 rtoy 1.66
1855 dtc 1.1
1856     /* Scavenging/transporting routines derived from gc.c */
1857    
1858 rtoy 1.66 static int (*scavtab[256]) (lispobj * where, lispobj object);
1859     static lispobj(*transother[256]) (lispobj object);
1860     static int (*sizetab[256]) (lispobj * where);
1861 dtc 1.1
1862     static struct weak_pointer *weak_pointers;
1863 dtc 1.14 static struct scavenger_hook *scavenger_hooks = (struct scavenger_hook *) NIL;
1864 dtc 1.1
1865 rtoy 1.95.2.1 /* Like (ceiling x y), but y is constrained to be a power of two */
1866 dtc 1.1 #define CEILING(x,y) (((x) + ((y) - 1)) & (~((y) - 1)))
1867 rtoy 1.66
1868 dtc 1.1
1869     /* Predicates */
1870    
1871 rtoy 1.66 static inline boolean
1872     from_space_p(lispobj obj)
1873 dtc 1.1 {
1874 rtoy 1.66 int page_index = (char *) obj - heap_base;
1875    
1876     return page_index >= 0
1877     && (page_index =
1878     (unsigned int) page_index / PAGE_SIZE) < dynamic_space_pages
1879     && PAGE_GENERATION(page_index) == from_space;
1880 dtc 1.14 }
1881    
1882 rtoy 1.66 static inline boolean
1883     new_space_p(lispobj obj)
1884 dtc 1.14 {
1885 rtoy 1.66 int page_index = (char *) obj - heap_base;
1886    
1887     return page_index >= 0
1888     && (page_index =
1889     (unsigned int) page_index / PAGE_SIZE) < dynamic_space_pages
1890     && PAGE_GENERATION(page_index) == new_space;
1891 dtc 1.1 }
1892 rtoy 1.66
1893 dtc 1.1
1894     /* Copying Objects */
1895    
1896    
1897     /* Copying Boxed Objects */
1898 rtoy 1.66 static inline lispobj
1899     copy_object(lispobj object, int nwords)
1900 dtc 1.1 {
1901 rtoy 1.66 int tag;
1902     lispobj *new;
1903     lispobj *source, *dest;
1904    
1905     gc_assert(Pointerp(object));
1906     gc_assert(from_space_p(object));
1907     gc_assert((nwords & 0x01) == 0);
1908 dtc 1.14
1909 rtoy 1.66 /* get tag of object */
1910     tag = LowtagOf(object);
1911    
1912     /* allocate space */
1913     new = gc_quick_alloc(nwords * sizeof(lispobj));
1914    
1915     dest = new;
1916     source = (lispobj *) PTR(object);
1917    
1918     /* copy the object */
1919     while (nwords > 0) {
1920     dest[0] = source[0];
1921     dest[1] = source[1];
1922     dest += 2;
1923     source += 2;
1924     nwords -= 2;
1925     }
1926    
1927     /* return lisp pointer of new object */
1928     return (lispobj) new | tag;
1929 dtc 1.1 }
1930    
1931 dtc 1.14 /*
1932     * Copying Large Boxed Objects. If the object is in a large object
1933     * region then it is simply promoted, else it is copied. If it's large
1934     * enough then it's copied to a large object region.
1935     *
1936     * Vectors may have shrunk. If the object is not copied the space
1937     * needs to be reclaimed, and the page_tables corrected.
1938     */
1939 rtoy 1.66 static lispobj
1940     copy_large_object(lispobj object, int nwords)
1941 dtc 1.1 {
1942 rtoy 1.66 int tag;
1943     lispobj *new;
1944     lispobj *source, *dest;
1945     int first_page;
1946    
1947     gc_assert(Pointerp(object));
1948     gc_assert(from_space_p(object));
1949     gc_assert((nwords & 0x01) == 0);
1950    
1951     if (gencgc_verbose && nwords > 1024 * 1024)
1952 agoncharov 1.90 fprintf(stderr, "** copy_large_object: %lu\n",
1953     (unsigned long) (nwords * sizeof(lispobj)));
1954 rtoy 1.66
1955     /* Check if it's a large object. */
1956     first_page = find_page_index((void *) object);
1957     gc_assert(first_page >= 0);
1958    
1959     if (PAGE_LARGE_OBJECT(first_page)) {
1960     /* Promote the object. */
1961     int remaining_bytes;
1962     int next_page;
1963     int bytes_freed;
1964     int old_bytes_used;
1965     int mmask, mflags;
1966    
1967     /*
1968     * Note: Any page write protection must be removed, else a later
1969     * scavenge_newspace may incorrectly not scavenge these pages.
1970     * This would not be necessary if they are added to the new areas,
1971     * but lets do it for them all (they'll probably be written
1972     * anyway?).
1973     */
1974    
1975     gc_assert(page_table[first_page].first_object_offset == 0);
1976    
1977     next_page = first_page;
1978     remaining_bytes = nwords * sizeof(lispobj);
1979     while (remaining_bytes > PAGE_SIZE) {
1980     gc_assert(PAGE_GENERATION(next_page) == from_space);
1981     gc_assert(PAGE_ALLOCATED(next_page));
1982     gc_assert(!PAGE_UNBOXED(next_page));
1983     gc_assert(PAGE_LARGE_OBJECT(next_page));
1984     gc_assert(page_table[next_page].first_object_offset ==
1985     PAGE_SIZE * (first_page - next_page));
1986     gc_assert(page_table[next_page].bytes_used == PAGE_SIZE);
1987 dtc 1.1
1988 rtoy 1.66 PAGE_FLAGS_UPDATE(next_page, PAGE_GENERATION_MASK, new_space);
1989    
1990     /*
1991     * Remove any write protection. Should be able to religh on the
1992     * WP flag to avoid redundant calls.
1993     */
1994     if (PAGE_WRITE_PROTECTED(next_page)) {
1995     os_protect((os_vm_address_t) page_address(next_page), PAGE_SIZE,
1996     OS_VM_PROT_ALL);
1997     page_table[next_page].flags &= ~PAGE_WRITE_PROTECTED_MASK;
1998     }
1999     remaining_bytes -= PAGE_SIZE;
2000     next_page++;
2001     }
2002 dtc 1.14
2003 rtoy 1.66 /*
2004     * Now only one page remains, but the object may have shrunk so
2005     * there may be more unused pages which will be freed.
2006     */
2007 dtc 1.1
2008 rtoy 1.66 /* Object may have shrunk but shouldn't have grown - check. */
2009     gc_assert(page_table[next_page].bytes_used >= remaining_bytes);
2010 dtc 1.1
2011 rtoy 1.66 PAGE_FLAGS_UPDATE(next_page, PAGE_GENERATION_MASK, new_space);
2012     gc_assert(PAGE_ALLOCATED(next_page));
2013     gc_assert(!PAGE_UNBOXED(next_page));
2014    
2015     /* Adjust the bytes_used. */
2016     old_bytes_used = page_table[next_page].bytes_used;
2017     page_table[next_page].bytes_used = remaining_bytes;
2018    
2019     bytes_freed = old_bytes_used - remaining_bytes;
2020    
2021     mmask = PAGE_ALLOCATED_MASK | PAGE_UNBOXED_MASK | PAGE_LARGE_OBJECT_MASK
2022     | PAGE_GENERATION_MASK;
2023     mflags = PAGE_ALLOCATED_MASK | PAGE_LARGE_OBJECT_MASK | from_space;
2024    
2025     /* Free any remaining pages; needs care. */
2026     next_page++;
2027     while (old_bytes_used == PAGE_SIZE &&
2028     PAGE_FLAGS(next_page, mmask) == mflags &&
2029     page_table[next_page].first_object_offset ==
2030     PAGE_SIZE * (first_page - next_page)) {
2031     /*
2032     * Checks out OK, free the page. Don't need to both zeroing
2033     * pages as this should have been done before shrinking the
2034     * object. These pages shouldn't be write protected as they
2035     * should be zero filled.
2036     */
2037     gc_assert(!PAGE_WRITE_PROTECTED(next_page));
2038 dtc 1.1
2039 rtoy 1.66 old_bytes_used = page_table[next_page].bytes_used;
2040     page_table[next_page].flags &= ~PAGE_ALLOCATED_MASK;
2041     page_table[next_page].bytes_used = 0;
2042     bytes_freed += old_bytes_used;
2043     next_page++;
2044     }
2045 dtc 1.14
2046 rtoy 1.66 if (gencgc_verbose && bytes_freed > 0)
2047     fprintf(stderr, "* copy_large_boxed bytes_freed %d\n", bytes_freed);
2048 dtc 1.14
2049 rtoy 1.66 generations[from_space].bytes_allocated -=
2050     sizeof(lispobj) * nwords + bytes_freed;
2051     generations[new_space].bytes_allocated += sizeof(lispobj) * nwords;
2052     bytes_allocated -= bytes_freed;
2053 dtc 1.14
2054 rtoy 1.66 /* Add the region to the new_areas if requested. */
2055     add_new_area(first_page, 0, nwords * sizeof(lispobj));
2056 dtc 1.14
2057 rtoy 1.66 return object;
2058     } else {
2059     /* get tag of object */
2060     tag = LowtagOf(object);
2061 dtc 1.14
2062 rtoy 1.66 /* allocate space */
2063     new = gc_quick_alloc_large(nwords * sizeof(lispobj));
2064 dtc 1.15
2065 rtoy 1.66 dest = new;
2066     source = (lispobj *) PTR(object);
2067 dtc 1.14
2068 rtoy 1.66 /* copy the object */
2069     while (nwords > 0) {
2070     dest[0] = source[0];
2071     dest[1] = source[1];
2072     dest += 2;
2073     source += 2;
2074     nwords -= 2;
2075     }
2076 dtc 1.14
2077 rtoy 1.66 /* return lisp pointer of new object */
2078     return (lispobj) new | tag;
2079     }
2080     }
2081 dtc 1.14
2082 rtoy 1.66 /* Copying UnBoxed Objects. */
2083     static inline lispobj
2084     copy_unboxed_object(lispobj object, int nwords)
2085     {
2086     int tag;
2087     lispobj *new;
2088     lispobj *source, *dest;
2089    
2090     gc_assert(Pointerp(object));
2091     gc_assert(from_space_p(object));
2092     gc_assert((nwords & 0x01) == 0);
2093 dtc 1.1
2094     /* get tag of object */
2095     tag = LowtagOf(object);
2096 dtc 1.14
2097 dtc 1.1 /* allocate space */
2098 rtoy 1.66 new = gc_quick_alloc_unboxed(nwords * sizeof(lispobj));
2099 dtc 1.14
2100 dtc 1.1 dest = new;
2101     source = (lispobj *) PTR(object);
2102 dtc 1.14
2103 rtoy 1.66 /* Copy the object */
2104 dtc 1.1 while (nwords > 0) {
2105 rtoy 1.66 dest[0] = source[0];
2106     dest[1] = source[1];
2107     dest += 2;
2108     source += 2;
2109     nwords -= 2;
2110 dtc 1.1 }
2111 dtc 1.14
2112 rtoy 1.66 /* Return lisp pointer of new object. */
2113 dtc 1.14 return (lispobj) new | tag;
2114 dtc 1.1 }
2115    
2116    
2117 dtc 1.14 /*
2118     * Copying Large Unboxed Objects. If the object is in a large object
2119     * region then it is simply promoted, else it is copied. If it's large
2120     * enough then it's copied to a large object region.
2121     *
2122     * Bignums and vectors may have shrunk. If the object is not copied
2123     * the space needs to be reclaimed, and the page_tables corrected.
2124     */
2125 rtoy 1.66 static lispobj
2126     copy_large_unboxed_object(lispobj object, int nwords)
2127 dtc 1.1 {
2128 rtoy 1.66 int tag;
2129     lispobj *new;
2130     lispobj *source, *dest;
2131     int first_page;
2132    
2133     gc_assert(Pointerp(object));
2134     gc_assert(from_space_p(object));
2135     gc_assert((nwords & 0x01) == 0);
2136    
2137     if (gencgc_verbose && nwords > 1024 * 1024)
2138 cshapiro 1.87 fprintf(stderr, "** copy_large_unboxed_object: %lu\n",
2139 agoncharov 1.90 (unsigned long) (nwords * sizeof(lispobj)));
2140 rtoy 1.66
2141     /* Check if it's a large object. */
2142     first_page = find_page_index((void *) object);
2143     gc_assert(first_page >= 0);
2144 dtc 1.14
2145 rtoy 1.66 if (PAGE_LARGE_OBJECT(first_page)) {
2146     /*
2147     * Promote the object. Note: Unboxed objects may have been
2148     * allocated to a BOXED region so it may be necessary to change
2149     * the region to UNBOXED.
2150     */
2151     int remaining_bytes;
2152     int next_page;
2153     int bytes_freed;
2154     int old_bytes_used;
2155     int mmask, mflags;
2156    
2157     gc_assert(page_table[first_page].first_object_offset == 0);
2158    
2159     next_page = first_page;
2160     remaining_bytes = nwords * sizeof(lispobj);
2161     while (remaining_bytes > PAGE_SIZE) {
2162     gc_assert(PAGE_GENERATION(next_page) == from_space);
2163     gc_assert(PAGE_ALLOCATED(next_page));
2164     gc_assert(PAGE_LARGE_OBJECT(next_page));
2165     gc_assert(page_table[next_page].first_object_offset ==
2166     PAGE_SIZE * (first_page - next_page));
2167     gc_assert(page_table[next_page].bytes_used == PAGE_SIZE);
2168    
2169     PAGE_FLAGS_UPDATE(next_page,
2170     PAGE_UNBOXED_MASK | PAGE_GENERATION_MASK,
2171     PAGE_UNBOXED_MASK | new_space);
2172     remaining_bytes -= PAGE_SIZE;
2173     next_page++;
2174     }
2175 dtc 1.1
2176 rtoy 1.66 /*
2177     * Now only one page remains, but the object may have shrunk so
2178     * there may be more unused pages which will be freed.
2179     */
2180 dtc 1.1
2181 rtoy 1.66 /* Object may have shrunk but shouldn't have grown - check. */
2182     gc_assert(page_table[next_page].bytes_used >= remaining_bytes);
2183 dtc 1.1
2184 rtoy 1.66 PAGE_FLAGS_UPDATE(next_page, PAGE_ALLOCATED_MASK | PAGE_UNBOXED_MASK
2185     | PAGE_GENERATION_MASK,
2186     PAGE_ALLOCATED_MASK | PAGE_UNBOXED_MASK | new_space);
2187    
2188     /* Adjust the bytes_used. */
2189     old_bytes_used = page_table[next_page].bytes_used;
2190     page_table[next_page].bytes_used = remaining_bytes;
2191    
2192     bytes_freed = old_bytes_used - remaining_bytes;
2193    
2194     mmask = PAGE_ALLOCATED_MASK | PAGE_LARGE_OBJECT_MASK
2195     | PAGE_GENERATION_MASK;
2196     mflags = PAGE_ALLOCATED_MASK | PAGE_LARGE_OBJECT_MASK | from_space;
2197    
2198     /* Free any remaining pages; needs care. */
2199     next_page++;
2200     while (old_bytes_used == PAGE_SIZE &&
2201     PAGE_FLAGS(next_page, mmask) == mflags &&
2202     page_table[next_page].first_object_offset ==
2203     PAGE_SIZE * (first_page - next_page)) {
2204     /*
2205     * Checks out OK, free the page. Don't need to both zeroing
2206     * pages as this should have been done before shrinking the
2207     * object. These pages shouldn't be write protected, even if
2208     * boxed they should be zero filled.
2209     */
2210     gc_assert(!PAGE_WRITE_PROTECTED(next_page));
2211 dtc 1.14
2212 rtoy 1.66 old_bytes_used = page_table[next_page].bytes_used;
2213     page_table[next_page].flags &= ~PAGE_ALLOCATED_MASK;
2214     page_table[next_page].bytes_used = 0;
2215     bytes_freed += old_bytes_used;
2216     next_page++;
2217     }
2218 dtc 1.14
2219 rtoy 1.66 if (gencgc_verbose && bytes_freed > 0)
2220     fprintf(stderr, "* copy_large_unboxed bytes_freed %d\n",
2221     bytes_freed);
2222    
2223     generations[from_space].bytes_allocated -=
2224     sizeof(lispobj) * nwords + bytes_freed;
2225     generations[new_space].bytes_allocated += sizeof(lispobj) * nwords;
2226     bytes_allocated -= bytes_freed;
2227 dtc 1.14
2228 rtoy 1.66 return object;
2229     } else {
2230     /* get tag of object */
2231     tag = LowtagOf(object);
2232 dtc 1.14
2233 rtoy 1.66 /* allocate space */
2234     new = gc_quick_alloc_large_unboxed(nwords * sizeof(lispobj));
2235 dtc 1.14
2236 rtoy 1.66 dest = new;
2237     source = (lispobj *) PTR(object);
2238 dtc 1.14
2239 rtoy 1.66 /* copy the object */
2240     while (nwords > 0) {
2241     dest[0] = source[0];
2242     dest[1] = source[1];
2243     dest += 2;
2244     source += 2;
2245     nwords -= 2;
2246     }
2247 dtc 1.14
2248 rtoy 1.66 /* return lisp pointer of new object */
2249     return (lispobj) new | tag;
2250 dtc 1.1 }
2251     }
2252 rtoy 1.66
2253 dtc 1.1
2254     /* Scavenging */
2255 toy 1.49
2256     /*
2257     * Douglas Crosher says:
2258     *
2259     * There were two different ways in which the scavenger dispatched,
2260     * and DIRECT_SCAV was one option. This code did work at one stage
2261     * but testing showed it to be slower. When DIRECT_SCAV is enabled
2262     * the scavenger dispatches via the scavtab for all objects, and when
2263     * disabled the scavenger firstly detects and handles some common
2264     * cases itself before dispatching.
2265     */
2266 dtc 1.1
2267     #define DIRECT_SCAV 0
2268    
2269 gerd 1.38 static void
2270 rtoy 1.66 scavenge(void *start_obj, long nwords)
2271 dtc 1.1 {
2272 rtoy 1.66 lispobj *start;
2273 toy 1.42
2274 rtoy 1.66 start = (lispobj *) start_obj;
2275    
2276     while (nwords > 0) {
2277     lispobj object;
2278     int words_scavenged;
2279 dtc 1.14
2280 rtoy 1.66 object = *start;
2281     /* Not a forwarding pointer. */
2282     gc_assert(object != 0x01);
2283 dtc 1.14
2284 dtc 1.1 #if DIRECT_SCAV
2285 rtoy 1.66 words_scavenged = scavtab[TypeOf(object)] (start, object);
2286     #else /* not DIRECT_SCAV */
2287     if (Pointerp(object)) {
2288 gerd 1.38 #ifdef GC_ASSERTIONS
2289 rtoy 1.66 check_escaped_stack_object(start, object);
2290 gerd 1.38 #endif
2291 dtc 1.14
2292 rtoy 1.66 if (from_space_p(object)) {
2293     lispobj *ptr = (lispobj *) PTR(object);
2294     lispobj first_word = *ptr;
2295    
2296     if (first_word == 0x01) {
2297     *start = ptr[1];
2298     words_scavenged = 1;
2299     } else
2300     words_scavenged = scavtab[TypeOf(object)] (start, object);
2301     } else
2302     words_scavenged = 1;
2303     } else if ((object & 3) == 0)
2304 gerd 1.38 words_scavenged = 1;
2305 rtoy 1.66 else
2306     words_scavenged = scavtab[TypeOf(object)] (start, object);
2307 gerd 1.38 #endif /* not DIRECT_SCAV */
2308 dtc 1.14
2309 rtoy 1.66 start += words_scavenged;
2310     nwords -= words_scavenged;
2311 gerd 1.38 }
2312 rtoy 1.66
2313     gc_assert(nwords == 0);
2314 dtc 1.1 }
2315 rtoy 1.66
2316 dtc 1.1
2317 cwang 1.55 #if !(defined(i386) || defined(__x86_64))
2318 toy 1.33 /* Scavenging Interrupt Contexts */
2319    
2320     static int boxed_registers[] = BOXED_REGISTERS;
2321    
2322 rtoy 1.66 static void
2323     scavenge_interrupt_context(os_context_t * context)
2324 toy 1.33 {
2325 rtoy 1.66 int i;
2326 rtoy 1.69 unsigned long pc_code_offset;
2327 rtoy 1.66
2328 toy 1.33 #ifdef reg_LIP
2329 rtoy 1.66 unsigned long lip;
2330     unsigned long lip_offset;
2331     int lip_register_pair;
2332 toy 1.33 #endif
2333 rtoy 1.68 #ifdef reg_LR
2334     unsigned long lr_code_offset;
2335     #endif
2336     #ifdef reg_CTR
2337     unsigned long ctr_code_offset;
2338     #endif
2339 toy 1.33 #ifdef SC_NPC
2340 rtoy 1.66 unsigned long npc_code_offset;
2341 toy 1.33 #endif
2342    
2343     #ifdef reg_LIP
2344 rtoy 1.66 /* Find the LIP's register pair and calculate it's offset */
2345     /* before we scavenge the context. */
2346 toy 1.33
2347 rtoy 1.66 /*
2348     * I (RLT) think this is trying to find the boxed register that is
2349     * closest to the LIP address, without going past it. Usually, it's
2350     * reg_CODE or reg_LRA. But sometimes, nothing can be found.
2351     */
2352     lip = SC_REG(context, reg_LIP);
2353     lip_offset = 0x7FFFFFFF;
2354     lip_register_pair = -1;
2355     for (i = 0; i < (sizeof(boxed_registers) / sizeof(int)); i++) {
2356     unsigned long reg;
2357     long offset;
2358     int index;
2359    
2360     index = boxed_registers[i];
2361     reg = SC_REG(context, index);
2362     if (Pointerp(reg) && PTR(reg) <= lip) {
2363     offset = lip - reg;
2364     if (offset < lip_offset) {
2365     lip_offset = offset;
2366     lip_register_pair = index;
2367     }
2368     }
2369 toy 1.33 }
2370     #endif /* reg_LIP */
2371    
2372 rtoy 1.69 /*
2373     * Compute the PC's offset from the start of the CODE
2374     * register.
2375     */
2376 rtoy 1.66 pc_code_offset = SC_PC(context) - SC_REG(context, reg_CODE);
2377 toy 1.33 #ifdef SC_NPC
2378 rtoy 1.66 npc_code_offset = SC_NPC(context) - SC_REG(context, reg_CODE);
2379 toy 1.33 #endif /* SC_NPC */
2380    
2381 rtoy 1.68 #ifdef reg_LR
2382     lr_code_offset = SC_REG(context, reg_LR) - SC_REG(context, reg_CODE);
2383     #endif
2384     #ifdef reg_CTR
2385     ctr_code_offset = SC_REG(context, reg_CTR) - SC_REG(context, reg_CODE);
2386     #endif
2387 rtoy 1.75
2388 rtoy 1.66 /* Scanvenge all boxed registers in the context. */
2389     for (i = 0; i < (sizeof(boxed_registers) / sizeof(int)); i++) {
2390     int index;
2391     lispobj foo;
2392    
2393     index = boxed_registers[i];
2394     foo = SC_REG(context, index);
2395     scavenge(&foo, 1);
2396     SC_REG(context, index) = foo;
2397    
2398     scavenge(&(SC_REG(context, index)), 1);
2399 toy 1.33 }
2400    
2401     #ifdef reg_LIP
2402 rtoy 1.66 /* Fix the LIP */
2403 toy 1.33
2404 rtoy 1.66 /*
2405     * But what happens if lip_register_pair is -1? SC_REG on Solaris
2406     * (see solaris_register_address in solaris-os.c) will return
2407     * &context->uc_mcontext.gregs[2]. But gregs[2] is REG_nPC. Is
2408     * that what we really want? My guess is that that is not what we
2409     * want, so if lip_register_pair is -1, we don't touch reg_LIP at
2410     * all. But maybe it doesn't really matter if LIP is trashed?
2411     */
2412     if (lip_register_pair >= 0) {
2413     SC_REG(context, reg_LIP) =
2414     SC_REG(context, lip_register_pair) + lip_offset;
2415 toy 1.33 }
2416     #endif /* reg_LIP */
2417 rtoy 1.66
2418     /* Fix the PC if it was in from space */
2419 rtoy 1.69 if (from_space_p(SC_PC(context))) {
2420     SC_PC(context) = SC_REG(context, reg_CODE) + pc_code_offset;
2421     }
2422 toy 1.33 #ifdef SC_NPC
2423 rtoy 1.69 if (from_space_p(SC_NPC(context))) {
2424 rtoy 1.66 SC_NPC(context) = SC_REG(context, reg_CODE) + npc_code_offset;
2425 rtoy 1.69 }
2426 toy 1.33 #endif /* SC_NPC */
2427 rtoy 1.68
2428     #ifdef reg_LR
2429 rtoy 1.69 if (from_space_p(SC_REG(context, reg_LR))) {
2430     SC_REG(context, reg_LR) = SC_REG(context, reg_CODE) + lr_code_offset;
2431 rtoy 1.68 }
2432     #endif
2433     #ifdef reg_CTR
2434 rtoy 1.69 if (from_space_p(SC_REG(context, reg_CTR))) {
2435     SC_REG(context, reg_CTR) = SC_REG(context, reg_CODE) + ctr_code_offset;
2436 rtoy 1.68 }
2437     #endif
2438 toy 1.33 }
2439    
2440 rtoy 1.66 void
2441     scavenge_interrupt_contexts(void)
2442 toy 1.33 {
2443 rtoy 1.66 int i, index;
2444     os_context_t *context;
2445 toy 1.33
2446 toy 1.40 #ifdef PRINTNOISE
2447 rtoy 1.66 printf("Scavenging interrupt contexts ...\n");
2448 toy 1.40 #endif
2449    
2450 rtoy 1.66 index = fixnum_value(SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX));
2451 toy 1.33
2452     #if defined(DEBUG_PRINT_CONTEXT_INDEX)
2453 rtoy 1.66 printf("Number of active contexts: %d\n", index);
2454 toy 1.33 #endif
2455    
2456 rtoy 1.66 for (i = 0; i < index; i++) {
2457     context = lisp_interrupt_contexts[i];
2458     scavenge_interrupt_context(context);
2459 toy 1.33 }
2460     }
2461     #endif
2462    
2463 dtc 1.1 /* Code and Code-Related Objects */
2464    
2465 toy 1.33 /*
2466     * Aargh! Why is SPARC so different here? What is the advantage of
2467     * making it different from all the other ports?
2468     */
2469 cshapiro 1.87 #if defined(sparc) || (defined(DARWIN) && defined(__ppc__))
2470 toy 1.33 #define RAW_ADDR_OFFSET 0
2471     #else
2472 dtc 1.14 #define RAW_ADDR_OFFSET (6 * sizeof(lispobj) - type_FunctionPointer)
2473 toy 1.33 #endif
2474 dtc 1.1
2475     static lispobj trans_function_header(lispobj object);
2476     static lispobj trans_boxed(lispobj object);
2477    
2478     #if DIRECT_SCAV
2479 rtoy 1.66 static int
2480     scav_function_pointer(lispobj * where, lispobj object)
2481 dtc 1.1 {
2482 rtoy 1.66 gc_assert(Pointerp(object));
2483 dtc 1.1
2484 rtoy 1.66 if (from_space_p(object)) {
2485     lispobj first, *first_pointer;
2486 dtc 1.14
2487 rtoy 1.66 /*
2488     * Object is a pointer into from space - check to see if it has
2489     * been forwarded.
2490     */
2491     first_pointer = (lispobj *) PTR(object);
2492     first = *first_pointer;
2493 dtc 1.14
2494 rtoy 1.66 if (first == 0x01) {
2495     /* Forwarded */
2496     *where = first_pointer[1];
2497     return 1;
2498     } else {
2499     int type;
2500     lispobj copy;
2501 dtc 1.14
2502 rtoy 1.66 /*
2503     * Must transport object -- object may point to either a
2504     * function header, a closure function header, or to a closure
2505     * header.
2506     */
2507 dtc 1.14
2508 rtoy 1.66 type = TypeOf(first);
2509     switch (type) {
2510     case type_FunctionHeader:
2511     case type_ClosureFunctionHeader:
2512     copy = trans_function_header(object);
2513     break;
2514     default:
2515     copy = trans_boxed(object);
2516     break;
2517     }
2518 dtc 1.14
2519 rtoy 1.66 if (copy != object) {
2520     /* Set forwarding pointer. */
2521     first_pointer[0] = 0x01;
2522     first_pointer[1] = copy;
2523     }
2524 dtc 1.14
2525 rtoy 1.66 first = copy;
2526     }
2527 dtc 1.14
2528 rtoy 1.66 gc_assert(Pointerp(first));
2529     gc_assert(!from_space_p(first));
2530 dtc 1.14
2531 rtoy 1.66 *where = first;
2532     }
2533     return 1;
2534 dtc 1.1 }
2535     #else
2536 rtoy 1.66 static int
2537     scav_function_pointer(lispobj * where, lispobj object)
2538 dtc 1.1 {
2539 rtoy 1.66 lispobj *first_pointer;
2540     lispobj copy;
2541    
2542     gc_assert(Pointerp(object));
2543    
2544     /* Object is a pointer into from space - no a FP. */
2545     first_pointer = (lispobj *) PTR(object);
2546 dtc 1.1
2547 rtoy 1.66 /*
2548     * Must transport object -- object may point to either a function
2549     * header, a closure function header, or to a closure header.
2550     */
2551 dtc 1.14
2552 rtoy 1.66 switch (TypeOf(*first_pointer)) {
2553     case type_FunctionHeader:
2554     case type_ClosureFunctionHeader:
2555     copy = trans_function_header(object);
2556     break;
2557     default:
2558     copy = trans_boxed(object);
2559     break;
2560     }
2561 dtc 1.14
2562 rtoy 1.66 if (copy != object) {
2563     /* Set forwarding pointer */
2564     first_pointer[0] = 0x01;
2565     first_pointer[1] = copy;
2566     }
2567 dtc 1.14
2568 rtoy 1.66 gc_assert(Pointerp(copy));
2569     gc_assert(!from_space_p(copy));
2570 dtc 1.1
2571 rtoy 1.66 *where = copy;
2572 dtc 1.14
2573 rtoy 1.66 return 1;
2574 dtc 1.1 }
2575     #endif
2576    
2577 cwang 1.55 #if defined(i386) || defined(__x86_64)
2578 dtc 1.14 /*
2579 cwang 1.54 * Scan an x86 compiled code object, looking for possible fixups that
2580 dtc 1.14 * have been missed after a move.
2581     *
2582     * Two types of fixups are needed:
2583     * 1. Absolution fixups to within the code object.
2584     * 2. Relative fixups to outside the code object.
2585     *
2586     * Currently only absolution fixups to the constant vector, or to the
2587     * code area are checked.
2588     */
2589 rtoy 1.66 void
2590     sniff_code_object(struct code *code, unsigned displacement)
2591 dtc 1.1 {
2592 rtoy 1.66 int nheader_words, ncode_words, nwords;
2593     void *p;
2594     void *constants_start_addr, *constants_end_addr;
2595     void *code_start_addr, *code_end_addr;
2596     int fixup_found = 0;
2597 dtc 1.14
2598 rtoy 1.66 if (!check_code_fixups)
2599     return;
2600 dtc 1.3
2601 rtoy 1.66 /*
2602     * It's ok if it's byte compiled code. The trace table offset will
2603     * be a fixnum if it's x86 compiled code - check.
2604     */
2605     if (code->trace_table_offset & 0x3) {
2606 dtc 1.14 #if 0
2607 rtoy 1.66 fprintf(stderr, "*** Sniffing byte compiled code object at %x.\n",
2608     code);
2609 dtc 1.14 #endif
2610 rtoy 1.66 return;
2611     }
2612    
2613     /* Else it's x86 machine code. */
2614    
2615     ncode_words = fixnum_value(code->code_size);
2616     nheader_words = HeaderValue(*(lispobj *) code);
2617     nwords = ncode_words + nheader_words;
2618    
2619     constants_start_addr = (void *) code + 5 * sizeof(lispobj);
2620     constants_end_addr = (void *) code + nheader_words * sizeof(lispobj);
2621     code_start_addr = (void *) code + nheader_words * sizeof(lispobj);
2622     code_end_addr = (void *) code + nwords * sizeof(lispobj);
2623    
2624     /* Work through the unboxed code. */
2625     for (p = code_start_addr; p < code_end_addr; p++) {
2626     void *data = *(void **) p;
2627     unsigned d1 = *((unsigned char *) p - 1);
2628     unsigned d2 = *((unsigned char *) p - 2);
2629     unsigned d3 = *((unsigned char *) p - 3);
2630     unsigned d4 = *((unsigned char *) p - 4);
2631     unsigned d5 = *((unsigned char *) p - 5);
2632     unsigned d6 = *((unsigned char *) p - 6);
2633    
2634     /*
2635     * Check for code references.
2636     *
2637     * Check for a 32 bit word that looks like an absolute reference
2638     * to within the code adea of the code object.
2639     */
2640     if (data >= code_start_addr - displacement
2641     && data < code_end_addr - displacement) {
2642     /* Function header */
2643     if (d4 == 0x5e
2644     && ((unsigned long) p - 4 -
2645     4 * HeaderValue(*((unsigned long *) p - 1))) ==
2646     (unsigned long) code) {
2647     /* Skip the function header */
2648     p += 6 * 4 - 4 - 1;
2649     continue;
2650     }
2651     /* Push imm32 */
2652     if (d1 == 0x68) {
2653     fixup_found = 1;
2654     fprintf(stderr,
2655     "Code ref. @ %lx: %.2x %.2x %.2x %.2x %.2x %.2x (%.8lx)\n",
2656     (unsigned long) p, d6, d5, d4, d3, d2, d1,
2657     (unsigned long) data);
2658     fprintf(stderr, "*** Push $0x%.8lx\n", (unsigned long) data);
2659     }
2660     /* Mov [reg-8],imm32 */
2661     if (d3 == 0xc7
2662     && (d2 == 0x40 || d2 == 0x41 || d2 == 0x42 || d2 == 0x43
2663     || d2 == 0x45 || d2 == 0x46 || d2 == 0x47)
2664     && d1 == 0xf8) {
2665     fixup_found = 1;
2666     fprintf(stderr,
2667     "Code ref. @ %lx: %.2x %.2x %.2x %.2x %.2x %.2x (%.8lx)\n",
2668     (unsigned long) p, d6, d5, d4, d3, d2, d1,
2669     (unsigned long) data);
2670     fprintf(stderr, "*** Mov [reg-8],$0x%.8lx\n",
2671     (unsigned long) data);
2672     }
2673     /* Lea reg, [disp32] */
2674     if (d2 == 0x8d && (d1 & 0xc7) == 5) {
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, "*** Lea reg,[$0x%.8lx]\n",
2681     (unsigned long) data);
2682     }
2683     }
2684    
2685     /*
2686     * Check for constant references.
2687     *
2688     * Check for a 32 bit word that looks like an absolution reference
2689     * to within the constant vector. Constant references will be
2690     * aligned.
2691     */
2692     if (data >= constants_start_addr - displacement
2693     && data < constants_end_addr - displacement
2694     && ((unsigned long) data & 0x3) == 0) {
2695     /* Mov eax,m32 */
2696     if (d1 == 0xa1) {
2697     fixup_found = 1;
2698     fprintf(stderr,
2699     "Abs. const. 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, "*** Mov eax,0x%.8lx\n", (unsigned long) data);
2703     }
2704    
2705     /* Mov m32,eax */
2706     if (d1 == 0xa3) {
2707     fixup_found = 1;
2708     fprintf(stderr,
2709     "Abs. const. ref. @ %lx: %.2x %.2x %.2x %.2x %.2x %.2x (%.8lx)\n",
2710     (unsigned long) p, d6, d5, d4, d3, d2, d1,
2711     (unsigned long) data);
2712     fprintf(stderr, "*** Mov 0x%.8lx,eax\n", (unsigned long) data);
2713     }
2714    
2715     /* Cmp m32,imm32 */
2716     if (d1 == 0x3d && d2 == 0x81) {
2717     fixup_found = 1;
2718     fprintf(stderr,
2719     "Abs. const. ref. @ %lx: %.2x %.2x %.2x %.2x %.2x %.2x (%.8lx)\n",
2720     (unsigned long) p, d6, d5, d4, d3, d2, d1,
2721     (unsigned long) data);
2722     /* XX Check this */
2723     fprintf(stderr, "*** Cmp 0x%.8lx,immed32\n",
2724     (unsigned long) data);
2725     }
2726    
2727     /* Check for a mod=00, r/m=101 byte. */
2728     if ((d1 & 0xc7) == 5) {
2729     /* Cmp m32,reg */
2730     if (d2 == 0x39) {
2731     fixup_found = 1;
2732     fprintf(stderr,
2733     "Abs. const. ref. @ %lx: %.2x %.2x %.2x %.2x %.2x %.2x (%.8lx)\n",
2734     (unsigned long) p, d6, d5, d4, d3, d2, d1,
2735     (unsigned long) data);
2736     fprintf(stderr, "*** Cmp 0x%.8lx,reg\n",
2737     (unsigned long) data);
2738     }
2739     /* Cmp reg32,m32 */
2740     if (d2 == 0x3b) {
2741     fixup_found = 1;
2742     fprintf(stderr,
2743     "Abs. const. ref. @ %lx: %.2x %.2x %.2x %.2x %.2x %.2x (%.8lx)\n",
2744     (unsigned long) p, d6, d5, d4, d3, d2, d1,
2745     (unsigned long) data);
2746     fprintf(stderr, "*** Cmp reg32,0x%.8lx\n",
2747     (unsigned long) data);
2748     }
2749     /* Mov m32,reg32 */
2750     if (d2 == 0x89) {
2751     fixup_found = 1;
2752     fprintf(stderr,
2753     "Abs. const. ref. @ %lx: %.2x %.2x %.2x %.2x %.2x %.2x (%.8lx)\n",
2754     (unsigned long) p, d6, d5, d4, d3, d2, d1,
2755     (unsigned long) data);
2756     fprintf(stderr, "*** Mov 0x%.8lx,reg32\n",
2757     (unsigned long) data);
2758     }
2759     /* Mov reg32,m32 */
2760     if (d2 == 0x8b) {
2761     fixup_found = 1;
2762     fprintf(stderr,
2763     "Abs. const. ref. @ %lx: %.2x %.2x %.2x %.2x %.2x %.2x (%.8lx)\n",
2764     (unsigned long) p, d6, d5, d4, d3, d2, d1,
2765     (unsigned long) data);
2766     fprintf(stderr, "*** Mov reg32,0x%.8lx\n",
2767     (unsigned long) data);
2768     }
2769     /* Lea reg32,m32 */
2770     if (d2 == 0x8d) {
2771     fixup_found = 1;
2772     fprintf(stderr,
2773     "Abs. const. ref. @ %lx: %.2x %.2x %.2x %.2x %.2x %.2x (%.8lx)\n",
2774     (unsigned long) p, d6, d5, d4, d3, d2, d1,
2775     (unsigned long) data);
2776     fprintf(stderr, "*** Lea reg32,0x%.8lx\n",
2777     (unsigned long) data);
2778     }
2779     }
2780     }
2781     }
2782    
2783     /* If anything was found print out some info. on the code object. */
2784     if (fixup_found) {
2785     fprintf(stderr,
2786     "*** Compiled code object at %lx: header_words=%d code_words=%d .\n",
2787     (unsigned long) code, nheader_words, ncode_words);
2788     fprintf(stderr,
2789     "*** Const. start = %lx; end= %lx; Code start = %lx; end = %lx\n",
2790     (unsigned long) constants_start_addr,
2791     (unsigned long) constants_end_addr,
2792     (unsigned long) code_start_addr, (unsigned long) code_end_addr);
2793     }
2794     }
2795 dtc 1.1
2796 rtoy 1.66 static void
2797     apply_code_fixups(struct code *old_code, struct code *new_code)
2798     {
2799     int nheader_words, ncode_words, nwords;
2800     void *constants_start_addr, *constants_end_addr;
2801     void *code_start_addr, *code_end_addr;
2802     lispobj fixups = NIL;
2803     unsigned long displacement =
2804 dtc 1.1
2805 rtoy 1.66 (unsigned long) new_code - (unsigned long) old_code;
2806     struct vector *fixups_vector;
2807 dtc 1.14
2808     /*
2809 rtoy 1.66 * It's ok if it's byte compiled code. The trace table offset will
2810     * be a fixnum if it's x86 compiled code - check.
2811 dtc 1.14 */
2812 rtoy 1.66 if (new_code->trace_table_offset & 0x3) {
2813     #if 0
2814     fprintf(stderr, "*** Byte compiled code object at %x.\n", new_code);
2815     #endif
2816     return;
2817 dtc 1.1 }
2818    
2819 rtoy 1.66 /* Else it's x86 machine code. */
2820     ncode_words = fixnum_value(new_code->code_size);
2821     nheader_words = HeaderValue(*(lispobj *) new_code);
2822     nwords = ncode_words + nheader_words;
2823 dtc 1.14 #if 0
2824 rtoy 1.66 fprintf(stderr,
2825     "*** Compiled code object at %x: header_words=%d code_words=%d .\n",
2826     new_code, nheader_words, ncode_words);
2827     #endif
2828     constants_start_addr = (void *) new_code + 5 * sizeof(lispobj);
2829     constants_end_addr = (void *) new_code + nheader_words * sizeof(lispobj);
2830     code_start_addr = (void *) new_code + nheader_words * sizeof(lispobj);
2831     code_end_addr = (void *) new_code + nwords * sizeof(lispobj);
2832     #if 0
2833     fprintf(stderr,
2834     "*** Const. start = %x; end= %x; Code start = %x; end = %x\n",
2835     constants_start_addr, constants_end_addr, code_start_addr,
2836     code_end_addr);
2837 dtc 1.14 #endif
2838 dtc 1.1
2839 rtoy 1.66 /*
2840     * The first constant should be a pointer to the fixups for this
2841     * code objects - Check.
2842     */
2843     fixups = new_code->constants[0];
2844    
2845     /*
2846     * It will be 0 or the unbound-marker if there are no fixups, and
2847     * will be an other pointer if it is valid.
2848     */
2849     if (fixups == 0 || fixups == type_UnboundMarker || !Pointerp(fixups)) {
2850     /* Check for possible errors. */
2851     if (check_code_fixups)
2852     sniff_code_object(new_code, displacement);
2853 dtc 1.14
2854     #if 0
2855 rtoy 1.66 fprintf(stderr, "Fixups for code object not found!?\n");
2856     fprintf(stderr,
2857     "*** Compiled code object at %x: header_words=%d code_words=%d .\n",
2858     new_code, nheader_words, ncode_words);
2859     fprintf(stderr,
2860     "*** Const. start = %x; end= %x; Code start = %x; end = %x\n",
2861     constants_start_addr, constants_end_addr, code_start_addr,
2862     code_end_addr);
2863 dtc 1.14 #endif
2864 rtoy 1.66 return;
2865     }
2866 dtc 1.1
2867 rtoy 1.66 fixups_vector = (struct vector *) PTR(fixups);
2868 dtc 1.1
2869 rtoy 1.66 /* Could be pointing to a forwarding pointer. */
2870     if (Pointerp(fixups) && find_page_index((void *) fixups_vector) != -1
2871     && fixups_vector->header == 0x01) {
2872 dtc 1.19 #if 0
2873 rtoy 1.66 fprintf(stderr, "* FF\n");
2874 dtc 1.19 #endif
2875 rtoy 1.66 /* If so then follow it. */
2876     fixups_vector = (struct vector *) PTR((lispobj) fixups_vector->length);
2877     }
2878 dtc 1.14 #if 0
2879 rtoy 1.66 fprintf(stderr, "Got the fixups\n");
2880 dtc 1.14 #endif
2881 dtc 1.1
2882 rtoy 1.66 if (TypeOf(fixups_vector->header) == type_SimpleArrayUnsignedByte32) {
2883 dtc 1.14 /*
2884 rtoy 1.66 * Got the fixups for the code block. Now work through the
2885     * vector, and apply a fixup at each address.
2886 dtc 1.14 */
2887 rtoy 1.66 int length = fixnum_value(fixups_vector->length);
2888     int i;
2889    
2890     for (i = 0; i < length; i++) {
2891     unsigned offset = fixups_vector->data[i];
2892    
2893     /* Now check the current value of offset. */
2894     unsigned long old_value =
2895     *(unsigned long *) ((unsigned long) code_start_addr + offset);
2896    
2897     /*
2898     * If it's within the old_code object then it must be an
2899     * absolute fixup (relative ones are not saved).
2900     */
2901     if (old_value >= (unsigned long) old_code
2902     && old_value <
2903     (unsigned long) old_code + nwords * sizeof(lispobj))
2904     /* So add the dispacement. */
2905     *(unsigned long *) ((unsigned long) code_start_addr + offset) =
2906     old_value + displacement;
2907     else
2908     /*
2909     * It is outside the old code object so it must be a relative
2910     * fixup (absolute fixups are not saved). So subtract the
2911     * displacement.
2912     */
2913     *(unsigned long *) ((unsigned long) code_start_addr + offset) =
2914     old_value - displacement;
2915     }
2916 dtc 1.1 }
2917 dtc 1.14
2918 rtoy 1.66 /* Check for possible errors. */
2919     if (check_code_fixups)
2920     sniff_code_object(new_code, displacement);
2921 dtc 1.1 }
2922 toy 1.33 #endif
2923 dtc 1.1
2924 rtoy 1.66 static struct code *
2925     trans_code(struct code *code)
2926 dtc 1.1 {
2927 rtoy 1.66 struct code *new_code;
2928     lispobj l_code, l_new_code;
2929     int nheader_words, ncode_words, nwords;
2930     unsigned long displacement;
2931     lispobj fheaderl, *prev_pointer;
2932 dtc 1.14
2933     #if 0
2934 rtoy 1.66 fprintf(stderr, "\nTransporting code object located at 0x%08x.\n",
2935     (unsigned long) code);
2936 dtc 1.14 #endif
2937    
2938 rtoy 1.66 /* If object has already been transported, just return pointer */
2939     if (*(lispobj *) code == 0x01) {
2940     return (struct code *) (((lispobj *) code)[1]);
2941 toy 1.33 }
2942 dtc 1.14
2943    
2944 rtoy 1.66 gc_assert(TypeOf(code->header) == type_CodeHeader);
2945 dtc 1.14
2946 rtoy 1.66 /* prepare to transport the code vector */
2947     l_code = (lispobj) code | type_OtherPointer;
2948 dtc 1.14
2949 rtoy 1.66 ncode_words = fixnum_value(code->code_size);
2950     nheader_words = HeaderValue(code->header);
2951     nwords = ncode_words + nheader_words;
2952     nwords = CEILING(nwords, 2);
2953 dtc 1.1
2954 rtoy 1.66 l_new_code = copy_large_object(l_code, nwords);
2955     new_code = (struct code *) PTR(l_new_code);
2956    
2957     /* May not have been moved. */
2958     if (new_code == code)
2959     return new_code;
2960 dtc 1.14
2961 rtoy 1.66 displacement = l_new_code - l_code;
2962 dtc 1.14
2963     #if 0
2964 rtoy 1.66 fprintf(stderr, "Old code object at 0x%08x, new code object at 0x%08x.\n",
2965     (unsigned long) code, (unsigned long) new_code);
2966     fprintf(stderr, "Code object is %d words long.\n", nwords);
2967 dtc 1.14 #endif
2968    
2969 rtoy 1.66 /* set forwarding pointer */
2970     ((lispobj *) code)[0] = 0x01;
2971     ((lispobj *) code)[1] = l_new_code;
2972 dtc 1.14
2973 rtoy 1.66 /*
2974     * Set forwarding pointers for all the function headers in the code
2975     * object; also fix all self pointers.
2976     */
2977 dtc 1.14
2978 rtoy 1.66 fheaderl = code->entry_points;
2979     prev_pointer = &new_code->entry_points;
2980 dtc 1.14
2981 rtoy 1.66 while (fheaderl != NIL) {
2982     struct function *fheaderp, *nfheaderp;
2983     lispobj nfheaderl;
2984 dtc 1.14
2985 rtoy 1.66 fheaderp = (struct function *) PTR(fheaderl);
2986     gc_assert(TypeOf(fheaderp->header) == type_FunctionHeader);
2987 dtc 1.14
2988 rtoy 1.66 /*
2989     * Calcuate the new function pointer and the new function header.
2990     */
2991     nfheaderl = fheaderl + displacement;
2992     nfheaderp = (struct function *) PTR(nfheaderl);
2993 dtc 1.14
2994 rtoy 1.66 /* set forwarding pointer */
2995     ((lispobj *) fheaderp)[0] = 0x01;
2996     ((lispobj *) fheaderp)[1] = nfheaderl;
2997 dtc 1.14
2998 rtoy 1.66 /* Fix self pointer */
2999     nfheaderp->self = nfheaderl + RAW_ADDR_OFFSET;
3000 dtc 1.14
3001 rtoy 1.66 *prev_pointer = nfheaderl;
3002 dtc 1.14
3003 rtoy 1.66 fheaderl = fheaderp->next;
3004     prev_pointer = &nfheaderp->next;
3005     }
3006 dtc 1.1
3007 dtc 1.14 #if 0
3008 rtoy 1.66 sniff_code_object(new_code, displacement);
3009 dtc 1.14 #endif
3010 cwang 1.55 #if defined(i386) || defined(__x86_64)
3011 rtoy 1.66 apply_code_fixups(code, new_code);
3012 toy 1.33 #else
3013 rtoy 1.66 /* From gc.c */
3014 toy 1.33 #ifndef MACH
3015 rtoy 1.66 os_flush_icache((os_vm_address_t) (((int *) new_code) + nheader_words),
3016     ncode_words * sizeof(int));
3017 toy 1.33 #endif
3018     #endif
3019 dtc 1.14
3020 rtoy 1.66 return new_code;
3021 dtc 1.1 }
3022    
3023 rtoy 1.66 static int
3024     scav_code_header(lispobj * where, lispobj object)
3025 dtc 1.1 {
3026 rtoy 1.66 struct code *code;
3027     int nheader_words, ncode_words, nwords;
3028     lispobj fheaderl;
3029     struct function *fheaderp;
3030    
3031     code = (struct code *) where;
3032     ncode_words = fixnum_value(code->code_size);
3033     nheader_words = HeaderValue(object);
3034     nwords = ncode_words + nheader_words;
3035     nwords = CEILING(nwords, 2);
3036 dtc 1.14
3037 rtoy 1.66 /* Scavenge the boxed section of the code data block */
3038     scavenge(where + 1, nheader_words - 1);
3039 dtc 1.1
3040 rtoy 1.66 /*
3041     * Scavenge the boxed section of each function object in the code
3042     * data block
3043     */
3044     fheaderl = code->entry_points;
3045     while (fheaderl != NIL) {
3046     fheaderp = (struct function *) PTR(fheaderl);
3047     gc_assert(TypeOf(fheaderp->header) == type_FunctionHeader);
3048 dtc 1.1
3049 rtoy 1.66 scavenge(&fheaderp->name, 1);
3050     scavenge(&fheaderp->arglist, 1);
3051     scavenge(&fheaderp->type, 1);
3052 dtc 1.14
3053 rtoy 1.66 fheaderl = fheaderp->next;
3054     }
3055 dtc 1.14
3056 rtoy 1.66 return nwords;
3057 dtc 1.1 }
3058    
3059 rtoy 1.66 static lispobj
3060     trans_code_header(lispobj object)
3061 dtc 1.1 {
3062 rtoy 1.66 struct code *ncode;
3063 dtc 1.1
3064 rtoy 1.66 ncode = trans_code((struct code *) PTR(object));
3065     return (lispobj) ncode | type_OtherPointer;
3066 dtc 1.1 }
3067    
3068 rtoy 1.66 static int
3069     size_code_header(lispobj * where)
3070 dtc 1.1 {
3071 rtoy 1.66 struct code *code;
3072     int nheader_words, ncode_words, nwords;
3073 dtc 1.1
3074 rtoy 1.66 code = (struct code *) where;
3075 dtc 1.14
3076 rtoy 1.66 ncode_words = fixnum_value(code->code_size);
3077     nheader_words = HeaderValue(code->header);
3078     nwords = ncode_words + nheader_words;
3079     nwords = CEILING(nwords, 2);
3080 dtc 1.1
3081 rtoy 1.66 return nwords;
3082 dtc 1.1 }
3083    
3084 cwang 1.55 #if !(defined(i386) || defined(__x86_64))
3085 dtc 1.1
3086 rtoy 1.66 static int
3087     scav_return_pc_header(lispobj * where, lispobj object)
3088 dtc 1.1 {
3089     fprintf(stderr, "GC lossage. Should not be scavenging a ");
3090     fprintf(stderr, "Return PC Header.\n");
3091 gerd 1.32 fprintf(stderr, "where = 0x%08lx, object = 0x%08lx",
3092 dtc 1.1 (unsigned long) where, (unsigned long) object);
3093     lose(NULL);
3094     return 0;
3095     }
3096    
3097 gerd 1.38 #endif /* not i386 */
3098    
3099 rtoy 1.66 static lispobj
3100     trans_return_pc_header(lispobj object)
3101 dtc 1.1 {
3102 rtoy 1.66 struct function *return_pc;
3103     unsigned long offset;
3104     struct code *code, *ncode;
3105 dtc 1.1
3106 rtoy 1.66 return_pc = (struct function *) PTR(object);
3107     offset = HeaderValue(return_pc->header) * sizeof(lispobj);
3108 dtc 1.1
3109 rtoy 1.66 /* Transport the whole code object */
3110     code = (struct code *) ((unsigned long) return_pc - offset);
3111 dtc 1.14
3112 rtoy 1.66 ncode = trans_code(code);
3113    
3114     return ((lispobj) ncode + offset) | type_OtherPointer;
3115 dtc 1.1 }
3116    
3117 dtc 1.14 /*
3118     * On the 386, closures hold a pointer to the raw address instead of
3119     * the function object.
3120     */
3121 cwang 1.55 #if defined(i386) || defined(__x86_64)
3122 gerd 1.38
3123 rtoy 1.66 static int
3124     scav_closure_header(lispobj * where, lispobj object)
3125 dtc 1.1 {
3126 rtoy 1.66 struct closure *closure;
3127     lispobj fun;
3128 dtc 1.1
3129 rtoy 1.66 closure = (struct closure *) where;
3130     fun = closure->function - RAW_ADDR_OFFSET;
3131     scavenge(&fun, 1);
3132     /* The function may have moved so update the raw address. But don't
3133     write unnecessarily. */
3134     if (closure->function != fun + RAW_ADDR_OFFSET)
3135     closure->function = fun + RAW_ADDR_OFFSET;
3136 dtc 1.14
3137 rtoy 1.66 return 2;
3138 dtc 1.1 }
3139 gerd 1.38
3140     #endif /* i386 */
3141    
3142 cwang 1.55 #if !(defined(i386) || defined(__x86_64))
3143 dtc 1.1
3144 rtoy 1.66 static int
3145     scav_function_header(lispobj * where, lispobj object)
3146 dtc 1.1 {
3147     fprintf(stderr, "GC lossage. Should not be scavenging a ");
3148     fprintf(stderr, "Function Header.\n");
3149 gerd 1.32 fprintf(stderr, "where = 0x%08lx, object = 0x%08lx",
3150 dtc 1.1 (unsigned long) where, (unsigned long) object);
3151     lose(NULL);
3152     return 0;
3153     }
3154    
3155 gerd 1.38 #endif /* not i386 */
3156    
3157 rtoy 1.66 static lispobj
3158     trans_function_header(lispobj object)
3159 dtc 1.1 {
3160 rtoy 1.66 struct function *fheader;
3161     unsigned long offset;
3162     struct code *code, *ncode;
3163 dtc 1.14
3164 rtoy 1.66 fheader = (struct function *) PTR(object);
3165     offset = HeaderValue(fheader->header) * sizeof(lispobj);
3166 dtc 1.14
3167 rtoy 1.66 /* Transport the whole code object */
3168     code = (struct code *) ((unsigned long) fheader - offset);
3169     ncode = trans_code(code);
3170 dtc 1.14
3171 rtoy 1.66 return ((lispobj) ncode + offset) | type_FunctionPointer;
3172 dtc 1.1 }
3173 rtoy 1.66
3174 dtc 1.1
3175     /* Instances */
3176    
3177     #if DIRECT_SCAV
3178 rtoy 1.66 static int
3179     scav_instance_pointer(lispobj * where, lispobj object)
3180 dtc 1.1 {
3181 rtoy 1.66 if (from_space_p(object)) {
3182     lispobj first, *first_pointer;
3183 dtc 1.14
3184 rtoy 1.66 /*
3185     * object is a pointer into from space. check to see if it has
3186     * been forwarded
3187     */
3188     first_pointer = (lispobj *) PTR(object);
3189     first = *first_pointer;
3190 dtc 1.14
3191 rtoy 1.66 if (first == 0x01)
3192     /* Forwarded. */
3193     first = first_pointer[1];
3194     else {
3195     first = trans_boxed(object);
3196     gc_assert(first != object);
3197     /* Set forwarding pointer */
3198     first_pointer[0] = 0x01;
3199     first_pointer[1] = first;
3200     }
3201     *where = first;
3202 dtc 1.1 }
3203 rtoy 1.66 return 1;
3204 dtc 1.1 }
3205     #else
3206 rtoy 1.66 static int
3207     scav_instance_pointer(lispobj * where, lispobj object)
3208 dtc 1.1 {
3209 rtoy 1.66 lispobj copy, *first_pointer;
3210 dtc 1.14
3211 rtoy 1.66 /* Object is a pointer into from space - not a FP */
3212     copy = trans_boxed(object);
3213 dtc 1.1
3214 rtoy 1.66 gc_assert(copy != object);
3215 dtc 1.1
3216 rtoy 1.66 first_pointer = (lispobj *) PTR(object);
3217 dtc 1.14
3218 rtoy 1.66 /* Set forwarding pointer. */
3219     first_pointer[0] = 0x01;
3220     first_pointer[1] = copy;
3221     *where = copy;
3222 dtc 1.1
3223 rtoy 1.66 return 1;
3224 dtc 1.1 }
3225     #endif
3226 rtoy 1.66
3227 dtc 1.1
3228     /* Lists and Conses */
3229    
3230     static lispobj trans_list(lispobj object);
3231    
3232     #if DIRECT_SCAV
3233 rtoy 1.66 static int
3234     scav_list_pointer(lispobj * where, lispobj object)
3235 dtc 1.1 {
3236 rtoy 1.66 gc_assert(Pointerp(object));
3237 dtc 1.1
3238 rtoy 1.66 if (from_space_p(object)) {
3239     lispobj first, *first_pointer;
3240 dtc 1.14
3241 rtoy 1.66 /*
3242     * Object is a pointer into from space - check to see if it has
3243     * been forwarded.
3244     */
3245     first_pointer = (lispobj *) PTR(object);
3246     first = *first_pointer;
3247 dtc 1.14
3248 rtoy 1.66 if (first == 0x01)
3249     /* Forwarded. */
3250     first = first_pointer[1];
3251     else {
3252     first = trans_list(object);
3253    
3254     /* Set forwarding pointer */
3255     first_pointer[0] = 0x01;
3256     first_pointer[1] = first;
3257     }
3258 dtc 1.14
3259 rtoy 1.66 gc_assert(Pointerp(first));
3260     gc_assert(!from_space_p(first));
3261     *where = first;
3262 dtc 1.1 }
3263 rtoy 1.66 return 1;
3264 dtc 1.1 }
3265     #else
3266 rtoy 1.66 static int
3267     scav_list_pointer(lispobj * where, lispobj object)
3268 dtc 1.1 {
3269 rtoy 1.66 lispobj first, *first_pointer;
3270 dtc 1.1
3271 rtoy 1.66 gc_assert(Pointerp(object));
3272 dtc 1.1
3273 rtoy 1.66 /* Object is a pointer into from space - not FP */
3274 dtc 1.14
3275 rtoy 1.66 first = trans_list(object);
3276     gc_assert(first != object);
3277 dtc 1.1
3278 rtoy 1.66 first_pointer = (lispobj *) PTR(object);
3279 dtc 1.1
3280 rtoy 1.66 /* Set forwarding pointer */
3281     first_pointer[0] = 0x01;
3282     first_pointer[1] = first;
3283 dtc 1.1
3284 rtoy 1.66 gc_assert(Pointerp(first));
3285     gc_assert(!from_space_p(first));
3286     *where = first;
3287     return 1;
3288 dtc 1.1 }
3289     #endif
3290    
3291 rtoy 1.66 static lispobj
3292     trans_list(lispobj object)
3293 dtc 1.1 {
3294 rtoy 1.66 lispobj new_list_pointer;
3295     struct cons *cons, *new_cons;
3296     lispobj cdr;
3297 dtc 1.1
3298 rtoy 1.66 gc_assert(from_space_p(object));
3299 dtc 1.1
3300 rtoy 1.66 cons = (struct cons *) PTR(object);
3301 dtc 1.14
3302 rtoy 1.66 /* copy 'object' */
3303     new_cons = (struct cons *) gc_quick_alloc(sizeof(struct cons));
3304 dtc 1.1
3305 rtoy 1.66 new_cons->car = cons->car;
3306     new_cons->cdr = cons->cdr; /* updated later */
3307     new_list_pointer = (lispobj) new_cons | LowtagOf(object);
3308    
3309     /* Grab the cdr before it is clobbered */
3310     cdr = cons->cdr;
3311 dtc 1.1
3312 rtoy 1.66 /* Set forwarding pointer (clobbers start of list). */
3313     cons->car = 0x01;
3314     cons->cdr = new_list_pointer;
3315    
3316     /* Try to linearize the list in the cdr direction to help reduce paging. */
3317     while (1) {
3318     lispobj new_cdr;
3319     struct cons *cdr_cons, *new_cdr_cons;
3320 dtc 1.1
3321 rtoy 1.66 if (LowtagOf(cdr) != type_ListPointer || !from_space_p(cdr)
3322     || *((lispobj *) PTR(cdr)) == 0x01)
3323     break;
3324 dtc 1.14
3325 rtoy 1.66 cdr_cons = (struct cons *) PTR(cdr);
3326 dtc 1.14
3327 rtoy 1.66 /* copy 'cdr' */
3328     new_cdr_cons = (struct cons *) gc_quick_alloc(sizeof(struct cons));
3329 dtc 1.14
3330 rtoy 1.66 new_cdr_cons->car = cdr_cons->car;
3331     new_cdr_cons->cdr = cdr_cons->cdr;
3332     new_cdr = (lispobj) new_cdr_cons | LowtagOf(cdr);
3333 dtc 1.14
3334 rtoy 1.66 /* Grab the cdr before it is clobbered */
3335     cdr = cdr_cons->cdr;
3336 dtc 1.14
3337 rtoy 1.66 /* Set forwarding pointer */
3338     cdr_cons->car = 0x01;
3339     cdr_cons->cdr = new_cdr;
3340 dtc 1.14
3341 rtoy 1.66 /*
3342     * Update the cdr of the last cons copied into new space to keep
3343     * the newspace scavenge from having to do it.
3344     */
3345     new_cons->cdr = new_cdr;
3346 dtc 1.14
3347 rtoy 1.66 new_cons = new_cdr_cons;
3348     }
3349 dtc 1.14
3350 rtoy 1.66 return new_list_pointer;
3351 dtc 1.1 }
3352 rtoy 1.66
3353 dtc 1.1
3354     /* Scavenging and Transporting Other Pointers */
3355    
3356     #if DIRECT_SCAV
3357 rtoy 1.66 static int
3358     scav_other_pointer(lispobj * where, lispobj object)
3359 dtc 1.1 {
3360 rtoy 1.66 gc_assert(Pointerp(object));
3361 dtc 1.1
3362 rtoy 1.66 if (from_space_p(object)) {
3363     lispobj first, *first_pointer;
3364    
3365     /*
3366     * Object is a pointer into from space. check to see if it has
3367     * been forwarded.
3368     */
3369     first_pointer = (lispobj *) PTR(object);
3370     first = *first_pointer;
3371    
3372     if (first == 0x01) {
3373     /* Forwarded. */
3374     first = first_pointer[1];
3375     *where = first;
3376     } else {
3377     first = (transother[TypeOf(first)]) (object);
3378    
3379     if (first != object) {
3380     /* Set forwarding pointer */
3381     first_pointer[0] = 0x01;
3382     first_pointer[1] = first;
3383     *where = first;
3384     }
3385     }
3386    
3387     gc_assert(Pointerp(first));
3388     gc_assert(!from_space_p(first));
3389     }
3390     return 1;
3391     }
3392     #else
3393     static int
3394     scav_other_pointer(lispobj * where, lispobj object)
3395     {
3396 dtc 1.1 lispobj first, *first_pointer;
3397 dtc 1.14
3398 rtoy 1.66 gc_assert(Pointerp(object));
3399    
3400     /* Object is a pointer into from space - not FP */
3401 dtc 1.1 first_pointer = (lispobj *) PTR(object);
3402 dtc 1.14
3403 rtoy 1.66 first = (transother[TypeOf(*first_pointer)]) (object);
3404 dtc 1.14
3405 rtoy 1.66 if (first != object) {
3406 dtc 1.1 /* Set forwarding pointer */
3407     first_pointer[0] = 0x01;
3408     first_pointer[1] = first;
3409     *where = first;
3410     }
3411 dtc 1.14
3412 dtc 1.1 gc_assert(Pointerp(first));
3413     gc_assert(!from_space_p(first));
3414    
3415 rtoy 1.66 return 1;
3416 dtc 1.1 }
3417     #endif
3418 rtoy 1.66
3419 dtc 1.1
3420     /* Immediate, Boxed, and Unboxed Objects */
3421    
3422 rtoy 1.66 static int
3423     size_pointer(lispobj * where)
3424 dtc 1.1 {
3425     return 1;
3426     }
3427    
3428 rtoy 1.66 static int
3429     scav_immediate(lispobj * where, lispobj object)
3430 dtc 1.1 {
3431     return 1;
3432     }
3433    
3434 rtoy 1.66 static lispobj
3435     trans_immediate(lispobj object)
3436 dtc 1.1 {
3437     fprintf(stderr, "GC lossage. Trying to transport an immediate!?\n");
3438     lose(NULL);
3439     return NIL;
3440     }
3441    
3442 rtoy 1.66 static int
3443     size_immediate(lispobj * where)
3444 dtc 1.1 {
3445     return 1;
3446     }
3447    
3448    
3449 rtoy 1.66 static int
3450     scav_boxed(lispobj * where, lispobj object)
3451 dtc 1.1 {
3452     return 1;
3453     }
3454    
3455 rtoy 1.66 static lispobj
3456     trans_boxed(lispobj object)
3457 dtc 1.1 {
3458 rtoy 1.66 lispobj header;
3459     unsigned long length;
3460 dtc 1.1
3461 rtoy 1.66 gc_assert(Pointerp(object));
3462 dtc 1.1
3463 rtoy 1.66 header = *((lispobj *) PTR(object));
3464     length = HeaderValue(header) + 1;
3465     length = CEILING(length, 2);
3466 dtc 1.1
3467 rtoy 1.66 return copy_object(object, length);
3468 dtc 1.1 }
3469    
3470 rtoy 1.66 static lispobj
3471     trans_boxed_large(lispobj object)
3472 dtc 1.1 {
3473 rtoy 1.66 lispobj header;
3474     unsigned long length;
3475 dtc 1.1
3476 rtoy 1.66 gc_assert(Pointerp(object));
3477 dtc 1.1
3478 rtoy 1.66 header = *((lispobj *) PTR(object));
3479     length = HeaderValue(header) + 1;
3480     length = CEILING(length, 2);
3481 dtc 1.1
3482 rtoy 1.66 return copy_large_object(object, length);
3483 dtc 1.1 }
3484    
3485 rtoy 1.66 static int
3486     size_boxed(lispobj * where)
3487 dtc 1.1 {
3488 rtoy 1.66 lispobj header;
3489     unsigned long length;
3490 dtc 1.1
3491 rtoy 1.66 header = *where;
3492     length = HeaderValue(header) + 1;
3493     length = CEILING(length, 2);
3494 dtc 1.1
3495 rtoy 1.66 return length;
3496 dtc 1.1 }
3497    
3498 rtoy 1.67 /* Not needed on sparc and ppc because the raw_addr has a function lowtag */
3499 cshapiro 1.87 #if !(defined(sparc) || (defined(DARWIN) && defined(__ppc__)))
3500 rtoy 1.66 static int
3501     scav_fdefn(lispobj * where, lispobj object)
3502 dtc 1.1 {
3503 rtoy 1.66 struct fdefn *fdefn;
3504 dtc 1.14
3505 rtoy 1.66 fdefn = (struct fdefn *) where;
3506 dtc 1.14
3507 rtoy 1.66 if ((char *) (fdefn->function + RAW_ADDR_OFFSET) == fdefn->raw_addr) {
3508     scavenge(where + 1, sizeof(struct fdefn) / sizeof(lispobj) - 1);
3509 dtc 1.14
3510 rtoy 1.66 /* Don't write unnecessarily */
3511     if (fdefn->raw_addr != (char *) (fdefn->function + RAW_ADDR_OFFSET))
3512     fdefn->raw_addr = (char *) (fdefn->function + RAW_ADDR_OFFSET);
3513 dtc 1.14
3514 rtoy 1.66 return sizeof(struct fdefn) / sizeof(lispobj);
3515     } else
3516     return 1;
3517 dtc 1.1 }
3518 rtoy 1.57 #endif
3519 dtc 1.1
3520 rtoy 1.66 static int
3521     scav_unboxed(lispobj * where, lispobj object)
3522 dtc 1.1 {
3523 rtoy 1.66 unsigned long length;
3524 dtc 1.1
3525 rtoy 1.66 length = HeaderValue(object) + 1;
3526     length = CEILING(length, 2);
3527 dtc 1.1
3528 rtoy 1.66 return length;
3529 dtc 1.1 }
3530    
3531 rtoy 1.66 static lispobj
3532     trans_unboxed(lispobj object)
3533 dtc 1.1 {
3534 rtoy 1.66 lispobj header;
3535     unsigned long length;
3536 dtc 1.1
3537    
3538 rtoy 1.66 gc_assert(Pointerp(object));
3539 dtc 1.1
3540 rtoy 1.66 header = *((lispobj *) PTR(object));
3541     length = HeaderValue(header) + 1;
3542     length = CEILING(length, 2);
3543 dtc 1.1
3544 rtoy 1.66 return copy_unboxed_object(object, length);
3545 dtc 1.1 }
3546    
3547 rtoy 1.66 static lispobj
3548     trans_unboxed_large(lispobj object)
3549 dtc 1.1 {
3550 rtoy 1.66 lispobj header;
3551     unsigned long length;
3552 dtc 1.1
3553    
3554 rtoy 1.66 gc_assert(Pointerp(object));
3555 dtc 1.1
3556 rtoy 1.66 header = *((lispobj *) PTR(object));
3557     length = HeaderValue(header) + 1;
3558     length = CEILING(length, 2);
3559 dtc 1.1
3560 rtoy 1.66 return copy_large_unboxed_object(object, length);
3561 dtc 1.1 }
3562    
3563 rtoy 1.66 static int
3564     size_unboxed(lispobj * where)
3565 dtc 1.1 {
3566 rtoy 1.66 lispobj header;
3567     unsigned long length;
3568 dtc 1.1
3569 rtoy 1.66 header = *where;
3570     length = HeaderValue(header) + 1;
3571     length = CEILING(length, 2);
3572 dtc 1.1
3573 rtoy 1.66 return length;
3574 dtc 1.1 }
3575 rtoy 1.66
3576 dtc 1.1
3577     /* Vector-Like Objects */
3578    
3579     #define NWORDS(x,y) (CEILING((x),(y)) / (y))
3580    
3581 rtoy 1.66 static int
3582     size_string(lispobj * where)
3583 dtc 1.1 {
3584 rtoy 1.66 struct vector *vector;
3585     int length, nwords;
3586 dtc 1.1
3587 rtoy 1.66 /*
3588     * NOTE: Strings contain one more byte of data than the length
3589     * slot indicates.
3590     */
3591 dtc 1.1
3592 rtoy 1.66 vector = (struct vector *) where;
3593     length = fixnum_value(vector->length) + 1;
3594 rtoy 1.95.2.1 #ifndef UNICODE
3595 cwang 1.55 #ifdef __x86_64
3596 rtoy 1.66 nwords = CEILING(NWORDS(length, 8) + 2, 2);
3597 cwang 1.55 #else
3598 rtoy 1.66 nwords = CEILING(NWORDS(length, 4) + 2, 2);
3599 cwang 1.55 #endif
3600 rtoy 1.95.2.1 #else
3601     /*
3602     * Strings are just like arrays with 16-bit elements, and contain
3603     * one more element than the slot length indicates.
3604     */
3605     nwords = CEILING(NWORDS(length, 2) + 2, 2);
3606     #endif
3607 rtoy 1.66 return nwords;
3608 dtc 1.1 }
3609    
3610 rtoy 1.66 static int
3611     scav_string(lispobj * where, lispobj object)
3612 dtc 1.1 {
3613 rtoy 1.66 return size_string(where);
3614 dtc 1.1 }
3615    
3616 rtoy 1.66 static lispobj
3617     trans_string(lispobj object)
3618 dtc 1.1 {
3619 rtoy 1.66 gc_assert(Pointerp(object));
3620     return copy_large_unboxed_object(object,
3621     size_string((lispobj *) PTR(object)));
3622 dtc 1.1 }
3623 rtoy 1.66
3624 dtc 1.1
3625 gerd 1.32 /************************************************************************
3626     Hash Tables
3627     ************************************************************************/
3628    
3629     /* This struct corresponds to the Lisp HASH-TABLE structure defined in
3630     hash-new.lisp. */
3631    
3632 rtoy 1.66 struct hash_table {
3633     lispobj instance_header; /* 0 */
3634     lispobj dummy2;
3635     lispobj test;
3636     lispobj test_fun;
3637     lispobj hash_fun;
3638     lispobj rehash_size; /* 5 */
3639     lispobj rehash_threshold;
3640     lispobj rehash_trigger;
3641     lispobj number_entries;
3642     lispobj table;
3643     lispobj weak_p; /* 10 */
3644     lispobj needing_rehash;
3645     lispobj next_free_kv;
3646     lispobj index_vector;
3647     lispobj next_vector;
3648     lispobj hash_vector; /* 15 */
3649 rtoy 1.79 lispobj next_weak_table;
3650 gerd 1.32 };
3651    
3652     /* The size of a hash-table in Lisp objects. */
3653 dtc 1.1
3654 gerd 1.32 #define HASH_TABLE_SIZE (sizeof (struct hash_table) / sizeof (lispobj))
3655 dtc 1.1
3656 gerd 1.32 /* Compute the EQ-hash of KEY. This must be the same as what's used
3657     in hash-new.lisp. */
3658 dtc 1.1
3659 gerd 1.32 #define EQ_HASH(key) ((key) & 0x1fffffff)
3660 dtc 1.1
3661 gerd 1.32 /* List of weak hash tables chained through their WEAK-P slot. Set to
3662     NIL at the start of a collection.
3663 rtoy 1.75
3664 gerd