ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 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 | |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 28 | #ifndef V8_REGEXP_STACK_H_ |
| 29 | #define V8_REGEXP_STACK_H_ |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 30 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 31 | namespace v8 { |
| 32 | namespace internal { |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 33 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 34 | class RegExpStack; |
| 35 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 36 | // Maintains a per-v8thread stack area that can be used by irregexp |
| 37 | // implementation for its backtracking stack. |
| 38 | // Since there is only one stack area, the Irregexp implementation is not |
| 39 | // re-entrant. I.e., no regular expressions may be executed in the same thread |
| 40 | // during a preempted Irregexp execution. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 41 | class RegExpStackScope { |
| 42 | public: |
| 43 | // Create and delete an instance to control the life-time of a growing stack. |
| 44 | |
| 45 | // Initializes the stack memory area if necessary. |
| 46 | explicit RegExpStackScope(Isolate* isolate); |
| 47 | ~RegExpStackScope(); // Releases the stack if it has grown. |
| 48 | |
| 49 | RegExpStack* stack() const { return regexp_stack_; } |
| 50 | |
| 51 | private: |
| 52 | RegExpStack* regexp_stack_; |
| 53 | |
| 54 | DISALLOW_COPY_AND_ASSIGN(RegExpStackScope); |
| 55 | }; |
| 56 | |
| 57 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 58 | class RegExpStack { |
| 59 | public: |
| 60 | // Number of allocated locations on the stack below the limit. |
| 61 | // No sequence of pushes must be longer that this without doing a stack-limit |
| 62 | // check. |
| 63 | static const int kStackLimitSlack = 32; |
| 64 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 65 | // Gives the top of the memory used as stack. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 66 | Address stack_base() { |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 67 | ASSERT(thread_local_.memory_size_ != 0); |
| 68 | return thread_local_.memory_ + thread_local_.memory_size_; |
| 69 | } |
| 70 | |
| 71 | // The total size of the memory allocated for the stack. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 72 | size_t stack_capacity() { return thread_local_.memory_size_; } |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 73 | |
| 74 | // If the stack pointer gets below the limit, we should react and |
| 75 | // either grow the stack or report an out-of-stack exception. |
| 76 | // There is only a limited number of locations below the stack limit, |
| 77 | // so users of the stack should check the stack limit during any |
| 78 | // sequence of pushes longer that this. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 79 | Address* limit_address() { return &(thread_local_.limit_); } |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 80 | |
| 81 | // Ensures that there is a memory area with at least the specified size. |
| 82 | // If passing zero, the default/minimum size buffer is allocated. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 83 | Address EnsureCapacity(size_t size); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 84 | |
| 85 | // Thread local archiving. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 86 | static int ArchiveSpacePerThread() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 87 | return static_cast<int>(sizeof(ThreadLocal)); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 88 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 89 | char* ArchiveStack(char* to); |
| 90 | char* RestoreStack(char* from); |
| 91 | void FreeThreadResources() { thread_local_.Free(); } |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 92 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 93 | private: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 94 | RegExpStack(); |
| 95 | ~RegExpStack(); |
| 96 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 97 | // Artificial limit used when no memory has been allocated. |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 98 | static const uintptr_t kMemoryTop = static_cast<uintptr_t>(-1); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 99 | |
| 100 | // Minimal size of allocated stack area. |
| 101 | static const size_t kMinimumStackSize = 1 * KB; |
| 102 | |
| 103 | // Maximal size of allocated stack area. |
ager@chromium.org | 7ffe94f | 2009-01-27 18:57:42 +0000 | [diff] [blame] | 104 | static const size_t kMaximumStackSize = 64 * MB; |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 105 | |
| 106 | // Structure holding the allocated memory, size and limit. |
| 107 | struct ThreadLocal { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 108 | ThreadLocal() { Clear(); } |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 109 | // If memory_size_ > 0 then memory_ must be non-NULL. |
| 110 | Address memory_; |
| 111 | size_t memory_size_; |
| 112 | Address limit_; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 113 | void Clear() { |
| 114 | memory_ = NULL; |
| 115 | memory_size_ = 0; |
| 116 | limit_ = reinterpret_cast<Address>(kMemoryTop); |
| 117 | } |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 118 | void Free(); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 119 | }; |
| 120 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 121 | // Address of allocated memory. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 122 | Address memory_address() { |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 123 | return reinterpret_cast<Address>(&thread_local_.memory_); |
| 124 | } |
| 125 | |
| 126 | // Address of size of allocated memory. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 127 | Address memory_size_address() { |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 128 | return reinterpret_cast<Address>(&thread_local_.memory_size_); |
| 129 | } |
| 130 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 131 | // Resets the buffer if it has grown beyond the default/minimum size. |
| 132 | // After this, the buffer is either the default size, or it is empty, so |
| 133 | // you have to call EnsureCapacity before using it again. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 134 | void Reset(); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 135 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 136 | ThreadLocal thread_local_; |
| 137 | Isolate* isolate_; |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 138 | |
| 139 | friend class ExternalReference; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 140 | friend class Isolate; |
| 141 | friend class RegExpStackScope; |
| 142 | |
| 143 | DISALLOW_COPY_AND_ASSIGN(RegExpStack); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 144 | }; |
| 145 | |
| 146 | }} // namespace v8::internal |
| 147 | |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 148 | #endif // V8_REGEXP_STACK_H_ |