From 83e70e1b6c7abd0315dd8cd5a98a91c58e0cd8bd Mon Sep 17 00:00:00 2001 From: Jakob Kaivo Date: Wed, 15 Jul 2020 16:43:26 -0400 Subject: munmap() extra pages during realloc() --- src/mapalloc.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/mapalloc.c b/src/mapalloc.c index afd8443..035332f 100644 --- a/src/mapalloc.c +++ b/src/mapalloc.c @@ -172,7 +172,12 @@ void *MA_realloc(void *ptr, size_t n) if (n < (b->allocated - (PAGESIZE * 2))) { b->used = n; - /* TODO: munmap() and mprotect() as necessary */ + char *over = (char*)ptr + b->used + (PAGESIZE - (PAGESIZE % b->used)); + if (over != b->over) { + mprotect(over, PAGESIZE, PROT_NONE); + munmap(over + PAGESIZE, (char*)b->over - over - PAGESIZE); + b->over = over; + } return ptr; } -- cgit v1.2.1