1 /* -----------------------------------------------------------------------------
2 * $Id: BlockAlloc.c,v 1.10 2001/08/14 13:40:09 sewardj 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"
26 static void initMBlock(void *mblock);
27 static bdescr *allocMegaGroup(nat mblocks);
28 static void freeMegaGroup(bdescr *bd);
30 static bdescr *free_list;
32 /* -----------------------------------------------------------------------------
34 -------------------------------------------------------------------------- */
36 void initBlockAllocator(void)
41 /* -----------------------------------------------------------------------------
43 -------------------------------------------------------------------------- */
46 initGroup(nat n, bdescr *head)
53 head->free = head->start;
54 for (i=1, bd = head+1; i < n; i++, bd++) {
69 if (n > BLOCKS_PER_MBLOCK) {
70 return allocMegaGroup(BLOCKS_TO_MBLOCKS(n));
74 for (bd = free_list; bd != NULL; bd = bd->link) {
75 if (bd->blocks == n) { /* exactly the right size! */
77 /* no initialisation necessary - this is already a
78 * self-contained block group. */
80 bd->free = bd->start; /* block isn't free now */
84 if (bd->blocks > n) { /* block too big... */
85 bd->blocks -= n; /* take a chunk off the *end* */
87 initGroup(n, bd); /* initialise it */
93 mblock = getMBlock(); /* get a new megablock */
94 initMBlock(mblock); /* initialise the start fields */
95 bd = FIRST_BDESCR(mblock);
96 initGroup(n,bd); /* we know the group will fit */
97 if (n < BLOCKS_PER_MBLOCK) {
98 initGroup(BLOCKS_PER_MBLOCK-n, bd+n);
99 freeGroup(bd+n); /* add the rest on to the free list */
107 return allocGroup(1);
110 /* -----------------------------------------------------------------------------
111 Any request larger than BLOCKS_PER_MBLOCK needs a megablock group.
112 First, search the free list for enough contiguous megablocks to
113 fulfill the request - if we don't have enough, we need to
114 allocate some new ones.
116 A megablock group looks just like a normal block group, except that
117 the blocks field in the head will be larger than BLOCKS_PER_MBLOCK.
119 Note that any objects placed in this group must start in the first
120 megablock, since the other blocks don't have block descriptors.
121 -------------------------------------------------------------------------- */
124 allocMegaGroup(nat n)
127 bdescr *bd, *last, *grp_start, *grp_prev;
133 for (bd = free_list; bd != NULL; bd = bd->link) {
135 if (bd->blocks == BLOCKS_PER_MBLOCK) { /* whole megablock found */
137 /* is it the first one we've found or a non-contiguous megablock? */
138 if (grp_start == NULL ||
139 bd->start != last->start + MBLOCK_SIZE/sizeof(W_)) {
147 if (mbs_found == n) { /* found enough contig megablocks? */
152 else { /* only a partial megablock, start again */
159 /* found all the megablocks we need on the free list
161 if (mbs_found == n) {
162 /* remove the megablocks from the free list */
163 if (grp_prev == NULL) { /* bd now points to the last mblock */
164 free_list = bd->link;
166 grp_prev->link = bd->link;
170 /* the free list wasn't sufficient, allocate all new mblocks.
173 void *mblock = getMBlocks(n);
174 initMBlock(mblock); /* only need to init the 1st one */
175 grp_start = FIRST_BDESCR(mblock);
178 /* set up the megablock group */
179 initGroup(BLOCKS_PER_MBLOCK, grp_start);
180 grp_start->blocks = MBLOCK_GROUP_BLOCKS(n);
184 /* -----------------------------------------------------------------------------
186 -------------------------------------------------------------------------- */
188 /* coalesce the group p with p->link if possible.
190 * Returns p->link if no coalescing was done, otherwise returns a
191 * pointer to the newly enlarged group p.
194 static inline bdescr *
201 if (q != NULL && p->start + p->blocks * BLOCK_SIZE_W == q->start) {
203 p->blocks += q->blocks;
205 for (i = 0, bd = q; i < q->blocks; bd++, i++) {
219 /* are we dealing with a megablock group? */
220 if (p->blocks > BLOCKS_PER_MBLOCK) {
226 p->free = (void *)-1; /* indicates that this block is free */
229 /* fill the block group with garbage if sanity checking is on */
230 IF_DEBUG(sanity,memset(p->start, 0xaa, p->blocks * BLOCK_SIZE));
233 /* find correct place in free list to place new group */
235 for (bd = free_list; bd != NULL && bd->start < p->start;
240 /* now, last = previous group (or NULL) */
245 /* coalesce with previous group if possible */
246 p->link = last->link;
251 /* coalesce with next group if possible */
253 IF_DEBUG(sanity, checkFreeListSanity());
257 freeMegaGroup(bdescr *p)
261 n = p->blocks * BLOCK_SIZE / MBLOCK_SIZE + 1;
262 for (; n > 0; (W_)p += MBLOCK_SIZE, n--) {
263 initMBlock((void *)((W_)p & ~MBLOCK_MASK));
264 initGroup(BLOCKS_PER_MBLOCK, p);
270 freeChain(bdescr *bd)
281 initMBlock(void *mblock)
286 /* the first few Bdescr's in a block are unused, so we don't want to
287 * put them all on the free list.
289 block = FIRST_BLOCK(mblock);
290 bd = FIRST_BDESCR(mblock);
292 /* Initialise the start field of each block descriptor
294 for (; block <= LAST_BLOCK(mblock); bd += 1, (lnat)block += BLOCK_SIZE) {
299 /* -----------------------------------------------------------------------------
301 -------------------------------------------------------------------------- */
305 checkFreeListSanity(void)
309 for (bd = free_list; bd != NULL; bd = bd->link) {
310 IF_DEBUG(block_alloc,
311 fprintf(stderr,"group at 0x%x, length %d blocks\n",
312 (nat)bd->start, bd->blocks));
313 ASSERT(bd->blocks > 0);
314 if (bd->link != NULL) {
315 /* make sure we're fully coalesced */
316 ASSERT(bd->start + bd->blocks * BLOCK_SIZE_W != bd->link->start);
317 ASSERT(bd->start < bd->link->start);
326 lnat total_blocks = 0;
328 for (bd = free_list; bd != NULL; bd = bd->link) {
329 total_blocks += bd->blocks;