clarify needs_recommit

This commit is contained in:
daanx 2023-04-04 13:05:48 -07:00
parent 5c39fe7246
commit 1d231be758
5 changed files with 22 additions and 22 deletions

View file

@ -47,11 +47,11 @@ int _mi_prim_alloc(size_t size, size_t try_alignment, bool commit, bool allow_la
// For example, on Linux this would make the memory PROT_READ|PROT_WRITE. // For example, on Linux this would make the memory PROT_READ|PROT_WRITE.
int _mi_prim_commit(void* addr, size_t size); int _mi_prim_commit(void* addr, size_t size);
// Decommit memory. Returns error code or 0 on success. The `decommitted` result is true // Decommit memory. Returns error code or 0 on success. The `needs_recommit` result is true
// if the memory would need to be re-committed. For example, on Windows this is always true, // if the memory would need to be re-committed. For example, on Windows this is always true,
// but on Linux we could use MADV_DONTNEED to decommit which does not need a recommit. // but on Linux we could use MADV_DONTNEED to decommit which does not need a recommit.
// pre: decommitted != NULL // pre: needs_recommit != NULL
int _mi_prim_decommit(void* addr, size_t size, bool* decommitted); int _mi_prim_decommit(void* addr, size_t size, bool* needs_recommit);
// Reset memory. The range keeps being accessible but the content might be reset. // Reset memory. The range keeps being accessible but the content might be reset.
// Returns error code or 0 on success. // Returns error code or 0 on success.

View file

@ -377,10 +377,10 @@ bool _mi_os_commit(void* addr, size_t size, bool* is_zero, mi_stats_t* tld_stats
return (err == 0); return (err == 0);
} }
static bool mi_os_decommit_ex(void* addr, size_t size, bool* decommitted, mi_stats_t* tld_stats) { static bool mi_os_decommit_ex(void* addr, size_t size, bool* needs_recommit, mi_stats_t* tld_stats) {
MI_UNUSED(tld_stats); MI_UNUSED(tld_stats);
mi_stats_t* stats = &_mi_stats_main; mi_stats_t* stats = &_mi_stats_main;
mi_assert_internal(decommitted!=NULL); mi_assert_internal(needs_recommit!=NULL);
_mi_stat_decrease(&stats->committed, size); _mi_stat_decrease(&stats->committed, size);
// page align // page align
@ -389,8 +389,8 @@ static bool mi_os_decommit_ex(void* addr, size_t size, bool* decommitted, mi_sta
if (csize == 0) return true; if (csize == 0) return true;
// decommit // decommit
*decommitted = true; *needs_recommit = true;
int err = _mi_prim_decommit(start,csize,decommitted); int err = _mi_prim_decommit(start,csize,needs_recommit);
if (err != 0) { if (err != 0) {
_mi_warning_message("cannot decommit OS memory (error: %d (0x%x), address: %p, size: 0x%zx bytes)\n", err, err, start, csize); _mi_warning_message("cannot decommit OS memory (error: %d (0x%x), address: %p, size: 0x%zx bytes)\n", err, err, start, csize);
} }
@ -399,8 +399,8 @@ static bool mi_os_decommit_ex(void* addr, size_t size, bool* decommitted, mi_sta
} }
bool _mi_os_decommit(void* addr, size_t size, mi_stats_t* tld_stats) { bool _mi_os_decommit(void* addr, size_t size, mi_stats_t* tld_stats) {
bool decommitted = true; bool needs_recommit;
return mi_os_decommit_ex(addr, size, &decommitted, tld_stats); return mi_os_decommit_ex(addr, size, &needs_recommit, tld_stats);
} }
@ -427,8 +427,8 @@ bool _mi_os_reset(void* addr, size_t size, mi_stats_t* stats) {
} }
// either resets or decommits memory, returns true if the memory was decommitted // either resets or decommits memory, returns true if the memory needs
// (in the sense that it needs to be re-committed if the memory is re-used later on). // to be recommitted if it is to be re-used later on.
bool _mi_os_purge(void* p, size_t size, mi_stats_t* stats) bool _mi_os_purge(void* p, size_t size, mi_stats_t* stats)
{ {
if (!mi_option_is_enabled(mi_option_allow_purge)) return false; if (!mi_option_is_enabled(mi_option_allow_purge)) return false;
@ -436,9 +436,9 @@ bool _mi_os_purge(void* p, size_t size, mi_stats_t* stats)
if (mi_option_is_enabled(mi_option_purge_decommits) && // should decommit? if (mi_option_is_enabled(mi_option_purge_decommits) && // should decommit?
!_mi_preloading()) // don't decommit during preloading (unsafe) !_mi_preloading()) // don't decommit during preloading (unsafe)
{ {
bool decommitted; bool needs_recommit;
mi_os_decommit_ex(p, size, &decommitted, stats); mi_os_decommit_ex(p, size, &needs_recommit, stats);
return decommitted; return needs_recommit;
} }
else { else {
_mi_os_reset(p, size, stats); _mi_os_reset(p, size, stats);

View file

@ -352,16 +352,16 @@ int _mi_prim_commit(void* start, size_t size) {
return err; return err;
} }
int _mi_prim_decommit(void* start, size_t size, bool* decommitted) { int _mi_prim_decommit(void* start, size_t size, bool* needs_recommit) {
int err = 0; int err = 0;
#if defined(MADV_DONTNEED) && !MI_DEBUG && !MI_SECURE #if defined(MADV_DONTNEED) && !MI_DEBUG && !MI_SECURE
// decommit: use MADV_DONTNEED as it decreases rss immediately (unlike MADV_FREE) // decommit: use MADV_DONTNEED as it decreases rss immediately (unlike MADV_FREE)
// (on the other hand, MADV_FREE would be good enough.. it is just not reflected in the stats :-( ) // (on the other hand, MADV_FREE would be good enough.. it is just not reflected in the stats :-( )
*decommitted = false; *needs_recommit = false;
err = unix_madvise(start, size, MADV_DONTNEED); err = unix_madvise(start, size, MADV_DONTNEED);
#else #else
// decommit: just disable access (also used in debug and secure mode to trap on illegal access) // decommit: just disable access (also used in debug and secure mode to trap on illegal access)
*decommitted = true; // needs recommit to reuse the memory *needs_recommit = true; // needs recommit to reuse the memory
err = mprotect(start, size, PROT_NONE); err = mprotect(start, size, PROT_NONE);
if (err != 0) { err = errno; } if (err != 0) { err = errno; }
#endif #endif

View file

@ -131,9 +131,9 @@ int _mi_prim_commit(void* addr, size_t size) {
return 0; return 0;
} }
int _mi_prim_decommit(void* addr, size_t size, bool* decommitted) { int _mi_prim_decommit(void* addr, size_t size, bool* needs_recommit) {
MI_UNUSED(addr); MI_UNUSED(size); MI_UNUSED(addr); MI_UNUSED(size);
*decommitted = false; *needs_recommit = false;
return 0; return 0;
} }

View file

@ -262,9 +262,9 @@ int _mi_prim_commit(void* addr, size_t size) {
return (p == addr ? 0 : (int)GetLastError()); return (p == addr ? 0 : (int)GetLastError());
} }
int _mi_prim_decommit(void* addr, size_t size, bool* decommitted) { int _mi_prim_decommit(void* addr, size_t size, bool* needs_recommit) {
BOOL ok = VirtualFree(addr, size, MEM_DECOMMIT); BOOL ok = VirtualFree(addr, size, MEM_DECOMMIT);
*decommitted = true; // for safetly, assume always decommitted even in the case of an error. *needs_recommit = true; // for safetly, assume always decommitted even in the case of an error.
return (ok ? 0 : (int)GetLastError()); return (ok ? 0 : (int)GetLastError());
} }