implement in-place shrinking for large allocations
parent
645209dcbf
commit
3a532b17dc
25
malloc.c
25
malloc.c
|
@ -785,6 +785,7 @@ EXPORT void *h_realloc(void *old, size_t size) {
|
||||||
fatal_error("invalid realloc");
|
fatal_error("invalid realloc");
|
||||||
}
|
}
|
||||||
old_size = region->size;
|
old_size = region->size;
|
||||||
|
size_t old_guard_size = region->guard_size;
|
||||||
if (PAGE_CEILING(old_size) == PAGE_CEILING(size)) {
|
if (PAGE_CEILING(old_size) == PAGE_CEILING(size)) {
|
||||||
region->size = size;
|
region->size = size;
|
||||||
pthread_mutex_unlock(®ions_lock);
|
pthread_mutex_unlock(®ions_lock);
|
||||||
|
@ -792,6 +793,29 @@ EXPORT void *h_realloc(void *old, size_t size) {
|
||||||
}
|
}
|
||||||
pthread_mutex_unlock(®ions_lock);
|
pthread_mutex_unlock(®ions_lock);
|
||||||
|
|
||||||
|
// in-place shrink
|
||||||
|
if (size < old_size && size > max_slab_size_class) {
|
||||||
|
size_t rounded_size = PAGE_CEILING(size);
|
||||||
|
size_t old_rounded_size = PAGE_CEILING(old_size);
|
||||||
|
|
||||||
|
void *new_end = (char *)old + rounded_size;
|
||||||
|
if (memory_map_fixed(new_end, old_guard_size)) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
void *new_guard_end = (char *)new_end + old_guard_size;
|
||||||
|
memory_unmap(new_guard_end, old_rounded_size - rounded_size);
|
||||||
|
|
||||||
|
pthread_mutex_lock(®ions_lock);
|
||||||
|
struct region_info *region = regions_find(old);
|
||||||
|
if (region == NULL) {
|
||||||
|
fatal_error("invalid realloc");
|
||||||
|
}
|
||||||
|
region->size = size;
|
||||||
|
pthread_mutex_unlock(®ions_lock);
|
||||||
|
|
||||||
|
return old;
|
||||||
|
}
|
||||||
|
|
||||||
size_t copy_size = size < old_size ? size : old_size;
|
size_t copy_size = size < old_size ? size : old_size;
|
||||||
if (copy_size >= mremap_threshold) {
|
if (copy_size >= mremap_threshold) {
|
||||||
void *new = allocate(size);
|
void *new = allocate(size);
|
||||||
|
@ -804,7 +828,6 @@ EXPORT void *h_realloc(void *old, size_t size) {
|
||||||
if (region == NULL) {
|
if (region == NULL) {
|
||||||
fatal_error("invalid realloc");
|
fatal_error("invalid realloc");
|
||||||
}
|
}
|
||||||
size_t old_guard_size = region->guard_size;
|
|
||||||
regions_delete(region);
|
regions_delete(region);
|
||||||
pthread_mutex_unlock(®ions_lock);
|
pthread_mutex_unlock(®ions_lock);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue