OLD | NEW |
---|---|
(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 <sys/mman.h> | |
8 #include <unistd.h> | |
9 | |
10 #include "base/basictypes.h" | |
11 #include "base/bits.h" | |
12 #include "base/logging.h" | |
13 #include "base/process/process_metrics.h" | |
14 | |
15 namespace base { | |
16 namespace trace_event { | |
17 | |
18 // static | |
19 void* AllocationRegister::AllocateVirtualMemory(size_t min_size) { | |
20 size_t size = bits::Align(min_size, GetPageSize()); | |
21 | |
22 // Add space for a guard page at the end. | |
23 size_t map_size = size + GetPageSize(); | |
24 | |
25 void* addr = mmap(nullptr, map_size, PROT_READ | PROT_WRITE, | |
26 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); | |
Primiano Tucci (use gerrit)
2015/10/06 13:34:44
I think you need some ifdef guard here, IIRC MAP_A
Ruud van Asseldonk
2015/10/06 15:44:35
Yep, found that out via the trybots. PartitionAllo
| |
27 | |
28 PCHECK(addr != MAP_FAILED); | |
29 | |
30 // Mark the last page of the allocated address space as inaccessible | |
31 // (PROT_NONE). The read/write accessible space is still at least |min_size| | |
32 // bytes. | |
33 void* guard_addr = static_cast<void*>(static_cast<uint8_t*>(addr) + size); | |
Primiano Tucci (use gerrit)
2015/10/06 13:34:44
s/uint8_t/uintptr_t/ (and use reinterpret_cast).
a
Ruud van Asseldonk
2015/10/06 15:44:35
Done. I casted to |uint8_t| because this is really
| |
34 size_t guard_size = map_size - size; | |
Primiano Tucci (use gerrit)
2015/10/06 13:34:44
probably the code here would be a bit more readabl
Ruud van Asseldonk
2015/10/06 15:44:35
As you wish.
| |
35 int result = mprotect(guard_addr, guard_size, PROT_NONE); | |
36 PCHECK(result == 0); | |
37 | |
38 return addr; | |
39 } | |
40 | |
41 // static | |
42 void AllocationRegister::FreeVirtualMemory(void* address, | |
43 size_t allocated_min_size) { | |
44 size_t size = bits::Align(allocated_min_size, GetPageSize()) + GetPageSize(); | |
45 munmap(address, size); | |
46 } | |
47 | |
48 } // namespace trace_event | |
49 } // namespace base | |
OLD | NEW |