blob: 34f9127f2fc922ea390dce16e56c4465d5df4347 [file] [log] [blame]
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001// 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
9namespace v8 {
10namespace internal {
11
12RegExpStackScope::RegExpStackScope(Isolate* isolate)
13 : regexp_stack_(isolate->regexp_stack()) {
14 // Initialize, if not already initialized.
15 regexp_stack_->EnsureCapacity(0);
16}
17
18
19RegExpStackScope::~RegExpStackScope() {
20 // Reset the buffer if it has grown.
21 regexp_stack_->Reset();
22}
23
24
25RegExpStack::RegExpStack()
26 : isolate_(NULL) {
27}
28
29
30RegExpStack::~RegExpStack() {
31 thread_local_.Free();
32}
33
34
35char* RegExpStack::ArchiveStack(char* to) {
36 size_t size = sizeof(thread_local_);
37 MemCopy(reinterpret_cast<void*>(to), &thread_local_, size);
38 thread_local_ = ThreadLocal();
39 return to + size;
40}
41
42
43char* RegExpStack::RestoreStack(char* from) {
44 size_t size = sizeof(thread_local_);
45 MemCopy(&thread_local_, reinterpret_cast<void*>(from), size);
46 return from + size;
47}
48
49
50void RegExpStack::Reset() {
51 if (thread_local_.memory_size_ > kMinimumStackSize) {
52 DeleteArray(thread_local_.memory_);
53 thread_local_ = ThreadLocal();
54 }
55}
56
57
58void RegExpStack::ThreadLocal::Free() {
59 if (memory_size_ > 0) {
60 DeleteArray(memory_);
61 Clear();
62 }
63}
64
65
66Address RegExpStack::EnsureCapacity(size_t size) {
67 if (size > kMaximumStackSize) return NULL;
68 if (size < kMinimumStackSize) size = kMinimumStackSize;
69 if (thread_local_.memory_size_ < size) {
70 Address new_memory = NewArray<byte>(static_cast<int>(size));
71 if (thread_local_.memory_size_ > 0) {
72 // Copy original memory into top of new memory.
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_);
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
87} // namespace internal
88} // namespace v8