blob: ab821d7714a5d3179f73d3b5bd584ae02faa386b [file] [log] [blame]
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "transaction.h"
18
19#include "base/stl_util.h"
20#include "base/logging.h"
21#include "gc/accounting/card_table-inl.h"
22#include "intern_table.h"
23#include "mirror/object-inl.h"
24#include "mirror/object_array-inl.h"
25
26#include <list>
27
28namespace art {
29
30// TODO: remove (only used for debugging purpose).
31static constexpr bool kEnableTransactionStats = false;
32
Sebastien Hertz1c80bec2015-02-03 11:58:06 +010033Transaction::Transaction()
34 : log_lock_("transaction log lock", kTransactionLogLock), aborted_(false) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080035 CHECK(Runtime::Current()->IsAotCompiler());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010036}
37
38Transaction::~Transaction() {
39 if (kEnableTransactionStats) {
40 MutexLock mu(Thread::Current(), log_lock_);
41 size_t objects_count = object_logs_.size();
42 size_t field_values_count = 0;
43 for (auto it : object_logs_) {
44 field_values_count += it.second.Size();
45 }
46 size_t array_count = array_logs_.size();
47 size_t array_values_count = 0;
48 for (auto it : array_logs_) {
49 array_values_count += it.second.Size();
50 }
51 size_t string_count = intern_string_logs_.size();
52 LOG(INFO) << "Transaction::~Transaction"
53 << ": objects_count=" << objects_count
54 << ", field_values_count=" << field_values_count
55 << ", array_count=" << array_count
56 << ", array_values_count=" << array_values_count
57 << ", string_count=" << string_count;
58 }
59}
60
Sebastien Hertz1c80bec2015-02-03 11:58:06 +010061void Transaction::Abort(const std::string& abort_message) {
62 MutexLock mu(Thread::Current(), log_lock_);
Sebastien Hertz2fd7e692015-04-02 11:11:19 +020063 // We may abort more than once if the exception thrown at the time of the
64 // previous abort has been caught during execution of a class initializer.
Sebastien Hertz1c80bec2015-02-03 11:58:06 +010065 // We just keep the message of the first abort because it will cause the
66 // transaction to be rolled back anyway.
67 if (!aborted_) {
68 aborted_ = true;
69 abort_message_ = abort_message;
70 }
71}
72
Sebastien Hertzb81e1cd2015-04-28 12:31:41 +020073void Transaction::ThrowAbortError(Thread* self, const std::string* abort_message) {
74 const bool rethrow = (abort_message == nullptr);
Sebastien Hertzbd9cf9f2015-03-03 12:16:13 +010075 if (kIsDebugBuild && rethrow) {
Sebastien Hertz2fd7e692015-04-02 11:11:19 +020076 CHECK(IsAborted()) << "Rethrow " << Transaction::kAbortExceptionDescriptor
77 << " while transaction is not aborted";
Sebastien Hertzbd9cf9f2015-03-03 12:16:13 +010078 }
Sebastien Hertzb81e1cd2015-04-28 12:31:41 +020079 if (rethrow) {
80 // Rethrow an exception with the earlier abort message stored in the transaction.
81 self->ThrowNewWrappedException(Transaction::kAbortExceptionSignature,
82 GetAbortMessage().c_str());
83 } else {
84 // Throw an exception with the given abort message.
85 self->ThrowNewWrappedException(Transaction::kAbortExceptionSignature,
86 abort_message->c_str());
87 }
Sebastien Hertz1c80bec2015-02-03 11:58:06 +010088}
89
90bool Transaction::IsAborted() {
91 MutexLock mu(Thread::Current(), log_lock_);
92 return aborted_;
93}
94
95const std::string& Transaction::GetAbortMessage() {
96 MutexLock mu(Thread::Current(), log_lock_);
97 return abort_message_;
98}
99
Fred Shih37f05ef2014-07-16 18:38:08 -0700100void Transaction::RecordWriteFieldBoolean(mirror::Object* obj, MemberOffset field_offset,
101 uint8_t value, bool is_volatile) {
102 DCHECK(obj != nullptr);
103 MutexLock mu(Thread::Current(), log_lock_);
104 ObjectLog& object_log = object_logs_[obj];
105 object_log.LogBooleanValue(field_offset, value, is_volatile);
106}
107
108void Transaction::RecordWriteFieldByte(mirror::Object* obj, MemberOffset field_offset,
109 int8_t value, bool is_volatile) {
110 DCHECK(obj != nullptr);
111 MutexLock mu(Thread::Current(), log_lock_);
112 ObjectLog& object_log = object_logs_[obj];
113 object_log.LogByteValue(field_offset, value, is_volatile);
114}
115
116void Transaction::RecordWriteFieldChar(mirror::Object* obj, MemberOffset field_offset,
117 uint16_t value, bool is_volatile) {
118 DCHECK(obj != nullptr);
119 MutexLock mu(Thread::Current(), log_lock_);
120 ObjectLog& object_log = object_logs_[obj];
121 object_log.LogCharValue(field_offset, value, is_volatile);
122}
123
124
125void Transaction::RecordWriteFieldShort(mirror::Object* obj, MemberOffset field_offset,
126 int16_t value, bool is_volatile) {
127 DCHECK(obj != nullptr);
128 MutexLock mu(Thread::Current(), log_lock_);
129 ObjectLog& object_log = object_logs_[obj];
130 object_log.LogShortValue(field_offset, value, is_volatile);
131}
132
133
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100134void Transaction::RecordWriteField32(mirror::Object* obj, MemberOffset field_offset, uint32_t value,
135 bool is_volatile) {
136 DCHECK(obj != nullptr);
137 MutexLock mu(Thread::Current(), log_lock_);
138 ObjectLog& object_log = object_logs_[obj];
139 object_log.Log32BitsValue(field_offset, value, is_volatile);
140}
141
142void Transaction::RecordWriteField64(mirror::Object* obj, MemberOffset field_offset, uint64_t value,
143 bool is_volatile) {
144 DCHECK(obj != nullptr);
145 MutexLock mu(Thread::Current(), log_lock_);
146 ObjectLog& object_log = object_logs_[obj];
147 object_log.Log64BitsValue(field_offset, value, is_volatile);
148}
149
150void Transaction::RecordWriteFieldReference(mirror::Object* obj, MemberOffset field_offset,
151 mirror::Object* value, bool is_volatile) {
152 DCHECK(obj != nullptr);
153 MutexLock mu(Thread::Current(), log_lock_);
154 ObjectLog& object_log = object_logs_[obj];
155 object_log.LogReferenceValue(field_offset, value, is_volatile);
156}
157
158void Transaction::RecordWriteArray(mirror::Array* array, size_t index, uint64_t value) {
159 DCHECK(array != nullptr);
160 DCHECK(array->IsArrayInstance());
Sebastien Hertzee1d79a2014-02-21 15:46:30 +0100161 DCHECK(!array->IsObjectArray());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100162 MutexLock mu(Thread::Current(), log_lock_);
163 ArrayLog& array_log = array_logs_[array];
164 array_log.LogValue(index, value);
165}
166
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700167void Transaction::RecordStrongStringInsertion(mirror::String* s) {
168 InternStringLog log(s, InternStringLog::kStrongString, InternStringLog::kInsert);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100169 LogInternedString(log);
170}
171
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700172void Transaction::RecordWeakStringInsertion(mirror::String* s) {
173 InternStringLog log(s, InternStringLog::kWeakString, InternStringLog::kInsert);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100174 LogInternedString(log);
175}
176
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700177void Transaction::RecordStrongStringRemoval(mirror::String* s) {
178 InternStringLog log(s, InternStringLog::kStrongString, InternStringLog::kRemove);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100179 LogInternedString(log);
180}
181
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -0700182void Transaction::RecordWeakStringRemoval(mirror::String* s) {
183 InternStringLog log(s, InternStringLog::kWeakString, InternStringLog::kRemove);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100184 LogInternedString(log);
185}
186
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700187void Transaction::LogInternedString(const InternStringLog& log) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100188 Locks::intern_table_lock_->AssertExclusiveHeld(Thread::Current());
189 MutexLock mu(Thread::Current(), log_lock_);
190 intern_string_logs_.push_front(log);
191}
192
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100193void Transaction::Rollback() {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100194 CHECK(!Runtime::Current()->IsActiveTransaction());
195 Thread* self = Thread::Current();
196 self->AssertNoPendingException();
197 MutexLock mu1(self, *Locks::intern_table_lock_);
198 MutexLock mu2(self, log_lock_);
199 UndoObjectModifications();
200 UndoArrayModifications();
201 UndoInternStringTableModifications();
202}
203
204void Transaction::UndoObjectModifications() {
205 // TODO we may not need to restore objects allocated during this transaction. Or we could directly
206 // remove them from the heap.
207 for (auto it : object_logs_) {
208 it.second.Undo(it.first);
209 }
210 object_logs_.clear();
211}
212
213void Transaction::UndoArrayModifications() {
214 // TODO we may not need to restore array allocated during this transaction. Or we could directly
215 // remove them from the heap.
216 for (auto it : array_logs_) {
217 it.second.Undo(it.first);
218 }
219 array_logs_.clear();
220}
221
222void Transaction::UndoInternStringTableModifications() {
223 InternTable* const intern_table = Runtime::Current()->GetInternTable();
224 // We want to undo each operation from the most recent to the oldest. List has been filled so the
225 // most recent operation is at list begin so just have to iterate over it.
226 for (InternStringLog& string_log : intern_string_logs_) {
227 string_log.Undo(intern_table);
228 }
229 intern_string_logs_.clear();
230}
231
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700232void Transaction::VisitRoots(RootVisitor* visitor) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100233 MutexLock mu(Thread::Current(), log_lock_);
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700234 VisitObjectLogs(visitor);
235 VisitArrayLogs(visitor);
236 VisitStringLogs(visitor);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100237}
238
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700239void Transaction::VisitObjectLogs(RootVisitor* visitor) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100240 // List of moving roots.
241 typedef std::pair<mirror::Object*, mirror::Object*> ObjectPair;
242 std::list<ObjectPair> moving_roots;
243
244 // Visit roots.
245 for (auto it : object_logs_) {
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700246 it.second.VisitRoots(visitor);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100247 mirror::Object* old_root = it.first;
Mathieu Chartier815873e2014-02-13 18:02:13 -0800248 mirror::Object* new_root = old_root;
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700249 visitor->VisitRoot(&new_root, RootInfo(kRootUnknown));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100250 if (new_root != old_root) {
251 moving_roots.push_back(std::make_pair(old_root, new_root));
252 }
253 }
254
255 // Update object logs with moving roots.
256 for (const ObjectPair& pair : moving_roots) {
257 mirror::Object* old_root = pair.first;
258 mirror::Object* new_root = pair.second;
259 auto old_root_it = object_logs_.find(old_root);
260 CHECK(old_root_it != object_logs_.end());
261 CHECK(object_logs_.find(new_root) == object_logs_.end());
262 object_logs_.insert(std::make_pair(new_root, old_root_it->second));
263 object_logs_.erase(old_root_it);
264 }
265}
266
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700267void Transaction::VisitArrayLogs(RootVisitor* visitor) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100268 // List of moving roots.
269 typedef std::pair<mirror::Array*, mirror::Array*> ArrayPair;
270 std::list<ArrayPair> moving_roots;
271
272 for (auto it : array_logs_) {
273 mirror::Array* old_root = it.first;
Sebastien Hertzee1d79a2014-02-21 15:46:30 +0100274 CHECK(!old_root->IsObjectArray());
Mathieu Chartier815873e2014-02-13 18:02:13 -0800275 mirror::Array* new_root = old_root;
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700276 visitor->VisitRoot(reinterpret_cast<mirror::Object**>(&new_root), RootInfo(kRootUnknown));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100277 if (new_root != old_root) {
278 moving_roots.push_back(std::make_pair(old_root, new_root));
279 }
280 }
281
282 // Update array logs with moving roots.
283 for (const ArrayPair& pair : moving_roots) {
284 mirror::Array* old_root = pair.first;
285 mirror::Array* new_root = pair.second;
286 auto old_root_it = array_logs_.find(old_root);
287 CHECK(old_root_it != array_logs_.end());
288 CHECK(array_logs_.find(new_root) == array_logs_.end());
289 array_logs_.insert(std::make_pair(new_root, old_root_it->second));
290 array_logs_.erase(old_root_it);
291 }
292}
293
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700294void Transaction::VisitStringLogs(RootVisitor* visitor) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100295 for (InternStringLog& log : intern_string_logs_) {
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700296 log.VisitRoots(visitor);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100297 }
298}
299
Fred Shih37f05ef2014-07-16 18:38:08 -0700300void Transaction::ObjectLog::LogBooleanValue(MemberOffset offset, uint8_t value, bool is_volatile) {
301 LogValue(ObjectLog::kBoolean, offset, value, is_volatile);
302}
303
304void Transaction::ObjectLog::LogByteValue(MemberOffset offset, int8_t value, bool is_volatile) {
305 LogValue(ObjectLog::kByte, offset, value, is_volatile);
306}
307
308void Transaction::ObjectLog::LogCharValue(MemberOffset offset, uint16_t value, bool is_volatile) {
309 LogValue(ObjectLog::kChar, offset, value, is_volatile);
310}
311
312void Transaction::ObjectLog::LogShortValue(MemberOffset offset, int16_t value, bool is_volatile) {
313 LogValue(ObjectLog::kShort, offset, value, is_volatile);
314}
315
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100316void Transaction::ObjectLog::Log32BitsValue(MemberOffset offset, uint32_t value, bool is_volatile) {
Fred Shih37f05ef2014-07-16 18:38:08 -0700317 LogValue(ObjectLog::k32Bits, offset, value, is_volatile);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100318}
319
320void Transaction::ObjectLog::Log64BitsValue(MemberOffset offset, uint64_t value, bool is_volatile) {
Fred Shih37f05ef2014-07-16 18:38:08 -0700321 LogValue(ObjectLog::k64Bits, offset, value, is_volatile);
322}
323
324void Transaction::ObjectLog::LogReferenceValue(MemberOffset offset, mirror::Object* obj, bool is_volatile) {
325 LogValue(ObjectLog::kReference, offset, reinterpret_cast<uintptr_t>(obj), is_volatile);
326}
327
328void Transaction::ObjectLog::LogValue(ObjectLog::FieldValueKind kind,
329 MemberOffset offset, uint64_t value, bool is_volatile) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100330 auto it = field_values_.find(offset.Uint32Value());
331 if (it == field_values_.end()) {
332 ObjectLog::FieldValue field_value;
333 field_value.value = value;
334 field_value.is_volatile = is_volatile;
Fred Shih37f05ef2014-07-16 18:38:08 -0700335 field_value.kind = kind;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100336 field_values_.insert(std::make_pair(offset.Uint32Value(), field_value));
337 }
338}
339
340void Transaction::ObjectLog::Undo(mirror::Object* obj) {
341 for (auto& it : field_values_) {
342 // Garbage collector needs to access object's class and array's length. So we don't rollback
343 // these values.
344 MemberOffset field_offset(it.first);
345 if (field_offset.Uint32Value() == mirror::Class::ClassOffset().Uint32Value()) {
346 // Skip Object::class field.
347 continue;
348 }
349 if (obj->IsArrayInstance() &&
350 field_offset.Uint32Value() == mirror::Array::LengthOffset().Uint32Value()) {
351 // Skip Array::length field.
352 continue;
353 }
354 FieldValue& field_value = it.second;
355 UndoFieldWrite(obj, field_offset, field_value);
356 }
357}
358
359void Transaction::ObjectLog::UndoFieldWrite(mirror::Object* obj, MemberOffset field_offset,
360 const FieldValue& field_value) {
361 // TODO We may want to abort a transaction while still being in transaction mode. In this case,
362 // we'd need to disable the check.
363 constexpr bool kCheckTransaction = true;
364 switch (field_value.kind) {
Fred Shih37f05ef2014-07-16 18:38:08 -0700365 case kBoolean:
366 if (UNLIKELY(field_value.is_volatile)) {
367 obj->SetFieldBooleanVolatile<false, kCheckTransaction>(field_offset,
368 static_cast<bool>(field_value.value));
369 } else {
370 obj->SetFieldBoolean<false, kCheckTransaction>(field_offset,
371 static_cast<bool>(field_value.value));
372 }
373 break;
374 case kByte:
375 if (UNLIKELY(field_value.is_volatile)) {
376 obj->SetFieldByteVolatile<false, kCheckTransaction>(field_offset,
377 static_cast<int8_t>(field_value.value));
378 } else {
379 obj->SetFieldByte<false, kCheckTransaction>(field_offset,
380 static_cast<int8_t>(field_value.value));
381 }
382 break;
383 case kChar:
384 if (UNLIKELY(field_value.is_volatile)) {
385 obj->SetFieldCharVolatile<false, kCheckTransaction>(field_offset,
386 static_cast<uint16_t>(field_value.value));
387 } else {
388 obj->SetFieldChar<false, kCheckTransaction>(field_offset,
389 static_cast<uint16_t>(field_value.value));
390 }
391 break;
392 case kShort:
393 if (UNLIKELY(field_value.is_volatile)) {
394 obj->SetFieldShortVolatile<false, kCheckTransaction>(field_offset,
395 static_cast<int16_t>(field_value.value));
396 } else {
397 obj->SetFieldShort<false, kCheckTransaction>(field_offset,
398 static_cast<int16_t>(field_value.value));
399 }
400 break;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100401 case k32Bits:
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700402 if (UNLIKELY(field_value.is_volatile)) {
403 obj->SetField32Volatile<false, kCheckTransaction>(field_offset,
404 static_cast<uint32_t>(field_value.value));
405 } else {
406 obj->SetField32<false, kCheckTransaction>(field_offset,
407 static_cast<uint32_t>(field_value.value));
408 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100409 break;
410 case k64Bits:
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700411 if (UNLIKELY(field_value.is_volatile)) {
412 obj->SetField64Volatile<false, kCheckTransaction>(field_offset, field_value.value);
413 } else {
414 obj->SetField64<false, kCheckTransaction>(field_offset, field_value.value);
415 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100416 break;
417 case kReference:
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700418 if (UNLIKELY(field_value.is_volatile)) {
419 obj->SetFieldObjectVolatile<false, kCheckTransaction>(field_offset,
420 reinterpret_cast<mirror::Object*>(field_value.value));
421 } else {
422 obj->SetFieldObject<false, kCheckTransaction>(field_offset,
423 reinterpret_cast<mirror::Object*>(field_value.value));
424 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100425 break;
426 default:
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700427 LOG(FATAL) << "Unknown value kind " << static_cast<int>(field_value.kind);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100428 break;
429 }
430}
431
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700432void Transaction::ObjectLog::VisitRoots(RootVisitor* visitor) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100433 for (auto it : field_values_) {
434 FieldValue& field_value = it.second;
435 if (field_value.kind == ObjectLog::kReference) {
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700436 visitor->VisitRootIfNonNull(reinterpret_cast<mirror::Object**>(&field_value.value),
437 RootInfo(kRootUnknown));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100438 }
439 }
440}
441
442void Transaction::InternStringLog::Undo(InternTable* intern_table) {
443 DCHECK(intern_table != nullptr);
444 switch (string_op_) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700445 case InternStringLog::kInsert: {
446 switch (string_kind_) {
447 case InternStringLog::kStrongString:
448 intern_table->RemoveStrongFromTransaction(str_);
449 break;
450 case InternStringLog::kWeakString:
451 intern_table->RemoveWeakFromTransaction(str_);
452 break;
453 default:
454 LOG(FATAL) << "Unknown interned string kind";
455 break;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100456 }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700457 break;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100458 }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700459 case InternStringLog::kRemove: {
460 switch (string_kind_) {
461 case InternStringLog::kStrongString:
462 intern_table->InsertStrongFromTransaction(str_);
463 break;
464 case InternStringLog::kWeakString:
465 intern_table->InsertWeakFromTransaction(str_);
466 break;
467 default:
468 LOG(FATAL) << "Unknown interned string kind";
469 break;
470 }
471 break;
472 }
473 default:
474 LOG(FATAL) << "Unknown interned string op";
475 break;
476 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100477}
478
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700479void Transaction::InternStringLog::VisitRoots(RootVisitor* visitor) {
480 visitor->VisitRoot(reinterpret_cast<mirror::Object**>(&str_), RootInfo(kRootInternedString));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100481}
482
483void Transaction::ArrayLog::LogValue(size_t index, uint64_t value) {
484 auto it = array_values_.find(index);
485 if (it == array_values_.end()) {
486 array_values_.insert(std::make_pair(index, value));
487 }
488}
489
490void Transaction::ArrayLog::Undo(mirror::Array* array) {
491 DCHECK(array != nullptr);
492 DCHECK(array->IsArrayInstance());
493 Primitive::Type type = array->GetClass()->GetComponentType()->GetPrimitiveType();
494 for (auto it : array_values_) {
495 UndoArrayWrite(array, type, it.first, it.second);
496 }
497}
498
499void Transaction::ArrayLog::UndoArrayWrite(mirror::Array* array, Primitive::Type array_type,
500 size_t index, uint64_t value) {
501 // TODO We may want to abort a transaction while still being in transaction mode. In this case,
502 // we'd need to disable the check.
503 switch (array_type) {
504 case Primitive::kPrimBoolean:
505 array->AsBooleanArray()->SetWithoutChecks<false>(index, static_cast<uint8_t>(value));
506 break;
507 case Primitive::kPrimByte:
508 array->AsByteArray()->SetWithoutChecks<false>(index, static_cast<int8_t>(value));
509 break;
510 case Primitive::kPrimChar:
511 array->AsCharArray()->SetWithoutChecks<false>(index, static_cast<uint16_t>(value));
512 break;
513 case Primitive::kPrimShort:
514 array->AsShortArray()->SetWithoutChecks<false>(index, static_cast<int16_t>(value));
515 break;
516 case Primitive::kPrimInt:
517 array->AsIntArray()->SetWithoutChecks<false>(index, static_cast<int32_t>(value));
518 break;
519 case Primitive::kPrimFloat:
520 array->AsFloatArray()->SetWithoutChecks<false>(index, static_cast<float>(value));
521 break;
522 case Primitive::kPrimLong:
523 array->AsLongArray()->SetWithoutChecks<false>(index, static_cast<int64_t>(value));
524 break;
525 case Primitive::kPrimDouble:
526 array->AsDoubleArray()->SetWithoutChecks<false>(index, static_cast<double>(value));
527 break;
Sebastien Hertzee1d79a2014-02-21 15:46:30 +0100528 case Primitive::kPrimNot:
529 LOG(FATAL) << "ObjectArray should be treated as Object";
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100530 break;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100531 default:
532 LOG(FATAL) << "Unsupported type " << array_type;
533 }
534}
535
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100536} // namespace art