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

Side by Side 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: use DPCHECK instead of DCHECK to print system error messages (and rebased) Created 4 years, 6 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 unified diff | Download patch
« no previous file with comments | « base/metrics/persistent_memory_allocator.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/metrics/persistent_memory_allocator.h" 5 #include "base/metrics/persistent_memory_allocator.h"
6 6
7 #include <assert.h> 7 #include <assert.h>
8 #include <algorithm> 8 #include <algorithm>
9 9
10 #if defined(OS_WIN)
11 #include "winbase.h"
12 #elif defined(OS_POSIX)
13 #include <sys/mman.h>
14 #endif
15
10 #include "base/files/memory_mapped_file.h" 16 #include "base/files/memory_mapped_file.h"
11 #include "base/logging.h" 17 #include "base/logging.h"
12 #include "base/memory/shared_memory.h" 18 #include "base/memory/shared_memory.h"
13 #include "base/metrics/histogram_macros.h" 19 #include "base/metrics/histogram_macros.h"
14 20
15 namespace { 21 namespace {
16 22
17 // Limit of memory segment size. It has to fit in an unsigned 32-bit number 23 // Limit of memory segment size. It has to fit in an unsigned 32-bit number
18 // and should be a power of 2 in order to accomodate almost any page size. 24 // and should be a power of 2 in order to accomodate almost any page size.
19 const uint32_t kSegmentMaxSize = 1 << 30; // 1 GiB 25 const uint32_t kSegmentMaxSize = 1 << 30; // 1 GiB
(...skipping 695 matching lines...) Expand 10 before | Expand all | Expand 10 after
715 } 721 }
716 } 722 }
717 723
718 724
719 //----- LocalPersistentMemoryAllocator ----------------------------------------- 725 //----- LocalPersistentMemoryAllocator -----------------------------------------
720 726
721 LocalPersistentMemoryAllocator::LocalPersistentMemoryAllocator( 727 LocalPersistentMemoryAllocator::LocalPersistentMemoryAllocator(
722 size_t size, 728 size_t size,
723 uint64_t id, 729 uint64_t id,
724 base::StringPiece name) 730 base::StringPiece name)
725 : PersistentMemoryAllocator(memset(new char[size], 0, size), 731 : PersistentMemoryAllocator(AllocateLocalMemory(size),
726 size, 0, id, name, false) {} 732 size, 0, id, name, false) {}
727 733
728 LocalPersistentMemoryAllocator::~LocalPersistentMemoryAllocator() { 734 LocalPersistentMemoryAllocator::~LocalPersistentMemoryAllocator() {
729 delete [] mem_base_; 735 DeallocateLocalMemory(const_cast<char*>(mem_base_), mem_size_);
736 }
737
738 // static
739 void* LocalPersistentMemoryAllocator::AllocateLocalMemory(size_t size) {
740 #if defined(OS_WIN)
741 void* address =
742 ::VirtualAlloc(nullptr, size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
743 DPCHECK(address);
744 return address;
745 #elif defined(OS_POSIX)
746 // MAP_ANON is deprecated on Linux but MAP_ANONYMOUS is not universal on Mac.
747 // MAP_SHARED is not available on Linux <2.4 but required on Mac.
748 void* address = ::mmap(nullptr, size, PROT_READ | PROT_WRITE,
749 MAP_ANON | MAP_SHARED, -1, 0);
750 DPCHECK(MAP_FAILED != address);
751 return address;
752 #else
753 #error This architecture is not (yet) supported.
754 #endif
755 }
756
757 // static
758 void LocalPersistentMemoryAllocator::DeallocateLocalMemory(void* memory,
759 size_t size) {
760 #if defined(OS_WIN)
761 BOOL success = ::VirtualFree(memory, 0, MEM_DECOMMIT);
762 DPCHECK(success);
763 #elif defined(OS_POSIX)
764 int result = ::munmap(memory, size);
765 DPCHECK(0 == result);
766 #else
767 #error This architecture is not (yet) supported.
768 #endif
730 } 769 }
731 770
732 771
733 //----- SharedPersistentMemoryAllocator ---------------------------------------- 772 //----- SharedPersistentMemoryAllocator ----------------------------------------
734 773
735 SharedPersistentMemoryAllocator::SharedPersistentMemoryAllocator( 774 SharedPersistentMemoryAllocator::SharedPersistentMemoryAllocator(
736 std::unique_ptr<SharedMemory> memory, 775 std::unique_ptr<SharedMemory> memory,
737 uint64_t id, 776 uint64_t id,
738 base::StringPiece name, 777 base::StringPiece name,
739 bool read_only) 778 bool read_only)
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
773 FilePersistentMemoryAllocator::~FilePersistentMemoryAllocator() {} 812 FilePersistentMemoryAllocator::~FilePersistentMemoryAllocator() {}
774 813
775 // static 814 // static
776 bool FilePersistentMemoryAllocator::IsFileAcceptable( 815 bool FilePersistentMemoryAllocator::IsFileAcceptable(
777 const MemoryMappedFile& file, 816 const MemoryMappedFile& file,
778 bool read_only) { 817 bool read_only) {
779 return IsMemoryAcceptable(file.data(), file.length(), 0, read_only); 818 return IsMemoryAcceptable(file.data(), file.length(), 0, read_only);
780 } 819 }
781 820
782 } // namespace base 821 } // namespace base
OLDNEW
« 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