From 07317eb3cbeec4684d0fbc8d3193ac8135d363e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Vavru=C5=A1a?= <marek.vavrusa@nic.cz> Date: Wed, 2 Apr 2014 12:23:52 +0200 Subject: [PATCH] mempool: partial import of bigalloc.c to use mempools in mmap'ed mode http://www.ucw.cz/gitweb/?p=libucw.git;a=blob;f=ucw/bigalloc.c;hb=HEAD --- src/common/mempool.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/common/mempool.c b/src/common/mempool.c index 082806afe4..cda8e6e3df 100644 --- a/src/common/mempool.c +++ b/src/common/mempool.c @@ -21,6 +21,7 @@ /** \todo This shouldn't be precalculated, but computed on load. */ #define CPU_PAGE_SIZE 4096 + /** Align an integer @s to the nearest higher multiple of @a (which should be a power of two) **/ #define ALIGN_TO(s, a) (((s)+a-1)&~(a-1)) #define MP_CHUNK_TAIL ALIGN_TO(sizeof(struct mempool_chunk), CPU_STRUCT_ALIGN) @@ -31,6 +32,33 @@ #endif #define DBG(s...) +/** \note Imported MMAP backend from bigalloc.c */ +#define CONFIG_UCW_POOL_IS_MMAP +#ifdef CONFIG_UCW_POOL_IS_MMAP +#include <sys/mman.h> +static void * +page_alloc(uint64_t len) +{ + if (!len) + return NULL; + if (len > SIZE_MAX) + return NULL; + assert(!(len & (CPU_PAGE_SIZE-1))); + uint8_t *p = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0); + if (p == (uint8_t*) MAP_FAILED) + return NULL; + return p; +} + +static void +page_free(void *start, uint64_t len) +{ + assert(!(len & (CPU_PAGE_SIZE-1))); + assert(!((uintptr_t) start & (CPU_PAGE_SIZE-1))); + munmap(start, len); +} +#endif + struct mempool_chunk { struct mempool_chunk *next; unsigned size; -- GitLab