X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;ds=inline;f=lib%2Fbigalloc.c;h=a1060e60be7d3cb7185bfff87b4f4c92aa33083f;hb=8c48090e240d68564c79eb29ac9004cc911bb5d0;hp=2ed76df31502b24886a6925fd3ad22d1c24ebb63;hpb=7442c1c86d2210ca3ef1538115f4a0423cd2f046;p=libucw.git diff --git a/lib/bigalloc.c b/lib/bigalloc.c index 2ed76df3..a1060e60 100644 --- a/lib/bigalloc.c +++ b/lib/bigalloc.c @@ -2,6 +2,7 @@ * UCW Library -- Allocation of Large Aligned Buffers * * (c) 2006--2007 Martin Mares + * (c) 2007 Pavel Charvat * * This software may be freely distributed and used according to the terms * of the GNU Lesser General Public License. @@ -10,6 +11,37 @@ #include "lib/lib.h" #include +#include +#include + +void * +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 %llu bytes of memory: %m", (long long)len); + return p; +} + +void +page_free(void *start, u64 len) +{ + ASSERT(!(len & (CPU_PAGE_SIZE-1))); + ASSERT(!((uintptr_t) start & (CPU_PAGE_SIZE-1))); + munmap(start, len); +} + +void * +page_realloc(void *start, u64 old_len, u64 new_len) +{ + void *p = page_alloc(new_len); + memcpy(p, start, MIN(old_len, new_len)); + page_free(start, old_len); + return p; +} static u64 big_round(u64 len) @@ -21,16 +53,12 @@ void * big_alloc(u64 len) { len = big_round(len); -#ifndef CPU_64BIT_POINTERS - if (len > 0xff000000) - die("big_alloc: Size %08Lx is too large for a 32-bit machine", (long long) len); -#endif + if (len > 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; #endif - 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); + byte *p = page_alloc(len); #ifdef CONFIG_DEBUG mprotect(p, CPU_PAGE_SIZE, PROT_NONE); mprotect(p+len-CPU_PAGE_SIZE, CPU_PAGE_SIZE, PROT_NONE); @@ -43,13 +71,12 @@ void big_free(void *start, u64 len) { byte *p = start; - ASSERT(!((uintptr_t) p & (CPU_PAGE_SIZE-1))); len = big_round(len); #ifdef CONFIG_DEBUG p -= CPU_PAGE_SIZE; len += 2*CPU_PAGE_SIZE; #endif - munmap(p, len); + page_free(p, len); } #ifdef TEST