Move control-stack scavenging to gc-common.c.
[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 #include <genesis/simple-fun.h>
20 #include "thread.h"
21
22 /* disabling gc assertions made no discernable difference to GC speed,
23  * last I tried it - dan 2003.12.21
24  *
25  * And it's unsafe to do so while things like gc_assert(0 ==
26  * thread_mutex_lock(&allocation_lock)) exist. - MG 2009-01-13
27  */
28 #if 1
29 # define gc_assert(ex)                                                 \
30 do {                                                                   \
31     if (!(ex)) gc_abort();                                             \
32 } while (0)
33 # define gc_assert_verbose(ex, fmt, ...)                               \
34 do {                                                                   \
35     if (!(ex)) {                                                       \
36         fprintf(stderr, fmt, ## __VA_ARGS__);                          \
37         gc_abort();                                                    \
38     }                                                                  \
39 } while (0)
40 #else
41 # define gc_assert(ex)
42 # define gc_assert_verbose(ex, fmt, ...)
43 #endif
44
45 #define gc_abort()                                                     \
46   lose("GC invariant lost, file \"%s\", line %d\n", __FILE__, __LINE__)
47
48 #define CEILING(x,y) (((x) + ((y) - 1)) & (~((y) - 1)))
49
50 static inline unsigned long
51 NWORDS(unsigned long x, unsigned long n_bits)
52 {
53     /* A good compiler should be able to constant-fold this whole thing,
54        even with the conditional. */
55     if(n_bits <= N_WORD_BITS) {
56         unsigned long elements_per_word = N_WORD_BITS/n_bits;
57
58         return CEILING(x, elements_per_word)/elements_per_word;
59     }
60     else {
61         /* FIXME: should have some sort of assertion that N_WORD_BITS
62            evenly divides n_bits */
63         return x * (n_bits/N_WORD_BITS);
64     }
65 }
66
67 /* FIXME: Shouldn't this be defined in sbcl.h? */
68
69 #if defined(LISP_FEATURE_SPARC)
70 #define FUN_RAW_ADDR_OFFSET 0
71 #else
72 #define FUN_RAW_ADDR_OFFSET (offsetof(struct simple_fun, code) - FUN_POINTER_LOWTAG)
73 #endif
74
75 /* values for the *_alloc_* parameters */
76 #define FREE_PAGE_FLAG 0
77 #define BOXED_PAGE_FLAG 1
78 #define UNBOXED_PAGE_FLAG 2
79 #define OPEN_REGION_PAGE_FLAG 4
80 #define CODE_PAGE_FLAG        (BOXED_PAGE_FLAG|UNBOXED_PAGE_FLAG)
81
82 #define ALLOC_BOXED 0
83 #define ALLOC_UNBOXED 1
84 #define ALLOC_QUICK 1
85
86 #ifdef LISP_FEATURE_GENCGC
87 #include "gencgc-alloc-region.h"
88 void *
89 gc_alloc_with_region(long nbytes,int page_type_flag, struct alloc_region *my_region,
90                      int quick_p);
91 static inline void *
92 gc_general_alloc(long nbytes, int page_type_flag, int quick_p)
93 {
94     struct alloc_region *my_region;
95     if (UNBOXED_PAGE_FLAG == page_type_flag) {
96         my_region = &unboxed_region;
97     } else if (BOXED_PAGE_FLAG & page_type_flag) {
98         my_region = &boxed_region;
99     } else {
100         lose("bad page type flag: %d", page_type_flag);
101     }
102     return gc_alloc_with_region(nbytes, page_type_flag, my_region, quick_p);
103 }
104 #else
105 extern void *gc_general_alloc(long nbytes,int page_type_flag,int quick_p);
106 #endif
107
108 extern long (*scavtab[256])(lispobj *where, lispobj object);
109 extern lispobj (*transother[256])(lispobj object);
110 extern long (*sizetab[256])(lispobj *where);
111
112 extern struct weak_pointer *weak_pointers; /* in gc-common.c */
113 extern struct hash_table *weak_hash_tables; /* in gc-common.c */
114
115 extern void scavenge(lispobj *start, long n_words);
116 extern void scavenge_interrupt_contexts(struct thread *thread);
117 extern void scav_weak_hash_tables(void);
118 extern void scan_weak_hash_tables(void);
119 extern void scan_weak_pointers(void);
120
121 lispobj  copy_large_unboxed_object(lispobj object, long nwords);
122 lispobj  copy_unboxed_object(lispobj object, long nwords);
123 lispobj  copy_large_object(lispobj object, long nwords);
124 lispobj  copy_object(lispobj object, long nwords);
125 lispobj  copy_code_object(lispobj object, long nwords);
126
127 lispobj *search_read_only_space(void *pointer);
128 lispobj *search_static_space(void *pointer);
129 lispobj *search_dynamic_space(void *pointer);
130
131 lispobj *gc_search_space(lispobj *start, size_t words, lispobj *pointer);
132
133 extern int looks_like_valid_lisp_pointer_p(lispobj pointer, lispobj *start_addr);
134
135 extern void scavenge_control_stack(struct thread *th);
136 extern void scrub_control_stack();
137
138 #include "fixnump.h"
139
140 #ifdef LISP_FEATURE_GENCGC
141 #include "gencgc-internal.h"
142 #else
143 #include "cheneygc-internal.h"
144 #endif
145
146 #if N_WORD_BITS == 32
147 # define SIMPLE_ARRAY_WORD_WIDETAG SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG
148 #elif N_WORD_BITS == 64
149 # define SIMPLE_ARRAY_WORD_WIDETAG SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG
150 #endif
151
152 #endif /* _GC_INTERNAL_H_ */