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

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: _linux -> _posix 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
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..3a188667eb7d4b6279747f674befdefd87ccd928
--- /dev/null
+++ b/base/trace_event/memory_profiler_allocation_register_win.cc
@@ -0,0 +1,56 @@
+// 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 {
+
+// static
+void* AllocationRegister::AllocateVirtualMemory(size_t min_size) {
+ size_t size = bits::Align(min_size, GetPageSize());
+
+ // Add space for a guard page at the end.
+ size_t map_size = size + GetPageSize();
+
+ // 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 = 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;
+}
+
+// static
+void AllocationRegister::FreeVirtualMemory(void* address,
+ size_t allocated_min_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

Powered by Google App Engine
This is Rietveld 408576698