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 #include "base/files/memory_mapped_file.h" | 10 #include "base/files/memory_mapped_file.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/memory/shared_memory.h" |
12 #include "base/metrics/histogram_macros.h" | 13 #include "base/metrics/histogram_macros.h" |
13 | 14 |
14 namespace { | 15 namespace { |
15 | 16 |
16 // Required range of memory segment sizes. It has to fit in an unsigned 32-bit | 17 // Required range of memory segment sizes. It has to fit in an unsigned 32-bit |
17 // number and should be a power of 2 in order to accomodate almost any page | 18 // number and should be a power of 2 in order to accomodate almost any page |
18 // size. | 19 // size. |
19 const uint32_t kSegmentMinSize = 1 << 10; // 1 KiB | 20 const uint32_t kSegmentMinSize = 1 << 10; // 1 KiB |
20 const uint32_t kSegmentMaxSize = 1 << 30; // 1 GiB | 21 const uint32_t kSegmentMaxSize = 1 << 30; // 1 GiB |
21 | 22 |
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
208 | 209 |
209 // Allocate space for the name so other processes can learn it. | 210 // Allocate space for the name so other processes can learn it. |
210 if (!name.empty()) { | 211 if (!name.empty()) { |
211 const size_t name_length = name.length() + 1; | 212 const size_t name_length = name.length() + 1; |
212 shared_meta()->name = Allocate(name_length, 0); | 213 shared_meta()->name = Allocate(name_length, 0); |
213 char* name_cstr = GetAsObject<char>(shared_meta()->name, 0); | 214 char* name_cstr = GetAsObject<char>(shared_meta()->name, 0); |
214 if (name_cstr) | 215 if (name_cstr) |
215 memcpy(name_cstr, name.data(), name.length()); | 216 memcpy(name_cstr, name.data(), name.length()); |
216 } | 217 } |
217 } else { | 218 } else { |
| 219 if (shared_meta()->size == 0 || |
| 220 shared_meta()->version == 0 || |
| 221 shared_meta()->freeptr.load() == 0 || |
| 222 shared_meta()->tailptr == 0 || |
| 223 shared_meta()->queue.cookie == 0 || |
| 224 shared_meta()->queue.next.load() == 0) { |
| 225 SetCorrupt(); |
| 226 } |
218 if (!readonly) { | 227 if (!readonly) { |
219 // The allocator is attaching to a previously initialized segment of | 228 // The allocator is attaching to a previously initialized segment of |
220 // memory. Make sure the embedded data matches what has been passed. | 229 // memory. Make sure the embedded data matches what has been passed. |
221 if (shared_meta()->size != mem_size_ || | 230 if (shared_meta()->size != mem_size_ || |
222 shared_meta()->page_size != mem_page_) { | 231 shared_meta()->page_size != mem_page_) { |
223 NOTREACHED(); | 232 NOTREACHED(); |
224 SetCorrupt(); | 233 SetCorrupt(); |
225 } | 234 } |
226 } | 235 } |
227 } | 236 } |
(...skipping 421 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
649 uint64_t id, | 658 uint64_t id, |
650 base::StringPiece name) | 659 base::StringPiece name) |
651 : PersistentMemoryAllocator(memset(new char[size], 0, size), | 660 : PersistentMemoryAllocator(memset(new char[size], 0, size), |
652 size, 0, id, name, false) {} | 661 size, 0, id, name, false) {} |
653 | 662 |
654 LocalPersistentMemoryAllocator::~LocalPersistentMemoryAllocator() { | 663 LocalPersistentMemoryAllocator::~LocalPersistentMemoryAllocator() { |
655 delete [] mem_base_; | 664 delete [] mem_base_; |
656 } | 665 } |
657 | 666 |
658 | 667 |
| 668 //----- SharedPersistentMemoryAllocator ---------------------------------------- |
| 669 |
| 670 SharedPersistentMemoryAllocator::SharedPersistentMemoryAllocator( |
| 671 scoped_ptr<SharedMemory> memory, |
| 672 uint64_t id, |
| 673 base::StringPiece name, |
| 674 bool read_only) |
| 675 : PersistentMemoryAllocator(static_cast<uint8_t*>(memory->memory()), |
| 676 memory->mapped_size(), 0, id, name, read_only), |
| 677 shared_memory_(std::move(memory)) {} |
| 678 |
| 679 SharedPersistentMemoryAllocator::~SharedPersistentMemoryAllocator() {} |
| 680 |
| 681 // static |
| 682 bool SharedPersistentMemoryAllocator::IsSharedMemoryAcceptable( |
| 683 const SharedMemory& memory) { |
| 684 return IsMemoryAcceptable(memory.memory(), memory.mapped_size(), 0, true); |
| 685 } |
| 686 |
| 687 |
659 //----- FilePersistentMemoryAllocator ------------------------------------------ | 688 //----- FilePersistentMemoryAllocator ------------------------------------------ |
660 | 689 |
661 FilePersistentMemoryAllocator::FilePersistentMemoryAllocator( | 690 FilePersistentMemoryAllocator::FilePersistentMemoryAllocator( |
662 MemoryMappedFile* file, | 691 scoped_ptr<MemoryMappedFile> file, |
663 uint64_t id, | 692 uint64_t id, |
664 base::StringPiece name) | 693 base::StringPiece name) |
665 : PersistentMemoryAllocator(const_cast<uint8_t*>(file->data()), | 694 : PersistentMemoryAllocator(const_cast<uint8_t*>(file->data()), |
666 file->length(), 0, id, name, true), | 695 file->length(), 0, id, name, true), |
667 mapped_file_(file) {} | 696 mapped_file_(std::move(file)) {} |
668 | 697 |
669 FilePersistentMemoryAllocator::~FilePersistentMemoryAllocator() { | 698 FilePersistentMemoryAllocator::~FilePersistentMemoryAllocator() {} |
670 } | |
671 | 699 |
672 // static | 700 // static |
673 bool FilePersistentMemoryAllocator::IsFileAcceptable( | 701 bool FilePersistentMemoryAllocator::IsFileAcceptable( |
674 const MemoryMappedFile& file) { | 702 const MemoryMappedFile& file) { |
675 return IsMemoryAcceptable(file.data(), file.length(), 0, true); | 703 return IsMemoryAcceptable(file.data(), file.length(), 0, true); |
676 } | 704 } |
677 | 705 |
678 } // namespace base | 706 } // namespace base |
OLD | NEW |