1 /* -----------------------------------------------------------------------------
3 * (c) The GHC Team, 1998-1999
5 * Block structure for the storage manager
7 * ---------------------------------------------------------------------------*/
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 */
59 unsigned int gen_no; /* generation */
60 struct step_ *step; /* step */
61 StgWord32 blocks; /* no. of blocks (if grp head, 0 otherwise) */
62 StgWord32 flags; /* block is in to-space */
63 #if SIZEOF_VOID_P == 8
64 StgWord32 _padding[2];
66 StgWord32 _padding[0];
71 #if SIZEOF_VOID_P == 8
72 #define BDESCR_SIZE 0x40
73 #define BDESCR_MASK 0x3f
74 #define BDESCR_SHIFT 6
76 #define BDESCR_SIZE 0x20
77 #define BDESCR_MASK 0x1f
78 #define BDESCR_SHIFT 5
81 /* Block contains objects evacuated during this GC */
82 #define BF_EVACUATED 1
83 /* Block is a large object */
87 /* Block is to be marked, not copied */
89 /* Block is free, and on the free list (TODO: is this used?) */
91 /* Block is executable */
93 /* Block contains only a small amount of live data */
94 #define BF_FRAGMENTED 64
97 /* Finding the block descriptor for a given block -------------------------- */
102 ((((p) & MBLOCK_MASK & ~BLOCK_MASK) >> (BLOCK_SHIFT-BDESCR_SHIFT)) \
103 | ((p) & ~MBLOCK_MASK))
107 INLINE_HEADER bdescr *Bdescr(StgPtr p)
110 ((((W_)p & MBLOCK_MASK & ~BLOCK_MASK) >> (BLOCK_SHIFT-BDESCR_SHIFT))
111 | ((W_)p & ~MBLOCK_MASK)
117 /* Useful Macros ------------------------------------------------------------ */
119 /* Offset of first real data block in a megablock */
121 #define FIRST_BLOCK_OFF \
122 ((W_)BLOCK_ROUND_UP(BDESCR_SIZE * (MBLOCK_SIZE / BLOCK_SIZE)))
124 /* First data block in a given megablock */
126 #define FIRST_BLOCK(m) ((void *)(FIRST_BLOCK_OFF + (W_)(m)))
128 /* Last data block in a given megablock */
130 #define LAST_BLOCK(m) ((void *)(MBLOCK_SIZE-BLOCK_SIZE + (W_)(m)))
132 /* First real block descriptor in a megablock */
134 #define FIRST_BDESCR(m) \
135 ((bdescr *)((FIRST_BLOCK_OFF>>(BLOCK_SHIFT-BDESCR_SHIFT)) + (W_)(m)))
137 /* Last real block descriptor in a megablock */
139 #define LAST_BDESCR(m) \
140 ((bdescr *)(((MBLOCK_SIZE-BLOCK_SIZE)>>(BLOCK_SHIFT-BDESCR_SHIFT)) + (W_)(m)))
142 /* Number of usable blocks in a megablock */
144 #define BLOCKS_PER_MBLOCK ((MBLOCK_SIZE - FIRST_BLOCK_OFF) / BLOCK_SIZE)
146 /* How many blocks in this megablock group */
148 #define MBLOCK_GROUP_BLOCKS(n) \
149 (BLOCKS_PER_MBLOCK + (n-1) * (MBLOCK_SIZE / BLOCK_SIZE))
151 /* Compute the required size of a megablock group */
153 #define BLOCKS_TO_MBLOCKS(n) \
154 (1 + (W_)MBLOCK_ROUND_UP((n-BLOCKS_PER_MBLOCK) * BLOCK_SIZE) / MBLOCK_SIZE)
160 /* Double-linked block lists: --------------------------------------------- */
163 dbl_link_onto(bdescr *bd, bdescr **list)
168 (*list)->u.back = bd; /* double-link the list */
174 dbl_link_remove(bdescr *bd, bdescr **list)
177 bd->u.back->link = bd->link;
182 bd->link->u.back = bd->u.back;
187 dbl_link_insert_after(bdescr *bd, bdescr *after)
189 bd->link = after->link;
192 after->link->u.back = bd;
198 dbl_link_replace(bdescr *new, bdescr *old, bdescr **list)
200 new->link = old->link;
201 new->u.back = old->u.back;
203 old->link->u.back = new;
206 old->u.back->link = new;
212 /* Initialisation ---------------------------------------------------------- */
214 extern void initBlockAllocator(void);
216 /* Allocation -------------------------------------------------------------- */
218 bdescr *allocGroup(nat n);
219 bdescr *allocBlock(void);
221 // versions that take the storage manager lock for you:
222 bdescr *allocGroup_lock(nat n);
223 bdescr *allocBlock_lock(void);
225 /* De-Allocation ----------------------------------------------------------- */
227 void freeGroup(bdescr *p);
228 void freeChain(bdescr *p);
230 // versions that take the storage manager lock for you:
231 void freeGroup_lock(bdescr *p);
232 void freeChain_lock(bdescr *p);
234 bdescr * splitBlockGroup (bdescr *bd, nat blocks);
236 /* Round a value to megablocks --------------------------------------------- */
238 // We want to allocate an object around a given size, round it up or
239 // down to the nearest size that will fit in an mblock group.
240 INLINE_HEADER StgWord
241 round_to_mblocks(StgWord words)
243 if (words > BLOCKS_PER_MBLOCK * BLOCK_SIZE_W) {
244 // first, ignore the gap at the beginning of the first mblock by
245 // adding it to the total words. Then we can pretend we're
246 // dealing in a uniform unit of megablocks.
247 words += FIRST_BLOCK_OFF/sizeof(W_);
249 if ((words % MBLOCK_SIZE_W) < (MBLOCK_SIZE_W / 2)) {
250 words = (words / MBLOCK_SIZE_W) * MBLOCK_SIZE_W;
252 words = ((words / MBLOCK_SIZE_W) + 1) * MBLOCK_SIZE_W;
255 words -= FIRST_BLOCK_OFF/sizeof(W_);
260 #endif /* !CMINUSMINUS */