/* * Allocate a block of size SZ. Redirect either to subpage_kmalloc or * alloc_kpages depending on how big SZ is. */ void * kmalloc(size_t sz) { size_t checksz; #ifdef LABELS vaddr_t label; #endif #ifdef LABELS #ifdef __GNUC__ label = (vaddr_t)__builtin_return_address(0); #else #error "Don't know how to get return address with this compiler" #endif /* __GNUC__ */ #endif /* LABELS */ checksz = sz + GUARD_OVERHEAD + LABEL_OVERHEAD; if (checksz >= LARGEST_SUBPAGE_SIZE) { unsigned long npages; vaddr_t address; /* Round up to a whole number of pages. */ npages = (sz + PAGE_SIZE - 1)/PAGE_SIZE; address = alloc_kpages(npages); if (address==0) { return NULL; } KASSERT(address % PAGE_SIZE == 0); return (void *)address; } #ifdef LABELS return subpage_kmalloc(sz, label); #else return subpage_kmalloc(sz); #endif }
void * kmalloc(size_t sz) { if (sz>=LARGEST_SUBPAGE_SIZE) { unsigned long npages; vaddr_t address; /* Round up to a whole number of pages. */ npages = (sz + PAGE_SIZE - 1)/PAGE_SIZE; address = alloc_kpages(npages); if (address==0) { return NULL; } return (void *)address; DEBUG(DB_KMALLOC, "KMALLOC DEBUG: %x\n", address); } return subpage_kmalloc(sz); }