00001 // Copyright 2006-2008 the V8 project authors. All rights reserved. 00002 // Redistribution and use in source and binary forms, with or without 00003 // modification, are permitted provided that the following conditions are 00004 // met: 00005 // 00006 // * Redistributions of source code must retain the above copyright 00007 // notice, this list of conditions and the following disclaimer. 00008 // * Redistributions in binary form must reproduce the above 00009 // copyright notice, this list of conditions and the following 00010 // disclaimer in the documentation and/or other materials provided 00011 // with the distribution. 00012 // * Neither the name of Google Inc. nor the names of its 00013 // contributors may be used to endorse or promote products derived 00014 // from this software without specific prior written permission. 00015 // 00016 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00017 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00018 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00019 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 00020 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00021 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00022 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00023 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00024 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00025 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00026 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00027 00028 #ifndef V8_ZONE_H_ 00029 #define V8_ZONE_H_ 00030 00031 namespace v8 { namespace internal { 00032 00033 00034 // Zone scopes are in one of two modes. Either they delete the zone 00035 // on exit or they do not. 00036 enum ZoneScopeMode { 00037 DELETE_ON_EXIT, 00038 DONT_DELETE_ON_EXIT 00039 }; 00040 00041 00042 // The Zone supports very fast allocation of small chunks of 00043 // memory. The chunks cannot be deallocated individually, but instead 00044 // the Zone supports deallocating all chunks in one fast 00045 // operation. The Zone is used to hold temporary data structures like 00046 // the abstract syntax tree, which is deallocated after compilation. 00047 00048 // Note: There is no need to initialize the Zone; the first time an 00049 // allocation is attempted, a segment of memory will be requested 00050 // through a call to malloc(). 00051 00052 // Note: The implementation is inherently not thread safe. Do not use 00053 // from multi-threaded code. 00054 00055 class Zone { 00056 public: 00057 // Allocate 'size' bytes of memory in the Zone; expands the Zone by 00058 // allocating new segments of memory on demand using malloc(). 00059 static inline void* New(int size); 00060 00061 // Delete all objects and free all memory allocated in the Zone. 00062 static void DeleteAll(); 00063 00064 private: 00065 // All pointers returned from New() have this alignment. 00066 static const int kAlignment = kPointerSize; 00067 00068 // Never allocate segments smaller than this size in bytes. 00069 static const int kMinimumSegmentSize = 8 * KB; 00070 00071 // Never keep segments larger than this size in bytes around. 00072 static const int kMaximumKeptSegmentSize = 64 * KB; 00073 00074 00075 // The Zone is intentionally a singleton; you should not try to 00076 // allocate instances of the class. 00077 Zone() { UNREACHABLE(); } 00078 00079 00080 // Expand the Zone to hold at least 'size' more bytes and allocate 00081 // the bytes. Returns the address of the newly allocated chunk of 00082 // memory in the Zone. Should only be called if there isn't enough 00083 // room in the Zone already. 00084 static Address NewExpand(int size); 00085 00086 00087 // The free region in the current (front) segment is represented as 00088 // the half-open interval [position, limit). The 'position' variable 00089 // is guaranteed to be aligned as dictated by kAlignment. 00090 static Address position_; 00091 static Address limit_; 00092 }; 00093 00094 00095 // ZoneObject is an abstraction that helps define classes of objects 00096 // allocated in the Zone. Use it as a base class; see ast.h. 00097 class ZoneObject { 00098 public: 00099 // Allocate a new ZoneObject of 'size' bytes in the Zone. 00100 void* operator new(size_t size) { return Zone::New(size); } 00101 00102 // Ideally, the delete operator should be private instead of 00103 // public, but unfortuately the compiler sometimes synthesizes 00104 // (unused) destructors for classes derived from ZoneObject, which 00105 // require the operator to be visible. MSVC requires the delete 00106 // operator to be public. 00107 00108 // ZoneObjects should never be deleted individually; use 00109 // Zone::DeleteAll() to delete all zone objects in one go. 00110 void operator delete(void*, size_t) { UNREACHABLE(); } 00111 }; 00112 00113 00114 class AssertNoZoneAllocation { 00115 public: 00116 AssertNoZoneAllocation() : prev_(allow_allocation_) { 00117 allow_allocation_ = false; 00118 } 00119 ~AssertNoZoneAllocation() { allow_allocation_ = prev_; } 00120 static bool allow_allocation() { return allow_allocation_; } 00121 private: 00122 bool prev_; 00123 static bool allow_allocation_; 00124 }; 00125 00126 00127 // The ZoneListAllocationPolicy is used to specialize the GenericList 00128 // implementation to allocate ZoneLists and their elements in the 00129 // Zone. 00130 class ZoneListAllocationPolicy { 00131 public: 00132 // Allocate 'size' bytes of memory in the zone. 00133 static void* New(int size) { return Zone::New(size); } 00134 00135 // De-allocation attempts are silently ignored. 00136 static void Delete(void* p) { } 00137 }; 00138 00139 00140 // ZoneLists are growable lists with constant-time access to the 00141 // elements. The list itself and all its elements are allocated in the 00142 // Zone. ZoneLists cannot be deleted individually; you can delete all 00143 // objects in the Zone by calling Zone::DeleteAll(). 00144 template<typename T> 00145 class ZoneList: public List<T, ZoneListAllocationPolicy> { 00146 public: 00147 // Construct a new ZoneList with the given capacity; the length is 00148 // always zero. The capacity must be non-negative. 00149 explicit ZoneList(int capacity) 00150 : List<T, ZoneListAllocationPolicy>(capacity) { } 00151 }; 00152 00153 00154 // ZoneScopes keep track of the current parsing and compilation 00155 // nesting and cleans up generated ASTs in the Zone when exiting the 00156 // outer-most scope. 00157 class ZoneScope BASE_EMBEDDED { 00158 public: 00159 explicit ZoneScope(ZoneScopeMode mode) : mode_(mode) { 00160 nesting_++; 00161 } 00162 00163 ~ZoneScope() { 00164 if (--nesting_ == 0 && mode_ == DELETE_ON_EXIT) Zone::DeleteAll(); 00165 } 00166 00167 // For ZoneScopes that do not delete on exit by default, call this 00168 // method to request deletion on exit. 00169 void DeleteOnExit() { 00170 mode_ = DELETE_ON_EXIT; 00171 } 00172 00173 private: 00174 ZoneScopeMode mode_; 00175 static int nesting_; 00176 }; 00177 00178 00179 } } // namespace v8::internal 00180 00181 #endif // V8_ZONE_H_