summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakob Kaivo <jkk@ung.org>2020-07-15 16:43:26 -0400
committerJakob Kaivo <jkk@ung.org>2020-07-15 16:43:26 -0400
commit83e70e1b6c7abd0315dd8cd5a98a91c58e0cd8bd (patch)
treeaea513037935d8d26717f5da7f6455586b8ea204
parentf94c3b213545868bfb9e353aa926071f303eb9d8 (diff)
munmap() extra pages during realloc()
-rw-r--r--src/mapalloc.c7
1 files changed, 6 insertions, 1 deletions
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;
}