1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  * vim: set ts=8 sts=4 et sw=4 tw=99: */
3 
4 // Copyright 2012 the V8 project authors. All rights reserved.
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 //     * Redistributions of source code must retain the above copyright
10 //       notice, this list of conditions and the following disclaimer.
11 //     * Redistributions in binary form must reproduce the above
12 //       copyright notice, this list of conditions and the following
13 //       disclaimer in the documentation and/or other materials provided
14 //       with the distribution.
15 //     * Neither the name of Google Inc. nor the names of its
16 //       contributors may be used to endorse or promote products derived
17 //       from this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 #include "irregexp/RegExpStack.h"
32 
33 #include "vm/JSContext.h"
34 
35 using namespace js;
36 using namespace js::irregexp;
37 
RegExpStackScope(JSContext * cx)38 RegExpStackScope::RegExpStackScope(JSContext* cx)
39   : regexp_stack(&cx->regexpStack.ref())
40 {}
41 
~RegExpStackScope()42 RegExpStackScope::~RegExpStackScope()
43 {
44     regexp_stack->reset();
45 }
46 
47 bool
GrowBacktrackStack(JSRuntime * rt)48 irregexp::GrowBacktrackStack(JSRuntime* rt)
49 {
50     AutoUnsafeCallWithABI unsafe;
51     return TlsContext.get()->regexpStack.ref().grow();
52 }
53 
RegExpStack()54 RegExpStack::RegExpStack()
55   : base_(nullptr), size(0), limit_(nullptr)
56 {}
57 
~RegExpStack()58 RegExpStack::~RegExpStack()
59 {
60     js_free(base_);
61 }
62 
63 bool
init()64 RegExpStack::init()
65 {
66     base_ = js_malloc(kMinimumStackSize);
67     if (!base_)
68         return false;
69 
70     size = kMinimumStackSize;
71     updateLimit();
72     return true;
73 }
74 
75 void
reset()76 RegExpStack::reset()
77 {
78     MOZ_ASSERT(size >= kMinimumStackSize);
79 
80     if (size != kMinimumStackSize) {
81         void* newBase = js_realloc(base_, kMinimumStackSize);
82         if (!newBase)
83             return;
84 
85         base_ = newBase;
86         size = kMinimumStackSize;
87         updateLimit();
88     }
89 }
90 
91 bool
grow()92 RegExpStack::grow()
93 {
94     size_t newSize = size * 2;
95     if (newSize > kMaximumStackSize)
96         return false;
97 
98     void* newBase = js_realloc(base_, newSize);
99     if (!newBase)
100         return false;
101 
102     base_ = newBase;
103     size = newSize;
104     updateLimit();
105 
106     return true;
107 }
108