regexp-stack.h 3.93 KB
Newer Older
1
// Copyright 2009 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4

5 6 7
#ifndef V8_REGEXP_REGEXP_STACK_H_
#define V8_REGEXP_REGEXP_STACK_H_

8 9
#include "src/base/logging.h"
#include "src/base/macros.h"
10
#include "src/globals.h"
11

12 13
namespace v8 {
namespace internal {
14

15 16
class RegExpStack;

17 18 19 20 21
// 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.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
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);
};


39 40 41 42 43 44 45 46
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.
47
  Address stack_base() {
48
    DCHECK_NE(0, thread_local_.memory_size_);
49 50
    return reinterpret_cast<Address>(thread_local_.memory_) +
           thread_local_.memory_size_;
51 52 53
  }

  // The total size of the memory allocated for the stack.
54
  size_t stack_capacity() { return thread_local_.memory_size_; }
55 56 57 58 59 60

  // 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.
61
  Address* limit_address() { return &(thread_local_.limit_); }
62 63 64

  // Ensures that there is a memory area with at least the specified size.
  // If passing zero, the default/minimum size buffer is allocated.
65
  Address EnsureCapacity(size_t size);
66 67

  // Thread local archiving.
68
  static int ArchiveSpacePerThread() {
69
    return static_cast<int>(sizeof(ThreadLocal));
70
  }
71 72 73
  char* ArchiveStack(char* to);
  char* RestoreStack(char* from);
  void FreeThreadResources() { thread_local_.Free(); }
74

75
 private:
76 77 78
  RegExpStack();
  ~RegExpStack();

79
  // Artificial limit used when no memory has been allocated.
80 81
  static const Address kMemoryTop =
      static_cast<Address>(static_cast<uintptr_t>(-1));
82 83 84 85 86

  // Minimal size of allocated stack area.
  static const size_t kMinimumStackSize = 1 * KB;

  // Maximal size of allocated stack area.
87
  static const size_t kMaximumStackSize = 64 * MB;
88 89 90

  // Structure holding the allocated memory, size and limit.
  struct ThreadLocal {
91
    ThreadLocal() { Clear(); }
92
    // If memory_size_ > 0 then memory_ must be non-nullptr.
93
    byte* memory_;
94 95
    size_t memory_size_;
    Address limit_;
96
    void Clear() {
97
      memory_ = nullptr;
98
      memory_size_ = 0;
99
      limit_ = kMemoryTop;
100
    }
101
    void Free();
102 103
  };

104
  // Address of allocated memory.
105
  Address memory_address() {
106 107 108 109
    return reinterpret_cast<Address>(&thread_local_.memory_);
  }

  // Address of size of allocated memory.
110
  Address memory_size_address() {
111 112 113
    return reinterpret_cast<Address>(&thread_local_.memory_size_);
  }

114 115 116
  // 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.
117
  void Reset();
118

119 120
  ThreadLocal thread_local_;
  Isolate* isolate_;
121 122

  friend class ExternalReference;
123 124 125 126
  friend class Isolate;
  friend class RegExpStackScope;

  DISALLOW_COPY_AND_ASSIGN(RegExpStack);
127 128
};

129 130
}  // namespace internal
}  // namespace v8
131

132
#endif  // V8_REGEXP_REGEXP_STACK_H_