8928f2bbc7e81b9380e306cdda0f6edf881dc9e0
[sbcl.git] / src / runtime / gc-internal.h
1 /*
2  * garbage collection - shared definitions for modules "inside" the GC system
3  */
4
5 /*
6  * This software is part of the SBCL system. See the README file for
7  * more information.
8  *
9  * This software is derived from the CMU CL system, which was
10  * written at Carnegie Mellon University and released into the
11  * public domain. The software is in the public domain and is
12  * provided with absolutely no warranty. See the COPYING and CREDITS
13  * files for more information.
14  */
15
16 #ifndef _GC_INTERNAL_H_
17 #define _GC_INTERNAL_H_
18
19 /* disabling gc assertions made no discernable difference to GC speed,
20  * last I tried it - dan 2003.12.21 */
21 #if 1
22 #define gc_assert(ex) do { \
23         if (!(ex)) gc_abort(); \
24 } while (0)
25 #else
26 #define gc_assert(ex)
27 #endif
28 #define gc_abort() lose("GC invariant lost, file \"%s\", line %d", \
29                         __FILE__, __LINE__)
30
31 #define CEILING(x,y) (((x) + ((y) - 1)) & (~((y) - 1)))
32
33 static inline unsigned int
34 NWORDS(unsigned int x, unsigned int n_bits)
35 {
36     unsigned int elements_per_word = N_WORD_BITS/n_bits;
37
38     return CEILING(x, elements_per_word)/elements_per_word;
39 }
40
41 /* FIXME: Shouldn't this be defined in sbcl.h? */
42 #define FUN_RAW_ADDR_OFFSET (6*sizeof(lispobj) - FUN_POINTER_LOWTAG)
43
44 /* values for the *_alloc_* parameters */
45 #define FREE_PAGE_FLAG 0
46 #define BOXED_PAGE_FLAG 1
47 #define UNBOXED_PAGE_FLAG 2
48 #define OPEN_REGION_PAGE_FLAG 4
49
50 #define ALLOC_BOXED 0
51 #define ALLOC_UNBOXED 1
52 #define ALLOC_QUICK 1
53
54 void *gc_general_alloc(int nbytes,int unboxed_p,int quick_p);
55
56 extern int (*scavtab[256])(lispobj *where, lispobj object);
57 extern lispobj (*transother[256])(lispobj object);
58 extern int (*sizetab[256])(lispobj *where);
59
60 extern struct weak_pointer *weak_pointers; /* in gc-common.c */
61
62 extern void scavenge(lispobj *start, long n_words);
63 extern void scan_weak_pointers(void);
64
65 lispobj  copy_large_unboxed_object(lispobj object, int nwords);
66 lispobj  copy_unboxed_object(lispobj object, int nwords);
67 lispobj  copy_large_object(lispobj object, int nwords);
68 lispobj  copy_object(lispobj object, int nwords);
69
70 lispobj *search_read_only_space(void *pointer);
71 lispobj *search_static_space(void *pointer);
72 lispobj *search_dynamic_space(void *pointer);
73
74 /* Scan an area looking for an object which encloses the given pointer.
75  * Return the object start on success or NULL on failure. */
76 static lispobj *
77 search_space(lispobj *start, size_t words, lispobj *pointer)
78 {
79     while (words > 0) {
80         size_t count = 1;
81         lispobj thing = *start;
82
83         /* If thing is an immediate then this is a cons. */
84         if (is_lisp_pointer(thing)
85             || ((thing & 3) == 0) /* fixnum */
86             || (widetag_of(thing) == BASE_CHAR_WIDETAG)
87             || (widetag_of(thing) == UNBOUND_MARKER_WIDETAG))
88             count = 2;
89         else
90             count = (sizetab[widetag_of(thing)])(start);
91
92         /* Check whether the pointer is within this object. */
93         if ((pointer >= start) && (pointer < (start+count))) {
94             /* found it! */
95             /*FSHOW((stderr,"/found %x in %x %x\n", pointer, start, thing));*/
96             return(start);
97         }
98
99         /* Round up the count. */
100         count = CEILING(count,2);
101
102         start += count;
103         words -= count;
104     }
105     return (NULL);
106 }
107
108 #ifdef LISP_FEATURE_GENCGC
109 #include "gencgc-internal.h"
110 #else
111 #include "cheneygc-internal.h"
112 #endif
113
114 #endif /* _GC_INTERNAL_H_ */