diff options
author | Timo Teras <timo.teras@iki.fi> | 2010-03-10 20:11:06 +0200 |
---|---|---|
committer | Timo Teras <timo.teras@iki.fi> | 2010-03-10 20:11:06 +0200 |
commit | 0183e33d9a4759764716e771b85e19f7a997b8bd (patch) | |
tree | 7921b52421171899d9df3999625b0f7134148350 /src/mem-mmap.c | |
parent | dc34e87746f69994aad893b39ee4cd3dda6e2f7b (diff) | |
download | libtf-0183e33d9a4759764716e771b85e19f7a997b8bd.tar.bz2 libtf-0183e33d9a4759764716e771b85e19f7a997b8bd.tar.xz |
mem: add mmap allocator
use it for heaps and fiber stacks.
Diffstat (limited to 'src/mem-mmap.c')
-rw-r--r-- | src/mem-mmap.c | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/src/mem-mmap.c b/src/mem-mmap.c new file mode 100644 index 0000000..b8e83cb --- /dev/null +++ b/src/mem-mmap.c @@ -0,0 +1,41 @@ +/* mem-mmap.c - mmap wrappings for large memory allocation + * + * Copyright (C) 2009 Timo Teräs <timo.teras@iki.fi> + * All rights reserved. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 or later as + * published by the Free Software Foundation. + * + * See http://www.gnu.org/ for details. + */ + +#include <sys/mman.h> +#include <libtf/defines.h> +#include <libtf/memory.h> + +void *tf_bmem_alloc(size_t size) +{ + void *ptr; + + ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, 0, 0); + if (ptr == MAP_FAILED) + return NULL; + return ptr; +} + +void *tf_bmem_resize(void *oldptr, size_t oldsize, size_t newsize) +{ + void *ptr; + + ptr = mremap(oldptr, oldsize, newsize, MREMAP_MAYMOVE); + if (ptr == MAP_FAILED) + return NULL; + return ptr; +} + +void tf_bmem_free(void *ptr, size_t size) +{ + munmap(ptr, size); +} |