1 /* -----------------------------------------------------------------------------
3 * (c) The GHC Team, 1998-1999
5 * Block structure for the storage manager
7 * ---------------------------------------------------------------------------*/
9 #ifndef RTS_STORAGE_BLOCK_H
10 #define RTS_STORAGE_BLOCK_H
12 /* The actual block and megablock-size constants are defined in
13 * includes/Constants.h, all constants here are derived from these.
16 /* Block related constants (BLOCK_SHIFT is defined in Constants.h) */
18 #define BLOCK_SIZE (1<<BLOCK_SHIFT)
19 #define BLOCK_SIZE_W (BLOCK_SIZE/sizeof(W_))
20 #define BLOCK_MASK (BLOCK_SIZE-1)
22 #define BLOCK_ROUND_UP(p) ((void *) (((W_)(p)+BLOCK_SIZE-1) & ~BLOCK_MASK))
23 #define BLOCK_ROUND_DOWN(p) ((void *) ((W_)(p) & ~BLOCK_MASK))
25 /* Megablock related constants (MBLOCK_SHIFT is defined in Constants.h) */
27 #define MBLOCK_SIZE (1<<MBLOCK_SHIFT)
28 #define MBLOCK_SIZE_W (MBLOCK_SIZE/sizeof(W_))
29 #define MBLOCK_MASK (MBLOCK_SIZE-1)
31 #define MBLOCK_ROUND_UP(p) ((void *)(((W_)(p)+MBLOCK_SIZE-1) & ~MBLOCK_MASK))
32 #define MBLOCK_ROUND_DOWN(p) ((void *)((W_)(p) & ~MBLOCK_MASK ))
34 /* The largest size an object can be before we give it a block of its
35 * own and treat it as an immovable object during GC, expressed as a
36 * fraction of BLOCK_SIZE.
38 #define LARGE_OBJECT_THRESHOLD ((nat)(BLOCK_SIZE * 8 / 10))
40 /* -----------------------------------------------------------------------------
41 * Block descriptor. This structure *must* be the right length, so we
42 * can do pointer arithmetic on pointers to it.
45 /* The block descriptor is 64 bytes on a 64-bit machine, and 32-bytes
46 * on a 32-bit machine.
49 // Note: fields marked with [READ ONLY] must not be modified by the
50 // client of the block allocator API. All other fields can be
54 typedef struct bdescr_ {
56 StgPtr start; // [READ ONLY] start addr of memory
58 StgPtr free; // first free byte of memory.
59 // NB. during use this value should lie
60 // between start and start + blocks *
61 // BLOCK_SIZE. Values outside this
62 // range are reserved for use by the
63 // block allocator. In particular, the
64 // value (StgPtr)(-1) is used to
65 // indicate that a block is unallocated.
67 struct bdescr_ *link; // used for chaining blocks together
70 struct bdescr_ *back; // used (occasionally) for doubly-linked lists
71 StgWord *bitmap; // bitmap for marking GC
72 StgPtr scan; // scan pointer for copying GC
75 struct generation_ *gen; // generation
77 StgWord16 gen_no; // gen->no, cached
78 StgWord16 dest_no; // number of destination generation
81 StgWord16 flags; // block flags, see below
83 StgWord32 blocks; // [READ ONLY] no. of blocks in a group
84 // (if group head, 0 otherwise)
86 #if SIZEOF_VOID_P == 8
87 StgWord32 _padding[3];
89 StgWord32 _padding[0];
94 #if SIZEOF_VOID_P == 8
95 #define BDESCR_SIZE 0x40
96 #define BDESCR_MASK 0x3f
97 #define BDESCR_SHIFT 6
99 #define BDESCR_SIZE 0x20
100 #define BDESCR_MASK 0x1f
101 #define BDESCR_SHIFT 5
104 /* Block contains objects evacuated during this GC */
105 #define BF_EVACUATED 1
106 /* Block is a large object */
108 /* Block is pinned */
110 /* Block is to be marked, not copied */
112 /* Block is free, and on the free list (TODO: is this used?) */
114 /* Block is executable */
116 /* Block contains only a small amount of live data */
117 #define BF_FRAGMENTED 64
118 /* we know about this block (for finding leaks) */
120 /* Block was swept in the last generation */
123 /* Finding the block descriptor for a given block -------------------------- */
128 ((((p) & MBLOCK_MASK & ~BLOCK_MASK) >> (BLOCK_SHIFT-BDESCR_SHIFT)) \
129 | ((p) & ~MBLOCK_MASK))
133 EXTERN_INLINE bdescr *Bdescr(StgPtr p);
134 EXTERN_INLINE bdescr *Bdescr(StgPtr p)
137 ((((W_)p & MBLOCK_MASK & ~BLOCK_MASK) >> (BLOCK_SHIFT-BDESCR_SHIFT))
138 | ((W_)p & ~MBLOCK_MASK)
144 /* Useful Macros ------------------------------------------------------------ */
146 /* Offset of first real data block in a megablock */
148 #define FIRST_BLOCK_OFF \
149 ((W_)BLOCK_ROUND_UP(BDESCR_SIZE * (MBLOCK_SIZE / BLOCK_SIZE)))
151 /* First data block in a given megablock */
153 #define FIRST_BLOCK(m) ((void *)(FIRST_BLOCK_OFF + (W_)(m)))
155 /* Last data block in a given megablock */
157 #define LAST_BLOCK(m) ((void *)(MBLOCK_SIZE-BLOCK_SIZE + (W_)(m)))
159 /* First real block descriptor in a megablock */
161 #define FIRST_BDESCR(m) \
162 ((bdescr *)((FIRST_BLOCK_OFF>>(BLOCK_SHIFT-BDESCR_SHIFT)) + (W_)(m)))
164 /* Last real block descriptor in a megablock */
166 #define LAST_BDESCR(m) \
167 ((bdescr *)(((MBLOCK_SIZE-BLOCK_SIZE)>>(BLOCK_SHIFT-BDESCR_SHIFT)) + (W_)(m)))
169 /* Number of usable blocks in a megablock */
171 #ifndef CMINUSMINUS // already defined in DerivedConstants.h
172 #define BLOCKS_PER_MBLOCK ((MBLOCK_SIZE - FIRST_BLOCK_OFF) / BLOCK_SIZE)
175 /* How many blocks in this megablock group */
177 #define MBLOCK_GROUP_BLOCKS(n) \
178 (BLOCKS_PER_MBLOCK + (n-1) * (MBLOCK_SIZE / BLOCK_SIZE))
180 /* Compute the required size of a megablock group */
182 #define BLOCKS_TO_MBLOCKS(n) \
183 (1 + (W_)MBLOCK_ROUND_UP((n-BLOCKS_PER_MBLOCK) * BLOCK_SIZE) / MBLOCK_SIZE)
189 /* Double-linked block lists: --------------------------------------------- */
192 dbl_link_onto(bdescr *bd, bdescr **list)
197 (*list)->u.back = bd; /* double-link the list */
203 dbl_link_remove(bdescr *bd, bdescr **list)
206 bd->u.back->link = bd->link;
211 bd->link->u.back = bd->u.back;
216 dbl_link_insert_after(bdescr *bd, bdescr *after)
218 bd->link = after->link;
221 after->link->u.back = bd;
227 dbl_link_replace(bdescr *new, bdescr *old, bdescr **list)
229 new->link = old->link;
230 new->u.back = old->u.back;
232 old->link->u.back = new;
235 old->u.back->link = new;
241 /* Initialisation ---------------------------------------------------------- */
243 extern void initBlockAllocator(void);
245 /* Allocation -------------------------------------------------------------- */
247 bdescr *allocGroup(nat n);
248 bdescr *allocBlock(void);
250 // versions that take the storage manager lock for you:
251 bdescr *allocGroup_lock(nat n);
252 bdescr *allocBlock_lock(void);
254 /* De-Allocation ----------------------------------------------------------- */
256 void freeGroup(bdescr *p);
257 void freeChain(bdescr *p);
259 // versions that take the storage manager lock for you:
260 void freeGroup_lock(bdescr *p);
261 void freeChain_lock(bdescr *p);
263 bdescr * splitBlockGroup (bdescr *bd, nat blocks);
265 /* Round a value to megablocks --------------------------------------------- */
267 // We want to allocate an object around a given size, round it up or
268 // down to the nearest size that will fit in an mblock group.
269 INLINE_HEADER StgWord
270 round_to_mblocks(StgWord words)
272 if (words > BLOCKS_PER_MBLOCK * BLOCK_SIZE_W) {
273 // first, ignore the gap at the beginning of the first mblock by
274 // adding it to the total words. Then we can pretend we're
275 // dealing in a uniform unit of megablocks.
276 words += FIRST_BLOCK_OFF/sizeof(W_);
278 if ((words % MBLOCK_SIZE_W) < (MBLOCK_SIZE_W / 2)) {
279 words = (words / MBLOCK_SIZE_W) * MBLOCK_SIZE_W;
281 words = ((words / MBLOCK_SIZE_W) + 1) * MBLOCK_SIZE_W;
284 words -= FIRST_BLOCK_OFF/sizeof(W_);
289 INLINE_HEADER StgWord
290 round_up_to_mblocks(StgWord words)
292 words += FIRST_BLOCK_OFF/sizeof(W_);
293 words = ((words / MBLOCK_SIZE_W) + 1) * MBLOCK_SIZE_W;
294 words -= FIRST_BLOCK_OFF/sizeof(W_);
298 #endif /* !CMINUSMINUS */
299 #endif /* RTS_STORAGE_BLOCK_H */