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

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: More tweaks. Created 9 years, 3 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
« no previous file with comments | « src/heap.h ('k') | src/json-parser.h » ('j') | src/objects.h » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 2307 matching lines...) Expand 10 before | Expand all | Expand 10 after
2318 { MaybeObject* maybe_obj = StringDictionary::Allocate(Runtime::kNumFunctions); 2318 { MaybeObject* maybe_obj = StringDictionary::Allocate(Runtime::kNumFunctions);
2319 if (!maybe_obj->ToObject(&obj)) return false; 2319 if (!maybe_obj->ToObject(&obj)) return false;
2320 } 2320 }
2321 { MaybeObject* maybe_obj = Runtime::InitializeIntrinsicFunctionNames(this, 2321 { MaybeObject* maybe_obj = Runtime::InitializeIntrinsicFunctionNames(this,
2322 obj); 2322 obj);
2323 if (!maybe_obj->ToObject(&obj)) return false; 2323 if (!maybe_obj->ToObject(&obj)) return false;
2324 } 2324 }
2325 set_intrinsic_function_names(StringDictionary::cast(obj)); 2325 set_intrinsic_function_names(StringDictionary::cast(obj));
2326 2326
2327 if (InitializeNumberStringCache()->IsFailure()) return false; 2327 if (InitializeNumberStringCache()->IsFailure()) return false;
2328 if (InitializeStringLocks()->IsFailure()) return false;
2328 2329
2329 // Allocate cache for single character ASCII strings. 2330 // Allocate cache for single character ASCII strings.
2330 { MaybeObject* maybe_obj = 2331 { MaybeObject* maybe_obj =
2331 AllocateFixedArray(String::kMaxAsciiCharCode + 1, TENURED); 2332 AllocateFixedArray(String::kMaxAsciiCharCode + 1, TENURED);
2332 if (!maybe_obj->ToObject(&obj)) return false; 2333 if (!maybe_obj->ToObject(&obj)) return false;
2333 } 2334 }
2334 set_single_character_string_cache(FixedArray::cast(obj)); 2335 set_single_character_string_cache(FixedArray::cast(obj));
2335 2336
2336 // Allocate cache for string split. 2337 // Allocate cache for string split.
2337 { MaybeObject* maybe_obj = 2338 { MaybeObject* maybe_obj =
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after
2524 } 2525 }
2525 2526
2526 Object* js_string; 2527 Object* js_string;
2527 MaybeObject* maybe_js_string = AllocateStringFromAscii(CStrVector(str)); 2528 MaybeObject* maybe_js_string = AllocateStringFromAscii(CStrVector(str));
2528 if (maybe_js_string->ToObject(&js_string)) { 2529 if (maybe_js_string->ToObject(&js_string)) {
2529 SetNumberStringCache(number, String::cast(js_string)); 2530 SetNumberStringCache(number, String::cast(js_string));
2530 } 2531 }
2531 return maybe_js_string; 2532 return maybe_js_string;
2532 } 2533 }
2533 2534
2534 2535
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.
2536 MaybeObject* Heap::LockString(String* string) {
2537 ASSERT(!string->IsConsString());
2538 FixedArray* locks = string_locks();
2539 ASSERT(locks->length() > 1);
2540 int length = locks->length();
2541 int element_count = Smi::cast(locks->get(0))->value();
2542 int element_index = element_count + 1;
2543 if (element_index >= length) {
2544 int new_length = length * 2;
2545 MaybeObject* allocation = AllocateFixedArray(new_length);
2546 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
2547 FixedArray* new_locks = FixedArray::cast(allocation->ToObjectUnchecked());
2548 for (int i = 1; i < length; i++) {
2549 new_locks->set(i, locks->get(i));
2550 }
2551 set_string_locks(new_locks);
2552 locks = new_locks;
2553 }
2554 locks->set(element_index, string);
2555 locks->set(0, Smi::FromInt(element_index));
2556 return string;
2557 }
2558
2559
2560 void Heap::UnlockString(String* string) {
2561 FixedArray* locks = string_locks();
2562 ASSERT(locks->length() > 1);
2563 int element_count = Smi::cast(locks->get(0))->value();
2564 ASSERT(element_count > 0);
2565 ASSERT(element_count < locks->length());
2566 for (int i = 1; i <= element_count; i++) {
2567 String* element = String::cast(locks->get(i));
2568 if (element == string) {
2569 if (i < element_count) {
2570 locks->set(i, locks->get(element_count));
2571 }
2572 locks->set_undefined(element_count);
2573 locks->set(0, Smi::FromInt(element_count - 1));
2574 return;
2575 }
2576 }
2577 // We should have found the string. It's an error to try to unlock
2578 // a string that hasn't been locked.
2579 UNREACHABLE();
2580 }
2581
2582
2583 bool Heap::IsStringLocked(String* string) {
2584 if (string->IsConsString()) return false;
2585 FixedArray* locks = string_locks();
2586 ASSERT(locks->length() > 1);
2587 int element_count = Smi::cast(locks->get(0))->value();
2588 for (int i = 1; i <= element_count; i++) {
2589 if (locks->get(i) == string) return true;
2590 }
2591 return false;
2592 }
2593
2594
2595 MaybeObject* Heap::InitializeStringLocks() {
2596 const int kInitialSize = 6;
2597 MaybeObject* allocation = AllocateFixedArray(kInitialSize);
2598 if (allocation->IsFailure()) return allocation;
2599 FixedArray* new_array = FixedArray::cast(allocation->ToObjectUnchecked());
2600 new_array->set(0, Smi::FromInt(0));
2601 set_string_locks(new_array);
2602 return new_array;
2603 }
2604
2605
2535 Map* Heap::MapForExternalArrayType(ExternalArrayType array_type) { 2606 Map* Heap::MapForExternalArrayType(ExternalArrayType array_type) {
2536 return Map::cast(roots_[RootIndexForExternalArrayType(array_type)]); 2607 return Map::cast(roots_[RootIndexForExternalArrayType(array_type)]);
2537 } 2608 }
2538 2609
2539 2610
2540 Heap::RootListIndex Heap::RootIndexForExternalArrayType( 2611 Heap::RootListIndex Heap::RootIndexForExternalArrayType(
2541 ExternalArrayType array_type) { 2612 ExternalArrayType array_type) {
2542 switch (array_type) { 2613 switch (array_type) {
2543 case kExternalByteArray: 2614 case kExternalByteArray:
2544 return kExternalByteArrayMapRootIndex; 2615 return kExternalByteArrayMapRootIndex;
(...skipping 3802 matching lines...) Expand 10 before | Expand all | Expand 10 after
6347 } 6418 }
6348 isolate_->heap()->store_buffer()->Filter(MemoryChunk::ABOUT_TO_BE_FREED); 6419 isolate_->heap()->store_buffer()->Filter(MemoryChunk::ABOUT_TO_BE_FREED);
6349 for (chunk = chunks_queued_for_free_; chunk != NULL; chunk = next) { 6420 for (chunk = chunks_queued_for_free_; chunk != NULL; chunk = next) {
6350 next = chunk->next_chunk(); 6421 next = chunk->next_chunk();
6351 isolate_->memory_allocator()->Free(chunk); 6422 isolate_->memory_allocator()->Free(chunk);
6352 } 6423 }
6353 chunks_queued_for_free_ = NULL; 6424 chunks_queued_for_free_ = NULL;
6354 } 6425 }
6355 6426
6356 } } // namespace v8::internal 6427 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/heap.h ('k') | src/json-parser.h » ('j') | src/objects.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698