X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;f=lib%2Fbigalloc.c;h=9581188cc046f8171241ef5bfde20e64a44e5a0c;hb=5a78c3505ae7fa76a061e26676450049ec5946d5;hp=91845b389309f522ecd715f47d64d83e6d875140;hpb=1645bab5164b42caf235e0bcfeb4b9833f6a6647;p=libucw.git diff --git a/lib/bigalloc.c b/lib/bigalloc.c index 91845b38..9581188c 100644 --- a/lib/bigalloc.c +++ b/lib/bigalloc.c @@ -1,7 +1,7 @@ /* * UCW Library -- Allocation of Large Aligned Buffers * - * (c) 2006 Martin Mares + * (c) 2006--2007 Martin Mares * (c) 2007 Pavel Charvat * * This software may be freely distributed and used according to the terms @@ -12,19 +12,30 @@ #include #include +#include void * -page_alloc(unsigned int len) +page_alloc(u64 len) { + if (len > SIZE_MAX) + die("page_alloc: Size %llu is too large for the current architecture", (long long) len); ASSERT(!(len & (CPU_PAGE_SIZE-1))); byte *p = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0); if (p == (byte*) MAP_FAILED) - die("Cannot mmap %d bytes of memory: %m", len); + die("Cannot mmap %llu bytes of memory: %m", (long long)len); + return p; +} + +void * +page_alloc_zero(u64 len) +{ + void *p = page_alloc(len); + bzero(p, len); return p; } void -page_free(void *start, unsigned int len) +page_free(void *start, u64 len) { ASSERT(!(len & (CPU_PAGE_SIZE-1))); ASSERT(!((uintptr_t) start & (CPU_PAGE_SIZE-1))); @@ -32,7 +43,7 @@ page_free(void *start, unsigned int len) } void * -page_realloc(void *start, unsigned int old_len, unsigned int new_len) +page_realloc(void *start, u64 old_len, u64 new_len) { void *p = page_alloc(new_len); memcpy(p, start, MIN(old_len, new_len)); @@ -40,38 +51,51 @@ page_realloc(void *start, unsigned int old_len, unsigned int new_len) return p; } -static unsigned int -big_round(unsigned int len) +static u64 +big_round(u64 len) { - return ALIGN_TO(len, CPU_PAGE_SIZE); + return ALIGN_TO(len, (u64)CPU_PAGE_SIZE); } void * -big_alloc(unsigned int len) +big_alloc(u64 len) { - len = big_round(len); + u64 l = big_round(len); + if (l > SIZE_MAX - 2*CPU_PAGE_SIZE) + die("big_alloc: Size %llu is too large for the current architecture", (long long) len); #ifdef CONFIG_DEBUG - len += 2*CPU_PAGE_SIZE; + l += 2*CPU_PAGE_SIZE; #endif - byte *p = page_alloc(len); + byte *p = page_alloc(l); #ifdef CONFIG_DEBUG + *(u64*)p = len; mprotect(p, CPU_PAGE_SIZE, PROT_NONE); - mprotect(p+len-CPU_PAGE_SIZE, CPU_PAGE_SIZE, PROT_NONE); + mprotect(p+l-CPU_PAGE_SIZE, CPU_PAGE_SIZE, PROT_NONE); p += CPU_PAGE_SIZE; #endif return p; } +void * +big_alloc_zero(u64 len) +{ + void *p = big_alloc(len); + bzero(p, big_round(len)); + return p; +} + void -big_free(void *start, unsigned int len) +big_free(void *start, u64 len) { byte *p = start; - len = big_round(len); + u64 l = big_round(len); #ifdef CONFIG_DEBUG p -= CPU_PAGE_SIZE; - len += 2*CPU_PAGE_SIZE; + mprotect(p, CPU_PAGE_SIZE, PROT_READ); + ASSERT(*(u64*)p == len); + l += 2*CPU_PAGE_SIZE; #endif - page_free(start, len); + page_free(p, l); } #ifdef TEST