Implement {resize,shrink}MutableByteArray# primops
[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 #ifndef RTS_STORAGE_BLOCK_H
10 #define RTS_STORAGE_BLOCK_H
11
12 #include "ghcconfig.h"
13
14 /* The actual block and megablock-size constants are defined in
15 * includes/Constants.h, all constants here are derived from these.
16 */
17
18 /* Block related constants (BLOCK_SHIFT is defined in Constants.h) */
19
20 #if SIZEOF_LONG == SIZEOF_VOID_P
21 #define UNIT 1UL
22 #elif SIZEOF_LONG_LONG == SIZEOF_VOID_P
23 #define UNIT 1ULL
24 #else
25 #error "Size of pointer is suspicious."
26 #endif
27
28 #ifdef CMINUSMINUS
29 #define BLOCK_SIZE (1<<BLOCK_SHIFT)
30 #else
31 #define BLOCK_SIZE (UNIT<<BLOCK_SHIFT)
32 // Note [integer overflow]
33 #endif
34
35 #define BLOCK_SIZE_W (BLOCK_SIZE/sizeof(W_))
36 #define BLOCK_MASK (BLOCK_SIZE-1)
37
38 #define BLOCK_ROUND_UP(p) (((W_)(p)+BLOCK_SIZE-1) & ~BLOCK_MASK)
39 #define BLOCK_ROUND_DOWN(p) ((void *) ((W_)(p) & ~BLOCK_MASK))
40
41 /* Megablock related constants (MBLOCK_SHIFT is defined in Constants.h) */
42
43 #ifdef CMINUSMINUS
44 #define MBLOCK_SIZE (1<<MBLOCK_SHIFT)
45 #else
46 #define MBLOCK_SIZE (UNIT<<MBLOCK_SHIFT)
47 // Note [integer overflow]
48 #endif
49
50 #define MBLOCK_SIZE_W (MBLOCK_SIZE/sizeof(W_))
51 #define MBLOCK_MASK (MBLOCK_SIZE-1)
52
53 #define MBLOCK_ROUND_UP(p) ((void *)(((W_)(p)+MBLOCK_SIZE-1) & ~MBLOCK_MASK))
54 #define MBLOCK_ROUND_DOWN(p) ((void *)((W_)(p) & ~MBLOCK_MASK ))
55
56 /* The largest size an object can be before we give it a block of its
57 * own and treat it as an immovable object during GC, expressed as a
58 * fraction of BLOCK_SIZE.
59 */
60 #define LARGE_OBJECT_THRESHOLD ((nat)(BLOCK_SIZE * 8 / 10))
61
62 /*
63 * Note [integer overflow]
64 *
65 * The UL suffix in BLOCK_SIZE and MBLOCK_SIZE promotes the expression
66 * to an unsigned long, which means that expressions involving these
67 * will be promoted to unsigned long, which makes integer overflow
68 * less likely. Historically, integer overflow in expressions like
69 * (n * BLOCK_SIZE)
70 * where n is int or unsigned int, have caused obscure segfaults in
71 * programs that use large amounts of memory (e.g. #7762, #5086).
72 */
73
74 /* -----------------------------------------------------------------------------
75 * Block descriptor. This structure *must* be the right length, so we
76 * can do pointer arithmetic on pointers to it.
77 */
78
79 /* The block descriptor is 64 bytes on a 64-bit machine, and 32-bytes
80 * on a 32-bit machine.
81 */
82
83 // Note: fields marked with [READ ONLY] must not be modified by the
84 // client of the block allocator API. All other fields can be
85 // freely modified.
86
87 #ifndef CMINUSMINUS
88 typedef struct bdescr_ {
89
90 StgPtr start; // [READ ONLY] start addr of memory
91
92 StgPtr free; // first free byte of memory.
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 _pad1;
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
157 /* Finding the block descriptor for a given block -------------------------- */
158
159 #ifdef CMINUSMINUS
160
161 #define Bdescr(p) \
162 ((((p) & MBLOCK_MASK & ~BLOCK_MASK) >> (BLOCK_SHIFT-BDESCR_SHIFT)) \
163 | ((p) & ~MBLOCK_MASK))
164
165 #else
166
167 EXTERN_INLINE bdescr *Bdescr(StgPtr p);
168 EXTERN_INLINE bdescr *Bdescr(StgPtr p)
169 {
170 return (bdescr *)
171 ((((W_)p & MBLOCK_MASK & ~BLOCK_MASK) >> (BLOCK_SHIFT-BDESCR_SHIFT))
172 | ((W_)p & ~MBLOCK_MASK)
173 );
174 }
175
176 #endif
177
178 /* Useful Macros ------------------------------------------------------------ */
179
180 /* Offset of first real data block in a megablock */
181
182 #define FIRST_BLOCK_OFF \
183 ((W_)BLOCK_ROUND_UP(BDESCR_SIZE * (MBLOCK_SIZE / BLOCK_SIZE)))
184
185 /* First data block in a given megablock */
186
187 #define FIRST_BLOCK(m) ((void *)(FIRST_BLOCK_OFF + (W_)(m)))
188
189 /* Last data block in a given megablock */
190
191 #define LAST_BLOCK(m) ((void *)(MBLOCK_SIZE-BLOCK_SIZE + (W_)(m)))
192
193 /* First real block descriptor in a megablock */
194
195 #define FIRST_BDESCR(m) \
196 ((bdescr *)((FIRST_BLOCK_OFF>>(BLOCK_SHIFT-BDESCR_SHIFT)) + (W_)(m)))
197
198 /* Last real block descriptor in a megablock */
199
200 #define LAST_BDESCR(m) \
201 ((bdescr *)(((MBLOCK_SIZE-BLOCK_SIZE)>>(BLOCK_SHIFT-BDESCR_SHIFT)) + (W_)(m)))
202
203 /* Number of usable blocks in a megablock */
204
205 #ifndef CMINUSMINUS // already defined in DerivedConstants.h
206 #define BLOCKS_PER_MBLOCK ((MBLOCK_SIZE - FIRST_BLOCK_OFF) / BLOCK_SIZE)
207 #endif
208
209 /* How many blocks in this megablock group */
210
211 #define MBLOCK_GROUP_BLOCKS(n) \
212 (BLOCKS_PER_MBLOCK + (n-1) * (MBLOCK_SIZE / BLOCK_SIZE))
213
214 /* Compute the required size of a megablock group */
215
216 #define BLOCKS_TO_MBLOCKS(n) \
217 (1 + (W_)MBLOCK_ROUND_UP((n-BLOCKS_PER_MBLOCK) * BLOCK_SIZE) / MBLOCK_SIZE)
218
219
220 #ifndef CMINUSMINUS
221 /* to the end... */
222
223 /* Double-linked block lists: --------------------------------------------- */
224
225 INLINE_HEADER void
226 dbl_link_onto(bdescr *bd, bdescr **list)
227 {
228 bd->link = *list;
229 bd->u.back = NULL;
230 if (*list) {
231 (*list)->u.back = bd; /* double-link the list */
232 }
233 *list = bd;
234 }
235
236 INLINE_HEADER void
237 dbl_link_remove(bdescr *bd, bdescr **list)
238 {
239 if (bd->u.back) {
240 bd->u.back->link = bd->link;
241 } else {
242 *list = bd->link;
243 }
244 if (bd->link) {
245 bd->link->u.back = bd->u.back;
246 }
247 }
248
249 INLINE_HEADER void
250 dbl_link_insert_after(bdescr *bd, bdescr *after)
251 {
252 bd->link = after->link;
253 bd->u.back = after;
254 if (after->link) {
255 after->link->u.back = bd;
256 }
257 after->link = bd;
258 }
259
260 INLINE_HEADER void
261 dbl_link_replace(bdescr *new_, bdescr *old, bdescr **list)
262 {
263 new_->link = old->link;
264 new_->u.back = old->u.back;
265 if (old->link) {
266 old->link->u.back = new_;
267 }
268 if (old->u.back) {
269 old->u.back->link = new_;
270 } else {
271 *list = new_;
272 }
273 }
274
275 /* Initialisation ---------------------------------------------------------- */
276
277 extern void initBlockAllocator(void);
278
279 /* Allocation -------------------------------------------------------------- */
280
281 bdescr *allocGroup(W_ n);
282 bdescr *allocBlock(void);
283
284 // versions that take the storage manager lock for you:
285 bdescr *allocGroup_lock(W_ n);
286 bdescr *allocBlock_lock(void);
287
288 /* De-Allocation ----------------------------------------------------------- */
289
290 void freeGroup(bdescr *p);
291 void freeChain(bdescr *p);
292
293 // versions that take the storage manager lock for you:
294 void freeGroup_lock(bdescr *p);
295 void freeChain_lock(bdescr *p);
296
297 bdescr * splitBlockGroup (bdescr *bd, nat blocks);
298
299 /* Round a value to megablocks --------------------------------------------- */
300
301 // We want to allocate an object around a given size, round it up or
302 // down to the nearest size that will fit in an mblock group.
303 INLINE_HEADER StgWord
304 round_to_mblocks(StgWord words)
305 {
306 if (words > BLOCKS_PER_MBLOCK * BLOCK_SIZE_W) {
307 // first, ignore the gap at the beginning of the first mblock by
308 // adding it to the total words. Then we can pretend we're
309 // dealing in a uniform unit of megablocks.
310 words += FIRST_BLOCK_OFF/sizeof(W_);
311
312 if ((words % MBLOCK_SIZE_W) < (MBLOCK_SIZE_W / 2)) {
313 words = (words / MBLOCK_SIZE_W) * MBLOCK_SIZE_W;
314 } else {
315 words = ((words / MBLOCK_SIZE_W) + 1) * MBLOCK_SIZE_W;
316 }
317
318 words -= FIRST_BLOCK_OFF/sizeof(W_);
319 }
320 return words;
321 }
322
323 INLINE_HEADER StgWord
324 round_up_to_mblocks(StgWord words)
325 {
326 words += FIRST_BLOCK_OFF/sizeof(W_);
327 words = ((words / MBLOCK_SIZE_W) + 1) * MBLOCK_SIZE_W;
328 words -= FIRST_BLOCK_OFF/sizeof(W_);
329 return words;
330 }
331
332 #endif /* !CMINUSMINUS */
333 #endif /* RTS_STORAGE_BLOCK_H */