Home | History | Annotate | Download | only in src
      1 // Copyright 2006-2008 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 #ifndef V8_ZONE_H_
     29 #define V8_ZONE_H_
     30 
     31 namespace v8 {
     32 namespace internal {
     33 
     34 
     35 // Zone scopes are in one of two modes.  Either they delete the zone
     36 // on exit or they do not.
     37 enum ZoneScopeMode {
     38   DELETE_ON_EXIT,
     39   DONT_DELETE_ON_EXIT
     40 };
     41 
     42 
     43 // The Zone supports very fast allocation of small chunks of
     44 // memory. The chunks cannot be deallocated individually, but instead
     45 // the Zone supports deallocating all chunks in one fast
     46 // operation. The Zone is used to hold temporary data structures like
     47 // the abstract syntax tree, which is deallocated after compilation.
     48 
     49 // Note: There is no need to initialize the Zone; the first time an
     50 // allocation is attempted, a segment of memory will be requested
     51 // through a call to malloc().
     52 
     53 // Note: The implementation is inherently not thread safe. Do not use
     54 // from multi-threaded code.
     55 
     56 class Zone {
     57  public:
     58   // Allocate 'size' bytes of memory in the Zone; expands the Zone by
     59   // allocating new segments of memory on demand using malloc().
     60   static inline void* New(int size);
     61 
     62   template <typename T>
     63   static inline T* NewArray(int length);
     64 
     65   // Delete all objects and free all memory allocated in the Zone.
     66   static void DeleteAll();
     67 
     68   // Returns true if more memory has been allocated in zones than
     69   // the limit allows.
     70   static inline bool excess_allocation();
     71 
     72   static inline void adjust_segment_bytes_allocated(int delta);
     73 
     74  private:
     75 
     76   // All pointers returned from New() have this alignment.
     77   static const int kAlignment = kPointerSize;
     78 
     79   // Never allocate segments smaller than this size in bytes.
     80   static const int kMinimumSegmentSize = 8 * KB;
     81 
     82   // Never allocate segments larger than this size in bytes.
     83   static const int kMaximumSegmentSize = 1 * MB;
     84 
     85   // Never keep segments larger than this size in bytes around.
     86   static const int kMaximumKeptSegmentSize = 64 * KB;
     87 
     88   // Report zone excess when allocation exceeds this limit.
     89   static int zone_excess_limit_;
     90 
     91   // The number of bytes allocated in segments.  Note that this number
     92   // includes memory allocated from the OS but not yet allocated from
     93   // the zone.
     94   static int segment_bytes_allocated_;
     95 
     96   // The Zone is intentionally a singleton; you should not try to
     97   // allocate instances of the class.
     98   Zone() { UNREACHABLE(); }
     99 
    100 
    101   // Expand the Zone to hold at least 'size' more bytes and allocate
    102   // the bytes. Returns the address of the newly allocated chunk of
    103   // memory in the Zone. Should only be called if there isn't enough
    104   // room in the Zone already.
    105   static Address NewExpand(int size);
    106 
    107 
    108   // The free region in the current (front) segment is represented as
    109   // the half-open interval [position, limit). The 'position' variable
    110   // is guaranteed to be aligned as dictated by kAlignment.
    111   static Address position_;
    112   static Address limit_;
    113 };
    114 
    115 
    116 // ZoneObject is an abstraction that helps define classes of objects
    117 // allocated in the Zone. Use it as a base class; see ast.h.
    118 class ZoneObject {
    119  public:
    120   // Allocate a new ZoneObject of 'size' bytes in the Zone.
    121   void* operator new(size_t size) { return Zone::New(static_cast<int>(size)); }
    122 
    123   // Ideally, the delete operator should be private instead of
    124   // public, but unfortunately the compiler sometimes synthesizes
    125   // (unused) destructors for classes derived from ZoneObject, which
    126   // require the operator to be visible. MSVC requires the delete
    127   // operator to be public.
    128 
    129   // ZoneObjects should never be deleted individually; use
    130   // Zone::DeleteAll() to delete all zone objects in one go.
    131   void operator delete(void*, size_t) { UNREACHABLE(); }
    132 };
    133 
    134 
    135 class AssertNoZoneAllocation {
    136  public:
    137   AssertNoZoneAllocation() : prev_(allow_allocation_) {
    138     allow_allocation_ = false;
    139   }
    140   ~AssertNoZoneAllocation() { allow_allocation_ = prev_; }
    141   static bool allow_allocation() { return allow_allocation_; }
    142  private:
    143   bool prev_;
    144   static bool allow_allocation_;
    145 };
    146 
    147 
    148 // The ZoneListAllocationPolicy is used to specialize the GenericList
    149 // implementation to allocate ZoneLists and their elements in the
    150 // Zone.
    151 class ZoneListAllocationPolicy {
    152  public:
    153   // Allocate 'size' bytes of memory in the zone.
    154   static void* New(int size) {  return Zone::New(size); }
    155 
    156   // De-allocation attempts are silently ignored.
    157   static void Delete(void* p) { }
    158 };
    159 
    160 
    161 // ZoneLists are growable lists with constant-time access to the
    162 // elements. The list itself and all its elements are allocated in the
    163 // Zone. ZoneLists cannot be deleted individually; you can delete all
    164 // objects in the Zone by calling Zone::DeleteAll().
    165 template<typename T>
    166 class ZoneList: public List<T, ZoneListAllocationPolicy> {
    167  public:
    168   // Construct a new ZoneList with the given capacity; the length is
    169   // always zero. The capacity must be non-negative.
    170   explicit ZoneList(int capacity)
    171       : List<T, ZoneListAllocationPolicy>(capacity) { }
    172 };
    173 
    174 
    175 // ZoneScopes keep track of the current parsing and compilation
    176 // nesting and cleans up generated ASTs in the Zone when exiting the
    177 // outer-most scope.
    178 class ZoneScope BASE_EMBEDDED {
    179  public:
    180   explicit ZoneScope(ZoneScopeMode mode) : mode_(mode) {
    181     nesting_++;
    182   }
    183 
    184   virtual ~ZoneScope() {
    185     if (ShouldDeleteOnExit()) Zone::DeleteAll();
    186     --nesting_;
    187   }
    188 
    189   bool ShouldDeleteOnExit() {
    190     return nesting_ == 1 && mode_ == DELETE_ON_EXIT;
    191   }
    192 
    193   // For ZoneScopes that do not delete on exit by default, call this
    194   // method to request deletion on exit.
    195   void DeleteOnExit() {
    196     mode_ = DELETE_ON_EXIT;
    197   }
    198 
    199   static int nesting() { return nesting_; }
    200 
    201  private:
    202   ZoneScopeMode mode_;
    203   static int nesting_;
    204 };
    205 
    206 
    207 // A zone splay tree.  The config type parameter encapsulates the
    208 // different configurations of a concrete splay tree:
    209 //
    210 //   typedef Key: the key type
    211 //   typedef Value: the value type
    212 //   static const kNoKey: the dummy key used when no key is set
    213 //   static const kNoValue: the dummy value used to initialize nodes
    214 //   int (Compare)(Key& a, Key& b) -> {-1, 0, 1}: comparison function
    215 //
    216 template <typename Config>
    217 class ZoneSplayTree : public ZoneObject {
    218  public:
    219   typedef typename Config::Key Key;
    220   typedef typename Config::Value Value;
    221 
    222   class Locator;
    223 
    224   ZoneSplayTree() : root_(NULL) { }
    225 
    226   // Inserts the given key in this tree with the given value.  Returns
    227   // true if a node was inserted, otherwise false.  If found the locator
    228   // is enabled and provides access to the mapping for the key.
    229   bool Insert(const Key& key, Locator* locator);
    230 
    231   // Looks up the key in this tree and returns true if it was found,
    232   // otherwise false.  If the node is found the locator is enabled and
    233   // provides access to the mapping for the key.
    234   bool Find(const Key& key, Locator* locator);
    235 
    236   // Finds the mapping with the greatest key less than or equal to the
    237   // given key.
    238   bool FindGreatestLessThan(const Key& key, Locator* locator);
    239 
    240   // Find the mapping with the greatest key in this tree.
    241   bool FindGreatest(Locator* locator);
    242 
    243   // Finds the mapping with the least key greater than or equal to the
    244   // given key.
    245   bool FindLeastGreaterThan(const Key& key, Locator* locator);
    246 
    247   // Find the mapping with the least key in this tree.
    248   bool FindLeast(Locator* locator);
    249 
    250   // Remove the node with the given key from the tree.
    251   bool Remove(const Key& key);
    252 
    253   bool is_empty() { return root_ == NULL; }
    254 
    255   // Perform the splay operation for the given key. Moves the node with
    256   // the given key to the top of the tree.  If no node has the given
    257   // key, the last node on the search path is moved to the top of the
    258   // tree.
    259   void Splay(const Key& key);
    260 
    261   class Node : public ZoneObject {
    262    public:
    263     Node(const Key& key, const Value& value)
    264         : key_(key),
    265           value_(value),
    266           left_(NULL),
    267           right_(NULL) { }
    268     Key key() { return key_; }
    269     Value value() { return value_; }
    270     Node* left() { return left_; }
    271     Node* right() { return right_; }
    272    private:
    273     friend class ZoneSplayTree;
    274     friend class Locator;
    275     Key key_;
    276     Value value_;
    277     Node* left_;
    278     Node* right_;
    279   };
    280 
    281   // A locator provides access to a node in the tree without actually
    282   // exposing the node.
    283   class Locator {
    284    public:
    285     explicit Locator(Node* node) : node_(node) { }
    286     Locator() : node_(NULL) { }
    287     const Key& key() { return node_->key_; }
    288     Value& value() { return node_->value_; }
    289     void set_value(const Value& value) { node_->value_ = value; }
    290     inline void bind(Node* node) { node_ = node; }
    291    private:
    292     Node* node_;
    293   };
    294 
    295   template <class Callback>
    296   void ForEach(Callback* callback);
    297 
    298  private:
    299   Node* root_;
    300 };
    301 
    302 
    303 } }  // namespace v8::internal
    304 
    305 #endif  // V8_ZONE_H_
    306