Chromium Code Reviews| Index: base/metrics/persistent_memory_allocator.cc |
| diff --git a/base/metrics/persistent_memory_allocator.cc b/base/metrics/persistent_memory_allocator.cc |
| index bc873fefa0aeebc1b631861d594e55bf4919cc69..39bdb40a6ced245069d479ddb78aacff23e603f7 100644 |
| --- a/base/metrics/persistent_memory_allocator.cc |
| +++ b/base/metrics/persistent_memory_allocator.cc |
| @@ -7,6 +7,12 @@ |
| #include <assert.h> |
| #include <algorithm> |
| +#if defined(OS_WIN) |
| +#include "winbase.h" |
| +#elif defined(OS_POSIX) |
| +#include <sys/mman.h> |
| +#endif |
| + |
| #include "base/files/memory_mapped_file.h" |
| #include "base/logging.h" |
| #include "base/memory/shared_memory.h" |
| @@ -716,11 +722,44 @@ LocalPersistentMemoryAllocator::LocalPersistentMemoryAllocator( |
| size_t size, |
| uint64_t id, |
| base::StringPiece name) |
| - : PersistentMemoryAllocator(memset(new char[size], 0, size), |
| + : PersistentMemoryAllocator(AllocateLocalMemory(size), |
| size, 0, id, name, false) {} |
| LocalPersistentMemoryAllocator::~LocalPersistentMemoryAllocator() { |
| - delete [] mem_base_; |
| + DeallocateLocalMemory(const_cast<char*>(mem_base_), mem_size_); |
| +} |
| + |
| +// static |
| +void* LocalPersistentMemoryAllocator::AllocateLocalMemory(size_t size) { |
| +#if defined(OS_WIN) |
| + void* address = |
| + ::VirtualAlloc(nullptr, size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); |
| + DCHECK(address) << ::GetLastError(); |
|
Ilya Sherman
2016/06/01 23:34:58
nit: Mebbe use PCHECK, here and elsewhere where yo
bcwhite
2016/06/02 13:47:05
Done.
|
| + return address; |
| +#elif defined(OS_POSIX) |
| + // MAP_ANON is deprecated on Linux but MAP_ANONYMOUS is not universal on Mac. |
| + // MAP_SHARED is not available on Linux <2.4 but required on Mac. |
|
Ilya Sherman
2016/06/01 23:34:58
Would it make more sense to have separate code pat
bcwhite
2016/06/02 13:47:05
I considered that but in the end decided that a co
|
| + void* address = ::mmap(nullptr, size, PROT_READ | PROT_WRITE, |
| + MAP_ANON | MAP_SHARED, -1, 0); |
| + DCHECK_NE(MAP_FAILED, address) << errno; |
| + return address; |
| +#else |
| +#error This architecture is not (yet) supported. |
| +#endif |
| +} |
| + |
| +// static |
| +void LocalPersistentMemoryAllocator::DeallocateLocalMemory(void* memory, |
| + size_t size) { |
| +#if defined(OS_WIN) |
| + BOOL success = ::VirtualFree(memory, 0, MEM_DECOMMIT); |
| + DCHECK(success) << ::GetLastError(); |
| +#elif defined(OS_POSIX) |
| + int result = ::munmap(memory, size); |
| + DCHECK_EQ(0, result) << errno; |
| +#else |
| +#error This architecture is not (yet) supported. |
| +#endif |
| } |