gencgc: Commentary fix for struct page, field region_start_offset.
[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 void gc_free_heap(void);
28 extern page_index_t find_page_index(void *);
29 extern void *page_address(page_index_t);
30 int gencgc_handle_wp_violation(void *);
31 \f
32
33 #if GENCGC_CARD_BYTES > USHRT_MAX
34 # if GENCGC_CARD_BYTES > UINT_MAX
35 #   error "GENCGC_CARD_BYTES unexpectedly large."
36 # else
37 #   define PAGE_BYTES_FMT "u"
38     typedef unsigned int page_bytes_t;
39 # endif
40 #else
41 # define PAGE_BYTES_FMT "hu"
42   typedef unsigned short page_bytes_t;
43 #endif
44
45 /* Note that this structure is also used from Lisp-side in
46  * src/code/room.lisp, and the Lisp-side structure layout is currently
47  * not groveled from C code but hardcoded. Any changes to the
48  * structure layout need to be also made there.
49  *
50  * FIXME: We should probably just define this structure in Lisp, and
51  * output the C version in genesis. -- JES, 2006-12-30.
52  */
53 struct page {
54     /* This is the offset from the first byte of some object in memory
55      * prior to and no closer than the start of the page to the start
56      * of the page.  Lower values here are better, 0 is ideal.  This
57      * is useful for determining where to start when scanning forward
58      * through a heap page (either for conservative root validation or
59      * for scavenging).
60      */
61     os_vm_size_t region_start_offset;
62
63     /* the number of bytes of this page that are used. This may be less
64      * than the actual bytes used for pages within the current
65      * allocation regions. It should be 0 for all unallocated pages (not
66      * hard to achieve).
67      */
68     page_bytes_t bytes_used;
69
70     unsigned
71         /* This is set when the page is write-protected. This should
72          * always reflect the actual write_protect status of a page.
73          * (If the page is written into, we catch the exception, make
74          * the page writable, and clear this flag.) */
75         write_protected :1,
76         /* This flag is set when the above write_protected flag is
77          * cleared by the SIGBUS handler (or SIGSEGV handler, for some
78          * OSes). This is useful for re-scavenging pages that are
79          * written during a GC. */
80         write_protected_cleared :1,
81         /*  000 free
82          *  ?01 boxed data
83          *  ?10 unboxed data
84          *  ?11 code
85          *  1?? open region
86          *
87          * Constants for this field are defined in gc-internal.h, the
88          * xxx_PAGE_FLAG definitions.
89          *
90          * If the page is free the following slots are invalid, except
91          * for the bytes_used which must be zero. */
92         allocated :3,
93         /* If this page should not be moved during a GC then this flag
94          * is set. It's only valid during a GC for allocated pages. */
95         dont_move :1,
96         /* If the page is part of a large object then this flag is
97          * set. No other objects should be allocated to these pages.
98          * This is only valid when the page is allocated. */
99         large_object :1,
100         /* Cleared if the page is known to contain only zeroes. */
101         need_to_zero :1;
102
103     /* the generation that this page belongs to. This should be valid
104      * for all pages that may have objects allocated, even current
105      * allocation region pages - this allows the space of an object to
106      * be easily determined. */
107     generation_index_t gen;
108 };
109
110
111 /* values for the page.allocated field */
112
113 \f
114 extern page_index_t page_table_pages;
115 extern struct page *page_table;
116
117 \f
118 /* forward declarations */
119
120 void sniff_code_object(struct code *code, os_vm_size_t displacement);
121 void gencgc_apply_code_fixups(struct code *old_code, struct code *new_code);
122
123 sword_t update_dynamic_space_free_pointer(void);
124 void gc_alloc_update_page_tables(int page_type_flag, struct alloc_region *alloc_region);
125 void gc_alloc_update_all_page_tables(void);
126 void gc_set_region_empty(struct alloc_region *region);
127
128 /*
129  * predicates
130  */
131
132 static inline boolean
133 space_matches_p(lispobj obj, generation_index_t space)
134 {
135     if (obj >= DYNAMIC_SPACE_START) {
136         page_index_t page_index=((pointer_sized_uint_t)obj
137                                  - DYNAMIC_SPACE_START) / GENCGC_CARD_BYTES;
138         return ((page_index < page_table_pages) &&
139                 (page_table[page_index].gen == space));
140     } else {
141         return 0;
142     }
143 }
144
145 static inline boolean
146 from_space_p(lispobj obj)
147 {
148     return space_matches_p(obj,from_space);
149 }
150
151 static inline boolean
152 new_space_p(lispobj obj)
153 {
154     return space_matches_p(obj,new_space);
155 }
156
157 extern page_index_t last_free_page;
158 extern boolean gencgc_partial_pickup;
159
160 #endif