1 /* -----------------------------------------------------------------------------
2 * $Id: BlockAlloc.c,v 1.4 1999/02/05 16:02:35 simonm Exp $
4 * (c) The GHC Team 1998-1999
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 * ---------------------------------------------------------------------------*/
22 #include "BlockAlloc.h"
25 static void initMBlock(void *mblock);
26 static bdescr *allocMegaGroup(nat mblocks);
27 static void freeMegaGroup(bdescr *bd);
29 static bdescr *free_list;
31 /* -----------------------------------------------------------------------------
33 -------------------------------------------------------------------------- */
35 void initBlockAllocator(void)
40 /* -----------------------------------------------------------------------------
42 -------------------------------------------------------------------------- */
45 initGroup(nat n, bdescr *head)
52 head->free = head->start;
53 for (i=1, bd = head+1; i < n; i++, bd++) {
66 if (n > BLOCKS_PER_MBLOCK) {
67 return allocMegaGroup(BLOCKS_TO_MBLOCKS(n));
71 for (bd = free_list; bd != NULL; bd = bd->link) {
72 if (bd->blocks == n) { /* exactly the right size! */
74 /* no initialisation necessary - this is already a
75 * self-contained block group. */
78 if (bd->blocks > n) { /* block too big... */
79 bd->blocks -= n; /* take a chunk off the *end* */
81 initGroup(n, bd); /* initialise it */
87 mblock = getMBlock(); /* get a new megablock */
88 initMBlock(mblock); /* initialise the start fields */
89 bd = FIRST_BDESCR(mblock);
90 initGroup(n,bd); /* we know the group will fit */
91 initGroup(BLOCKS_PER_MBLOCK-n, bd+n);
92 freeGroup(bd+n); /* add the rest on to the free list */
102 /* -----------------------------------------------------------------------------
103 Any request larger than BLOCKS_PER_MBLOCK needs a megablock group.
104 First, search the free list for enough contiguous megablocks to
105 fulfill the request - if we don't have enough, we need to
106 allocate some new ones.
108 A megablock group looks just like a normal block group, except that
109 the blocks field in the head will be larger than BLOCKS_PER_MBLOCK.
111 Note that any objects placed in this group must start in the first
112 megablock, since the other blocks don't have block descriptors.
113 -------------------------------------------------------------------------- */
116 allocMegaGroup(nat n)
119 bdescr *bd, *last, *grp_start, *grp_prev;
125 for (bd = free_list; bd != NULL; bd = bd->link) {
127 if (bd->blocks == BLOCKS_PER_MBLOCK) { /* whole megablock found */
129 if (grp_start == NULL) { /* is it the first one we've found? */
137 if (mbs_found == n) { /* found enough contig megablocks? */
142 else { /* only a partial megablock, start again */
149 /* found all the megablocks we need on the free list
151 if (mbs_found == n) {
152 /* remove the megablocks from the free list */
153 if (grp_prev == NULL) { /* bd now points to the last mblock */
154 free_list = bd->link;
156 grp_prev->link = bd->link;
160 /* the free list wasn't sufficient, allocate all new mblocks.
163 void *mblock = getMBlocks(n);
164 initMBlock(mblock); /* only need to init the 1st one */
165 grp_start = FIRST_BDESCR(mblock);
168 /* set up the megablock group */
169 initGroup(BLOCKS_PER_MBLOCK, grp_start);
170 grp_start->blocks = MBLOCK_GROUP_BLOCKS(n);
174 /* -----------------------------------------------------------------------------
176 -------------------------------------------------------------------------- */
178 /* coalesce the group p with p->link if possible.
180 * Returns p->link if no coalescing was done, otherwise returns a
181 * pointer to the newly enlarged group p.
184 static inline bdescr *
191 if (q != NULL && p->start + p->blocks * BLOCK_SIZE_W == q->start) {
193 p->blocks += q->blocks;
195 for (i = 0, bd = q; i < q->blocks; bd++, i++) {
209 /* are we dealing with a megablock group? */
210 if (p->blocks > BLOCKS_PER_MBLOCK) {
216 p->free = (void *)-1; /* indicates that this block is free */
219 /* fill the block group with garbage if sanity checking is on */
220 IF_DEBUG(sanity,memset(p->start, 0xaa, p->blocks * BLOCK_SIZE));
223 /* find correct place in free list to place new group */
225 for (bd = free_list; bd != NULL && bd->start < p->start;
230 /* now, last = previous group (or NULL) */
235 /* coalesce with previous group if possible */
236 p->link = last->link;
241 /* coalesce with next group if possible */
243 IF_DEBUG(sanity, checkFreeListSanity());
247 freeMegaGroup(bdescr *p)
251 n = p->blocks * BLOCK_SIZE / MBLOCK_SIZE + 1;
252 for (; n > 0; (W_)p += MBLOCK_SIZE, n--) {
253 initMBlock((void *)((W_)p & ~MBLOCK_MASK));
254 initGroup(BLOCKS_PER_MBLOCK, p);
260 freeChain(bdescr *bd)
271 initMBlock(void *mblock)
276 /* the first few Bdescr's in a block are unused, so we don't want to
277 * put them all on the free list.
279 block = FIRST_BLOCK(mblock);
280 bd = FIRST_BDESCR(mblock);
282 /* Initialise the start field of each block descriptor
284 for (; block <= LAST_BLOCK(mblock); bd += 1, (lnat)block += BLOCK_SIZE) {
289 /* -----------------------------------------------------------------------------
291 -------------------------------------------------------------------------- */
295 checkFreeListSanity(void)
299 for (bd = free_list; bd != NULL; bd = bd->link) {
300 IF_DEBUG(block_alloc,
301 fprintf(stderr,"group at 0x%x, length %d blocks\n",
302 (nat)bd->start, bd->blocks));
303 ASSERT(bd->blocks > 0);
304 if (bd->link != NULL) {
305 /* make sure we're fully coalesced */
306 ASSERT(bd->start + bd->blocks * BLOCK_SIZE_W != bd->link->start);
307 ASSERT(bd->start < bd->link->start);
316 lnat total_blocks = 0;
318 for (bd = free_list; bd != NULL; bd = bd->link) {
319 total_blocks += bd->blocks;