| 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 1115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1126 DCHECK_NE(0U, type_); | 1126 DCHECK_NE(0U, type_); |
| 1127 DCHECK_LT(0U, size_); | 1127 DCHECK_LT(0U, size_); |
| 1128 DCHECK(reference_); | 1128 DCHECK(reference_); |
| 1129 } | 1129 } |
| 1130 | 1130 |
| 1131 DelayedPersistentAllocation::~DelayedPersistentAllocation() {} | 1131 DelayedPersistentAllocation::~DelayedPersistentAllocation() {} |
| 1132 | 1132 |
| 1133 void* DelayedPersistentAllocation::Get() const { | 1133 void* DelayedPersistentAllocation::Get() const { |
| 1134 // Relaxed operations are acceptable here because it's not protecting the | 1134 // Relaxed operations are acceptable here because it's not protecting the |
| 1135 // contents of the allocation in any way. | 1135 // contents of the allocation in any way. |
| 1136 Reference ref = reference_->load(std::memory_order_relaxed); | 1136 Reference ref = reference_->load(std::memory_order_acquire); |
| 1137 if (!ref) { | 1137 if (!ref) { |
| 1138 ref = allocator_->Allocate(size_, type_); | 1138 ref = allocator_->Allocate(size_, type_); |
| 1139 if (!ref) | 1139 if (!ref) |
| 1140 return nullptr; | 1140 return nullptr; |
| 1141 | 1141 |
| 1142 // Store the new reference in its proper location using compare-and-swap. | 1142 // Store the new reference in its proper location using compare-and-swap. |
| 1143 // Use a "strong" exchange to ensure no false-negatives since the operation | 1143 // Use a "strong" exchange to ensure no false-negatives since the operation |
| 1144 // cannot be retried. | 1144 // cannot be retried. |
| 1145 Reference existing = 0; // Must be mutable; receives actual value. | 1145 Reference existing = 0; // Must be mutable; receives actual value. |
| 1146 if (reference_->compare_exchange_strong(existing, ref, | 1146 if (reference_->compare_exchange_strong(existing, ref, |
| 1147 std::memory_order_relaxed, | 1147 std::memory_order_release, |
| 1148 std::memory_order_relaxed)) { | 1148 std::memory_order_relaxed)) { |
| 1149 if (make_iterable_) | 1149 if (make_iterable_) |
| 1150 allocator_->MakeIterable(ref); | 1150 allocator_->MakeIterable(ref); |
| 1151 } else { | 1151 } else { |
| 1152 // Failure indicates that something else has raced ahead, performed the | 1152 // Failure indicates that something else has raced ahead, performed the |
| 1153 // allocation, and stored its reference. Purge the allocation that was | 1153 // allocation, and stored its reference. Purge the allocation that was |
| 1154 // just done and use the other one instead. | 1154 // just done and use the other one instead. |
| 1155 DCHECK_EQ(type_, allocator_->GetType(existing)); | 1155 DCHECK_EQ(type_, allocator_->GetType(existing)); |
| 1156 DCHECK_LE(size_, allocator_->GetAllocSize(existing)); | 1156 DCHECK_LE(size_, allocator_->GetAllocSize(existing)); |
| 1157 allocator_->ChangeType(ref, 0, type_, /*clear=*/false); | 1157 allocator_->ChangeType(ref, 0, type_, /*clear=*/false); |
| 1158 ref = existing; | 1158 ref = existing; |
| 1159 } | 1159 } |
| 1160 } | 1160 } |
| 1161 | 1161 |
| 1162 char* mem = allocator_->GetAsArray<char>(ref, type_, size_); | 1162 char* mem = allocator_->GetAsArray<char>(ref, type_, size_); |
| 1163 if (!mem) { | 1163 if (!mem) { |
| 1164 // This should never happen but be tolerant if it does as corruption from | 1164 // This should never happen but be tolerant if it does as corruption from |
| 1165 // the outside is something to guard against. | 1165 // the outside is something to guard against. |
| 1166 NOTREACHED(); | 1166 NOTREACHED(); |
| 1167 return nullptr; | 1167 return nullptr; |
| 1168 } | 1168 } |
| 1169 return mem + offset_; | 1169 return mem + offset_; |
| 1170 } | 1170 } |
| 1171 | 1171 |
| 1172 } // namespace base | 1172 } // namespace base |
| OLD | NEW |