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

Side by Side 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "base/trace_event/memory_profiler_allocation_register.h"
6
7 #include <windows.h>
8
9 #include "base/bits.h"
10 #include "base/logging.h"
11 #include "base/process/process_metrics.h"
12
13 namespace base {
14 namespace trace_event {
15
16 // static
17 void* AllocationRegister::AllocateVirtualMemory(size_t min_size) {
18 size_t size = bits::Align(min_size, GetPageSize());
19
20 // Add space for a guard page at the end.
21 size_t map_size = size + GetPageSize();
22
23 // Reserve the address space. This does not make the memory usable yet.
24 void* addr = VirtualAlloc(nullptr, map_size, MEM_RESERVE, PAGE_NOACCESS);
25
26 PCHECK(addr != nullptr);
27
28 // Commit the non-guard pages as read-write memory.
29 void* result = VirtualAlloc(addr, size, MEM_COMMIT, PAGE_READWRITE);
30
31 PCHECK(result != nullptr);
32
33 // Mark the last page of the allocated address space as guard page. (NB: The
34 // |PAGE_GUARD| flag is not the flag to use here, that flag can be used to
35 // detect and intercept access to a certain memory region. Accessing a
36 // |PAGE_NOACCESS| page will raise a general protection fault.) The
37 // read/write accessible space is still at least |min_size| bytes.
38 void* guard_addr = static_cast<void*>(static_cast<uint8_t*>(addr) + size);
39 size_t guard_size = map_size - size;
40 result = VirtualAlloc(guard_addr, guard_size, MEM_COMMIT, PAGE_NOACCESS);
41 PCHECK(result != nullptr);
42
43 return addr;
44 }
45
46 // static
47 void AllocationRegister::FreeVirtualMemory(void* address,
48 size_t allocated_min_size) {
49 // For |VirtualFree|, the size passed with |MEM_RELEASE| mut be 0. Windows
50 // automatically frees the entire region that was reserved by the
51 // |VirtualAlloc| with flag |MEM_RESERVE|.
52 VirtualFree(address, 0, MEM_RELEASE);
53 }
54
55 } // namespace trace_event
56 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698