blob: f114ae44240c9f56a66044716c4ea0495f6fb964 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2009 the V8 project authors. All rights reserved.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Steve Blocka7e24c12009-10-30 11:49:00 +00004
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005#include "src/v8.h"
6
7#include "src/regexp-stack.h"
Steve Blocka7e24c12009-10-30 11:49:00 +00008
9namespace v8 {
10namespace internal {
11
Steve Block44f0eee2011-05-26 01:26:41 +010012RegExpStackScope::RegExpStackScope(Isolate* isolate)
13 : regexp_stack_(isolate->regexp_stack()) {
Steve Blocka7e24c12009-10-30 11:49:00 +000014 // Initialize, if not already initialized.
Steve Block44f0eee2011-05-26 01:26:41 +010015 regexp_stack_->EnsureCapacity(0);
16}
17
18
19RegExpStackScope::~RegExpStackScope() {
Steve Block44f0eee2011-05-26 01:26:41 +010020 // Reset the buffer if it has grown.
21 regexp_stack_->Reset();
22}
23
24
25RegExpStack::RegExpStack()
26 : isolate_(NULL) {
Steve Blocka7e24c12009-10-30 11:49:00 +000027}
28
29
30RegExpStack::~RegExpStack() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000031 thread_local_.Free();
Steve Blocka7e24c12009-10-30 11:49:00 +000032}
33
34
35char* RegExpStack::ArchiveStack(char* to) {
36 size_t size = sizeof(thread_local_);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000037 MemCopy(reinterpret_cast<void*>(to), &thread_local_, size);
Steve Blocka7e24c12009-10-30 11:49:00 +000038 thread_local_ = ThreadLocal();
39 return to + size;
40}
41
42
43char* RegExpStack::RestoreStack(char* from) {
44 size_t size = sizeof(thread_local_);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000045 MemCopy(&thread_local_, reinterpret_cast<void*>(from), size);
Steve Blocka7e24c12009-10-30 11:49:00 +000046 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() {
Steve Block44f0eee2011-05-26 01:26:41 +010059 if (memory_size_ > 0) {
60 DeleteArray(memory_);
61 Clear();
Steve Blocka7e24c12009-10-30 11:49:00 +000062 }
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) {
Steve Blockd0582a62009-12-15 09:54:21 +000070 Address new_memory = NewArray<byte>(static_cast<int>(size));
Steve Blocka7e24c12009-10-30 11:49:00 +000071 if (thread_local_.memory_size_ > 0) {
72 // Copy original memory into top of new memory.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000073 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 Blocka7e24c12009-10-30 11:49:00 +000077 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 Blocka7e24c12009-10-30 11:49:00 +000087}} // namespace v8::internal