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

Unified Diff: base/metrics/persistent_memory_allocator.cc

Issue 2016013003: Properly create zero-initialized local memory. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: cleaned up error message Created 4 years, 7 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/metrics/persistent_memory_allocator.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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
}
« no previous file with comments | « base/metrics/persistent_memory_allocator.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698