18dc9fbdb1c47316827f4941577f377cf87706f5
[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 void gc_free_heap(void);
23 inline int find_page_index(void *);
24 inline void *page_address(int);
25 int gencgc_handle_wp_violation(void *);
26 lispobj *search_dynamic_space(lispobj *);
27 \f
28 struct page {
29
30     unsigned
31         /* This is set when the page is write-protected. This should
32          * always reflect the actual write_protect status of a page.
33          * (If the page is written into, we catch the exception, make
34          * the page writable, and clear this flag.) */
35         write_protected :1,
36         /* This flag is set when the above write_protected flag is 
37          * cleared by the SIGBUS handler (or SIGSEGV handler, for some
38          * OSes). This is useful for re-scavenging pages that are
39          * written during a GC. */
40         write_protected_cleared :1,
41         /* the region the page is allocated to: 0 for a free page; 1
42          * for boxed objects; 2 for unboxed objects. If the page is
43          * free the following slots are invalid (well the bytes_used
44          * must be 0). */
45         allocated :2,
46         /* If this page should not be moved during a GC then this flag
47          * is set. It's only valid during a GC for allocated pages. */
48         dont_move :1,
49         /* If the page is part of a large object then this flag is
50          * set. No other objects should be allocated to these pages.
51          * This is only valid when the page is allocated. */
52         large_object :1;
53
54     /* the generation that this page belongs to. This should be valid
55      * for all pages that may have objects allocated, even current
56      * allocation region pages - this allows the space of an object to
57      * be easily determined. */
58     int  gen;
59
60     /* the number of bytes of this page that are used. This may be less
61      * than the actual bytes used for pages within the current
62      * allocation regions. It should be 0 for all unallocated pages (not
63      * hard to achieve). */
64     int  bytes_used;
65
66     /* It is important to know the offset to the first object in the
67      * page. Currently it's only important to know if an object starts
68      * at the beginning of the page in which case the offset would be 0. */
69     int  first_object_offset;
70 };
71
72 /* values for the page.allocated field */
73
74 \f
75 /* the number of pages needed for the dynamic space - rounding up */
76 #define NUM_PAGES ((DYNAMIC_SPACE_SIZE+4095)/4096)
77 extern struct page page_table[NUM_PAGES];
78 \f
79 /* Abstract out the data for an allocation region allowing a single
80  * routine to be used for allocation and closing. */
81 struct alloc_region {
82
83     /* These two are needed for quick allocation. */
84     void  *free_pointer;
85     void  *end_addr; /* pointer to the byte after the last usable byte */
86
87     /* These are needed when closing the region. */
88     int  first_page;
89     int  last_page;
90     void  *start_addr;
91 };
92
93 extern struct alloc_region  boxed_region;
94 extern struct alloc_region  unboxed_region;
95 extern int from_space, new_space;
96 extern struct weak_pointer *weak_pointers;
97
98 extern void *current_region_free_pointer;
99 extern void *current_region_end_addr;
100
101 \f
102 void  gencgc_pickup_dynamic(void);
103
104 void sniff_code_object(struct code *code, unsigned displacement);
105 void gencgc_apply_code_fixups(struct code *old_code, struct code *new_code);
106
107 int  update_x86_dynamic_space_free_pointer(void);
108 void  gc_alloc_update_page_tables(int unboxed,
109                                   struct alloc_region *alloc_region);
110 /*
111  * predicates
112  */
113 static inline int 
114 space_matches_p(lispobj obj, int space)
115 {
116     int page_index=(void*)obj - (void *)DYNAMIC_SPACE_START;
117     return ((page_index >= 0)
118             && ((page_index = ((unsigned int)page_index)/4096) < NUM_PAGES)
119             && (page_table[page_index].gen == space));
120 }
121
122 static inline boolean
123 from_space_p(lispobj obj)
124 {
125     return space_matches_p(obj,from_space);
126 }
127
128 static inline boolean
129 new_space_p(lispobj obj)
130 {
131     return space_matches_p(obj,new_space);
132 }
133
134
135
136 #endif