more conservative shrinking of memory

This commit is contained in:
daan 2019-07-02 22:49:12 -07:00
parent 5c0ffe9865
commit 10a29f17c8
3 changed files with 39 additions and 28 deletions

View File

@ -60,8 +60,8 @@ terms of the MIT license. A copy of the license can be found in the file
void* calloc(size_t size, size_t n) mi_attr_noexcept MI_FORWARD2(mi_calloc, size, n);
void* realloc(void* p, size_t newsize) mi_attr_noexcept MI_FORWARD2(mi_realloc, p, newsize);
void free(void* p) mi_attr_noexcept MI_FORWARD0(mi_free, p);
char* strdup(const char* s) MI_FORWARD1(mi_strdup, s);
char* strndup(const char* s, size_t n) MI_FORWARD2(mi_strndup, s, n);
//char* strdup(const char* s) MI_FORWARD1(mi_strdup, s);
//char* strndup(const char* s, size_t n) MI_FORWARD2(mi_strndup, s, n);
#endif
#if (defined(__GNUC__) || defined(__clang__)) && !defined(__MACH__)

View File

@ -276,15 +276,19 @@ static void* mi_os_mem_alloc_aligned(size_t size, size_t alignment, bool commit,
return p;
}
// Conservatively OS page align within a given area
static void* mi_os_page_align_area(void* addr, size_t size, size_t* newsize) {
// OS page align within a given area,
// either conservative (pages inside the area only),
// or not (straddling pages outside the area is possible)
static void* mi_os_page_align_areax(bool conservative, void* addr, size_t size, size_t* newsize) {
mi_assert(addr != NULL && size > 0);
if (newsize != NULL) *newsize = 0;
if (size == 0 || addr == NULL) return NULL;
// page align conservatively within the range
void* start = mi_align_up_ptr(addr, _mi_os_page_size());
void* end = mi_align_down_ptr((uint8_t*)addr + size, _mi_os_page_size());
void* start = (conservative ? mi_align_up_ptr(addr, _mi_os_page_size())
: mi_align_down_ptr(addr, _mi_os_page_size()));
void* end = (conservative ? mi_align_down_ptr((uint8_t*)addr + size, _mi_os_page_size())
: mi_align_up_ptr((uint8_t*)addr + size, _mi_os_page_size()));
ptrdiff_t diff = (uint8_t*)end - (uint8_t*)start;
if (diff <= 0) return NULL;
@ -293,6 +297,12 @@ static void* mi_os_page_align_area(void* addr, size_t size, size_t* newsize) {
return start;
}
static void* mi_os_page_align_area_conservative(void* addr, size_t size, size_t* newsize) {
return mi_os_page_align_areax(true,addr,size,newsize);
}
// Signal to the OS that the address range is no longer in use
// but may be used later again. This will release physical memory
// pages and reduce swapping while keeping the memory committed.
@ -300,7 +310,7 @@ static void* mi_os_page_align_area(void* addr, size_t size, size_t* newsize) {
bool _mi_os_reset(void* addr, size_t size, mi_stats_t* stats) {
// page align conservatively within the range
size_t csize;
void* start = mi_os_page_align_area(addr,size,&csize);
void* start = mi_os_page_align_area_conservative(addr,size,&csize);
if (csize==0) return true;
UNUSED(stats); // if !STATS
mi_stat_increase(stats->reset, csize);
@ -346,7 +356,7 @@ bool _mi_os_reset(void* addr, size_t size, mi_stats_t* stats) {
static bool mi_os_protectx(void* addr, size_t size, bool protect) {
// page align conservatively within the range
size_t csize = 0;
void* start = mi_os_page_align_area(addr, size, &csize);
void* start = mi_os_page_align_area_conservative(addr, size, &csize);
if (csize==0) return false;
int err = 0;
@ -374,9 +384,9 @@ bool _mi_os_unprotect(void* addr, size_t size) {
// Commit/Decommit memory.
// We page align to a conservative area inside the range to reset.
static bool mi_os_commitx(void* addr, size_t size, bool commit, mi_stats_t* stats) {
// page align conservatively within the range
// page align in the range, commit liberally, decommit conservative
size_t csize;
void* start = mi_os_page_align_area(addr, size, &csize);
void* start = mi_os_page_align_areax(!commit, addr, size, &csize);
if (csize == 0) return true;
int err = 0;
UNUSED(stats); // if !STATS
@ -424,12 +434,12 @@ bool _mi_os_shrink(void* p, size_t oldsize, size_t newsize, mi_stats_t* stats) {
// oldsize and newsize should be page aligned or we cannot shrink precisely
void* addr = (uint8_t*)p + newsize;
size_t size = 0;
void* start = mi_os_page_align_area(addr, oldsize - newsize, &size);
void* start = mi_os_page_align_area_conservative(addr, oldsize - newsize, &size);
if (size==0 || start != addr) return false;
#ifdef _WIN32
// we cannot shrink on windows
return false;
// we cannot shrink on windows, but we can decommit
return mi_os_decommit(start, size, stats);
#else
return mi_os_mem_free(start, size, stats);
#endif
@ -448,7 +458,8 @@ void* _mi_os_alloc(size_t size, mi_stats_t* stats) {
}
void _mi_os_free(void* p, size_t size, mi_stats_t* stats) {
UNUSED(stats);
if (size==0) return;
size = mi_os_good_alloc_size(size, 0);
mi_os_mem_free(p, size, stats);
}