From 927e1bb31959c313d21f2b7c5d209a8d7692872d Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Wed, 16 Dec 2015 18:09:17 +0100 Subject: fix issue #6 on github MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/malloc.c | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/malloc.c b/src/malloc.c index c6c1629..1a0913c 100644 --- a/src/malloc.c +++ b/src/malloc.c @@ -39,6 +39,25 @@ +/** + * Return the pagesize. If it it cannot be retrieved, + * use a fallback value. + * + * @return The pagesize, or a fallback value. + */ +__attribute__((__warn_unused_result__, const)) +static size_t get_pagesize(void) +{ + static size_t pagesize = 0; + if (pagesize == 0) + { + long r = sysconf(_SC_PAGESIZE); + pagesize = (size_t)(r == -1 ? 4096 : r); + } + return pagesize; +} + + /** * Create a new memory allocation on the heap. * The allocation will not be initialised. @@ -377,7 +396,7 @@ int posix_memalign(void** ptrptr, size_t boundary, size_t size) */ void* valloc(size_t size) { - return memalign((size_t)sysconf(_SC_PAGESIZE), size); + return memalign(get_pagesize(), size); } @@ -402,7 +421,7 @@ void* valloc(size_t size) */ void* pvalloc(size_t size) { - size_t boundary = (size_t)sysconf(_SC_PAGESIZE); + size_t boundary = get_pagesize(); size_t full_size = 2 * sizeof(size_t) + boundary - 1 + size; size_t rounding = 0; -- cgit v1.2.3-70-g09d2