add wrapper for mremap with sanity check

This commit is contained in:
Daniel Micay 2018-08-29 10:43:54 -04:00
parent 5bc6820c24
commit 1cb28531a8
3 changed files with 14 additions and 2 deletions

View File

@ -778,7 +778,7 @@ EXPORT void *h_realloc(void *old, size_t size) {
regions_delete(region); regions_delete(region);
pthread_mutex_unlock(&regions_lock); pthread_mutex_unlock(&regions_lock);
if (mremap(old, PAGE_CEILING(old_size), PAGE_CEILING(size), MREMAP_MAYMOVE|MREMAP_FIXED, new) == MAP_FAILED) { if (memory_remap_fixed(old, old_size, new, size)) {
memcpy(new, old, copy_size); memcpy(new, old, copy_size);
deallocate_pages(old, old_size, old_guard_size); deallocate_pages(old, old_size, old_guard_size);
} else { } else {
@ -933,7 +933,7 @@ EXPORT int h_malloc_trim(size_t pad) {
for (unsigned i = 0; i < N_SIZE_CLASSES; i++) { for (unsigned i = 0; i < N_SIZE_CLASSES; i++) {
struct size_class *c = &size_class_metadata[i]; struct size_class *c = &size_class_metadata[i];
pthread_mutex_lock(&c->mutex); pthread_mutex_lock(&c->mutex);
// TODO: purge and mprotect all free slabs // TODO: purge and memory protect all free slabs
pthread_mutex_unlock(&c->mutex); pthread_mutex_unlock(&c->mutex);
} }

View File

@ -31,3 +31,14 @@ int memory_protect(void *ptr, size_t size, int prot) {
} }
return ret; return ret;
} }
int memory_remap_fixed(void *old, size_t old_size, void *new, size_t new_size) {
void *ptr = mremap(old, old_size, new_size, MREMAP_MAYMOVE|MREMAP_FIXED, new);
if (unlikely(ptr == MAP_FAILED)) {
if (errno != ENOMEM) {
fatal_error("non-ENOMEM mremap failure");
}
return 1;
}
return 0;
}

View File

@ -6,5 +6,6 @@
void *memory_map(size_t size); void *memory_map(size_t size);
int memory_unmap(void *ptr, size_t size); int memory_unmap(void *ptr, size_t size);
int memory_protect(void *ptr, size_t size, int prot); int memory_protect(void *ptr, size_t size, int prot);
int memory_remap_fixed(void *old, size_t old_size, void *new, size_t new_size);
#endif #endif