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