sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 1 | // Copyright 2011 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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_INL_H_ |
| 29 | #define V8_ZONE_INL_H_ |
| 30 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 31 | #include "isolate.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 32 | #include "zone.h" |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 33 | #include "v8-counters.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 34 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 35 | namespace v8 { |
| 36 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 37 | |
| 38 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 39 | AssertNoZoneAllocation::AssertNoZoneAllocation() |
| 40 | : prev_(Isolate::Current()->zone_allow_allocation()) { |
| 41 | Isolate::Current()->set_zone_allow_allocation(false); |
| 42 | } |
| 43 | |
| 44 | |
| 45 | AssertNoZoneAllocation::~AssertNoZoneAllocation() { |
| 46 | Isolate::Current()->set_zone_allow_allocation(prev_); |
| 47 | } |
| 48 | |
| 49 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 50 | inline void* Zone::New(int size) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 51 | ASSERT(Isolate::Current()->zone_allow_allocation()); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 52 | ASSERT(ZoneScope::nesting() > 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 53 | // Round up the requested size to fit the alignment. |
| 54 | size = RoundUp(size, kAlignment); |
| 55 | |
| 56 | // Check if the requested size is available without expanding. |
| 57 | Address result = position_; |
| 58 | if ((position_ += size) > limit_) result = NewExpand(size); |
| 59 | |
| 60 | // Check that the result has the proper alignment and return it. |
| 61 | ASSERT(IsAddressAligned(result, kAlignment, 0)); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 62 | allocation_size_ += size; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 63 | return reinterpret_cast<void*>(result); |
| 64 | } |
| 65 | |
| 66 | |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 67 | template <typename T> |
| 68 | T* Zone::NewArray(int length) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 69 | return static_cast<T*>(New(length * sizeof(T))); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 73 | bool Zone::excess_allocation() { |
| 74 | return segment_bytes_allocated_ > zone_excess_limit_; |
| 75 | } |
| 76 | |
| 77 | |
| 78 | void Zone::adjust_segment_bytes_allocated(int delta) { |
| 79 | segment_bytes_allocated_ += delta; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 80 | isolate_->counters()->zone_segment_bytes()->Set(segment_bytes_allocated_); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 84 | template <typename Config> |
| 85 | ZoneSplayTree<Config>::~ZoneSplayTree() { |
| 86 | // Reset the root to avoid unneeded iteration over all tree nodes |
| 87 | // in the destructor. For a zone-allocated tree, nodes will be |
| 88 | // freed by the Zone. |
| 89 | SplayTree<Config, ZoneListAllocationPolicy>::ResetRoot(); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 93 | // TODO(isolates): for performance reasons, this should be replaced with a new |
| 94 | // operator that takes the zone in which the object should be |
| 95 | // allocated. |
| 96 | void* ZoneObject::operator new(size_t size) { |
| 97 | return ZONE->New(static_cast<int>(size)); |
| 98 | } |
| 99 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 100 | void* ZoneObject::operator new(size_t size, Zone* zone) { |
| 101 | return zone->New(static_cast<int>(size)); |
| 102 | } |
| 103 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 104 | |
| 105 | inline void* ZoneListAllocationPolicy::New(int size) { |
| 106 | return ZONE->New(size); |
| 107 | } |
| 108 | |
| 109 | |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 110 | template <typename T> |
| 111 | void* ZoneList<T>::operator new(size_t size) { |
| 112 | return ZONE->New(static_cast<int>(size)); |
| 113 | } |
| 114 | |
| 115 | |
| 116 | template <typename T> |
| 117 | void* ZoneList<T>::operator new(size_t size, Zone* zone) { |
| 118 | return zone->New(static_cast<int>(size)); |
| 119 | } |
| 120 | |
| 121 | |
| 122 | ZoneScope::ZoneScope(Isolate* isolate, ZoneScopeMode mode) |
| 123 | : isolate_(isolate), mode_(mode) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 124 | isolate_->zone()->scope_nesting_++; |
| 125 | } |
| 126 | |
| 127 | |
| 128 | bool ZoneScope::ShouldDeleteOnExit() { |
| 129 | return isolate_->zone()->scope_nesting_ == 1 && mode_ == DELETE_ON_EXIT; |
| 130 | } |
| 131 | |
| 132 | |
| 133 | int ZoneScope::nesting() { |
| 134 | return Isolate::Current()->zone()->scope_nesting_; |
| 135 | } |
| 136 | |
| 137 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 138 | } } // namespace v8::internal |
| 139 | |
| 140 | #endif // V8_ZONE_INL_H_ |