Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(718)

Unified Diff: base/trace_event/memory_profiler_allocation_register_win.cc

Issue 1371053002: [Tracing] Add allocation register for heap profiling (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@backtrace
Patch Set: Address primiano comments + try to fix build on Win/Mac Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/trace_event/memory_profiler_allocation_register_unittest.cc ('k') | base/trace_event/trace_event.gypi » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..b94c75e35e2511cae9d2907d435203ae0c857007
--- /dev/null
+++ b/base/trace_event/memory_profiler_allocation_register_win.cc
@@ -0,0 +1,62 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// 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/bits.h"
+#include "base/logging.h"
+#include "base/process/process_metrics.h"
+
+namespace base {
+namespace trace_event {
+
+namespace {
+size_t GetGuardSize() {
+ return GetPageSize();
+}
+}
+
+// static
+void* AllocationRegister::AllocateVirtualMemory(size_t size) {
+ size = bits::Align(size, GetPageSize());
+
+ // Add space for a guard page at the end.
+ size_t map_size = size + GetGuardSize();
+
+ // 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);
+
+ // Mark the last page of the allocated address space as guard page. (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.
+ void* guard_addr =
+ reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(addr) + size);
+ result = VirtualAlloc(guard_addr, GetGuardSize(), MEM_COMMIT, PAGE_NOACCESS);
+ PCHECK(result != nullptr);
+
+ return addr;
+}
+
+// static
+void AllocationRegister::FreeVirtualMemory(void* address,
+ size_t allocated_size) {
+ // 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
« no previous file with comments | « base/trace_event/memory_profiler_allocation_register_unittest.cc ('k') | base/trace_event/trace_event.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698