Chromium Code Reviews| 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..5f39f89915fda1e435b5271f930dff06728c414f |
| --- /dev/null |
| +++ b/base/trace_event/memory_profiler_allocation_register_posix.cc |
| @@ -0,0 +1,49 @@ |
| +// 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" |
| + |
| +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(); |
| + |
| + void* addr = mmap(nullptr, map_size, PROT_READ | PROT_WRITE, |
| + 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
|
| + |
| + 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 = 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
|
| + 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.
|
| + int result = mprotect(guard_addr, guard_size, PROT_NONE); |
| + PCHECK(result == 0); |
| + |
| + return addr; |
| +} |
| + |
| +// static |
| +void AllocationRegister::FreeVirtualMemory(void* address, |
| + size_t allocated_min_size) { |
| + size_t size = bits::Align(allocated_min_size, GetPageSize()) + GetPageSize(); |
| + munmap(address, size); |
| +} |
| + |
| +} // namespace trace_event |
| +} // namespace base |