114f5142eda178b7cf64450b053749b81b37d3c6
[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     /* A good compiler should be able to constant-fold this whole thing,
37        even with the conditional. */
38     if(n_bits <= N_WORD_BITS) {
39         unsigned int elements_per_word = N_WORD_BITS/n_bits;
40
41         return CEILING(x, elements_per_word)/elements_per_word;
42     }
43     else {
44         /* FIXME: should have some sort of assertion that N_WORD_BITS
45            evenly divides n_bits */
46         return x * (n_bits/N_WORD_BITS);
47     }
48 }
49
50 /* FIXME: Shouldn't this be defined in sbcl.h? */
51 #define FUN_RAW_ADDR_OFFSET (6*sizeof(lispobj) - FUN_POINTER_LOWTAG)
52
53 /* values for the *_alloc_* parameters */
54 #define FREE_PAGE_FLAG 0
55 #define BOXED_PAGE_FLAG 1
56 #define UNBOXED_PAGE_FLAG 2
57 #define OPEN_REGION_PAGE_FLAG 4
58
59 #define ALLOC_BOXED 0
60 #define ALLOC_UNBOXED 1
61 #define ALLOC_QUICK 1
62
63 void *gc_general_alloc(int nbytes,int unboxed_p,int quick_p);
64
65 extern int (*scavtab[256])(lispobj *where, lispobj object);
66 extern lispobj (*transother[256])(lispobj object);
67 extern int (*sizetab[256])(lispobj *where);
68
69 extern struct weak_pointer *weak_pointers; /* in gc-common.c */
70
71 extern void scavenge(lispobj *start, long n_words);
72 extern void scan_weak_pointers(void);
73
74 lispobj  copy_large_unboxed_object(lispobj object, int nwords);
75 lispobj  copy_unboxed_object(lispobj object, int nwords);
76 lispobj  copy_large_object(lispobj object, int nwords);
77 lispobj  copy_object(lispobj object, int nwords);
78
79 lispobj *search_read_only_space(void *pointer);
80 lispobj *search_static_space(void *pointer);
81 lispobj *search_dynamic_space(void *pointer);
82
83 #include "fixnump.h"
84
85 /* Scan an area looking for an object which encloses the given pointer.
86  * Return the object start on success or NULL on failure. */
87 static lispobj *
88 search_space(lispobj *start, size_t words, lispobj *pointer)
89 {
90     while (words > 0) {
91         size_t count = 1;
92         lispobj thing = *start;
93
94         /* If thing is an immediate then this is a cons. */
95         if (is_lisp_pointer(thing)
96             || (fixnump(thing))
97             || (widetag_of(thing) == CHARACTER_WIDETAG)
98             || (widetag_of(thing) == UNBOUND_MARKER_WIDETAG))
99             count = 2;
100         else
101             count = (sizetab[widetag_of(thing)])(start);
102
103         /* Check whether the pointer is within this object. */
104         if ((pointer >= start) && (pointer < (start+count))) {
105             /* found it! */
106             /*FSHOW((stderr,"/found %x in %x %x\n", pointer, start, thing));*/
107             return(start);
108         }
109
110         /* Round up the count. */
111         count = CEILING(count,2);
112
113         start += count;
114         words -= count;
115     }
116     return (NULL);
117 }
118
119 #ifdef LISP_FEATURE_GENCGC
120 #include "gencgc-internal.h"
121 #else
122 #include "cheneygc-internal.h"
123 #endif
124
125 #endif /* _GC_INTERNAL_H_ */