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