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"
27 static void initMBlock(void *mblock);
28 static bdescr *allocMegaGroup(nat mblocks);
29 static void freeMegaGroup(bdescr *bd);
31 static bdescr *free_list = NULL;
33 /* -----------------------------------------------------------------------------
35 -------------------------------------------------------------------------- */
37 void initBlockAllocator(void)
39 // The free list starts off NULL
42 /* -----------------------------------------------------------------------------
44 -------------------------------------------------------------------------- */
47 initGroup(nat n, bdescr *head)
54 head->free = head->start;
55 for (i=1, bd = head+1; i < n; i++, bd++) {
71 if (n > BLOCKS_PER_MBLOCK) {
72 return allocMegaGroup(BLOCKS_TO_MBLOCKS(n));
76 for (bd = free_list; bd != NULL; bd = bd->link) {
77 if (bd->blocks == n) { /* exactly the right size! */
79 /* no initialisation necessary - this is already a
80 * self-contained block group. */
82 bd->free = bd->start; /* block isn't free now */
86 if (bd->blocks > n) { /* block too big... */
87 bd->blocks -= n; /* take a chunk off the *end* */
89 initGroup(n, bd); /* initialise it */
95 mblock = getMBlock(); /* get a new megablock */
96 initMBlock(mblock); /* initialise the start fields */
97 bd = FIRST_BDESCR(mblock);
98 initGroup(n,bd); /* we know the group will fit */
99 if (n < BLOCKS_PER_MBLOCK) {
100 initGroup(BLOCKS_PER_MBLOCK-n, bd+n);
101 freeGroup(bd+n); /* add the rest on to the free list */
109 return allocGroup(1);
112 /* -----------------------------------------------------------------------------
113 Any request larger than BLOCKS_PER_MBLOCK needs a megablock group.
114 First, search the free list for enough contiguous megablocks to
115 fulfill the request - if we don't have enough, we need to
116 allocate some new ones.
118 A megablock group looks just like a normal block group, except that
119 the blocks field in the head will be larger than BLOCKS_PER_MBLOCK.
121 Note that any objects placed in this group must start in the first
122 megablock, since the other blocks don't have block descriptors.
123 -------------------------------------------------------------------------- */
126 allocMegaGroup(nat n)
129 bdescr *bd, *last, *grp_start, *grp_prev;
135 for (bd = free_list; bd != NULL; bd = bd->link) {
137 if (bd->blocks == BLOCKS_PER_MBLOCK) { /* whole megablock found */
139 /* is it the first one we've found or a non-contiguous megablock? */
140 if (grp_start == NULL ||
141 bd->start != last->start + MBLOCK_SIZE/sizeof(W_)) {
149 if (mbs_found == n) { /* found enough contig megablocks? */
154 else { /* only a partial megablock, start again */
161 /* found all the megablocks we need on the free list
163 if (mbs_found == n) {
164 /* remove the megablocks from the free list */
165 if (grp_prev == NULL) { /* bd now points to the last mblock */
166 free_list = bd->link;
168 grp_prev->link = bd->link;
172 /* the free list wasn't sufficient, allocate all new mblocks.
175 void *mblock = getMBlocks(n);
176 initMBlock(mblock); /* only need to init the 1st one */
177 grp_start = FIRST_BDESCR(mblock);
180 /* set up the megablock group */
181 initGroup(BLOCKS_PER_MBLOCK, grp_start);
182 grp_start->blocks = MBLOCK_GROUP_BLOCKS(n);
186 /* -----------------------------------------------------------------------------
188 -------------------------------------------------------------------------- */
190 /* coalesce the group p with p->link if possible.
192 * Returns p->link if no coalescing was done, otherwise returns a
193 * pointer to the newly enlarged group p.
196 STATIC_INLINE bdescr *
203 if (q != NULL && p->start + p->blocks * BLOCK_SIZE_W == q->start) {
205 p->blocks += q->blocks;
208 for (i = 0, bd = q; i < blocks; bd++, i++) {
223 /* are we dealing with a megablock group? */
224 if (p->blocks > BLOCKS_PER_MBLOCK) {
230 p->free = (void *)-1; /* indicates that this block is free */
233 /* fill the block group with garbage if sanity checking is on */
234 IF_DEBUG(sanity,memset(p->start, 0xaa, p->blocks * BLOCK_SIZE));
237 /* find correct place in free list to place new group */
239 for (bd = free_list; bd != NULL && bd->start < p->start;
244 /* now, last = previous group (or NULL) */
249 /* coalesce with previous group if possible */
250 p->link = last->link;
255 /* coalesce with next group if possible */
257 IF_DEBUG(sanity, checkFreeListSanity());
261 freeMegaGroup(bdescr *p)
266 n = ((bdescr *)q)->blocks * BLOCK_SIZE / MBLOCK_SIZE + 1;
267 for (; n > 0; q += MBLOCK_SIZE, n--) {
268 initMBlock(MBLOCK_ROUND_DOWN(q));
269 initGroup(BLOCKS_PER_MBLOCK, (bdescr *)q);
270 freeGroup((bdescr *)q);
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, 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 debugBelch("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;