kheap - #include <types.h> #include...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: #include <types.h> #include <lib.h> #include <vm.h> #include <machine/spl.h> static void fill_deadbeef(void *vptr, size_t len) { u_int32_t *ptr = vptr; size_t i; for (i=0; i<len/sizeof(u_int32_t); i++) { ptr[i] = 0xdeadbeef; } } //////////////////////////////////////////////////////////// // // Pool-based subpage allocator. // // It works like this: // // We allocate one page at a time and fill it with objects of size k, // for various k. Each page has its own freelist, maintained by a // linked list in the first word of each object. Each page also has a // freecount, so we know when the page is completely free and can // release it. // // No assumptions are made about the sizes k; they need not be // powers of two. Note, however, that malloc must always return // pointers aligned to the maximum alignment requirements of the // platform; thus block sizes must at least be multiples of 4, // preferably 8. They must also be at least sizeof(struct // freelist). It is only worth defining an additional block size if // more blocks would fit on a page than with the existing block // sizes, and large numbers of items of the new size are allocated. // // The free counts and addresses of the pages are maintained in // another list. Maintaining this table is a nuisance, because it // cannot recursively use the subpage allocator. (We could probably // make that work, but it would be painful.) // #undef SLOW /* consistency checks */ #undef SLOWER /* lots of consistency checks */ //////////////////////////////////////// #if PAGE_SIZE == 4096 #define NSIZES 8 static const size_t sizes[NSIZES] = { 16, 32, 64, 128, 256, 512, 1024, 2048 }; #define SMALLEST_SUBPAGE_SIZE 16 #define LARGEST_SUBPAGE_SIZE 2048 #elif PAGE_SIZE == 8192 #error "No support for 8k pages" #else #error "Odd page size" #endif //////////////////////////////////////// struct freelist { struct freelist *next; }; struct pageref { struct pageref *next_samesize; struct pageref *next_all; vaddr_t pageaddr_and_blocktype; u_int16_t freelist_offset; u_int16_t nfree; }; #define INVALID_OFFSET (0xffff) #define PR_PAGEADDR(pr) ((pr)->pageaddr_and_blocktype & PAGE_FRAME) #define PR_BLOCKTYPE(pr) ((pr)->pageaddr_and_blocktype & ~PAGE_FRAME) #define MKPAB(pa, blk) (((pa)&PAGE_FRAME) | ((blk) & ~PAGE_FRAME)) //////////////////////////////////////// /* * This is cheesy. * * The problem is not that it's wasteful - we can only allocate whole * pages of pageref structures at a time anyway. The problem is that * we really ought to be able to have more than one of these pages. * * However, for the time being, one page worth of pagerefs gives us * 256 pagerefs; this lets us manage 256 * 4k = 1M of kernel heap....
View Full Document

Page1 / 10

kheap - #include <types.h> #include...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online