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 1045 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1056 // been written to disk and so is applicable to "flush". | 1056 // been written to disk and so is applicable to "flush". |
1057 int result = ::msync(const_cast<void*>(data()), length, | 1057 int result = ::msync(const_cast<void*>(data()), length, |
1058 MS_INVALIDATE | (sync ? MS_SYNC : MS_ASYNC)); | 1058 MS_INVALIDATE | (sync ? MS_SYNC : MS_ASYNC)); |
1059 DCHECK_NE(EINVAL, result); | 1059 DCHECK_NE(EINVAL, result); |
1060 #else | 1060 #else |
1061 #error Unsupported OS. | 1061 #error Unsupported OS. |
1062 #endif | 1062 #endif |
1063 } | 1063 } |
1064 #endif // !defined(OS_NACL) | 1064 #endif // !defined(OS_NACL) |
1065 | 1065 |
| 1066 //----- DelayedPersistentAllocation -------------------------------------------- |
| 1067 |
| 1068 // Forwarding constructors. |
| 1069 DelayedPersistentAllocation::DelayedPersistentAllocation( |
| 1070 PersistentMemoryAllocator* allocator, |
| 1071 subtle::Atomic32* ref, |
| 1072 uint32_t type, |
| 1073 size_t size, |
| 1074 bool make_iterable) |
| 1075 : DelayedPersistentAllocation( |
| 1076 allocator, |
| 1077 reinterpret_cast<std::atomic<Reference>*>(ref), |
| 1078 type, |
| 1079 size, |
| 1080 0, |
| 1081 make_iterable) {} |
| 1082 |
| 1083 DelayedPersistentAllocation::DelayedPersistentAllocation( |
| 1084 PersistentMemoryAllocator* allocator, |
| 1085 subtle::Atomic32* ref, |
| 1086 uint32_t type, |
| 1087 size_t size, |
| 1088 size_t offset, |
| 1089 bool make_iterable) |
| 1090 : DelayedPersistentAllocation( |
| 1091 allocator, |
| 1092 reinterpret_cast<std::atomic<Reference>*>(ref), |
| 1093 type, |
| 1094 size, |
| 1095 offset, |
| 1096 make_iterable) {} |
| 1097 |
| 1098 DelayedPersistentAllocation::DelayedPersistentAllocation( |
| 1099 PersistentMemoryAllocator* allocator, |
| 1100 std::atomic<Reference>* ref, |
| 1101 uint32_t type, |
| 1102 size_t size, |
| 1103 bool make_iterable) |
| 1104 : DelayedPersistentAllocation(allocator, |
| 1105 ref, |
| 1106 type, |
| 1107 size, |
| 1108 0, |
| 1109 make_iterable) {} |
| 1110 |
| 1111 // Real constructor. |
| 1112 DelayedPersistentAllocation::DelayedPersistentAllocation( |
| 1113 PersistentMemoryAllocator* allocator, |
| 1114 std::atomic<Reference>* ref, |
| 1115 uint32_t type, |
| 1116 size_t size, |
| 1117 size_t offset, |
| 1118 bool make_iterable) |
| 1119 : allocator_(allocator), |
| 1120 type_(type), |
| 1121 size_(size), |
| 1122 offset_(offset), |
| 1123 make_iterable_(make_iterable), |
| 1124 reference_(ref) { |
| 1125 DCHECK(allocator_); |
| 1126 DCHECK_NE(0U, type_); |
| 1127 DCHECK_LT(0U, size_); |
| 1128 DCHECK(reference_); |
| 1129 } |
| 1130 |
| 1131 DelayedPersistentAllocation::~DelayedPersistentAllocation() {} |
| 1132 |
| 1133 void* DelayedPersistentAllocation::Get() const { |
| 1134 // Relaxed operations are acceptable here because it's not protecting the |
| 1135 // contents of the allocation in any way. |
| 1136 Reference ref = reference_->load(std::memory_order_relaxed); |
| 1137 if (!ref) { |
| 1138 ref = allocator_->Allocate(size_, type_); |
| 1139 if (!ref) |
| 1140 return nullptr; |
| 1141 |
| 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 |
| 1144 // cannot be retried. |
| 1145 Reference existing = 0; // Must be mutable; receives actual value. |
| 1146 if (reference_->compare_exchange_strong(existing, ref, |
| 1147 std::memory_order_relaxed, |
| 1148 std::memory_order_relaxed)) { |
| 1149 if (make_iterable_) |
| 1150 allocator_->MakeIterable(ref); |
| 1151 } else { |
| 1152 // Failure indicates that something else has raced ahead, performed the |
| 1153 // allocation, and stored its reference. Purge the allocation that was |
| 1154 // just done and use the other one instead. |
| 1155 DCHECK_EQ(type_, allocator_->GetType(existing)); |
| 1156 DCHECK_LE(size_, allocator_->GetAllocSize(existing)); |
| 1157 allocator_->ChangeType(ref, 0, type_, /*clear=*/false); |
| 1158 ref = existing; |
| 1159 } |
| 1160 } |
| 1161 |
| 1162 char* mem = allocator_->GetAsArray<char>(ref, type_, size_); |
| 1163 if (!mem) { |
| 1164 // This should never happen but be tolerant if it does as corruption from |
| 1165 // the outside is something to guard against. |
| 1166 NOTREACHED(); |
| 1167 return nullptr; |
| 1168 } |
| 1169 return mem + offset_; |
| 1170 } |
| 1171 |
1066 } // namespace base | 1172 } // namespace base |
OLD | NEW |