Index: src/heap.cc |
diff --git a/src/heap.cc b/src/heap.cc |
index 3395531cf5ca91301df5ffe6d188ff853d7587e7..7f66f349ed9dc12fb74fb5f40a373ba12996009d 100644 |
--- a/src/heap.cc |
+++ b/src/heap.cc |
@@ -2325,6 +2325,7 @@ bool Heap::CreateInitialObjects() { |
set_intrinsic_function_names(StringDictionary::cast(obj)); |
if (InitializeNumberStringCache()->IsFailure()) return false; |
+ if (InitializeStringLocks()->IsFailure()) return false; |
// Allocate cache for single character ASCII strings. |
{ MaybeObject* maybe_obj = |
@@ -2532,6 +2533,76 @@ MaybeObject* Heap::NumberToString(Object* number, |
} |
Rico
2011/09/23 07:06:52
Add a note about the layout of the fixed array, e.
Lasse Reichstein
2011/09/23 09:54:39
Done.
|
+MaybeObject* Heap::LockString(String* string) { |
+ ASSERT(!string->IsConsString()); |
+ FixedArray* locks = string_locks(); |
+ ASSERT(locks->length() > 1); |
+ int length = locks->length(); |
+ int element_count = Smi::cast(locks->get(0))->value(); |
+ int element_index = element_count + 1; |
+ if (element_index >= length) { |
+ int new_length = length * 2; |
+ MaybeObject* allocation = AllocateFixedArray(new_length); |
+ if (allocation->IsFailure()) return allocation; |
Rico
2011/09/23 07:06:52
Why not just use our standard way of doing this us
Lasse Reichstein
2011/09/23 09:54:39
I don't particularly like our "standard way" - the
|
+ FixedArray* new_locks = FixedArray::cast(allocation->ToObjectUnchecked()); |
+ for (int i = 1; i < length; i++) { |
+ new_locks->set(i, locks->get(i)); |
+ } |
+ set_string_locks(new_locks); |
+ locks = new_locks; |
+ } |
+ locks->set(element_index, string); |
+ locks->set(0, Smi::FromInt(element_index)); |
+ return string; |
+} |
+ |
+ |
+void Heap::UnlockString(String* string) { |
+ FixedArray* locks = string_locks(); |
+ ASSERT(locks->length() > 1); |
+ int element_count = Smi::cast(locks->get(0))->value(); |
+ ASSERT(element_count > 0); |
+ ASSERT(element_count < locks->length()); |
+ for (int i = 1; i <= element_count; i++) { |
+ String* element = String::cast(locks->get(i)); |
+ if (element == string) { |
+ if (i < element_count) { |
+ locks->set(i, locks->get(element_count)); |
+ } |
+ locks->set_undefined(element_count); |
+ locks->set(0, Smi::FromInt(element_count - 1)); |
+ return; |
+ } |
+ } |
+ // We should have found the string. It's an error to try to unlock |
+ // a string that hasn't been locked. |
+ UNREACHABLE(); |
+} |
+ |
+ |
+bool Heap::IsStringLocked(String* string) { |
+ if (string->IsConsString()) return false; |
+ FixedArray* locks = string_locks(); |
+ ASSERT(locks->length() > 1); |
+ int element_count = Smi::cast(locks->get(0))->value(); |
+ for (int i = 1; i <= element_count; i++) { |
+ if (locks->get(i) == string) return true; |
+ } |
+ return false; |
+} |
+ |
+ |
+MaybeObject* Heap::InitializeStringLocks() { |
+ const int kInitialSize = 6; |
+ MaybeObject* allocation = AllocateFixedArray(kInitialSize); |
+ if (allocation->IsFailure()) return allocation; |
+ FixedArray* new_array = FixedArray::cast(allocation->ToObjectUnchecked()); |
+ new_array->set(0, Smi::FromInt(0)); |
+ set_string_locks(new_array); |
+ return new_array; |
+} |
+ |
+ |
Map* Heap::MapForExternalArrayType(ExternalArrayType array_type) { |
return Map::cast(roots_[RootIndexForExternalArrayType(array_type)]); |
} |