Chromium Code Reviews| Index: base/trace_event/memory_profiler_allocation_register_win.cc |
| diff --git a/base/trace_event/memory_profiler_allocation_register_win.cc b/base/trace_event/memory_profiler_allocation_register_win.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..881840ad9b6201452b57869eb92fafe3901b71f1 |
| --- /dev/null |
| +++ b/base/trace_event/memory_profiler_allocation_register_win.cc |
| @@ -0,0 +1,63 @@ |
| +// 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
|
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "base/trace_event/memory_profiler_allocation_register.h" |
| + |
| +#include <windows.h> |
| + |
| +#include "base/logging.h" |
| + |
| +namespace base { |
| +namespace trace_event { |
| + |
| +size_t GetSystemPageSize() { |
| + SYSTEM_INFO system_info; |
| + GetSystemInfo(&system_info); |
| + return system_info.dwPageSize; |
| +} |
| + |
| +void* AllocateVirtualMemory(size_t min_size, VirtualMemoryGuard guard) { |
| + size_t size = RoundUpToPageSize(min_size); |
| + size_t map_size = size; |
| + |
| + if (guard == VirtualMemoryGuard::kGuardPageAfter) |
| + map_size += GetSystemPageSize(); |
| + |
| + // Reserve the address space. This does not make the memory usable yet. |
| + void* addr = VirtualAlloc(nullptr, map_size, MEM_RESERVE, PAGE_NOACCESS); |
| + |
| + PCHECK(addr != nullptr); |
| + |
| + // Commit the non-guard pages as read-write memory. |
| + void* result = VirtualAlloc(addr, size, MEM_COMMIT, PAGE_READWRITE); |
| + |
| + PCHECK(result != nullptr); |
| + |
| + // If there is a guard page after, mark the last page of the allocated |
| + // address space as such. (NB: The |PAGE_GUARD| flag is not the flag to use |
| + // here, that flag can be used to detect and intercept access to a certain |
| + // memory region. Accessing a |PAGE_NOACCESS| page will raise a general |
| + // protection fault.) The read/write accessible space is still at least |
| + // |min_size| bytes. |
| + if (guard == VirtualMemoryGuard::kGuardPageAfter) { |
| + void* guard_addr = static_cast<void*>(static_cast<uint8_t*>(addr) + size); |
| + size_t guard_size = map_size - size; |
| + result = VirtualAlloc(guard_addr, guard_size, MEM_COMMIT, PAGE_NOACCESS); |
| + PCHECK(result != nullptr); |
| + } |
| + |
| + return addr; |
| +} |
| + |
| +void FreeVirtualMemory(void* address, |
| + size_t allocated_min_size, |
| + VirtualMemoryGuard allocated_guard) { |
| + // For |VirtualFree|, the size passed with |MEM_RELEASE| mut be 0. Windows |
| + // automatically frees the entire region that was reserved by the |
| + // |VirtualAlloc| with flag |MEM_RESERVE|. |
| + VirtualFree(address, 0, MEM_RELEASE); |
| +} |
| + |
| +} // namespace trace_event |
| +} // namespace base |