1 /* -----------------------------------------------------------------------------
2 * $Id: BlockAlloc.c,v 1.14 2002/07/17 09:21:49 simonmar Exp $
4 * (c) The GHC Team 1998-2000
6 * The block allocator and free list manager.
8 * This is the architecture independent part of the block allocator.
9 * It requires only the following support from the operating system:
13 * returns the address of an MBLOCK_SIZE region of memory, aligned on
14 * an MBLOCK_SIZE boundary. There is no requirement for successive
15 * calls to getMBlock to return strictly increasing addresses.
17 * ---------------------------------------------------------------------------*/
19 #include "PosixSource.h"
23 #include "BlockAlloc.h"
28 static void initMBlock(void *mblock);
29 static bdescr *allocMegaGroup(nat mblocks);
30 static void freeMegaGroup(bdescr *bd);
32 static bdescr *free_list;
34 /* -----------------------------------------------------------------------------
36 -------------------------------------------------------------------------- */
38 void initBlockAllocator(void)
43 /* -----------------------------------------------------------------------------
45 -------------------------------------------------------------------------- */
48 initGroup(nat n, bdescr *head)
55 head->free = head->start;
56 for (i=1, bd = head+1; i < n; i++, bd++) {
72 if (n > BLOCKS_PER_MBLOCK) {
73 return allocMegaGroup(BLOCKS_TO_MBLOCKS(n));
77 for (bd = free_list; bd != NULL; bd = bd->link) {
78 if (bd->blocks == n) { /* exactly the right size! */
80 /* no initialisation necessary - this is already a
81 * self-contained block group. */
83 bd->free = bd->start; /* block isn't free now */
87 if (bd->blocks > n) { /* block too big... */
88 bd->blocks -= n; /* take a chunk off the *end* */
90 initGroup(n, bd); /* initialise it */
96 mblock = getMBlock(); /* get a new megablock */
97 initMBlock(mblock); /* initialise the start fields */
98 bd = FIRST_BDESCR(mblock);
99 initGroup(n,bd); /* we know the group will fit */
100 if (n < BLOCKS_PER_MBLOCK) {
101 initGroup(BLOCKS_PER_MBLOCK-n, bd+n);
102 freeGroup(bd+n); /* add the rest on to the free list */
110 return allocGroup(1);
113 /* -----------------------------------------------------------------------------
114 Any request larger than BLOCKS_PER_MBLOCK needs a megablock group.
115 First, search the free list for enough contiguous megablocks to
116 fulfill the request - if we don't have enough, we need to
117 allocate some new ones.
119 A megablock group looks just like a normal block group, except that
120 the blocks field in the head will be larger than BLOCKS_PER_MBLOCK.
122 Note that any objects placed in this group must start in the first
123 megablock, since the other blocks don't have block descriptors.
124 -------------------------------------------------------------------------- */
127 allocMegaGroup(nat n)
130 bdescr *bd, *last, *grp_start, *grp_prev;
136 for (bd = free_list; bd != NULL; bd = bd->link) {
138 if (bd->blocks == BLOCKS_PER_MBLOCK) { /* whole megablock found */
140 /* is it the first one we've found or a non-contiguous megablock? */
141 if (grp_start == NULL ||
142 bd->start != last->start + MBLOCK_SIZE/sizeof(W_)) {
150 if (mbs_found == n) { /* found enough contig megablocks? */
155 else { /* only a partial megablock, start again */
162 /* found all the megablocks we need on the free list
164 if (mbs_found == n) {
165 /* remove the megablocks from the free list */
166 if (grp_prev == NULL) { /* bd now points to the last mblock */
167 free_list = bd->link;
169 grp_prev->link = bd->link;
173 /* the free list wasn't sufficient, allocate all new mblocks.
176 void *mblock = getMBlocks(n);
177 initMBlock(mblock); /* only need to init the 1st one */
178 grp_start = FIRST_BDESCR(mblock);
181 /* set up the megablock group */
182 initGroup(BLOCKS_PER_MBLOCK, grp_start);
183 grp_start->blocks = MBLOCK_GROUP_BLOCKS(n);
187 /* -----------------------------------------------------------------------------
189 -------------------------------------------------------------------------- */
191 /* coalesce the group p with p->link if possible.
193 * Returns p->link if no coalescing was done, otherwise returns a
194 * pointer to the newly enlarged group p.
197 static inline bdescr *
204 if (q != NULL && p->start + p->blocks * BLOCK_SIZE_W == q->start) {
206 p->blocks += q->blocks;
209 for (i = 0, bd = q; i < blocks; bd++, i++) {
224 /* are we dealing with a megablock group? */
225 if (p->blocks > BLOCKS_PER_MBLOCK) {
231 p->free = (void *)-1; /* indicates that this block is free */
234 /* fill the block group with garbage if sanity checking is on */
235 IF_DEBUG(sanity,memset(p->start, 0xaa, p->blocks * BLOCK_SIZE));
238 /* find correct place in free list to place new group */
240 for (bd = free_list; bd != NULL && bd->start < p->start;
245 /* now, last = previous group (or NULL) */
250 /* coalesce with previous group if possible */
251 p->link = last->link;
256 /* coalesce with next group if possible */
258 IF_DEBUG(sanity, checkFreeListSanity());
262 freeMegaGroup(bdescr *p)
266 n = p->blocks * BLOCK_SIZE / MBLOCK_SIZE + 1;
267 for (; n > 0; (W_)p += MBLOCK_SIZE, n--) {
268 initMBlock((void *)((W_)p & ~MBLOCK_MASK));
269 initGroup(BLOCKS_PER_MBLOCK, p);
275 freeChain(bdescr *bd)
286 initMBlock(void *mblock)
291 /* the first few Bdescr's in a block are unused, so we don't want to
292 * put them all on the free list.
294 block = FIRST_BLOCK(mblock);
295 bd = FIRST_BDESCR(mblock);
297 /* Initialise the start field of each block descriptor
299 for (; block <= LAST_BLOCK(mblock); bd += 1, (lnat)block += BLOCK_SIZE) {
304 /* -----------------------------------------------------------------------------
306 -------------------------------------------------------------------------- */
310 checkWellFormedGroup( bdescr *bd )
314 for (i = 1; i < bd->blocks; i++) {
315 ASSERT(bd[i].blocks == 0);
316 ASSERT(bd[i].free == 0);
317 ASSERT(bd[i].link == bd);
322 checkFreeListSanity(void)
326 for (bd = free_list; bd != NULL; bd = bd->link) {
327 IF_DEBUG(block_alloc,
328 fprintf(stderr,"group at 0x%x, length %d blocks\n",
329 (nat)bd->start, bd->blocks));
330 ASSERT(bd->blocks > 0);
331 checkWellFormedGroup(bd);
332 if (bd->link != NULL) {
333 /* make sure we're fully coalesced */
334 ASSERT(bd->start + bd->blocks * BLOCK_SIZE_W != bd->link->start);
335 ASSERT(bd->start < bd->link->start);
344 lnat total_blocks = 0;
346 for (bd = free_list; bd != NULL; bd = bd->link) {
347 total_blocks += bd->blocks;