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 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
200 | 201 |
201 // Allocate space for the name so other processes can learn it. | 202 // Allocate space for the name so other processes can learn it. |
202 if (!name.empty()) { | 203 if (!name.empty()) { |
203 const size_t name_length = name.length() + 1; | 204 const size_t name_length = name.length() + 1; |
204 shared_meta()->name = Allocate(name_length, 0); | 205 shared_meta()->name = Allocate(name_length, 0); |
205 char* name_cstr = GetAsObject<char>(shared_meta()->name, 0); | 206 char* name_cstr = GetAsObject<char>(shared_meta()->name, 0); |
206 if (name_cstr) | 207 if (name_cstr) |
207 strcpy(name_cstr, name.c_str()); | 208 strcpy(name_cstr, name.c_str()); |
208 } | 209 } |
209 } else { | 210 } else { |
211 if (shared_meta()->size == 0 || | |
212 shared_meta()->version == 0 || | |
213 shared_meta()->freeptr.load() == 0 || | |
214 shared_meta()->tailptr == 0 || | |
215 shared_meta()->queue.cookie == 0 || | |
216 shared_meta()->queue.next.load() == 0) { | |
217 NOTREACHED(); | |
218 SetCorrupt(); | |
219 } | |
210 if (readonly) { | 220 if (readonly) { |
211 // For read-only access, validate reasonable ctor parameters. | 221 // For read-only access, validate reasonable ctor parameters. |
212 DCHECK_GE(mem_size_, shared_meta()->freeptr.load()); | 222 DCHECK_GE(mem_size_, shared_meta()->freeptr.load()); |
213 } else { | 223 } else { |
214 // The allocator is attaching to a previously initialized segment of | 224 // The allocator is attaching to a previously initialized segment of |
215 // memory. Make sure the embedded data matches what has been passed. | 225 // memory. Make sure the embedded data matches what has been passed. |
216 if (shared_meta()->size != mem_size_ || | 226 if (shared_meta()->size != mem_size_ || |
217 shared_meta()->page_size != mem_page_) { | 227 shared_meta()->page_size != mem_page_) { |
218 NOTREACHED(); | 228 NOTREACHED(); |
219 SetCorrupt(); | 229 SetCorrupt(); |
(...skipping 421 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
641 uint64_t id, | 651 uint64_t id, |
642 const std::string& name) | 652 const std::string& name) |
643 : PersistentMemoryAllocator(memset(new char[size], 0, size), | 653 : PersistentMemoryAllocator(memset(new char[size], 0, size), |
644 size, 0, id, name, false) {} | 654 size, 0, id, name, false) {} |
645 | 655 |
646 LocalPersistentMemoryAllocator::~LocalPersistentMemoryAllocator() { | 656 LocalPersistentMemoryAllocator::~LocalPersistentMemoryAllocator() { |
647 delete [] mem_base_; | 657 delete [] mem_base_; |
648 } | 658 } |
649 | 659 |
650 | 660 |
661 //----- SharedPersistentMemoryAllocator ---------------------------------------- | |
662 | |
663 SharedPersistentMemoryAllocator::SharedPersistentMemoryAllocator( | |
664 scoped_ptr<SharedMemory> shmem, | |
Alexei Svitkine (slow)
2016/02/05 21:54:02
Nit: Make param consistent with header.
bcwhite
2016/02/06 04:26:11
Done.
| |
665 uint64_t id, | |
666 const std::string& name, | |
667 bool read_only) | |
668 : PersistentMemoryAllocator(static_cast<uint8_t*>(shmem->memory()), | |
669 shmem->mapped_size(), 0, id, name, read_only), | |
670 shared_memory_(std::move(shmem)) {} | |
671 | |
672 SharedPersistentMemoryAllocator::~SharedPersistentMemoryAllocator() { | |
673 } | |
674 | |
675 // static | |
676 bool SharedPersistentMemoryAllocator::IsSharedMemoryAcceptable( | |
677 const SharedMemory& shmem) { | |
678 return IsMemoryAcceptable(shmem.memory(), shmem.mapped_size(), 0, true); | |
679 } | |
680 | |
681 | |
651 //----- FilePersistentMemoryAllocator ------------------------------------------ | 682 //----- FilePersistentMemoryAllocator ------------------------------------------ |
652 | 683 |
653 FilePersistentMemoryAllocator::FilePersistentMemoryAllocator( | 684 FilePersistentMemoryAllocator::FilePersistentMemoryAllocator( |
654 MemoryMappedFile* file, | 685 scoped_ptr<MemoryMappedFile> file, |
655 uint64_t id, | 686 uint64_t id, |
656 const std::string& name) | 687 const std::string& name) |
657 : PersistentMemoryAllocator(const_cast<uint8_t*>(file->data()), | 688 : PersistentMemoryAllocator(const_cast<uint8_t*>(file->data()), |
658 file->length(), 0, id, name, true), | 689 file->length(), 0, id, name, true), |
659 mapped_file_(file) {} | 690 mapped_file_(std::move(file)) {} |
660 | 691 |
661 FilePersistentMemoryAllocator::~FilePersistentMemoryAllocator() { | 692 FilePersistentMemoryAllocator::~FilePersistentMemoryAllocator() { |
662 } | 693 } |
663 | 694 |
664 // static | 695 // static |
665 bool FilePersistentMemoryAllocator::IsFileAcceptable( | 696 bool FilePersistentMemoryAllocator::IsFileAcceptable( |
666 const MemoryMappedFile& file) { | 697 const MemoryMappedFile& file) { |
667 return IsMemoryAcceptable(file.data(), file.length(), 0, true); | 698 return IsMemoryAcceptable(file.data(), file.length(), 0, true); |
668 } | 699 } |
669 | 700 |
670 } // namespace base | 701 } // namespace base |
OLD | NEW |