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. Zone()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. new(size_t size)121 void* operator new(size_t size) { return Zone::New(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. delete(void *,size_t)131 void operator delete(void*, size_t) { UNREACHABLE(); } 132 }; 133 134 135 class AssertNoZoneAllocation { 136 public: AssertNoZoneAllocation()137 AssertNoZoneAllocation() : prev_(allow_allocation_) { 138 allow_allocation_ = false; 139 } ~AssertNoZoneAllocation()140 ~AssertNoZoneAllocation() { allow_allocation_ = prev_; } allow_allocation()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. New(int size)154 static void* New(int size) { return Zone::New(size); } 155 156 // De-allocation attempts are silently ignored. Delete(void * p)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. ZoneList(int capacity)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: ZoneScope(ZoneScopeMode mode)180 explicit ZoneScope(ZoneScopeMode mode) : mode_(mode) { 181 nesting_++; 182 } 183 ~ZoneScope()184 virtual ~ZoneScope() { 185 if (ShouldDeleteOnExit()) Zone::DeleteAll(); 186 --nesting_; 187 } 188 ShouldDeleteOnExit()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. DeleteOnExit()195 void DeleteOnExit() { 196 mode_ = DELETE_ON_EXIT; 197 } 198 nesting()199 static int nesting() { return nesting_; } 200 201 private: 202 ZoneScopeMode mode_; 203 static int nesting_; 204 }; 205 206 207 template <typename Node, class Callback> 208 static void DoForEach(Node* node, Callback* callback); 209 210 211 // A zone splay tree. The config type parameter encapsulates the 212 // different configurations of a concrete splay tree: 213 // 214 // typedef Key: the key type 215 // typedef Value: the value type 216 // static const kNoKey: the dummy key used when no key is set 217 // static const kNoValue: the dummy value used to initialize nodes 218 // int (Compare)(Key& a, Key& b) -> {-1, 0, 1}: comparison function 219 // 220 template <typename Config> 221 class ZoneSplayTree : public ZoneObject { 222 public: 223 typedef typename Config::Key Key; 224 typedef typename Config::Value Value; 225 226 class Locator; 227 ZoneSplayTree()228 ZoneSplayTree() : root_(NULL) { } 229 230 // Inserts the given key in this tree with the given value. Returns 231 // true if a node was inserted, otherwise false. If found the locator 232 // is enabled and provides access to the mapping for the key. 233 bool Insert(const Key& key, Locator* locator); 234 235 // Looks up the key in this tree and returns true if it was found, 236 // otherwise false. If the node is found the locator is enabled and 237 // provides access to the mapping for the key. 238 bool Find(const Key& key, Locator* locator); 239 240 // Finds the mapping with the greatest key less than or equal to the 241 // given key. 242 bool FindGreatestLessThan(const Key& key, Locator* locator); 243 244 // Find the mapping with the greatest key in this tree. 245 bool FindGreatest(Locator* locator); 246 247 // Finds the mapping with the least key greater than or equal to the 248 // given key. 249 bool FindLeastGreaterThan(const Key& key, Locator* locator); 250 251 // Find the mapping with the least key in this tree. 252 bool FindLeast(Locator* locator); 253 254 // Remove the node with the given key from the tree. 255 bool Remove(const Key& key); 256 is_empty()257 bool is_empty() { return root_ == NULL; } 258 259 // Perform the splay operation for the given key. Moves the node with 260 // the given key to the top of the tree. If no node has the given 261 // key, the last node on the search path is moved to the top of the 262 // tree. 263 void Splay(const Key& key); 264 265 class Node : public ZoneObject { 266 public: Node(const Key & key,const Value & value)267 Node(const Key& key, const Value& value) 268 : key_(key), 269 value_(value), 270 left_(NULL), 271 right_(NULL) { } key()272 Key key() { return key_; } value()273 Value value() { return value_; } left()274 Node* left() { return left_; } right()275 Node* right() { return right_; } 276 private: 277 friend class ZoneSplayTree; 278 friend class Locator; 279 Key key_; 280 Value value_; 281 Node* left_; 282 Node* right_; 283 }; 284 285 // A locator provides access to a node in the tree without actually 286 // exposing the node. 287 class Locator { 288 public: Locator(Node * node)289 explicit Locator(Node* node) : node_(node) { } Locator()290 Locator() : node_(NULL) { } key()291 const Key& key() { return node_->key_; } value()292 Value& value() { return node_->value_; } set_value(const Value & value)293 void set_value(const Value& value) { node_->value_ = value; } bind(Node * node)294 inline void bind(Node* node) { node_ = node; } 295 private: 296 Node* node_; 297 }; 298 299 template <class Callback> ForEach(Callback * c)300 void ForEach(Callback* c) { 301 DoForEach<typename ZoneSplayTree<Config>::Node, Callback>(root_, c); 302 } 303 304 private: 305 Node* root_; 306 }; 307 308 309 } } // namespace v8::internal 310 311 #endif // V8_ZONE_H_ 312