From 9b9f0e9a6edd8b3792693e14b131dce2ca393b39 Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Tue, 2 Jan 2007 15:23:38 +0100 Subject: [PATCH] Added missing lib/bigalloc.c from dev-async-shep. --- lib/bigalloc.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 lib/bigalloc.c diff --git a/lib/bigalloc.c b/lib/bigalloc.c new file mode 100644 index 00000000..b4589c4a --- /dev/null +++ b/lib/bigalloc.c @@ -0,0 +1,62 @@ +/* + * UCW Library -- Allocation of Large Aligned Buffers + * + * (c) 2006 Martin Mares + * + * This software may be freely distributed and used according to the terms + * of the GNU Lesser General Public License. + */ + +#include "lib/lib.h" + +#include +#include + +static unsigned int +big_round(unsigned int len) +{ + return ALIGN_TO(len, PAGE_SIZE); +} + +void * +big_alloc(unsigned int len) +{ + len = big_round(len); +#ifdef CONFIG_DEBUG + len += 2*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); +#ifdef CONFIG_DEBUG + mprotect(p, PAGE_SIZE, PROT_NONE); + mprotect(p+len-PAGE_SIZE, PAGE_SIZE, PROT_NONE); + p += PAGE_SIZE; +#endif + return p; +} + +void +big_free(void *start, unsigned int len) +{ + byte *p = start; + ASSERT(!((addr_int_t) p & (PAGE_SIZE-1))); + len = big_round(len); +#ifdef CONFIG_DEBUG + p -= PAGE_SIZE; + len += 2*PAGE_SIZE; +#endif + munmap(p, len); +} + +#ifdef TEST + +int main(void) +{ + byte *p = big_alloc(123456); + // p[-1] = 1; + big_free(p, 123456); + return 0; +} + +#endif -- 2.39.2