OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/trace_event/memory_profiler_allocation_register.h" |
| 6 |
| 7 #include <windows.h> |
| 8 |
| 9 #include "base/bits.h" |
| 10 #include "base/logging.h" |
| 11 #include "base/process/process_metrics.h" |
| 12 |
| 13 namespace base { |
| 14 namespace trace_event { |
| 15 |
| 16 namespace { |
| 17 size_t GetGuardSize() { |
| 18 return GetPageSize(); |
| 19 } |
| 20 } |
| 21 |
| 22 // static |
| 23 void* AllocationRegister::AllocateVirtualMemory(size_t size) { |
| 24 size = bits::Align(size, GetPageSize()); |
| 25 |
| 26 // Add space for a guard page at the end. |
| 27 size_t map_size = size + GetGuardSize(); |
| 28 |
| 29 // Reserve the address space. This does not make the memory usable yet. |
| 30 void* addr = VirtualAlloc(nullptr, map_size, MEM_RESERVE, PAGE_NOACCESS); |
| 31 |
| 32 PCHECK(addr != nullptr); |
| 33 |
| 34 // Commit the non-guard pages as read-write memory. |
| 35 void* result = VirtualAlloc(addr, size, MEM_COMMIT, PAGE_READWRITE); |
| 36 |
| 37 PCHECK(result != nullptr); |
| 38 |
| 39 // Mark the last page of the allocated address space as guard page. (NB: The |
| 40 // |PAGE_GUARD| flag is not the flag to use here, that flag can be used to |
| 41 // detect and intercept access to a certain memory region. Accessing a |
| 42 // |PAGE_NOACCESS| page will raise a general protection fault.) The |
| 43 // read/write accessible space is still at least |min_size| bytes. |
| 44 void* guard_addr = |
| 45 reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(addr) + size); |
| 46 result = VirtualAlloc(guard_addr, GetGuardSize(), MEM_COMMIT, PAGE_NOACCESS); |
| 47 PCHECK(result != nullptr); |
| 48 |
| 49 return addr; |
| 50 } |
| 51 |
| 52 // static |
| 53 void AllocationRegister::FreeVirtualMemory(void* address, |
| 54 size_t allocated_size) { |
| 55 // For |VirtualFree|, the size passed with |MEM_RELEASE| mut be 0. Windows |
| 56 // automatically frees the entire region that was reserved by the |
| 57 // |VirtualAlloc| with flag |MEM_RESERVE|. |
| 58 VirtualFree(address, 0, MEM_RELEASE); |
| 59 } |
| 60 |
| 61 } // namespace trace_event |
| 62 } // namespace base |
OLD | NEW |