1 /* -----------------------------------------------------------------------------
3 * (c) The GHC Team 1998-2006
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 THREADED_RTS mode, the free list is protected by sm_mutex.
33 static bdescr *free_list = NULL;
35 /* -----------------------------------------------------------------------------
37 -------------------------------------------------------------------------- */
39 void initBlockAllocator(void)
41 // The free list starts off NULL
44 /* -----------------------------------------------------------------------------
46 -------------------------------------------------------------------------- */
49 initGroup(nat n, bdescr *head)
56 head->free = head->start;
58 for (i=1, bd = head+1; i < n; i++, bd++) {
75 if (n > BLOCKS_PER_MBLOCK) {
76 return allocMegaGroup(BLOCKS_TO_MBLOCKS(n));
80 for (bd = free_list; bd != NULL; bd = bd->link) {
81 if (bd->blocks == n) { /* exactly the right size! */
83 /* no initialisation necessary - this is already a
84 * self-contained block group. */
85 bd->free = bd->start; /* block isn't free now */
89 if (bd->blocks > n) { /* block too big... */
90 bd->blocks -= n; /* take a chunk off the *end* */
92 initGroup(n, bd); /* initialise it */
98 mblock = getMBlock(); /* get a new megablock */
99 initMBlock(mblock); /* initialise the start fields */
100 bd = FIRST_BDESCR(mblock);
101 initGroup(n,bd); /* we know the group will fit */
102 if (n < BLOCKS_PER_MBLOCK) {
103 initGroup(BLOCKS_PER_MBLOCK-n, bd+n);
104 freeGroup(bd+n); /* add the rest on to the free list */
110 allocGroup_lock(nat n)
122 return allocGroup(1);
126 allocBlock_lock(void)
135 /* -----------------------------------------------------------------------------
136 Any request larger than BLOCKS_PER_MBLOCK needs a megablock group.
137 First, search the free list for enough contiguous megablocks to
138 fulfill the request - if we don't have enough, we need to
139 allocate some new ones.
141 A megablock group looks just like a normal block group, except that
142 the blocks field in the head will be larger than BLOCKS_PER_MBLOCK.
144 Note that any objects placed in this group must start in the first
145 megablock, since the other blocks don't have block descriptors.
146 -------------------------------------------------------------------------- */
149 allocMegaGroup(nat n)
152 bdescr *bd, *last, *grp_start, *grp_prev;
158 for (bd = free_list; bd != NULL; bd = bd->link) {
160 if (bd->blocks == BLOCKS_PER_MBLOCK) { /* whole megablock found */
162 /* is it the first one we've found or a non-contiguous megablock? */
163 if (grp_start == NULL ||
164 bd->start != last->start + MBLOCK_SIZE/sizeof(W_)) {
172 if (mbs_found == n) { /* found enough contig megablocks? */
177 else { /* only a partial megablock, start again */
184 /* found all the megablocks we need on the free list
186 if (mbs_found == n) {
187 /* remove the megablocks from the free list */
188 if (grp_prev == NULL) { /* bd now points to the last mblock */
189 free_list = bd->link;
191 grp_prev->link = bd->link;
195 /* the free list wasn't sufficient, allocate all new mblocks.
198 void *mblock = getMBlocks(n);
199 initMBlock(mblock); /* only need to init the 1st one */
200 grp_start = FIRST_BDESCR(mblock);
203 /* set up the megablock group */
204 initGroup(BLOCKS_PER_MBLOCK, grp_start);
205 grp_start->blocks = MBLOCK_GROUP_BLOCKS(n);
209 /* -----------------------------------------------------------------------------
211 -------------------------------------------------------------------------- */
213 /* coalesce the group p with p->link if possible.
215 * Returns p->link if no coalescing was done, otherwise returns a
216 * pointer to the newly enlarged group p.
219 STATIC_INLINE bdescr *
226 if (q != NULL && p->start + p->blocks * BLOCK_SIZE_W == q->start) {
228 p->blocks += q->blocks;
231 for (i = 0, bd = q; i < blocks; bd++, i++) {
248 /* are we dealing with a megablock group? */
249 if (p->blocks > BLOCKS_PER_MBLOCK) {
255 p->free = (void *)-1; /* indicates that this block is free */
258 /* fill the block group with garbage if sanity checking is on */
259 IF_DEBUG(sanity,memset(p->start, 0xaa, p->blocks * BLOCK_SIZE));
261 /* find correct place in free list to place new group */
263 for (bd = free_list; bd != NULL && bd->start < p->start;
268 /* now, last = previous group (or NULL) */
273 /* coalesce with previous group if possible */
274 p->link = last->link;
279 /* coalesce with next group if possible */
281 IF_DEBUG(sanity, checkFreeListSanity());
285 freeGroup_lock(bdescr *p)
293 freeMegaGroup(bdescr *p)
298 n = ((bdescr *)q)->blocks * BLOCK_SIZE / MBLOCK_SIZE + 1;
299 for (; n > 0; q += MBLOCK_SIZE, n--) {
300 initMBlock(MBLOCK_ROUND_DOWN(q));
301 initGroup(BLOCKS_PER_MBLOCK, (bdescr *)q);
302 freeGroup((bdescr *)q);
307 freeChain(bdescr *bd)
318 freeChain_lock(bdescr *bd)
326 initMBlock(void *mblock)
331 /* the first few Bdescr's in a block are unused, so we don't want to
332 * put them all on the free list.
334 block = FIRST_BLOCK(mblock);
335 bd = FIRST_BDESCR(mblock);
337 /* Initialise the start field of each block descriptor
339 for (; block <= LAST_BLOCK(mblock); bd += 1, block += BLOCK_SIZE) {
344 /* -----------------------------------------------------------------------------
346 -------------------------------------------------------------------------- */
350 checkWellFormedGroup( bdescr *bd )
354 for (i = 1; i < bd->blocks; i++) {
355 ASSERT(bd[i].blocks == 0);
356 ASSERT(bd[i].free == 0);
357 ASSERT(bd[i].link == bd);
362 checkFreeListSanity(void)
366 for (bd = free_list; bd != NULL; bd = bd->link) {
367 IF_DEBUG(block_alloc,
368 debugBelch("group at 0x%p, length %ld blocks\n",
369 bd->start, (long)bd->blocks));
370 ASSERT(bd->blocks > 0);
371 checkWellFormedGroup(bd);
372 if (bd->link != NULL) {
373 /* make sure we're fully coalesced */
374 ASSERT(bd->start + bd->blocks * BLOCK_SIZE_W != bd->link->start);
375 ASSERT(bd->start < bd->link->start);
384 lnat total_blocks = 0;
386 for (bd = free_list; bd != NULL; bd = bd->link) {
387 total_blocks += bd->blocks;