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