From 8e0d846b40fa44225227e01531c6b45a1f6f79a8 Mon Sep 17 00:00:00 2001 From: daan Date: Sun, 6 Sep 2020 12:19:05 -0700 Subject: [PATCH] consistent commit order --- src/os.c | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/src/os.c b/src/os.c index 13785b65..7cede199 100644 --- a/src/os.c +++ b/src/os.c @@ -704,8 +704,7 @@ static bool mi_os_commitx(void* addr, size_t size, bool commit, bool conservativ #if defined(_WIN32) if (commit) { - // if the memory was already committed, the call succeeds but it is not zero'd - // *is_zero = true; + // *is_zero = true; // note: if the memory was already committed, the call succeeds but the memory is not zero'd void* p = VirtualAlloc(start, csize, MEM_COMMIT, PAGE_READWRITE); err = (p == start ? 0 : GetLastError()); } @@ -717,22 +716,27 @@ static bool mi_os_commitx(void* addr, size_t size, bool commit, bool conservativ // WebAssembly guests can't control memory protection #elif defined(MAP_FIXED) && !defined(__APPLE__) // Linux - if (!commit) { + if (commit) { + // commit: just change the protection + err = mprotect(start, csize, (PROT_READ | PROT_WRITE)); + if (err != 0) { err = errno; } + } + else { // decommit: use mmap with MAP_FIXED to discard the existing memory (and reduce rss) const int fd = mi_unix_mmap_fd(); void* p = mmap(start, csize, PROT_NONE, (MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE), fd, 0); if (p != start) { err = errno; } } - else { - // commit: just change the protection + #else + // macOSX and others. + if (commit) { + // commit: ensure we can access the area err = mprotect(start, csize, (PROT_READ | PROT_WRITE)); if (err != 0) { err = errno; } - } - #else - // MacOS and others. - if (!commit) { + } + else { #if defined(MADV_DONTNEED) - // decommit: use MADV_DONTNEED as it decrease rss immediately (unlike MADV_FREE) + // decommit: use MADV_DONTNEED as it decreases rss immediately (unlike MADV_FREE) err = madvise(start, csize, MADV_DONTNEED); #else // decommit: just disable access @@ -740,11 +744,6 @@ static bool mi_os_commitx(void* addr, size_t size, bool commit, bool conservativ if (err != 0) { err = errno; } #endif } - else { - // commit: ensure we can access the area - err = mprotect(start, csize, (PROT_READ | PROT_WRITE)); - if (err != 0) { err = errno; } - } #endif if (err != 0) { _mi_warning_message("%s error: start: %p, csize: 0x%x, err: %i\n", commit ? "commit" : "decommit", start, csize, err);