~jkaivo/mapalloc

83e70e1b6c7abd0315dd8cd5a98a91c58e0cd8bd — Jakob Kaivo 2 years ago f94c3b2
munmap() extra pages during realloc()
1 files changed, 6 insertions(+), 1 deletions(-)

M src/mapalloc.c
M src/mapalloc.c => src/mapalloc.c +6 -1
@@ 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;
	}