Made a mistake. Fixed for non-apple unix

This commit is contained in:
iurii zakipnyi 2025-06-04 10:29:39 -07:00
parent 157b201004
commit e784983b76

View file

@ -440,18 +440,18 @@ int _mi_prim_commit(void* start, size_t size, bool* is_zero) {
} }
int _mi_prim_decommit(void* start, size_t size, bool* needs_recommit) { int _mi_prim_decommit(void* start, size_t size, bool* needs_recommit) {
int err = 0;
#if defined(__APPLE__) #if defined(__APPLE__)
// On Apple platforms there is MADV_FREE_REUSABLE -- it marks memory as RESUABLE to reduce memory pressure. // On Apple platforms there is MADV_FREE_REUSABLE -- it marks memory as RESUABLE to reduce memory pressure.
err = unix_madvise(start, size, MADV_FREE_REUSABLE); int err = unix_madvise(start, size, MADV_FREE_REUSABLE);
// if this fails - fallback to MADV_DONTNEED if (err != 0) // if MADV_FREE_REUSABLE fails - fallback to MADV_DONTNEED
#endif {
// use MADV_DONTNEED as it decreases rss immediately (unlike MADV_FREE)
if (err) err = unix_madvise(start, size, MADV_DONTNEED);
{ }
#else
// use MADV_DONTNEED as it decreases rss immediately (unlike MADV_FREE) // use MADV_DONTNEED as it decreases rss immediately (unlike MADV_FREE)
err = unix_madvise(start, size, MADV_DONTNEED); int err = unix_madvise(start, size, MADV_DONTNEED);
} #endif
#if !MI_DEBUG && MI_SECURE<=2 #if !MI_DEBUG && MI_SECURE<=2
*needs_recommit = false; *needs_recommit = false;
@ -474,28 +474,27 @@ int _mi_prim_reset(void* start, size_t size) {
#if defined(__APPLE__) #if defined(__APPLE__)
// On Apple platforms there is MADV_FREE_REUSABLE -- it marks memory as RESUABLE to reduce memory pressure. // On Apple platforms there is MADV_FREE_REUSABLE -- it marks memory as RESUABLE to reduce memory pressure.
err = unix_madvise(start, size, MADV_FREE_REUSABLE); err = unix_madvise(start, size, MADV_FREE_REUSABLE);
if (err == 0)
return 0;
// if this fails - fallback to MADV_FREE/MADV_DONTNEED // if this fails - fallback to MADV_FREE/MADV_DONTNEED
#endif #endif
if (err) // We try to use `MADV_FREE` as that is the fastest. A drawback though is that it
{ // will not reduce the `rss` stats in tools like `top` even though the memory is available
// We try to use `MADV_FREE` as that is the fastest. A drawback though is that it // to other processes. With the default `MIMALLOC_PURGE_DECOMMITS=1` we ensure that by
// will not reduce the `rss` stats in tools like `top` even though the memory is available // default `MADV_DONTNEED` is used though.
// to other processes. With the default `MIMALLOC_PURGE_DECOMMITS=1` we ensure that by #if defined(MADV_FREE)
// default `MADV_DONTNEED` is used though. static _Atomic(size_t) advice = MI_ATOMIC_VAR_INIT(MADV_FREE);
#if defined(MADV_FREE) int oadvice = (int)mi_atomic_load_relaxed(&advice);
static _Atomic(size_t) advice = MI_ATOMIC_VAR_INIT(MADV_FREE); while ((err = unix_madvise(start, size, oadvice)) != 0 && errno == EAGAIN) { errno = 0; };
int oadvice = (int)mi_atomic_load_relaxed(&advice); if (err != 0 && errno == EINVAL && oadvice == MADV_FREE) {
while ((err = unix_madvise(start, size, oadvice)) != 0 && errno == EAGAIN) { errno = 0; }; // if MADV_FREE is not supported, fall back to MADV_DONTNEED from now on
if (err != 0 && errno == EINVAL && oadvice == MADV_FREE) { mi_atomic_store_release(&advice, (size_t)MADV_DONTNEED);
// if MADV_FREE is not supported, fall back to MADV_DONTNEED from now on
mi_atomic_store_release(&advice, (size_t)MADV_DONTNEED);
err = unix_madvise(start, size, MADV_DONTNEED);
}
#else
err = unix_madvise(start, size, MADV_DONTNEED); err = unix_madvise(start, size, MADV_DONTNEED);
#endif }
} #else
err = unix_madvise(start, size, MADV_DONTNEED);
#endif
return err; return err;
} }