mirror of
https://github.com/danbulant/Cosmos
synced 2026-05-22 13:58:47 +00:00
168 lines
4.8 KiB
Text
168 lines
4.8 KiB
Text
|
|
/*
|
|
* Initialise allocator, placing addresses [@min,@max] in free pool.
|
|
* @min and @max are PHYSICAL addresses.
|
|
*/
|
|
static void init_page_allocator(unsigned long min, unsigned long max)
|
|
{
|
|
int i;
|
|
unsigned long range, bitmap_size;
|
|
chunk_head_t *ch;
|
|
chunk_tail_t *ct;
|
|
for ( i = 0; i < FREELIST_SIZE; i++ )
|
|
{
|
|
free_head[i] = &free_tail[i];
|
|
free_tail[i].pprev = &free_head[i];
|
|
free_tail[i].next = NULL;
|
|
}
|
|
|
|
min = round_pgup (min);
|
|
max = round_pgdown(max);
|
|
|
|
/* Allocate space for the allocation bitmap. */
|
|
bitmap_size = (max+1) >> (PAGE_SHIFT+3);
|
|
bitmap_size = round_pgup(bitmap_size);
|
|
alloc_bitmap = (unsigned long *)to_virt(min);
|
|
min += bitmap_size;
|
|
range = max - min;
|
|
|
|
/* All allocated by default. */
|
|
memset(alloc_bitmap, ~0, bitmap_size);
|
|
/* Free up the memory we've been given to play with. */
|
|
map_free(min>>PAGE_SHIFT, range>>PAGE_SHIFT);
|
|
|
|
/* The buddy lists are addressed in high memory. */
|
|
min += VIRT_START;
|
|
max += VIRT_START;
|
|
|
|
while ( range != 0 )
|
|
{
|
|
/*
|
|
* Next chunk is limited by alignment of min, but also
|
|
* must not be bigger than remaining range.
|
|
*/
|
|
for ( i = PAGE_SHIFT; (1<<(i+1)) <= range; i++ )
|
|
if ( min & (1<<i) ) break;
|
|
|
|
|
|
ch = (chunk_head_t *)min;
|
|
min += (1<<i);
|
|
range -= (1<<i);
|
|
ct = (chunk_tail_t *)min-1;
|
|
i -= PAGE_SHIFT;
|
|
ch->level = i;
|
|
ch->next = free_head[i];
|
|
ch->pprev = &free_head[i];
|
|
ch->next->pprev = &ch->next;
|
|
free_head[i] = ch;
|
|
ct->level = i;
|
|
}
|
|
}
|
|
|
|
|
|
/* Allocate 2^@order contiguous pages. Returns a VIRTUAL address. */
|
|
unsigned long alloc_pages(int order)
|
|
{
|
|
int i;
|
|
chunk_head_t *alloc_ch, *spare_ch;
|
|
chunk_tail_t *spare_ct;
|
|
|
|
|
|
/* Find smallest order which can satisfy the request. */
|
|
for ( i = order; i < FREELIST_SIZE; i++ ) {
|
|
if ( !FREELIST_EMPTY(free_head[i]) )
|
|
break;
|
|
}
|
|
|
|
if ( i == FREELIST_SIZE ) goto no_memory;
|
|
|
|
/* Unlink a chunk. */
|
|
alloc_ch = free_head[i];
|
|
free_head[i] = alloc_ch->next;
|
|
alloc_ch->next->pprev = alloc_ch->pprev;
|
|
|
|
/* We may have to break the chunk a number of times. */
|
|
while ( i != order )
|
|
{
|
|
/* Split into two equal parts. */
|
|
i--;
|
|
spare_ch = (chunk_head_t *)((char *)alloc_ch + (1<<(i+PAGE_SHIFT)));
|
|
spare_ct = (chunk_tail_t *)((char *)spare_ch + (1<<(i+PAGE_SHIFT)))-1;
|
|
|
|
/* Create new header for spare chunk. */
|
|
spare_ch->level = i;
|
|
spare_ch->next = free_head[i];
|
|
spare_ch->pprev = &free_head[i];
|
|
spare_ct->level = i;
|
|
|
|
/* Link in the spare chunk. */
|
|
spare_ch->next->pprev = &spare_ch->next;
|
|
free_head[i] = spare_ch;
|
|
}
|
|
|
|
map_alloc(to_phys(alloc_ch)>>PAGE_SHIFT, 1<<order);
|
|
|
|
return((unsigned long)alloc_ch);
|
|
|
|
no_memory:
|
|
|
|
printk("Cannot handle page request order %d!\n", order);
|
|
|
|
return 0;
|
|
}
|
|
|
|
void free_pages(void *pointer, int order)
|
|
{
|
|
chunk_head_t *freed_ch, *to_merge_ch;
|
|
chunk_tail_t *freed_ct;
|
|
unsigned long mask;
|
|
|
|
/* First free the chunk */
|
|
map_free(virt_to_pfn(pointer), 1 << order);
|
|
|
|
/* Create free chunk */
|
|
freed_ch = (chunk_head_t *)pointer;
|
|
freed_ct = (chunk_tail_t *)((char *)pointer + (1<<(order + PAGE_SHIFT)))-1;
|
|
|
|
/* Now, possibly we can conseal chunks together */
|
|
while(order < FREELIST_SIZE)
|
|
{
|
|
mask = 1 << (order + PAGE_SHIFT);
|
|
if((unsigned long)freed_ch & mask)
|
|
{
|
|
to_merge_ch = (chunk_head_t *)((char *)freed_ch - mask);
|
|
if(allocated_in_map(virt_to_pfn(to_merge_ch)) ||
|
|
to_merge_ch->level != order)
|
|
break;
|
|
|
|
/* Merge with predecessor */
|
|
freed_ch = to_merge_ch;
|
|
}
|
|
else
|
|
{
|
|
to_merge_ch = (chunk_head_t *)((char *)freed_ch + mask);
|
|
if(allocated_in_map(virt_to_pfn(to_merge_ch)) ||
|
|
to_merge_ch->level != order)
|
|
break;
|
|
|
|
/* Merge with successor */
|
|
freed_ct = (chunk_tail_t *)((char *)to_merge_ch + mask) - 1;
|
|
}
|
|
|
|
/* We are commited to merging, unlink the chunk */
|
|
*(to_merge_ch->pprev) = to_merge_ch->next;
|
|
to_merge_ch->next->pprev = to_merge_ch->pprev;
|
|
|
|
order++;
|
|
}
|
|
|
|
/* Link the new chunk */
|
|
freed_ch->level = order;
|
|
freed_ch->next = free_head[order];
|
|
freed_ch->pprev = &free_head[order];
|
|
freed_ct->level = order;
|
|
|
|
freed_ch->next->pprev = &freed_ch->next;
|
|
free_head[order] = freed_ch;
|
|
|
|
}
|