1// Copyright 2009 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6//     * Redistributions of source code must retain the above copyright
7//       notice, this list of conditions and the following disclaimer.
8//     * Redistributions in binary form must reproduce the above
9//       copyright notice, this list of conditions and the following
10//       disclaimer in the documentation and/or other materials provided
11//       with the distribution.
12//     * Neither the name of Google Inc. nor the names of its
13//       contributors may be used to endorse or promote products derived
14//       from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include "v8.h"
29#include "top.h"
30#include "regexp-stack.h"
31
32namespace v8 {
33namespace internal {
34
35RegExpStack::RegExpStack() {
36  // Initialize, if not already initialized.
37  RegExpStack::EnsureCapacity(0);
38}
39
40
41RegExpStack::~RegExpStack() {
42  // Reset the buffer if it has grown.
43  RegExpStack::Reset();
44}
45
46
47char* RegExpStack::ArchiveStack(char* to) {
48  size_t size = sizeof(thread_local_);
49  memcpy(reinterpret_cast<void*>(to),
50         &thread_local_,
51         size);
52  thread_local_ = ThreadLocal();
53  return to + size;
54}
55
56
57char* RegExpStack::RestoreStack(char* from) {
58  size_t size = sizeof(thread_local_);
59  memcpy(&thread_local_, reinterpret_cast<void*>(from), size);
60  return from + size;
61}
62
63
64void RegExpStack::Reset() {
65  if (thread_local_.memory_size_ > kMinimumStackSize) {
66    DeleteArray(thread_local_.memory_);
67    thread_local_ = ThreadLocal();
68  }
69}
70
71
72void RegExpStack::ThreadLocal::Free() {
73  if (thread_local_.memory_size_ > 0) {
74    DeleteArray(thread_local_.memory_);
75    thread_local_ = ThreadLocal();
76  }
77}
78
79
80Address RegExpStack::EnsureCapacity(size_t size) {
81  if (size > kMaximumStackSize) return NULL;
82  if (size < kMinimumStackSize) size = kMinimumStackSize;
83  if (thread_local_.memory_size_ < size) {
84    Address new_memory = NewArray<byte>(static_cast<int>(size));
85    if (thread_local_.memory_size_ > 0) {
86      // Copy original memory into top of new memory.
87      memcpy(reinterpret_cast<void*>(
88          new_memory + size - thread_local_.memory_size_),
89             reinterpret_cast<void*>(thread_local_.memory_),
90             thread_local_.memory_size_);
91      DeleteArray(thread_local_.memory_);
92    }
93    thread_local_.memory_ = new_memory;
94    thread_local_.memory_size_ = size;
95    thread_local_.limit_ = new_memory + kStackLimitSlack * kPointerSize;
96  }
97  return thread_local_.memory_ + thread_local_.memory_size_;
98}
99
100
101RegExpStack::ThreadLocal RegExpStack::thread_local_;
102
103}}  // namespace v8::internal
104