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.
50 typedef struct bdescr_ {
51 StgPtr start; /* start addr of memory */
52 StgPtr free; /* first free byte of memory */
53 struct bdescr_ *link; /* used for chaining blocks together */
55 struct bdescr_ *back; /* used (occasionally) for doubly-linked lists*/
57 StgPtr scan; /* scan pointer for copying GC */
60 struct generation_ *gen; /* generation */
61 struct generation_ *dest; /* destination gen */
63 StgWord32 blocks; /* no. of blocks (if grp head, 0 otherwise) */
66 StgWord16 flags; /* block flags, see below */
67 #if SIZEOF_VOID_P == 8
68 StgWord32 _padding[2];
70 StgWord32 _padding[0];
75 #if SIZEOF_VOID_P == 8
76 #define BDESCR_SIZE 0x40
77 #define BDESCR_MASK 0x3f
78 #define BDESCR_SHIFT 6
80 #define BDESCR_SIZE 0x20
81 #define BDESCR_MASK 0x1f
82 #define BDESCR_SHIFT 5
85 /* Block contains objects evacuated during this GC */
86 #define BF_EVACUATED 1
87 /* Block is a large object */
91 /* Block is to be marked, not copied */
93 /* Block is free, and on the free list (TODO: is this used?) */
95 /* Block is executable */
97 /* Block contains only a small amount of live data */
98 #define BF_FRAGMENTED 64
99 /* we know about this block (for finding leaks) */
101 /* Block was swept in the last generation */
104 /* Finding the block descriptor for a given block -------------------------- */
109 ((((p) & MBLOCK_MASK & ~BLOCK_MASK) >> (BLOCK_SHIFT-BDESCR_SHIFT)) \
110 | ((p) & ~MBLOCK_MASK))
114 EXTERN_INLINE bdescr *Bdescr(StgPtr p);
115 EXTERN_INLINE bdescr *Bdescr(StgPtr p)
118 ((((W_)p & MBLOCK_MASK & ~BLOCK_MASK) >> (BLOCK_SHIFT-BDESCR_SHIFT))
119 | ((W_)p & ~MBLOCK_MASK)
125 /* Useful Macros ------------------------------------------------------------ */
127 /* Offset of first real data block in a megablock */
129 #define FIRST_BLOCK_OFF \
130 ((W_)BLOCK_ROUND_UP(BDESCR_SIZE * (MBLOCK_SIZE / BLOCK_SIZE)))
132 /* First data block in a given megablock */
134 #define FIRST_BLOCK(m) ((void *)(FIRST_BLOCK_OFF + (W_)(m)))
136 /* Last data block in a given megablock */
138 #define LAST_BLOCK(m) ((void *)(MBLOCK_SIZE-BLOCK_SIZE + (W_)(m)))
140 /* First real block descriptor in a megablock */
142 #define FIRST_BDESCR(m) \
143 ((bdescr *)((FIRST_BLOCK_OFF>>(BLOCK_SHIFT-BDESCR_SHIFT)) + (W_)(m)))
145 /* Last real block descriptor in a megablock */
147 #define LAST_BDESCR(m) \
148 ((bdescr *)(((MBLOCK_SIZE-BLOCK_SIZE)>>(BLOCK_SHIFT-BDESCR_SHIFT)) + (W_)(m)))
150 /* Number of usable blocks in a megablock */
152 #define BLOCKS_PER_MBLOCK ((MBLOCK_SIZE - FIRST_BLOCK_OFF) / BLOCK_SIZE)
154 /* How many blocks in this megablock group */
156 #define MBLOCK_GROUP_BLOCKS(n) \
157 (BLOCKS_PER_MBLOCK + (n-1) * (MBLOCK_SIZE / BLOCK_SIZE))
159 /* Compute the required size of a megablock group */
161 #define BLOCKS_TO_MBLOCKS(n) \
162 (1 + (W_)MBLOCK_ROUND_UP((n-BLOCKS_PER_MBLOCK) * BLOCK_SIZE) / MBLOCK_SIZE)
168 /* Double-linked block lists: --------------------------------------------- */
171 dbl_link_onto(bdescr *bd, bdescr **list)
176 (*list)->u.back = bd; /* double-link the list */
182 dbl_link_remove(bdescr *bd, bdescr **list)
185 bd->u.back->link = bd->link;
190 bd->link->u.back = bd->u.back;
195 dbl_link_insert_after(bdescr *bd, bdescr *after)
197 bd->link = after->link;
200 after->link->u.back = bd;
206 dbl_link_replace(bdescr *new, bdescr *old, bdescr **list)
208 new->link = old->link;
209 new->u.back = old->u.back;
211 old->link->u.back = new;
214 old->u.back->link = new;
220 /* Initialisation ---------------------------------------------------------- */
222 extern void initBlockAllocator(void);
224 /* Allocation -------------------------------------------------------------- */
226 bdescr *allocGroup(nat n);
227 bdescr *allocBlock(void);
229 // versions that take the storage manager lock for you:
230 bdescr *allocGroup_lock(nat n);
231 bdescr *allocBlock_lock(void);
233 /* De-Allocation ----------------------------------------------------------- */
235 void freeGroup(bdescr *p);
236 void freeChain(bdescr *p);
238 // versions that take the storage manager lock for you:
239 void freeGroup_lock(bdescr *p);
240 void freeChain_lock(bdescr *p);
242 bdescr * splitBlockGroup (bdescr *bd, nat blocks);
244 /* Round a value to megablocks --------------------------------------------- */
246 // We want to allocate an object around a given size, round it up or
247 // down to the nearest size that will fit in an mblock group.
248 INLINE_HEADER StgWord
249 round_to_mblocks(StgWord words)
251 if (words > BLOCKS_PER_MBLOCK * BLOCK_SIZE_W) {
252 // first, ignore the gap at the beginning of the first mblock by
253 // adding it to the total words. Then we can pretend we're
254 // dealing in a uniform unit of megablocks.
255 words += FIRST_BLOCK_OFF/sizeof(W_);
257 if ((words % MBLOCK_SIZE_W) < (MBLOCK_SIZE_W / 2)) {
258 words = (words / MBLOCK_SIZE_W) * MBLOCK_SIZE_W;
260 words = ((words / MBLOCK_SIZE_W) + 1) * MBLOCK_SIZE_W;
263 words -= FIRST_BLOCK_OFF/sizeof(W_);
268 INLINE_HEADER StgWord
269 round_up_to_mblocks(StgWord words)
271 words += FIRST_BLOCK_OFF/sizeof(W_);
272 words = ((words / MBLOCK_SIZE_W) + 1) * MBLOCK_SIZE_W;
273 words -= FIRST_BLOCK_OFF/sizeof(W_);
277 #endif /* !CMINUSMINUS */
278 #endif /* RTS_STORAGE_BLOCK_H */