OLD | NEW |
---|---|
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 // Required range of memory segment sizes. It has to fit in an unsigned 32-bit | 23 // Required range of memory segment sizes. It has to fit in an unsigned 32-bit |
18 // number and should be a power of 2 in order to accomodate almost any page | 24 // number and should be a power of 2 in order to accomodate almost any page |
19 // size. | 25 // size. |
(...skipping 689 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
709 } | 715 } |
710 } | 716 } |
711 | 717 |
712 | 718 |
713 //----- LocalPersistentMemoryAllocator ----------------------------------------- | 719 //----- LocalPersistentMemoryAllocator ----------------------------------------- |
714 | 720 |
715 LocalPersistentMemoryAllocator::LocalPersistentMemoryAllocator( | 721 LocalPersistentMemoryAllocator::LocalPersistentMemoryAllocator( |
716 size_t size, | 722 size_t size, |
717 uint64_t id, | 723 uint64_t id, |
718 base::StringPiece name) | 724 base::StringPiece name) |
719 : PersistentMemoryAllocator(memset(new char[size], 0, size), | 725 : PersistentMemoryAllocator(AllocateLocalMemory(size), |
720 size, 0, id, name, false) {} | 726 size, 0, id, name, false) {} |
721 | 727 |
722 LocalPersistentMemoryAllocator::~LocalPersistentMemoryAllocator() { | 728 LocalPersistentMemoryAllocator::~LocalPersistentMemoryAllocator() { |
723 delete [] mem_base_; | 729 DeallocateLocalMemory(const_cast<char*>(mem_base_), mem_size_); |
730 } | |
731 | |
732 // static | |
733 void* LocalPersistentMemoryAllocator::AllocateLocalMemory(size_t size) { | |
734 #if defined(OS_WIN) | |
735 void* address = | |
736 ::VirtualAlloc(nullptr, size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); | |
737 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.
| |
738 return address; | |
739 #elif defined(OS_POSIX) | |
740 // MAP_ANON is deprecated on Linux but MAP_ANONYMOUS is not universal on Mac. | |
741 // 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
| |
742 void* address = ::mmap(nullptr, size, PROT_READ | PROT_WRITE, | |
743 MAP_ANON | MAP_SHARED, -1, 0); | |
744 DCHECK_NE(MAP_FAILED, address) << errno; | |
745 return address; | |
746 #else | |
747 #error This architecture is not (yet) supported. | |
748 #endif | |
749 } | |
750 | |
751 // static | |
752 void LocalPersistentMemoryAllocator::DeallocateLocalMemory(void* memory, | |
753 size_t size) { | |
754 #if defined(OS_WIN) | |
755 BOOL success = ::VirtualFree(memory, 0, MEM_DECOMMIT); | |
756 DCHECK(success) << ::GetLastError(); | |
757 #elif defined(OS_POSIX) | |
758 int result = ::munmap(memory, size); | |
759 DCHECK_EQ(0, result) << errno; | |
760 #else | |
761 #error This architecture is not (yet) supported. | |
762 #endif | |
724 } | 763 } |
725 | 764 |
726 | 765 |
727 //----- SharedPersistentMemoryAllocator ---------------------------------------- | 766 //----- SharedPersistentMemoryAllocator ---------------------------------------- |
728 | 767 |
729 SharedPersistentMemoryAllocator::SharedPersistentMemoryAllocator( | 768 SharedPersistentMemoryAllocator::SharedPersistentMemoryAllocator( |
730 std::unique_ptr<SharedMemory> memory, | 769 std::unique_ptr<SharedMemory> memory, |
731 uint64_t id, | 770 uint64_t id, |
732 base::StringPiece name, | 771 base::StringPiece name, |
733 bool read_only) | 772 bool read_only) |
(...skipping 30 matching lines...) Expand all Loading... | |
764 | 803 |
765 FilePersistentMemoryAllocator::~FilePersistentMemoryAllocator() {} | 804 FilePersistentMemoryAllocator::~FilePersistentMemoryAllocator() {} |
766 | 805 |
767 // static | 806 // static |
768 bool FilePersistentMemoryAllocator::IsFileAcceptable( | 807 bool FilePersistentMemoryAllocator::IsFileAcceptable( |
769 const MemoryMappedFile& file) { | 808 const MemoryMappedFile& file) { |
770 return IsMemoryAcceptable(file.data(), file.length(), 0, true); | 809 return IsMemoryAcceptable(file.data(), file.length(), 0, true); |
771 } | 810 } |
772 | 811 |
773 } // namespace base | 812 } // namespace base |
OLD | NEW |