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

Unified Diff: base/trace_event/memory_profiler_allocation_register_linux.cc

Issue 1371053002: [Tracing] Add allocation register for heap profiling (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@backtrace
Patch Set: Created 5 years, 3 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_linux.cc
diff --git a/base/trace_event/memory_profiler_allocation_register_linux.cc b/base/trace_event/memory_profiler_allocation_register_linux.cc
new file mode 100644
index 0000000000000000000000000000000000000000..4760682fd3a64344cc2b03a97208e79a574ee2ae
--- /dev/null
+++ b/base/trace_event/memory_profiler_allocation_register_linux.cc
@@ -0,0 +1,57 @@
+// 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/logging.h"
+
+namespace base {
+namespace trace_event {
+
+size_t GetSystemPageSize() {
+ return sysconf(_SC_PAGE_SIZE);
+}
+
+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();
+
+ void* addr = mmap(nullptr, map_size, PROT_READ | PROT_WRITE,
+ MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+
+ PCHECK(addr != MAP_FAILED);
+
+ // If there is a guard page after, 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.
+ if (guard == VirtualMemoryGuard::kGuardPageAfter) {
+ void* guard_addr = static_cast<void*>(static_cast<uint8_t*>(addr) + size);
+ size_t guard_size = map_size - size;
+ int result = mprotect(guard_addr, guard_size, PROT_NONE);
+ PCHECK(result == 0);
+ }
+
+ return addr;
+}
+
+void FreeVirtualMemory(void* address,
+ size_t allocated_min_size,
+ VirtualMemoryGuard allocated_guard) {
+ size_t size = RoundUpToPageSize(allocated_min_size);
+
+ if (allocated_guard == VirtualMemoryGuard::kGuardPageAfter)
+ size += GetSystemPageSize();
+
+ munmap(address, size);
+}
+
+} // namespace trace_event
+} // namespace base

Powered by Google App Engine
This is Rietveld 408576698