add wrapper for mremap with sanity check
This commit is contained in:
parent
5bc6820c24
commit
1cb28531a8
4
malloc.c
4
malloc.c
@ -778,7 +778,7 @@ EXPORT void *h_realloc(void *old, size_t size) {
|
|||||||
regions_delete(region);
|
regions_delete(region);
|
||||||
pthread_mutex_unlock(®ions_lock);
|
pthread_mutex_unlock(®ions_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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
11
memory.c
11
memory.c
@ -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;
|
||||||
|
}
|
||||||
|
1
memory.h
1
memory.h
@ -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
|
||||||
|
Loading…
Reference in New Issue
Block a user