| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/allocator/partition_allocator/page_allocator.h" | 5 #include "base/allocator/partition_allocator/page_allocator.h" |
| 6 | 6 |
| 7 #include <limits.h> | 7 #include <limits.h> |
| 8 | 8 |
| 9 #include <atomic> | 9 #include <atomic> |
| 10 | 10 |
| (...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 206 | 206 |
| 207 bool SetSystemPagesAccessible(void* address, size_t length) { | 207 bool SetSystemPagesAccessible(void* address, size_t length) { |
| 208 DCHECK(!(length & kSystemPageOffsetMask)); | 208 DCHECK(!(length & kSystemPageOffsetMask)); |
| 209 #if defined(OS_POSIX) | 209 #if defined(OS_POSIX) |
| 210 return !mprotect(address, length, PROT_READ | PROT_WRITE); | 210 return !mprotect(address, length, PROT_READ | PROT_WRITE); |
| 211 #else | 211 #else |
| 212 return !!VirtualAlloc(address, length, MEM_COMMIT, PAGE_READWRITE); | 212 return !!VirtualAlloc(address, length, MEM_COMMIT, PAGE_READWRITE); |
| 213 #endif | 213 #endif |
| 214 } | 214 } |
| 215 | 215 |
| 216 void DecommitSystemPages(void* address, size_t length) { | 216 void DecommitSystemPages(void* address, size_t length, bool purge_hint) { |
| 217 DCHECK(!(length & kSystemPageOffsetMask)); | 217 DCHECK(!(length & kSystemPageOffsetMask)); |
| 218 ALLOW_UNUSED_LOCAL(purge_hint); |
| 218 #if defined(OS_POSIX) | 219 #if defined(OS_POSIX) |
| 219 #if defined(OS_MACOSX) | 220 #if defined(OS_MACOSX) |
| 220 // On macOS, MADV_FREE_REUSABLE has comparable behavior to MADV_FREE, but also | 221 // On macOS, MADV_FREE_REUSABLE has comparable behavior to MADV_FREE, but also |
| 221 // marks the pages with the reusable bit, which allows both Activity Monitor | 222 // marks the pages with the reusable bit, which allows both Activity Monitor |
| 222 // and memory-infra to correctly track the pages. | 223 // and memory-infra to correctly track the pages. |
| 223 int ret = madvise(address, length, MADV_FREE_REUSABLE); | 224 int ret = madvise(address, length, MADV_FREE_REUSABLE); |
| 224 #else | 225 #else |
| 225 int ret = madvise(address, length, MADV_FREE); | 226 int advice = MADV_FREE; |
| 227 // For platforms known to support MADV_REMOVE, use it to promptly |
| 228 // let go of the pages. |purge_hint| is otherwise disregarded, leaving |
| 229 // it to the OS' policy for recycling MADV_FREE'd pages. |
| 230 #if defined(OS_LINUX) || defined(OS_ANDROID) |
| 231 if (purge_hint) |
| 232 advice = MADV_REMOVE; |
| 233 #endif |
| 234 int ret = madvise(address, length, advice); |
| 235 #if defined(OS_LINUX) || defined(OS_ANDROID) |
| 236 // If MADV_REMOVE's zero'ing fails due to not being writable, |
| 237 // fall back to freeing. |
| 238 if (ret != 0 && purge_hint) |
| 239 ret = madvise(address, length, MADV_FREE); |
| 240 #endif |
| 226 #endif | 241 #endif |
| 227 if (ret != 0 && errno == EINVAL) { | 242 if (ret != 0 && errno == EINVAL) { |
| 228 // MADV_FREE only works on Linux 4.5+ . If request failed, | 243 // MADV_FREE only works on Linux 4.5+ . If request failed, |
| 229 // retry with older MADV_DONTNEED . Note that MADV_FREE | 244 // retry with older MADV_DONTNEED . Note that MADV_FREE |
| 230 // being defined at compile time doesn't imply runtime support. | 245 // being defined at compile time doesn't imply runtime support. |
| 231 ret = madvise(address, length, MADV_DONTNEED); | 246 ret = madvise(address, length, MADV_DONTNEED); |
| 232 } | 247 } |
| 233 CHECK(!ret); | 248 CHECK(!ret); |
| 234 #else | 249 #else |
| 235 SetSystemPagesInaccessible(address, length); | 250 SetSystemPagesInaccessible(address, length); |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 277 CHECK(ret); | 292 CHECK(ret); |
| 278 } | 293 } |
| 279 #endif | 294 #endif |
| 280 } | 295 } |
| 281 | 296 |
| 282 uint32_t GetAllocPageErrorCode() { | 297 uint32_t GetAllocPageErrorCode() { |
| 283 return s_allocPageErrorCode; | 298 return s_allocPageErrorCode; |
| 284 } | 299 } |
| 285 | 300 |
| 286 } // namespace base | 301 } // namespace base |
| OLD | NEW |