Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(401)

Side by Side Diff: src/heap.cc

Issue 7977001: Added ability to lock strings to prevent their representation or encoding from changing. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Addressed review comments Created 9 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2011 the V8 project authors. All rights reserved. 1 // Copyright 2011 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 2315 matching lines...) Expand 10 before | Expand all | Expand 10 after
2326 { MaybeObject* maybe_obj = StringDictionary::Allocate(Runtime::kNumFunctions); 2326 { MaybeObject* maybe_obj = StringDictionary::Allocate(Runtime::kNumFunctions);
2327 if (!maybe_obj->ToObject(&obj)) return false; 2327 if (!maybe_obj->ToObject(&obj)) return false;
2328 } 2328 }
2329 { MaybeObject* maybe_obj = Runtime::InitializeIntrinsicFunctionNames(this, 2329 { MaybeObject* maybe_obj = Runtime::InitializeIntrinsicFunctionNames(this,
2330 obj); 2330 obj);
2331 if (!maybe_obj->ToObject(&obj)) return false; 2331 if (!maybe_obj->ToObject(&obj)) return false;
2332 } 2332 }
2333 set_intrinsic_function_names(StringDictionary::cast(obj)); 2333 set_intrinsic_function_names(StringDictionary::cast(obj));
2334 2334
2335 if (InitializeNumberStringCache()->IsFailure()) return false; 2335 if (InitializeNumberStringCache()->IsFailure()) return false;
2336 if (InitializeStringLocks()->IsFailure()) return false;
2336 2337
2337 // Allocate cache for single character ASCII strings. 2338 // Allocate cache for single character ASCII strings.
2338 { MaybeObject* maybe_obj = 2339 { MaybeObject* maybe_obj =
2339 AllocateFixedArray(String::kMaxAsciiCharCode + 1, TENURED); 2340 AllocateFixedArray(String::kMaxAsciiCharCode + 1, TENURED);
2340 if (!maybe_obj->ToObject(&obj)) return false; 2341 if (!maybe_obj->ToObject(&obj)) return false;
2341 } 2342 }
2342 set_single_character_string_cache(FixedArray::cast(obj)); 2343 set_single_character_string_cache(FixedArray::cast(obj));
2343 2344
2344 // Allocate cache for string split. 2345 // Allocate cache for string split.
2345 { MaybeObject* maybe_obj = 2346 { MaybeObject* maybe_obj =
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after
2542 2543
2543 MaybeObject* Heap::Uint32ToString(uint32_t value, 2544 MaybeObject* Heap::Uint32ToString(uint32_t value,
2544 bool check_number_string_cache) { 2545 bool check_number_string_cache) {
2545 Object* number; 2546 Object* number;
2546 MaybeObject* maybe = NumberFromUint32(value); 2547 MaybeObject* maybe = NumberFromUint32(value);
2547 if (!maybe->To<Object>(&number)) return maybe; 2548 if (!maybe->To<Object>(&number)) return maybe;
2548 return NumberToString(number, check_number_string_cache); 2549 return NumberToString(number, check_number_string_cache);
2549 } 2550 }
2550 2551
2551 2552
2553 MaybeObject* Heap::LockString(String* string) {
2554 ASSERT(!string->IsConsString());
2555 FixedArray* locks = string_locks();
2556 ASSERT(locks->length() > 1);
2557 int length = locks->length();
2558 int element_count = Smi::cast(locks->get(0))->value();
2559 int element_index = element_count + 1;
2560 if (element_index >= length) {
2561 int new_length = length * 2;
2562 MaybeObject* allocation = AllocateFixedArray(new_length);
2563 FixedArray* new_locks = NULL; // Initialized to please compiler.
2564 if (!allocation->To<FixedArray>(&new_locks)) return allocation;
2565 for (int i = 1; i < length; i++) {
2566 new_locks->set(i, locks->get(i));
2567 }
2568 set_string_locks(new_locks);
2569 locks = new_locks;
2570 }
2571 locks->set(element_index, string);
2572 locks->set(0, Smi::FromInt(element_index));
2573 return string;
2574 }
2575
2576
2577 void Heap::UnlockString(String* string) {
2578 FixedArray* locks = string_locks();
2579 ASSERT(locks->length() > 1);
2580 int element_count = Smi::cast(locks->get(0))->value();
2581 ASSERT(element_count > 0);
2582 ASSERT(element_count < locks->length());
2583 for (int i = 1; i <= element_count; i++) {
2584 String* element = String::cast(locks->get(i));
2585 if (element == string) {
2586 if (i < element_count) {
2587 locks->set(i, locks->get(element_count));
2588 }
2589 locks->set_undefined(element_count);
2590 locks->set(0, Smi::FromInt(element_count - 1));
2591 return;
2592 }
2593 }
2594 // We should have found the string. It's an error to try to unlock
2595 // a string that hasn't been locked.
2596 UNREACHABLE();
2597 }
2598
2599
2600 bool Heap::IsStringLocked(String* string) {
2601 if (string->IsConsString()) return false;
2602 FixedArray* locks = string_locks();
2603 ASSERT(locks->length() > 1);
2604 int element_count = Smi::cast(locks->get(0))->value();
2605 for (int i = 1; i <= element_count; i++) {
2606 if (locks->get(i) == string) return true;
2607 }
2608 return false;
2609 }
2610
2611
2612 MaybeObject* Heap::InitializeStringLocks() {
2613 const int kInitialSize = 6;
2614 MaybeObject* allocation = AllocateFixedArray(kInitialSize);
2615 if (allocation->IsFailure()) return allocation;
2616 FixedArray* new_array = FixedArray::cast(allocation->ToObjectUnchecked());
2617 new_array->set(0, Smi::FromInt(0));
2618 set_string_locks(new_array);
2619 return new_array;
2620 }
2621
2622
2552 Map* Heap::MapForExternalArrayType(ExternalArrayType array_type) { 2623 Map* Heap::MapForExternalArrayType(ExternalArrayType array_type) {
2553 return Map::cast(roots_[RootIndexForExternalArrayType(array_type)]); 2624 return Map::cast(roots_[RootIndexForExternalArrayType(array_type)]);
2554 } 2625 }
2555 2626
2556 2627
2557 Heap::RootListIndex Heap::RootIndexForExternalArrayType( 2628 Heap::RootListIndex Heap::RootIndexForExternalArrayType(
2558 ExternalArrayType array_type) { 2629 ExternalArrayType array_type) {
2559 switch (array_type) { 2630 switch (array_type) {
2560 case kExternalByteArray: 2631 case kExternalByteArray:
2561 return kExternalByteArrayMapRootIndex; 2632 return kExternalByteArrayMapRootIndex;
(...skipping 3826 matching lines...) Expand 10 before | Expand all | Expand 10 after
6388 } 6459 }
6389 isolate_->heap()->store_buffer()->Filter(MemoryChunk::ABOUT_TO_BE_FREED); 6460 isolate_->heap()->store_buffer()->Filter(MemoryChunk::ABOUT_TO_BE_FREED);
6390 for (chunk = chunks_queued_for_free_; chunk != NULL; chunk = next) { 6461 for (chunk = chunks_queued_for_free_; chunk != NULL; chunk = next) {
6391 next = chunk->next_chunk(); 6462 next = chunk->next_chunk();
6392 isolate_->memory_allocator()->Free(chunk); 6463 isolate_->memory_allocator()->Free(chunk);
6393 } 6464 }
6394 chunks_queued_for_free_ = NULL; 6465 chunks_queued_for_free_ = NULL;
6395 } 6466 }
6396 6467
6397 } } // namespace v8::internal 6468 } } // namespace v8::internal
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698