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/logging.h" |
| 12 |
| 13 namespace base { |
| 14 namespace trace_event { |
| 15 |
| 16 size_t GetSystemPageSize() { |
| 17 return sysconf(_SC_PAGE_SIZE); |
| 18 } |
| 19 |
| 20 void* AllocateVirtualMemory(size_t min_size, VirtualMemoryGuard guard) { |
| 21 size_t size = RoundUpToPageSize(min_size); |
| 22 size_t map_size = size; |
| 23 |
| 24 if (guard == VirtualMemoryGuard::kGuardPageAfter) |
| 25 map_size += GetSystemPageSize(); |
| 26 |
| 27 void* addr = mmap(nullptr, map_size, PROT_READ | PROT_WRITE, |
| 28 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
| 29 |
| 30 PCHECK(addr != MAP_FAILED); |
| 31 |
| 32 // If there is a guard page after, mark the last page of the allocated |
| 33 // address space as inaccessible (PROT_NONE). The read/write accessible space |
| 34 // is still at least |min_size| bytes. |
| 35 if (guard == VirtualMemoryGuard::kGuardPageAfter) { |
| 36 void* guard_addr = static_cast<void*>(static_cast<uint8_t*>(addr) + size); |
| 37 size_t guard_size = map_size - size; |
| 38 int result = mprotect(guard_addr, guard_size, PROT_NONE); |
| 39 PCHECK(result == 0); |
| 40 } |
| 41 |
| 42 return addr; |
| 43 } |
| 44 |
| 45 void FreeVirtualMemory(void* address, |
| 46 size_t allocated_min_size, |
| 47 VirtualMemoryGuard allocated_guard) { |
| 48 size_t size = RoundUpToPageSize(allocated_min_size); |
| 49 |
| 50 if (allocated_guard == VirtualMemoryGuard::kGuardPageAfter) |
| 51 size += GetSystemPageSize(); |
| 52 |
| 53 munmap(address, size); |
| 54 } |
| 55 |
| 56 } // namespace trace_event |
| 57 } // namespace base |
OLD | NEW |