1 /* -----------------------------------------------------------------------------
3 * (c) The GHC Team 1998-2000
5 * The block allocator and free list manager.
7 * This is the architecture independent part of the block allocator.
8 * It requires only the following support from the operating system:
12 * returns the address of an MBLOCK_SIZE region of memory, aligned on
13 * an MBLOCK_SIZE boundary. There is no requirement for successive
14 * calls to getMBlock to return strictly increasing addresses.
16 * ---------------------------------------------------------------------------*/
18 #include "PosixSource.h"
22 #include "BlockAlloc.h"
28 static void initMBlock(void *mblock);
29 static bdescr *allocMegaGroup(nat mblocks);
30 static void freeMegaGroup(bdescr *bd);
32 // In SMP mode, the free list is protected by sm_mutex. In the
33 // threaded RTS, it is protected by the Capability.
34 static bdescr *free_list = NULL;
36 /* -----------------------------------------------------------------------------
38 -------------------------------------------------------------------------- */
40 void initBlockAllocator(void)
42 // The free list starts off NULL
45 /* -----------------------------------------------------------------------------
47 -------------------------------------------------------------------------- */
50 initGroup(nat n, bdescr *head)
57 head->free = head->start;
59 for (i=1, bd = head+1; i < n; i++, bd++) {
76 if (n > BLOCKS_PER_MBLOCK) {
77 return allocMegaGroup(BLOCKS_TO_MBLOCKS(n));
81 for (bd = free_list; bd != NULL; bd = bd->link) {
82 if (bd->blocks == n) { /* exactly the right size! */
84 /* no initialisation necessary - this is already a
85 * self-contained block group. */
86 bd->free = bd->start; /* block isn't free now */
90 if (bd->blocks > n) { /* block too big... */
91 bd->blocks -= n; /* take a chunk off the *end* */
93 initGroup(n, bd); /* initialise it */
99 mblock = getMBlock(); /* get a new megablock */
100 initMBlock(mblock); /* initialise the start fields */
101 bd = FIRST_BDESCR(mblock);
102 initGroup(n,bd); /* we know the group will fit */
103 if (n < BLOCKS_PER_MBLOCK) {
104 initGroup(BLOCKS_PER_MBLOCK-n, bd+n);
105 freeGroup(bd+n); /* add the rest on to the free list */
111 allocGroup_lock(nat n)
123 return allocGroup(1);
127 allocBlock_lock(void)
136 /* -----------------------------------------------------------------------------
137 Any request larger than BLOCKS_PER_MBLOCK needs a megablock group.
138 First, search the free list for enough contiguous megablocks to
139 fulfill the request - if we don't have enough, we need to
140 allocate some new ones.
142 A megablock group looks just like a normal block group, except that
143 the blocks field in the head will be larger than BLOCKS_PER_MBLOCK.
145 Note that any objects placed in this group must start in the first
146 megablock, since the other blocks don't have block descriptors.
147 -------------------------------------------------------------------------- */
150 allocMegaGroup(nat n)
153 bdescr *bd, *last, *grp_start, *grp_prev;
159 for (bd = free_list; bd != NULL; bd = bd->link) {
161 if (bd->blocks == BLOCKS_PER_MBLOCK) { /* whole megablock found */
163 /* is it the first one we've found or a non-contiguous megablock? */
164 if (grp_start == NULL ||
165 bd->start != last->start + MBLOCK_SIZE/sizeof(W_)) {
173 if (mbs_found == n) { /* found enough contig megablocks? */
178 else { /* only a partial megablock, start again */
185 /* found all the megablocks we need on the free list
187 if (mbs_found == n) {
188 /* remove the megablocks from the free list */
189 if (grp_prev == NULL) { /* bd now points to the last mblock */
190 free_list = bd->link;
192 grp_prev->link = bd->link;
196 /* the free list wasn't sufficient, allocate all new mblocks.
199 void *mblock = getMBlocks(n);
200 initMBlock(mblock); /* only need to init the 1st one */
201 grp_start = FIRST_BDESCR(mblock);
204 /* set up the megablock group */
205 initGroup(BLOCKS_PER_MBLOCK, grp_start);
206 grp_start->blocks = MBLOCK_GROUP_BLOCKS(n);
210 /* -----------------------------------------------------------------------------
212 -------------------------------------------------------------------------- */
214 /* coalesce the group p with p->link if possible.
216 * Returns p->link if no coalescing was done, otherwise returns a
217 * pointer to the newly enlarged group p.
220 STATIC_INLINE bdescr *
227 if (q != NULL && p->start + p->blocks * BLOCK_SIZE_W == q->start) {
229 p->blocks += q->blocks;
232 for (i = 0, bd = q; i < blocks; bd++, i++) {
249 /* are we dealing with a megablock group? */
250 if (p->blocks > BLOCKS_PER_MBLOCK) {
256 p->free = (void *)-1; /* indicates that this block is free */
259 /* fill the block group with garbage if sanity checking is on */
260 IF_DEBUG(sanity,memset(p->start, 0xaa, p->blocks * BLOCK_SIZE));
262 /* find correct place in free list to place new group */
264 for (bd = free_list; bd != NULL && bd->start < p->start;
269 /* now, last = previous group (or NULL) */
274 /* coalesce with previous group if possible */
275 p->link = last->link;
280 /* coalesce with next group if possible */
282 IF_DEBUG(sanity, checkFreeListSanity());
286 freeGroup_lock(bdescr *p)
294 freeMegaGroup(bdescr *p)
299 n = ((bdescr *)q)->blocks * BLOCK_SIZE / MBLOCK_SIZE + 1;
300 for (; n > 0; q += MBLOCK_SIZE, n--) {
301 initMBlock(MBLOCK_ROUND_DOWN(q));
302 initGroup(BLOCKS_PER_MBLOCK, (bdescr *)q);
303 freeGroup((bdescr *)q);
308 freeChain(bdescr *bd)
319 freeChain_lock(bdescr *bd)
327 initMBlock(void *mblock)
332 /* the first few Bdescr's in a block are unused, so we don't want to
333 * put them all on the free list.
335 block = FIRST_BLOCK(mblock);
336 bd = FIRST_BDESCR(mblock);
338 /* Initialise the start field of each block descriptor
340 for (; block <= LAST_BLOCK(mblock); bd += 1, block += BLOCK_SIZE) {
345 /* -----------------------------------------------------------------------------
347 -------------------------------------------------------------------------- */
351 checkWellFormedGroup( bdescr *bd )
355 for (i = 1; i < bd->blocks; i++) {
356 ASSERT(bd[i].blocks == 0);
357 ASSERT(bd[i].free == 0);
358 ASSERT(bd[i].link == bd);
363 checkFreeListSanity(void)
367 for (bd = free_list; bd != NULL; bd = bd->link) {
368 IF_DEBUG(block_alloc,
369 debugBelch("group at 0x%p, length %d blocks\n",
370 bd->start, bd->blocks));
371 ASSERT(bd->blocks > 0);
372 checkWellFormedGroup(bd);
373 if (bd->link != NULL) {
374 /* make sure we're fully coalesced */
375 ASSERT(bd->start + bd->blocks * BLOCK_SIZE_W != bd->link->start);
376 ASSERT(bd->start < bd->link->start);
385 lnat total_blocks = 0;
387 for (bd = free_list; bd != NULL; bd = bd->link) {
388 total_blocks += bd->blocks;