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*/
58 unsigned int gen_no; /* generation */
59 struct step_ *step; /* step */
60 StgWord32 blocks; /* no. of blocks (if grp head, 0 otherwise) */
61 StgWord32 flags; /* block is in to-space */
62 #if SIZEOF_VOID_P == 8
63 StgWord32 _padding[2];
65 StgWord32 _padding[0];
70 #if SIZEOF_VOID_P == 8
71 #define BDESCR_SIZE 0x40
72 #define BDESCR_MASK 0x3f
73 #define BDESCR_SHIFT 6
75 #define BDESCR_SIZE 0x20
76 #define BDESCR_MASK 0x1f
77 #define BDESCR_SHIFT 5
80 /* Block contains objects evacuated during this GC */
81 #define BF_EVACUATED 1
82 /* Block is a large object */
86 /* Block is part of a compacted generation */
87 #define BF_COMPACTED 8
88 /* Block is free, and on the free list */
91 /* Finding the block descriptor for a given block -------------------------- */
96 ((((p) & MBLOCK_MASK & ~BLOCK_MASK) >> (BLOCK_SHIFT-BDESCR_SHIFT)) \
97 | ((p) & ~MBLOCK_MASK))
101 INLINE_HEADER bdescr *Bdescr(StgPtr p)
104 ((((W_)p & MBLOCK_MASK & ~BLOCK_MASK) >> (BLOCK_SHIFT-BDESCR_SHIFT))
105 | ((W_)p & ~MBLOCK_MASK)
111 /* Useful Macros ------------------------------------------------------------ */
113 /* Offset of first real data block in a megablock */
115 #define FIRST_BLOCK_OFF \
116 ((W_)BLOCK_ROUND_UP(BDESCR_SIZE * (MBLOCK_SIZE / BLOCK_SIZE)))
118 /* First data block in a given megablock */
120 #define FIRST_BLOCK(m) ((void *)(FIRST_BLOCK_OFF + (W_)(m)))
122 /* Last data block in a given megablock */
124 #define LAST_BLOCK(m) ((void *)(MBLOCK_SIZE-BLOCK_SIZE + (W_)(m)))
126 /* First real block descriptor in a megablock */
128 #define FIRST_BDESCR(m) \
129 ((bdescr *)((FIRST_BLOCK_OFF>>(BLOCK_SHIFT-BDESCR_SHIFT)) + (W_)(m)))
131 /* Number of usable blocks in a megablock */
133 #define BLOCKS_PER_MBLOCK ((MBLOCK_SIZE - FIRST_BLOCK_OFF) / BLOCK_SIZE)
135 /* How many blocks in this megablock group */
137 #define MBLOCK_GROUP_BLOCKS(n) \
138 (BLOCKS_PER_MBLOCK + (n-1) * (MBLOCK_SIZE / BLOCK_SIZE))
140 /* Compute the required size of a megablock group */
142 #define BLOCKS_TO_MBLOCKS(n) \
143 (1 + (W_)MBLOCK_ROUND_UP((n-BLOCKS_PER_MBLOCK) * BLOCK_SIZE) / MBLOCK_SIZE)
149 /* Double-linked block lists: --------------------------------------------- */
152 dbl_link_onto(bdescr *bd, bdescr **list)
157 (*list)->u.back = bd; /* double-link the list */
162 /* Initialisation ---------------------------------------------------------- */
164 extern void initBlockAllocator(void);
166 /* Allocation -------------------------------------------------------------- */
168 bdescr *allocGroup(nat n);
169 bdescr *allocBlock(void);
171 // versions that take the storage manager lock for you:
172 bdescr *allocGroup_lock(nat n);
173 bdescr *allocBlock_lock(void);
175 /* De-Allocation ----------------------------------------------------------- */
177 void freeGroup(bdescr *p);
178 void freeChain(bdescr *p);
180 // versions that take the storage manager lock for you:
181 void freeGroup_lock(bdescr *p);
182 void freeChain_lock(bdescr *p);
184 /* Round a value to megablocks --------------------------------------------- */
186 #define WORDS_PER_MBLOCK (BLOCKS_PER_MBLOCK * BLOCK_SIZE_W)
189 round_to_mblocks(nat words)
191 if (words > WORDS_PER_MBLOCK) {
192 if ((words % WORDS_PER_MBLOCK) < (WORDS_PER_MBLOCK / 2)) {
193 words = (words / WORDS_PER_MBLOCK) * WORDS_PER_MBLOCK;
195 words = ((words / WORDS_PER_MBLOCK) + 1) * WORDS_PER_MBLOCK;
201 #endif /* !CMINUSMINUS */