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