Deobfuscate pointer preservation in gencgc a tiny bit.
[sbcl.git] / src / runtime / gc-internal.h
blob44802af08ce7f84c82d82fee996424fe046d7077
1 /*
2 * garbage collection - shared definitions for modules "inside" the GC system
3 */
5 /*
6 * This software is part of the SBCL system. See the README file for
7 * more information.
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.
16 #ifndef _GC_INTERNAL_H_
17 #define _GC_INTERNAL_H_
19 #include "genesis/simple-fun.h"
20 #include "thread.h"
21 #include "interr.h"
23 #ifdef LISP_FEATURE_GENCGC
24 #include "gencgc-internal.h"
25 #else
26 #include "cheneygc-internal.h"
27 #endif
29 /* disabling gc assertions made no discernable difference to GC speed,
30 * last I tried it - dan 2003.12.21
32 * And it's unsafe to do so while things like gc_assert(0 ==
33 * thread_mutex_lock(&allocation_lock)) exist. - MG 2009-01-13
35 #if 1
36 # define gc_assert(ex) \
37 do { \
38 if (!(ex)) gc_abort(); \
39 } while (0)
40 # define gc_assert_verbose(ex, fmt, ...) \
41 do { \
42 if (!(ex)) { \
43 fprintf(stderr, fmt, ## __VA_ARGS__); \
44 gc_abort(); \
45 } \
46 } while (0)
47 #else
48 # define gc_assert(ex)
49 # define gc_assert_verbose(ex, fmt, ...)
50 #endif
52 #define gc_abort() \
53 lose("GC invariant lost, file \"%s\", line %d\n", __FILE__, __LINE__)
55 #define CEILING(x,y) (((x) + ((y) - 1)) & (~((y) - 1)))
57 static inline uword_t
58 NWORDS(uword_t x, uword_t n_bits)
60 /* A good compiler should be able to constant-fold this whole thing,
61 even with the conditional. */
62 if(n_bits <= N_WORD_BITS) {
63 uword_t elements_per_word = N_WORD_BITS/n_bits;
65 return CEILING(x, elements_per_word)/elements_per_word;
67 else {
68 /* FIXME: should have some sort of assertion that N_WORD_BITS
69 evenly divides n_bits */
70 return x * (n_bits/N_WORD_BITS);
74 /* FIXME: Shouldn't this be defined in sbcl.h? */
76 #if defined(LISP_FEATURE_SPARC) || defined(LISP_FEATURE_ARM)
77 #define FUN_RAW_ADDR_OFFSET 0
78 #else
79 #define FUN_RAW_ADDR_OFFSET (offsetof(struct simple_fun, code) - FUN_POINTER_LOWTAG)
80 #endif
82 #define SIMPLE_FUN_SCAV_START(fun_ptr) &fun_ptr->name
83 #define SIMPLE_FUN_SCAV_NWORDS(fun_ptr) ((lispobj*)fun_ptr->code - &fun_ptr->name)
85 /* values for the *_alloc_* parameters, also see the commentary for
86 * struct page in gencgc-internal.h. FIXME: Perhaps these constants
87 * should be there, or at least defined on gencgc only? */
88 #define FREE_PAGE_FLAG 0
89 #define BOXED_PAGE_FLAG 1
90 #define UNBOXED_PAGE_FLAG 2
91 #define OPEN_REGION_PAGE_FLAG 4
92 #define CODE_PAGE_FLAG (BOXED_PAGE_FLAG|UNBOXED_PAGE_FLAG)
94 #define ALLOC_BOXED 0
95 #define ALLOC_UNBOXED 1
96 #define ALLOC_QUICK 1
98 #ifdef LISP_FEATURE_GENCGC
99 #include "gencgc-alloc-region.h"
100 void *
101 gc_alloc_with_region(sword_t nbytes,int page_type_flag, struct alloc_region *my_region,
102 int quick_p);
103 static inline void *
104 gc_general_alloc(sword_t nbytes, int page_type_flag, int quick_p)
106 struct alloc_region *my_region;
107 if (UNBOXED_PAGE_FLAG == page_type_flag) {
108 my_region = &unboxed_region;
109 } else if (BOXED_PAGE_FLAG & page_type_flag) {
110 my_region = &boxed_region;
111 } else {
112 lose("bad page type flag: %d", page_type_flag);
114 return gc_alloc_with_region(nbytes, page_type_flag, my_region, quick_p);
116 #else
117 extern void *gc_general_alloc(word_t nbytes,int page_type_flag,int quick_p);
118 #endif
120 static inline lispobj
121 gc_general_copy_object(lispobj object, long nwords, int page_type_flag)
123 lispobj *new;
125 gc_assert(is_lisp_pointer(object));
126 gc_assert(from_space_p(object));
127 gc_assert((nwords & 0x01) == 0);
129 /* Allocate space. */
130 new = gc_general_alloc(nwords*N_WORD_BYTES, page_type_flag, ALLOC_QUICK);
132 /* Copy the object. */
133 memcpy(new,native_pointer(object),nwords*N_WORD_BYTES);
135 return make_lispobj(new, lowtag_of(object));
138 extern sword_t (*scavtab[256])(lispobj *where, lispobj object);
139 extern lispobj (*transother[256])(lispobj object);
140 extern sword_t (*sizetab[256])(lispobj *where);
142 extern struct weak_pointer *weak_pointers; /* in gc-common.c */
143 extern struct hash_table *weak_hash_tables; /* in gc-common.c */
145 extern void scavenge(lispobj *start, sword_t n_words);
146 extern void scavenge_interrupt_contexts(struct thread *thread);
147 extern void scav_weak_hash_tables(void);
148 extern void scan_weak_hash_tables(void);
149 extern void scan_weak_pointers(void);
151 lispobj copy_large_unboxed_object(lispobj object, sword_t nwords);
152 lispobj copy_unboxed_object(lispobj object, sword_t nwords);
153 lispobj copy_large_object(lispobj object, sword_t nwords);
154 lispobj copy_object(lispobj object, sword_t nwords);
155 lispobj copy_code_object(lispobj object, sword_t nwords);
157 lispobj *search_read_only_space(void *pointer);
158 lispobj *search_static_space(void *pointer);
159 lispobj *search_immobile_space(void *pointer);
160 lispobj *search_dynamic_space(void *pointer);
162 lispobj *gc_search_space(lispobj *start, size_t words, lispobj *pointer);
164 extern int properly_tagged_descriptor_p(lispobj pointer, lispobj *start_addr);
166 extern void scavenge_control_stack(struct thread *th);
167 extern void scrub_control_stack(void);
168 extern void scrub_thread_control_stack(struct thread *);
170 #include "fixnump.h"
172 #ifdef LISP_FEATURE_GENCGC
173 #include "gencgc-internal.h"
174 #else
175 #include "cheneygc-internal.h"
176 #endif
178 #if N_WORD_BITS == 32
179 # define SIMPLE_ARRAY_WORD_WIDETAG SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG
180 #elif N_WORD_BITS == 64
181 # define SIMPLE_ARRAY_WORD_WIDETAG SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG
182 #endif
184 extern void
185 instance_scan_interleaved(void (*proc)(),
186 lispobj *instance_ptr,
187 sword_t n_words,
188 lispobj *layout_obj);
190 // Generalization of instance_scan_interleaved
191 #define BIT_SCAN_INVERT 1
192 #define BIT_SCAN_CLEAR 2
193 typedef uword_t in_use_marker_t;
194 extern void
195 bitmap_scan(in_use_marker_t* bitmap, int n_bitmap_words, int flags,
196 void (*proc)(void*, int, int), void* arg);
198 #ifdef LISP_FEATURE_IMMOBILE_SPACE
200 static inline boolean immobile_space_p(lispobj obj)
202 return IMMOBILE_SPACE_START <= obj && obj < IMMOBILE_SPACE_END;
205 // Note that find_page_index is in gencgc,
206 // but because this is inline and needed by 2 files, it's in a header.
207 typedef int low_page_index_t;
208 static inline low_page_index_t find_immobile_page_index(void *addr)
210 if (addr >= (void*)IMMOBILE_SPACE_START) {
211 // Must use full register size here to avoid truncation of quotient
212 // and bogus result!
213 page_index_t index =
214 ((pointer_sized_uint_t)addr -
215 (pointer_sized_uint_t)IMMOBILE_SPACE_START) / IMMOBILE_CARD_BYTES;
216 if (index < (int)(IMMOBILE_SPACE_SIZE/IMMOBILE_CARD_BYTES))
217 return index;
219 return -1;
221 int immobile_obj_younger_p(lispobj,generation_index_t);
222 void promote_immobile_obj(lispobj*,int);
224 // Maximum number of boxed words in a code component
225 #define CODE_HEADER_COUNT_MASK 0xFFFFFF
227 #define IMMOBILE_OBJ_VISITED_FLAG 0x10
228 #define IMMOBILE_OBJ_GENERATION_MASK 0x0f // mask off the VISITED flag
230 #define IMMOBILE_VARYOBJ_SUBSPACE_START (IMMOBILE_SPACE_START+IMMOBILE_FIXEDOBJ_SUBSPACE_SIZE)
232 // Note: this does not work on a SIMPLE-FUN
233 // because a simple-fun header does not contain a generation.
234 #define __immobile_obj_generation(x) (__immobile_obj_gen_bits(x) & IMMOBILE_OBJ_GENERATION_MASK)
236 static inline struct code *code_obj_from_simple_fun(struct simple_fun *fun)
238 // The upper 4 bytes of any function header will point to its layout,
239 // so mask those bytes off.
240 uword_t offset = (HeaderValue(fun->header) & CODE_HEADER_COUNT_MASK)
241 * N_WORD_BYTES;
242 return (struct code *)((uword_t)fun - offset);
245 #ifdef LISP_FEATURE_LITTLE_ENDIAN
246 static inline int immobile_obj_gen_bits(lispobj* pointer) // native pointer
248 if (widetag_of(*pointer) == SIMPLE_FUN_HEADER_WIDETAG)
249 pointer = (lispobj*)code_obj_from_simple_fun((struct simple_fun*)pointer);
250 return ((generation_index_t*)pointer)[3];
252 // Faster way when we know that the object can't be a simple-fun,
253 // such as when walking the immobile space.
254 static inline int __immobile_obj_gen_bits(lispobj* pointer) // native pointer
256 return ((generation_index_t*)pointer)[3];
258 #else
259 #error "Need to define immobile_obj_gen_bits() for big-endian"
260 #endif /* little-endian */
262 static inline boolean immobile_filler_p(lispobj* obj) {
263 return *(int*)obj == (2<<N_WIDETAG_BITS | CODE_HEADER_WIDETAG);
265 #endif /* immobile space */
267 #endif /* _GC_INTERNAL_H_ */