| 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 "irregexp/imported/regexp-stack.h" |
| 6 |
|
| 7 |
|
| 8 |
namespace v8 { |
| 9 |
namespace internal { |
| 10 |
|
| 11 |
RegExpStackScope::RegExpStackScope(Isolate* isolate) |
| 12 |
: regexp_stack_(isolate->regexp_stack()), |
| 13 |
old_sp_top_delta_(regexp_stack_->sp_top_delta()) { |
| 14 |
DCHECK(regexp_stack_->IsValid()); |
| 15 |
} |
| 16 |
|
| 17 |
RegExpStackScope::~RegExpStackScope() { |
| 18 |
CHECK_EQ(old_sp_top_delta_, regexp_stack_->sp_top_delta()); |
| 19 |
regexp_stack_->ResetIfEmpty(); |
| 20 |
} |
| 21 |
|
| 22 |
RegExpStack::RegExpStack() : thread_local_(this) {} |
| 23 |
|
| 24 |
RegExpStack::~RegExpStack() { thread_local_.FreeAndInvalidate(); } |
| 25 |
|
| 26 |
char* RegExpStack::ArchiveStack(char* to) { |
| 27 |
if (!thread_local_.owns_memory_) { |
| 28 |
// Force dynamic stacks prior to archiving. Any growth will do. A dynamic |
| 29 |
// stack is needed because stack archival & restoration rely on `memory_` |
| 30 |
// pointing at a fixed-location backing store, whereas the static stack is |
| 31 |
// tied to a RegExpStack instance. |
| 32 |
EnsureCapacity(thread_local_.memory_size_ + 1); |
| 33 |
DCHECK(thread_local_.owns_memory_); |
| 34 |
} |
| 35 |
|
| 36 |
MemCopy(reinterpret_cast<void*>(to), &thread_local_, kThreadLocalSize); |
| 37 |
thread_local_ = ThreadLocal(this); |
| 38 |
return to + kThreadLocalSize; |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
char* RegExpStack::RestoreStack(char* from) { |
| 43 |
MemCopy(&thread_local_, reinterpret_cast<void*>(from), kThreadLocalSize); |
| 44 |
return from + kThreadLocalSize; |
| 45 |
} |
| 46 |
|
| 47 |
void RegExpStack::ThreadLocal::ResetToStaticStack(RegExpStack* regexp_stack) { |
| 48 |
if (owns_memory_) DeleteArray(memory_); |
| 49 |
|
| 50 |
memory_ = regexp_stack->static_stack_; |
| 51 |
memory_top_ = regexp_stack->static_stack_ + kStaticStackSize; |
| 52 |
memory_size_ = kStaticStackSize; |
| 53 |
stack_pointer_ = memory_top_; |
| 54 |
limit_ = reinterpret_cast<Address>(regexp_stack->static_stack_) + |
| 55 |
kStackLimitSlackSize; |
| 56 |
owns_memory_ = false; |
| 57 |
} |
| 58 |
|
| 59 |
void RegExpStack::ThreadLocal::FreeAndInvalidate() { |
| 60 |
if (owns_memory_) DeleteArray(memory_); |
| 61 |
|
| 62 |
// This stack may not be used after being freed. Just reset to invalid values |
| 63 |
// to ensure we don't accidentally use old memory areas. |
| 64 |
memory_ = nullptr; |
| 65 |
memory_top_ = nullptr; |
| 66 |
memory_size_ = 0; |
| 67 |
stack_pointer_ = nullptr; |
| 68 |
limit_ = kMemoryTop; |
| 69 |
} |
| 70 |
|
| 71 |
Address RegExpStack::EnsureCapacity(size_t size) { |
| 72 |
if (size > kMaximumStackSize) return kNullAddress; |
| 73 |
if (thread_local_.memory_size_ < size) { |
| 74 |
if (size < kMinimumDynamicStackSize) size = kMinimumDynamicStackSize; |
| 75 |
uint8_t* new_memory = NewArray<uint8_t>(size); |
| 76 |
if (thread_local_.memory_size_ > 0) { |
| 77 |
// Copy original memory into top of new memory. |
| 78 |
MemCopy(new_memory + size - thread_local_.memory_size_, |
| 79 |
thread_local_.memory_, thread_local_.memory_size_); |
| 80 |
if (thread_local_.owns_memory_) DeleteArray(thread_local_.memory_); |
| 81 |
} |
| 82 |
ptrdiff_t delta = sp_top_delta(); |
| 83 |
thread_local_.memory_ = new_memory; |
| 84 |
thread_local_.memory_top_ = new_memory + size; |
| 85 |
thread_local_.memory_size_ = size; |
| 86 |
thread_local_.stack_pointer_ = thread_local_.memory_top_ + delta; |
| 87 |
thread_local_.limit_ = |
| 88 |
reinterpret_cast<Address>(new_memory) + kStackLimitSlackSize; |
| 89 |
thread_local_.owns_memory_ = true; |
| 90 |
} |
| 91 |
return reinterpret_cast<Address>(thread_local_.memory_top_); |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
} // namespace internal |
| 96 |
} // namespace v8 |