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