1 // Copyright 2009 the V8 project authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "src/v8.h" 6 #include "src/regexp-stack.h" 7 8 namespace v8 { 9 namespace internal { 10 11 RegExpStackScope::RegExpStackScope(Isolate* isolate) 12 : regexp_stack_(isolate->regexp_stack()) { 13 // Initialize, if not already initialized. 14 regexp_stack_->EnsureCapacity(0); 15 } 16 17 18 RegExpStackScope::~RegExpStackScope() { 19 // Reset the buffer if it has grown. 20 regexp_stack_->Reset(); 21 } 22 23 24 RegExpStack::RegExpStack() 25 : isolate_(NULL) { 26 } 27 28 29 RegExpStack::~RegExpStack() { 30 thread_local_.Free(); 31 } 32 33 34 char* RegExpStack::ArchiveStack(char* to) { 35 size_t size = sizeof(thread_local_); 36 MemCopy(reinterpret_cast<void*>(to), &thread_local_, size); 37 thread_local_ = ThreadLocal(); 38 return to + size; 39 } 40 41 42 char* RegExpStack::RestoreStack(char* from) { 43 size_t size = sizeof(thread_local_); 44 MemCopy(&thread_local_, reinterpret_cast<void*>(from), size); 45 return from + size; 46 } 47 48 49 void RegExpStack::Reset() { 50 if (thread_local_.memory_size_ > kMinimumStackSize) { 51 DeleteArray(thread_local_.memory_); 52 thread_local_ = ThreadLocal(); 53 } 54 } 55 56 57 void RegExpStack::ThreadLocal::Free() { 58 if (memory_size_ > 0) { 59 DeleteArray(memory_); 60 Clear(); 61 } 62 } 63 64 65 Address RegExpStack::EnsureCapacity(size_t size) { 66 if (size > kMaximumStackSize) return NULL; 67 if (size < kMinimumStackSize) size = kMinimumStackSize; 68 if (thread_local_.memory_size_ < size) { 69 Address new_memory = NewArray<byte>(static_cast<int>(size)); 70 if (thread_local_.memory_size_ > 0) { 71 // Copy original memory into top of new memory. 72 MemCopy(reinterpret_cast<void*>(new_memory + size - 73 thread_local_.memory_size_), 74 reinterpret_cast<void*>(thread_local_.memory_), 75 thread_local_.memory_size_); 76 DeleteArray(thread_local_.memory_); 77 } 78 thread_local_.memory_ = new_memory; 79 thread_local_.memory_size_ = size; 80 thread_local_.limit_ = new_memory + kStackLimitSlack * kPointerSize; 81 } 82 return thread_local_.memory_ + thread_local_.memory_size_; 83 } 84 85 86 }} // namespace v8::internal 87