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