| 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) | 10 #if defined(OS_WIN) |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 126 FILE_PATH_LITERAL(".pma"); | 126 FILE_PATH_LITERAL(".pma"); |
| 127 | 127 |
| 128 | 128 |
| 129 PersistentMemoryAllocator::Iterator::Iterator( | 129 PersistentMemoryAllocator::Iterator::Iterator( |
| 130 const PersistentMemoryAllocator* allocator) | 130 const PersistentMemoryAllocator* allocator) |
| 131 : allocator_(allocator), last_record_(kReferenceQueue), record_count_(0) {} | 131 : allocator_(allocator), last_record_(kReferenceQueue), record_count_(0) {} |
| 132 | 132 |
| 133 PersistentMemoryAllocator::Iterator::Iterator( | 133 PersistentMemoryAllocator::Iterator::Iterator( |
| 134 const PersistentMemoryAllocator* allocator, | 134 const PersistentMemoryAllocator* allocator, |
| 135 Reference starting_after) | 135 Reference starting_after) |
| 136 : allocator_(allocator), last_record_(starting_after), record_count_(0) { | 136 : allocator_(allocator), last_record_(0), record_count_(0) { |
| 137 Reset(starting_after); |
| 138 } |
| 139 |
| 140 void PersistentMemoryAllocator::Iterator::Reset() { |
| 141 last_record_.store(kReferenceQueue, std::memory_order_relaxed); |
| 142 record_count_.store(0, std::memory_order_relaxed); |
| 143 } |
| 144 |
| 145 void PersistentMemoryAllocator::Iterator::Reset(Reference starting_after) { |
| 146 last_record_.store(starting_after, std::memory_order_relaxed); |
| 147 record_count_.store(0, std::memory_order_relaxed); |
| 148 |
| 137 // Ensure that the starting point is a valid, iterable block (meaning it can | 149 // Ensure that the starting point is a valid, iterable block (meaning it can |
| 138 // be read and has a non-zero "next" pointer). | 150 // be read and has a non-zero "next" pointer). |
| 139 const volatile BlockHeader* block = | 151 const volatile BlockHeader* block = |
| 140 allocator_->GetBlock(starting_after, 0, 0, false, false); | 152 allocator_->GetBlock(starting_after, 0, 0, false, false); |
| 141 if (!block || block->next.load(std::memory_order_relaxed) == 0) { | 153 if (!block || block->next.load(std::memory_order_relaxed) == 0) { |
| 142 NOTREACHED(); | 154 NOTREACHED(); |
| 143 last_record_.store(kReferenceQueue, std::memory_order_release); | 155 last_record_.store(kReferenceQueue, std::memory_order_release); |
| 144 } | 156 } |
| 145 } | 157 } |
| 146 | 158 |
| 147 PersistentMemoryAllocator::Reference | 159 PersistentMemoryAllocator::Reference |
| 160 PersistentMemoryAllocator::Iterator::GetLast() { |
| 161 Reference last = last_record_.load(std::memory_order_relaxed); |
| 162 if (last == kReferenceQueue) |
| 163 return kReferenceNull; |
| 164 return last; |
| 165 } |
| 166 |
| 167 PersistentMemoryAllocator::Reference |
| 148 PersistentMemoryAllocator::Iterator::GetNext(uint32_t* type_return) { | 168 PersistentMemoryAllocator::Iterator::GetNext(uint32_t* type_return) { |
| 149 // Make a copy of the existing count of found-records, acquiring all changes | 169 // Make a copy of the existing count of found-records, acquiring all changes |
| 150 // made to the allocator, notably "freeptr" (see comment in loop for why | 170 // made to the allocator, notably "freeptr" (see comment in loop for why |
| 151 // the load of that value cannot be moved above here) that occurred during | 171 // the load of that value cannot be moved above here) that occurred during |
| 152 // any previous runs of this method, including those by parallel threads | 172 // any previous runs of this method, including those by parallel threads |
| 153 // that interrupted it. It pairs with the Release at the end of this method. | 173 // that interrupted it. It pairs with the Release at the end of this method. |
| 154 // | 174 // |
| 155 // Otherwise, if the compiler were to arrange the two loads such that | 175 // Otherwise, if the compiler were to arrange the two loads such that |
| 156 // "count" was fetched _after_ "freeptr" then it would be possible for | 176 // "count" was fetched _after_ "freeptr" then it would be possible for |
| 157 // this thread to be interrupted between them and other threads perform | 177 // this thread to be interrupted between them and other threads perform |
| (...skipping 702 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 860 | 880 |
| 861 // static | 881 // static |
| 862 bool FilePersistentMemoryAllocator::IsFileAcceptable( | 882 bool FilePersistentMemoryAllocator::IsFileAcceptable( |
| 863 const MemoryMappedFile& file, | 883 const MemoryMappedFile& file, |
| 864 bool read_only) { | 884 bool read_only) { |
| 865 return IsMemoryAcceptable(file.data(), file.length(), 0, read_only); | 885 return IsMemoryAcceptable(file.data(), file.length(), 0, read_only); |
| 866 } | 886 } |
| 867 #endif // !defined(OS_NACL) | 887 #endif // !defined(OS_NACL) |
| 868 | 888 |
| 869 } // namespace base | 889 } // namespace base |
| OLD | NEW |