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

Unified Diff: base/trace_event/memory_profiler_allocation_register_posix.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
Index: base/trace_event/memory_profiler_allocation_register_posix.cc
diff --git a/base/trace_event/memory_profiler_allocation_register_posix.cc b/base/trace_event/memory_profiler_allocation_register_posix.cc
new file mode 100644
index 0000000000000000000000000000000000000000..e2231a84a39c286b33c2fc7af381458f17b25546
--- /dev/null
+++ b/base/trace_event/memory_profiler_allocation_register_posix.cc
@@ -0,0 +1,59 @@
+// 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 <sys/mman.h>
+#include <unistd.h>
+
+#include "base/basictypes.h"
+#include "base/bits.h"
+#include "base/logging.h"
+#include "base/process/process_metrics.h"
+
+#ifndef MAP_ANONYMOUS
+#define MAP_ANONYMOUS MAP_ANON
+#endif
+
+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();
+
+ void* addr = mmap(nullptr, map_size, PROT_READ | PROT_WRITE,
+ MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+
+ PCHECK(addr != MAP_FAILED);
+
+ // Mark the last page of the allocated address space as inaccessible
+ // (PROT_NONE). The read/write accessible space is still at least |min_size|
+ // bytes.
+ void* guard_addr =
+ reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(addr) + size);
+ int result = mprotect(guard_addr, GetGuardSize(), PROT_NONE);
+ PCHECK(result == 0);
+
+ return addr;
+}
+
+// static
+void AllocationRegister::FreeVirtualMemory(void* address,
+ size_t allocated_size) {
+ size_t size = bits::Align(allocated_size, GetPageSize()) + GetGuardSize();
+ munmap(address, size);
+}
+
+} // namespace trace_event
+} // namespace base

Powered by Google App Engine
This is Rietveld 408576698