0.9.8.18:
[sbcl.git] / src / runtime / gencgc-internal.h
1 /*
2  * Generational Conservative Garbage Collector for SBCL x86
3  *
4  * inline functions that gc-common.c needs sight of
5  */
6
7
8 /*
9  * This software is part of the SBCL system. See the README file for
10  * more information.
11  *
12  * This software is derived from the CMU CL system, which was
13  * written at Carnegie Mellon University and released into the
14  * public domain. The software is in the public domain and is
15  * provided with absolutely no warranty. See the COPYING and CREDITS
16  * files for more information.
17  */
18
19 #ifndef _GENCGC_INTERNAL_H_
20 #define _GENCGC_INTERNAL_H_
21
22 #include <limits.h>
23 #include "gc.h"
24 #include "gencgc-alloc-region.h"
25 #include "genesis/code.h"
26
27 #define PAGE_BYTES GENCGC_PAGE_SIZE
28
29 void gc_free_heap(void);
30 inline page_index_t find_page_index(void *);
31 inline void *page_address(page_index_t);
32 int gencgc_handle_wp_violation(void *);
33 \f
34 struct page {
35     /* The name of this field is not well-chosen for its actual use.
36      * This is the offset from the start of the page to the start
37      * of the alloc_region which contains/contained it.  It's negative or 0
38      */
39     long  first_object_offset;
40
41     /* the number of bytes of this page that are used. This may be less
42      * than the actual bytes used for pages within the current
43      * allocation regions. It should be 0 for all unallocated pages (not
44      * hard to achieve).
45      *
46      * Currently declared as an unsigned short to make the struct size
47      * smaller. This means that GENCGC-PAGE-SIZE is constrained to fit
48      * inside a short.
49      */
50     unsigned short bytes_used;
51
52 #if USHRT_MAX < PAGE_BYTES
53 #error "PAGE_BYTES too large"
54 #endif
55
56     unsigned
57         /* This is set when the page is write-protected. This should
58          * always reflect the actual write_protect status of a page.
59          * (If the page is written into, we catch the exception, make
60          * the page writable, and clear this flag.) */
61         write_protected :1,
62         /* This flag is set when the above write_protected flag is
63          * cleared by the SIGBUS handler (or SIGSEGV handler, for some
64          * OSes). This is useful for re-scavenging pages that are
65          * written during a GC. */
66         write_protected_cleared :1,
67         /* the region the page is allocated to: 0 for a free page; 1
68          * for boxed objects; 2 for unboxed objects. If the page is
69          * free the following slots are invalid (well the bytes_used
70          * must be 0). */
71         allocated :3,
72         /* If this page should not be moved during a GC then this flag
73          * is set. It's only valid during a GC for allocated pages. */
74         dont_move :1,
75         /* If the page is part of a large object then this flag is
76          * set. No other objects should be allocated to these pages.
77          * This is only valid when the page is allocated. */
78         large_object :1;
79
80     /* the generation that this page belongs to. This should be valid
81      * for all pages that may have objects allocated, even current
82      * allocation region pages - this allows the space of an object to
83      * be easily determined. */
84     generation_index_t gen;
85 };
86
87
88 /* values for the page.allocated field */
89
90 \f
91 /* the number of pages needed for the dynamic space - rounding up */
92 #define NUM_PAGES ((page_index_t) ((DYNAMIC_SPACE_SIZE+PAGE_BYTES-1)/PAGE_BYTES))
93
94 extern struct page page_table[NUM_PAGES];
95
96 \f
97 /* forward declarations */
98
99 void sniff_code_object(struct code *code, unsigned long displacement);
100 void gencgc_apply_code_fixups(struct code *old_code, struct code *new_code);
101
102 long update_dynamic_space_free_pointer(void);
103 void gc_alloc_update_page_tables(int unboxed,
104                                  struct alloc_region *alloc_region);
105 void gc_alloc_update_all_page_tables(void);
106 void gc_set_region_empty(struct alloc_region *region);
107
108 /*
109  * predicates
110  */
111 static inline boolean
112 space_matches_p(lispobj obj, generation_index_t space)
113 {
114     page_index_t page_index=(void*)obj - (void *)DYNAMIC_SPACE_START;
115     return ((page_index >= 0)
116             && ((page_index =
117                  ((unsigned long)page_index)/PAGE_BYTES) < NUM_PAGES)
118             && (page_table[page_index].gen == space));
119 }
120
121 static inline boolean
122 from_space_p(lispobj obj)
123 {
124     return space_matches_p(obj,from_space);
125 }
126
127 static inline boolean
128 new_space_p(lispobj obj)
129 {
130     return space_matches_p(obj,new_space);
131 }
132
133 extern page_index_t last_free_page;
134 extern boolean gencgc_partial_pickup;
135
136 #endif