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/regexp/regexp-stack.h" 6 7 #include "src/isolate.h" 8 9 namespace v8 { 10 namespace internal { 11 RegExpStackScope(Isolate * isolate)12RegExpStackScope::RegExpStackScope(Isolate* isolate) 13 : regexp_stack_(isolate->regexp_stack()) { 14 // Initialize, if not already initialized. 15 regexp_stack_->EnsureCapacity(0); 16 } 17 18 ~RegExpStackScope()19RegExpStackScope::~RegExpStackScope() { 20 // Reset the buffer if it has grown. 21 regexp_stack_->Reset(); 22 } 23 RegExpStack()24RegExpStack::RegExpStack() : isolate_(nullptr) {} 25 ~RegExpStack()26RegExpStack::~RegExpStack() { 27 thread_local_.Free(); 28 } 29 30 ArchiveStack(char * to)31char* RegExpStack::ArchiveStack(char* to) { 32 size_t size = sizeof(thread_local_); 33 MemCopy(reinterpret_cast<void*>(to), &thread_local_, size); 34 thread_local_ = ThreadLocal(); 35 return to + size; 36 } 37 38 RestoreStack(char * from)39char* RegExpStack::RestoreStack(char* from) { 40 size_t size = sizeof(thread_local_); 41 MemCopy(&thread_local_, reinterpret_cast<void*>(from), size); 42 return from + size; 43 } 44 45 Reset()46void RegExpStack::Reset() { 47 if (thread_local_.memory_size_ > kMinimumStackSize) { 48 DeleteArray(thread_local_.memory_); 49 thread_local_ = ThreadLocal(); 50 } 51 } 52 53 Free()54void RegExpStack::ThreadLocal::Free() { 55 if (memory_size_ > 0) { 56 DeleteArray(memory_); 57 Clear(); 58 } 59 } 60 61 EnsureCapacity(size_t size)62Address RegExpStack::EnsureCapacity(size_t size) { 63 if (size > kMaximumStackSize) return kNullAddress; 64 if (size < kMinimumStackSize) size = kMinimumStackSize; 65 if (thread_local_.memory_size_ < size) { 66 byte* new_memory = NewArray<byte>(size); 67 if (thread_local_.memory_size_ > 0) { 68 // Copy original memory into top of new memory. 69 MemCopy(new_memory + size - thread_local_.memory_size_, 70 thread_local_.memory_, thread_local_.memory_size_); 71 DeleteArray(thread_local_.memory_); 72 } 73 thread_local_.memory_ = new_memory; 74 thread_local_.memory_size_ = size; 75 thread_local_.limit_ = 76 reinterpret_cast<Address>(new_memory) + kStackLimitSlack * kPointerSize; 77 } 78 return reinterpret_cast<Address>(thread_local_.memory_) + 79 thread_local_.memory_size_; 80 } 81 82 83 } // namespace internal 84 } // namespace v8 85