2009-01-12 13:05:23 +00:00
|
|
|
// Copyright 2009 the V8 project authors. All rights reserved.
|
2014-04-29 06:42:26 +00:00
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
2009-01-12 13:05:23 +00:00
|
|
|
|
2009-05-04 13:36:43 +00:00
|
|
|
#ifndef V8_REGEXP_STACK_H_
|
|
|
|
#define V8_REGEXP_STACK_H_
|
2009-01-12 13:05:23 +00:00
|
|
|
|
2009-05-25 10:05:56 +00:00
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
2009-01-12 13:05:23 +00:00
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
class RegExpStack;
|
|
|
|
|
2009-01-12 13:05:23 +00:00
|
|
|
// Maintains a per-v8thread stack area that can be used by irregexp
|
|
|
|
// implementation for its backtracking stack.
|
|
|
|
// Since there is only one stack area, the Irregexp implementation is not
|
|
|
|
// re-entrant. I.e., no regular expressions may be executed in the same thread
|
|
|
|
// during a preempted Irregexp execution.
|
2011-03-18 20:35:07 +00:00
|
|
|
class RegExpStackScope {
|
|
|
|
public:
|
|
|
|
// Create and delete an instance to control the life-time of a growing stack.
|
|
|
|
|
|
|
|
// Initializes the stack memory area if necessary.
|
|
|
|
explicit RegExpStackScope(Isolate* isolate);
|
|
|
|
~RegExpStackScope(); // Releases the stack if it has grown.
|
|
|
|
|
|
|
|
RegExpStack* stack() const { return regexp_stack_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
RegExpStack* regexp_stack_;
|
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(RegExpStackScope);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2009-01-12 13:05:23 +00:00
|
|
|
class RegExpStack {
|
|
|
|
public:
|
|
|
|
// Number of allocated locations on the stack below the limit.
|
|
|
|
// No sequence of pushes must be longer that this without doing a stack-limit
|
|
|
|
// check.
|
|
|
|
static const int kStackLimitSlack = 32;
|
|
|
|
|
|
|
|
// Gives the top of the memory used as stack.
|
2011-03-18 20:35:07 +00:00
|
|
|
Address stack_base() {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(thread_local_.memory_size_ != 0);
|
2009-01-12 13:05:23 +00:00
|
|
|
return thread_local_.memory_ + thread_local_.memory_size_;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The total size of the memory allocated for the stack.
|
2011-03-18 20:35:07 +00:00
|
|
|
size_t stack_capacity() { return thread_local_.memory_size_; }
|
2009-01-12 13:05:23 +00:00
|
|
|
|
|
|
|
// If the stack pointer gets below the limit, we should react and
|
|
|
|
// either grow the stack or report an out-of-stack exception.
|
|
|
|
// There is only a limited number of locations below the stack limit,
|
|
|
|
// so users of the stack should check the stack limit during any
|
|
|
|
// sequence of pushes longer that this.
|
2011-03-18 20:35:07 +00:00
|
|
|
Address* limit_address() { return &(thread_local_.limit_); }
|
2009-01-12 13:05:23 +00:00
|
|
|
|
|
|
|
// Ensures that there is a memory area with at least the specified size.
|
|
|
|
// If passing zero, the default/minimum size buffer is allocated.
|
2011-03-18 20:35:07 +00:00
|
|
|
Address EnsureCapacity(size_t size);
|
2009-01-12 13:05:23 +00:00
|
|
|
|
|
|
|
// Thread local archiving.
|
2009-11-11 09:50:06 +00:00
|
|
|
static int ArchiveSpacePerThread() {
|
2011-03-18 20:35:07 +00:00
|
|
|
return static_cast<int>(sizeof(ThreadLocal));
|
2009-11-11 09:50:06 +00:00
|
|
|
}
|
2011-03-18 20:35:07 +00:00
|
|
|
char* ArchiveStack(char* to);
|
|
|
|
char* RestoreStack(char* from);
|
|
|
|
void FreeThreadResources() { thread_local_.Free(); }
|
2011-09-08 19:57:14 +00:00
|
|
|
|
2011-03-18 19:41:05 +00:00
|
|
|
private:
|
2011-03-18 20:35:07 +00:00
|
|
|
RegExpStack();
|
|
|
|
~RegExpStack();
|
|
|
|
|
2009-01-12 13:05:23 +00:00
|
|
|
// Artificial limit used when no memory has been allocated.
|
2009-08-14 12:20:33 +00:00
|
|
|
static const uintptr_t kMemoryTop = static_cast<uintptr_t>(-1);
|
2009-01-12 13:05:23 +00:00
|
|
|
|
|
|
|
// Minimal size of allocated stack area.
|
|
|
|
static const size_t kMinimumStackSize = 1 * KB;
|
|
|
|
|
|
|
|
// Maximal size of allocated stack area.
|
2009-01-27 13:43:42 +00:00
|
|
|
static const size_t kMaximumStackSize = 64 * MB;
|
2009-01-12 13:05:23 +00:00
|
|
|
|
|
|
|
// Structure holding the allocated memory, size and limit.
|
|
|
|
struct ThreadLocal {
|
2011-03-18 20:35:07 +00:00
|
|
|
ThreadLocal() { Clear(); }
|
2009-01-12 13:05:23 +00:00
|
|
|
// If memory_size_ > 0 then memory_ must be non-NULL.
|
|
|
|
Address memory_;
|
|
|
|
size_t memory_size_;
|
|
|
|
Address limit_;
|
2011-03-18 20:35:07 +00:00
|
|
|
void Clear() {
|
|
|
|
memory_ = NULL;
|
|
|
|
memory_size_ = 0;
|
|
|
|
limit_ = reinterpret_cast<Address>(kMemoryTop);
|
|
|
|
}
|
2009-09-28 12:25:21 +00:00
|
|
|
void Free();
|
2009-01-12 13:05:23 +00:00
|
|
|
};
|
|
|
|
|
2010-01-06 11:09:30 +00:00
|
|
|
// Address of allocated memory.
|
2011-03-18 20:35:07 +00:00
|
|
|
Address memory_address() {
|
2010-01-06 11:09:30 +00:00
|
|
|
return reinterpret_cast<Address>(&thread_local_.memory_);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Address of size of allocated memory.
|
2011-03-18 20:35:07 +00:00
|
|
|
Address memory_size_address() {
|
2010-01-06 11:09:30 +00:00
|
|
|
return reinterpret_cast<Address>(&thread_local_.memory_size_);
|
|
|
|
}
|
|
|
|
|
2009-01-12 13:05:23 +00:00
|
|
|
// Resets the buffer if it has grown beyond the default/minimum size.
|
|
|
|
// After this, the buffer is either the default size, or it is empty, so
|
|
|
|
// you have to call EnsureCapacity before using it again.
|
2011-03-18 20:35:07 +00:00
|
|
|
void Reset();
|
2009-01-12 13:05:23 +00:00
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
ThreadLocal thread_local_;
|
|
|
|
Isolate* isolate_;
|
2010-01-06 11:09:30 +00:00
|
|
|
|
|
|
|
friend class ExternalReference;
|
2011-03-18 20:35:07 +00:00
|
|
|
friend class Isolate;
|
|
|
|
friend class RegExpStackScope;
|
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(RegExpStack);
|
2009-01-12 13:05:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}} // namespace v8::internal
|
|
|
|
|
2009-05-04 19:31:36 +00:00
|
|
|
#endif // V8_REGEXP_STACK_H_
|