cpp: Use #pragma once instead of #ifndef guards
[ghc.git] / includes / rts / storage / Block.h
1 /* -----------------------------------------------------------------------------
2 *
3 * (c) The GHC Team, 1998-1999
4 *
5 * Block structure for the storage manager
6 *
7 * ---------------------------------------------------------------------------*/
8
9 #pragma once
10
11 #include "ghcconfig.h"
12
13 /* The actual block and megablock-size constants are defined in
14 * includes/Constants.h, all constants here are derived from these.
15 */
16
17 /* Block related constants (BLOCK_SHIFT is defined in Constants.h) */
18
19 #if SIZEOF_LONG == SIZEOF_VOID_P
20 #define UNIT 1UL
21 #elif SIZEOF_LONG_LONG == SIZEOF_VOID_P
22 #define UNIT 1ULL
23 #else
24 #error "Size of pointer is suspicious."
25 #endif
26
27 #ifdef CMINUSMINUS
28 #define BLOCK_SIZE (1<<BLOCK_SHIFT)
29 #else
30 #define BLOCK_SIZE (UNIT<<BLOCK_SHIFT)
31 // Note [integer overflow]
32 #endif
33
34 #define BLOCK_SIZE_W (BLOCK_SIZE/sizeof(W_))
35 #define BLOCK_MASK (BLOCK_SIZE-1)
36
37 #define BLOCK_ROUND_UP(p) (((W_)(p)+BLOCK_SIZE-1) & ~BLOCK_MASK)
38 #define BLOCK_ROUND_DOWN(p) ((void *) ((W_)(p) & ~BLOCK_MASK))
39
40 /* Megablock related constants (MBLOCK_SHIFT is defined in Constants.h) */
41
42 #ifdef CMINUSMINUS
43 #define MBLOCK_SIZE (1<<MBLOCK_SHIFT)
44 #else
45 #define MBLOCK_SIZE (UNIT<<MBLOCK_SHIFT)
46 // Note [integer overflow]
47 #endif
48
49 #define MBLOCK_SIZE_W (MBLOCK_SIZE/sizeof(W_))
50 #define MBLOCK_MASK (MBLOCK_SIZE-1)
51
52 #define MBLOCK_ROUND_UP(p) ((void *)(((W_)(p)+MBLOCK_SIZE-1) & ~MBLOCK_MASK))
53 #define MBLOCK_ROUND_DOWN(p) ((void *)((W_)(p) & ~MBLOCK_MASK ))
54
55 /* The largest size an object can be before we give it a block of its
56 * own and treat it as an immovable object during GC, expressed as a
57 * fraction of BLOCK_SIZE.
58 */
59 #define LARGE_OBJECT_THRESHOLD ((uint32_t)(BLOCK_SIZE * 8 / 10))
60
61 /*
62 * Note [integer overflow]
63 *
64 * The UL suffix in BLOCK_SIZE and MBLOCK_SIZE promotes the expression
65 * to an unsigned long, which means that expressions involving these
66 * will be promoted to unsigned long, which makes integer overflow
67 * less likely. Historically, integer overflow in expressions like
68 * (n * BLOCK_SIZE)
69 * where n is int or unsigned int, have caused obscure segfaults in
70 * programs that use large amounts of memory (e.g. #7762, #5086).
71 */
72
73 /* -----------------------------------------------------------------------------
74 * Block descriptor. This structure *must* be the right length, so we
75 * can do pointer arithmetic on pointers to it.
76 */
77
78 /* The block descriptor is 64 bytes on a 64-bit machine, and 32-bytes
79 * on a 32-bit machine.
80 */
81
82 // Note: fields marked with [READ ONLY] must not be modified by the
83 // client of the block allocator API. All other fields can be
84 // freely modified.
85
86 #ifndef CMINUSMINUS
87 typedef struct bdescr_ {
88
89 StgPtr start; // [READ ONLY] start addr of memory
90
91 StgPtr free; // First free byte of memory.
92 // allocGroup() sets this to the value of start.
93 // NB. during use this value should lie
94 // between start and start + blocks *
95 // BLOCK_SIZE. Values outside this
96 // range are reserved for use by the
97 // block allocator. In particular, the
98 // value (StgPtr)(-1) is used to
99 // indicate that a block is unallocated.
100
101 struct bdescr_ *link; // used for chaining blocks together
102
103 union {
104 struct bdescr_ *back; // used (occasionally) for doubly-linked lists
105 StgWord *bitmap; // bitmap for marking GC
106 StgPtr scan; // scan pointer for copying GC
107 } u;
108
109 struct generation_ *gen; // generation
110
111 StgWord16 gen_no; // gen->no, cached
112 StgWord16 dest_no; // number of destination generation
113 StgWord16 node; // which memory node does this block live on?
114
115 StgWord16 flags; // block flags, see below
116
117 StgWord32 blocks; // [READ ONLY] no. of blocks in a group
118 // (if group head, 0 otherwise)
119
120 #if SIZEOF_VOID_P == 8
121 StgWord32 _padding[3];
122 #else
123 StgWord32 _padding[0];
124 #endif
125 } bdescr;
126 #endif
127
128 #if SIZEOF_VOID_P == 8
129 #define BDESCR_SIZE 0x40
130 #define BDESCR_MASK 0x3f
131 #define BDESCR_SHIFT 6
132 #else
133 #define BDESCR_SIZE 0x20
134 #define BDESCR_MASK 0x1f
135 #define BDESCR_SHIFT 5
136 #endif
137
138 /* Block contains objects evacuated during this GC */
139 #define BF_EVACUATED 1
140 /* Block is a large object */
141 #define BF_LARGE 2
142 /* Block is pinned */
143 #define BF_PINNED 4
144 /* Block is to be marked, not copied */
145 #define BF_MARKED 8
146 /* Block is free, and on the free list (TODO: is this used?) */
147 #define BF_FREE 16
148 /* Block is executable */
149 #define BF_EXEC 32
150 /* Block contains only a small amount of live data */
151 #define BF_FRAGMENTED 64
152 /* we know about this block (for finding leaks) */
153 #define BF_KNOWN 128
154 /* Block was swept in the last generation */
155 #define BF_SWEPT 256
156 /* Block is part of a Compact */
157 #define BF_COMPACT 512
158 /* Maximum flag value (do not define anything higher than this!) */
159 #define BF_FLAG_MAX (1 << 15)
160
161 /* Finding the block descriptor for a given block -------------------------- */
162
163 #ifdef CMINUSMINUS
164
165 #define Bdescr(p) \
166 ((((p) & MBLOCK_MASK & ~BLOCK_MASK) >> (BLOCK_SHIFT-BDESCR_SHIFT)) \
167 | ((p) & ~MBLOCK_MASK))
168
169 #else
170
171 EXTERN_INLINE bdescr *Bdescr(StgPtr p);
172 EXTERN_INLINE bdescr *Bdescr(StgPtr p)
173 {
174 return (bdescr *)
175 ((((W_)p & MBLOCK_MASK & ~BLOCK_MASK) >> (BLOCK_SHIFT-BDESCR_SHIFT))
176 | ((W_)p & ~MBLOCK_MASK)
177 );
178 }
179
180 #endif
181
182 /* Useful Macros ------------------------------------------------------------ */
183
184 /* Offset of first real data block in a megablock */
185
186 #define FIRST_BLOCK_OFF \
187 ((W_)BLOCK_ROUND_UP(BDESCR_SIZE * (MBLOCK_SIZE / BLOCK_SIZE)))
188
189 /* First data block in a given megablock */
190
191 #define FIRST_BLOCK(m) ((void *)(FIRST_BLOCK_OFF + (W_)(m)))
192
193 /* Last data block in a given megablock */
194
195 #define LAST_BLOCK(m) ((void *)(MBLOCK_SIZE-BLOCK_SIZE + (W_)(m)))
196
197 /* First real block descriptor in a megablock */
198
199 #define FIRST_BDESCR(m) \
200 ((bdescr *)((FIRST_BLOCK_OFF>>(BLOCK_SHIFT-BDESCR_SHIFT)) + (W_)(m)))
201
202 /* Last real block descriptor in a megablock */
203
204 #define LAST_BDESCR(m) \
205 ((bdescr *)(((MBLOCK_SIZE-BLOCK_SIZE)>>(BLOCK_SHIFT-BDESCR_SHIFT)) + (W_)(m)))
206
207 /* Number of usable blocks in a megablock */
208
209 #ifndef CMINUSMINUS // already defined in DerivedConstants.h
210 #define BLOCKS_PER_MBLOCK ((MBLOCK_SIZE - FIRST_BLOCK_OFF) / BLOCK_SIZE)
211 #endif
212
213 /* How many blocks in this megablock group */
214
215 #define MBLOCK_GROUP_BLOCKS(n) \
216 (BLOCKS_PER_MBLOCK + (n-1) * (MBLOCK_SIZE / BLOCK_SIZE))
217
218 /* Compute the required size of a megablock group */
219
220 #define BLOCKS_TO_MBLOCKS(n) \
221 (1 + (W_)MBLOCK_ROUND_UP((n-BLOCKS_PER_MBLOCK) * BLOCK_SIZE) / MBLOCK_SIZE)
222
223
224 #ifndef CMINUSMINUS
225 /* to the end... */
226
227 /* Double-linked block lists: --------------------------------------------- */
228
229 INLINE_HEADER void
230 dbl_link_onto(bdescr *bd, bdescr **list)
231 {
232 bd->link = *list;
233 bd->u.back = NULL;
234 if (*list) {
235 (*list)->u.back = bd; /* double-link the list */
236 }
237 *list = bd;
238 }
239
240 INLINE_HEADER void
241 dbl_link_remove(bdescr *bd, bdescr **list)
242 {
243 if (bd->u.back) {
244 bd->u.back->link = bd->link;
245 } else {
246 *list = bd->link;
247 }
248 if (bd->link) {
249 bd->link->u.back = bd->u.back;
250 }
251 }
252
253 INLINE_HEADER void
254 dbl_link_insert_after(bdescr *bd, bdescr *after)
255 {
256 bd->link = after->link;
257 bd->u.back = after;
258 if (after->link) {
259 after->link->u.back = bd;
260 }
261 after->link = bd;
262 }
263
264 INLINE_HEADER void
265 dbl_link_replace(bdescr *new_, bdescr *old, bdescr **list)
266 {
267 new_->link = old->link;
268 new_->u.back = old->u.back;
269 if (old->link) {
270 old->link->u.back = new_;
271 }
272 if (old->u.back) {
273 old->u.back->link = new_;
274 } else {
275 *list = new_;
276 }
277 }
278
279 /* Initialisation ---------------------------------------------------------- */
280
281 extern void initBlockAllocator(void);
282
283 /* Allocation -------------------------------------------------------------- */
284
285 bdescr *allocGroup(W_ n);
286
287 EXTERN_INLINE bdescr* allocBlock(void);
288 EXTERN_INLINE bdescr* allocBlock(void)
289 {
290 return allocGroup(1);
291 }
292
293 bdescr *allocGroupOnNode(uint32_t node, W_ n);
294
295 EXTERN_INLINE bdescr* allocBlockOnNode(uint32_t node);
296 EXTERN_INLINE bdescr* allocBlockOnNode(uint32_t node)
297 {
298 return allocGroupOnNode(node,1);
299 }
300
301 // versions that take the storage manager lock for you:
302 bdescr *allocGroup_lock(W_ n);
303 bdescr *allocBlock_lock(void);
304
305 bdescr *allocGroupOnNode_lock(uint32_t node, W_ n);
306 bdescr *allocBlockOnNode_lock(uint32_t node);
307
308 /* De-Allocation ----------------------------------------------------------- */
309
310 void freeGroup(bdescr *p);
311 void freeChain(bdescr *p);
312
313 // versions that take the storage manager lock for you:
314 void freeGroup_lock(bdescr *p);
315 void freeChain_lock(bdescr *p);
316
317 bdescr * splitBlockGroup (bdescr *bd, uint32_t blocks);
318
319 /* Round a value to megablocks --------------------------------------------- */
320
321 // We want to allocate an object around a given size, round it up or
322 // down to the nearest size that will fit in an mblock group.
323 INLINE_HEADER StgWord
324 round_to_mblocks(StgWord words)
325 {
326 if (words > BLOCKS_PER_MBLOCK * BLOCK_SIZE_W) {
327 // first, ignore the gap at the beginning of the first mblock by
328 // adding it to the total words. Then we can pretend we're
329 // dealing in a uniform unit of megablocks.
330 words += FIRST_BLOCK_OFF/sizeof(W_);
331
332 if ((words % MBLOCK_SIZE_W) < (MBLOCK_SIZE_W / 2)) {
333 words = (words / MBLOCK_SIZE_W) * MBLOCK_SIZE_W;
334 } else {
335 words = ((words / MBLOCK_SIZE_W) + 1) * MBLOCK_SIZE_W;
336 }
337
338 words -= FIRST_BLOCK_OFF/sizeof(W_);
339 }
340 return words;
341 }
342
343 INLINE_HEADER StgWord
344 round_up_to_mblocks(StgWord words)
345 {
346 words += FIRST_BLOCK_OFF/sizeof(W_);
347 words = ((words / MBLOCK_SIZE_W) + 1) * MBLOCK_SIZE_W;
348 words -= FIRST_BLOCK_OFF/sizeof(W_);
349 return words;
350 }
351
352 #endif /* !CMINUSMINUS */