OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
Primiano Tucci (use gerrit)
2015/10/05 14:11:06
I'd probably defer introducing this file to once y
Ruud van Asseldonk
2015/10/06 09:26:45
I ran it on the Windows trybots to debug and it wo
| |
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/logging.h" | |
10 | |
11 namespace base { | |
12 namespace trace_event { | |
13 | |
14 size_t GetSystemPageSize() { | |
15 SYSTEM_INFO system_info; | |
16 GetSystemInfo(&system_info); | |
17 return system_info.dwPageSize; | |
18 } | |
19 | |
20 void* AllocateVirtualMemory(size_t min_size, VirtualMemoryGuard guard) { | |
21 size_t size = RoundUpToPageSize(min_size); | |
22 size_t map_size = size; | |
23 | |
24 if (guard == VirtualMemoryGuard::kGuardPageAfter) | |
25 map_size += GetSystemPageSize(); | |
26 | |
27 // Reserve the address space. This does not make the memory usable yet. | |
28 void* addr = VirtualAlloc(nullptr, map_size, MEM_RESERVE, PAGE_NOACCESS); | |
29 | |
30 PCHECK(addr != nullptr); | |
31 | |
32 // Commit the non-guard pages as read-write memory. | |
33 void* result = VirtualAlloc(addr, size, MEM_COMMIT, PAGE_READWRITE); | |
34 | |
35 PCHECK(result != nullptr); | |
36 | |
37 // If there is a guard page after, mark the last page of the allocated | |
38 // address space as such. (NB: The |PAGE_GUARD| flag is not the flag to use | |
39 // here, that flag can be used to detect and intercept access to a certain | |
40 // memory region. Accessing a |PAGE_NOACCESS| page will raise a general | |
41 // protection fault.) The read/write accessible space is still at least | |
42 // |min_size| bytes. | |
43 if (guard == VirtualMemoryGuard::kGuardPageAfter) { | |
44 void* guard_addr = static_cast<void*>(static_cast<uint8_t*>(addr) + size); | |
45 size_t guard_size = map_size - size; | |
46 result = VirtualAlloc(guard_addr, guard_size, MEM_COMMIT, PAGE_NOACCESS); | |
47 PCHECK(result != nullptr); | |
48 } | |
49 | |
50 return addr; | |
51 } | |
52 | |
53 void FreeVirtualMemory(void* address, | |
54 size_t allocated_min_size, | |
55 VirtualMemoryGuard allocated_guard) { | |
56 // For |VirtualFree|, the size passed with |MEM_RELEASE| mut be 0. Windows | |
57 // automatically frees the entire region that was reserved by the | |
58 // |VirtualAlloc| with flag |MEM_RESERVE|. | |
59 VirtualFree(address, 0, MEM_RELEASE); | |
60 } | |
61 | |
62 } // namespace trace_event | |
63 } // namespace base | |
OLD | NEW |