ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 1 | // Copyright 2006-2008 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_HEAP_INL_H_ |
| 29 | #define V8_HEAP_INL_H_ |
| 30 | |
| 31 | #include "log.h" |
| 32 | #include "v8-counters.h" |
| 33 | |
| 34 | namespace v8 { namespace internal { |
| 35 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 36 | int Heap::MaxHeapObjectSize() { |
| 37 | return Page::kMaxHeapObjectSize; |
| 38 | } |
| 39 | |
| 40 | |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 41 | Object* Heap::AllocateRaw(int size_in_bytes, |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame^] | 42 | AllocationSpace space, |
| 43 | AllocationSpace retry_space) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 44 | ASSERT(allocation_allowed_ && gc_state_ == NOT_IN_GC); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame^] | 45 | ASSERT(space != NEW_SPACE || |
| 46 | retry_space == OLD_POINTER_SPACE || |
| 47 | retry_space == OLD_DATA_SPACE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 48 | #ifdef DEBUG |
| 49 | if (FLAG_gc_interval >= 0 && |
| 50 | !disallow_allocation_failure_ && |
| 51 | Heap::allocation_timeout_-- <= 0) { |
| 52 | return Failure::RetryAfterGC(size_in_bytes, space); |
| 53 | } |
| 54 | Counters::objs_since_last_full.Increment(); |
| 55 | Counters::objs_since_last_young.Increment(); |
| 56 | #endif |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame^] | 57 | Object* result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 58 | if (NEW_SPACE == space) { |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame^] | 59 | result = new_space_.AllocateRaw(size_in_bytes); |
| 60 | if (always_allocate() && result->IsFailure()) { |
| 61 | space = retry_space; |
| 62 | } else { |
| 63 | return result; |
| 64 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 65 | } |
| 66 | |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 67 | if (OLD_POINTER_SPACE == space) { |
| 68 | result = old_pointer_space_->AllocateRaw(size_in_bytes); |
| 69 | } else if (OLD_DATA_SPACE == space) { |
| 70 | result = old_data_space_->AllocateRaw(size_in_bytes); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 71 | } else if (CODE_SPACE == space) { |
| 72 | result = code_space_->AllocateRaw(size_in_bytes); |
| 73 | } else if (LO_SPACE == space) { |
| 74 | result = lo_space_->AllocateRaw(size_in_bytes); |
| 75 | } else { |
| 76 | ASSERT(MAP_SPACE == space); |
| 77 | result = map_space_->AllocateRaw(size_in_bytes); |
| 78 | } |
| 79 | if (result->IsFailure()) old_gen_exhausted_ = true; |
| 80 | return result; |
| 81 | } |
| 82 | |
| 83 | |
| 84 | Object* Heap::NumberFromInt32(int32_t value) { |
| 85 | if (Smi::IsValid(value)) return Smi::FromInt(value); |
| 86 | // Bypass NumberFromDouble to avoid various redundant checks. |
| 87 | return AllocateHeapNumber(FastI2D(value)); |
| 88 | } |
| 89 | |
| 90 | |
| 91 | Object* Heap::NumberFromUint32(uint32_t value) { |
| 92 | if ((int32_t)value >= 0 && Smi::IsValid((int32_t)value)) { |
| 93 | return Smi::FromInt((int32_t)value); |
| 94 | } |
| 95 | // Bypass NumberFromDouble to avoid various redundant checks. |
| 96 | return AllocateHeapNumber(FastUI2D(value)); |
| 97 | } |
| 98 | |
| 99 | |
| 100 | Object* Heap::AllocateRawMap(int size_in_bytes) { |
| 101 | #ifdef DEBUG |
| 102 | Counters::objs_since_last_full.Increment(); |
| 103 | Counters::objs_since_last_young.Increment(); |
| 104 | #endif |
| 105 | Object* result = map_space_->AllocateRaw(size_in_bytes); |
| 106 | if (result->IsFailure()) old_gen_exhausted_ = true; |
| 107 | return result; |
| 108 | } |
| 109 | |
| 110 | |
| 111 | bool Heap::InNewSpace(Object* object) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 112 | return new_space_.Contains(object); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | |
| 116 | bool Heap::InFromSpace(Object* object) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 117 | return new_space_.FromSpaceContains(object); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | |
| 121 | bool Heap::InToSpace(Object* object) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 122 | return new_space_.ToSpaceContains(object); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | |
| 126 | bool Heap::ShouldBePromoted(Address old_address, int object_size) { |
| 127 | // An object should be promoted if: |
| 128 | // - the object has survived a scavenge operation or |
| 129 | // - to space is already 25% full. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 130 | return old_address < new_space_.age_mark() |
| 131 | || (new_space_.Size() + object_size) >= (new_space_.Capacity() >> 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | |
| 135 | void Heap::RecordWrite(Address address, int offset) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 136 | if (new_space_.Contains(address)) return; |
| 137 | ASSERT(!new_space_.FromSpaceContains(address)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 138 | SLOW_ASSERT(Contains(address + offset)); |
| 139 | Page::SetRSet(address, offset); |
| 140 | } |
| 141 | |
| 142 | |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 143 | OldSpace* Heap::TargetSpace(HeapObject* object) { |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame^] | 144 | InstanceType type = object->map()->instance_type(); |
| 145 | AllocationSpace space = TargetSpaceId(type); |
| 146 | return (space == OLD_POINTER_SPACE) |
| 147 | ? old_pointer_space_ |
| 148 | : old_data_space_; |
| 149 | } |
| 150 | |
| 151 | |
| 152 | AllocationSpace Heap::TargetSpaceId(InstanceType type) { |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 153 | // Heap numbers and sequential strings are promoted to old data space, all |
| 154 | // other object types are promoted to old pointer space. We do not use |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 155 | // object->IsHeapNumber() and object->IsSeqString() because we already |
| 156 | // know that object has the heap object tag. |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 157 | ASSERT((type != CODE_TYPE) && (type != MAP_TYPE)); |
| 158 | bool has_pointers = |
| 159 | type != HEAP_NUMBER_TYPE && |
| 160 | (type >= FIRST_NONSTRING_TYPE || |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame^] | 161 | (type & kStringRepresentationMask) != kSeqStringTag); |
| 162 | return has_pointers ? OLD_POINTER_SPACE : OLD_DATA_SPACE; |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 166 | void Heap::CopyBlock(Object** dst, Object** src, int byte_size) { |
| 167 | ASSERT(IsAligned(byte_size, kPointerSize)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 168 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 169 | // Use block copying memcpy if the segment we're copying is |
| 170 | // enough to justify the extra call/setup overhead. |
| 171 | static const int kBlockCopyLimit = 16 * kPointerSize; |
| 172 | |
| 173 | if (byte_size >= kBlockCopyLimit) { |
| 174 | memcpy(dst, src, byte_size); |
| 175 | } else { |
| 176 | int remaining = byte_size / kPointerSize; |
| 177 | do { |
| 178 | remaining--; |
| 179 | *dst++ = *src++; |
| 180 | } while (remaining > 0); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | |
| 185 | Object* Heap::GetKeyedLookupCache() { |
| 186 | if (keyed_lookup_cache()->IsUndefined()) { |
| 187 | Object* obj = LookupCache::Allocate(4); |
| 188 | if (obj->IsFailure()) return obj; |
| 189 | keyed_lookup_cache_ = obj; |
| 190 | } |
| 191 | return keyed_lookup_cache(); |
| 192 | } |
| 193 | |
| 194 | |
| 195 | void Heap::SetKeyedLookupCache(LookupCache* cache) { |
| 196 | keyed_lookup_cache_ = cache; |
| 197 | } |
| 198 | |
| 199 | |
| 200 | void Heap::ClearKeyedLookupCache() { |
| 201 | keyed_lookup_cache_ = undefined_value(); |
| 202 | } |
| 203 | |
| 204 | |
| 205 | #define GC_GREEDY_CHECK() \ |
| 206 | ASSERT(!FLAG_gc_greedy || v8::internal::Heap::GarbageCollectionGreedyCheck()) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 207 | |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame^] | 208 | |
| 209 | // Calls the FUNCTION_CALL function and retries it up to three times |
| 210 | // to guarantee that any allocations performed during the call will |
| 211 | // succeed if there's enough memory. |
| 212 | |
| 213 | // Warning: Do not use the identifiers __object__ or __scope__ in a |
| 214 | // call to this macro. |
| 215 | |
| 216 | #define CALL_AND_RETRY(FUNCTION_CALL, RETURN_VALUE, RETURN_EMPTY) \ |
| 217 | do { \ |
| 218 | GC_GREEDY_CHECK(); \ |
| 219 | Object* __object__ = FUNCTION_CALL; \ |
| 220 | if (!__object__->IsFailure()) return RETURN_VALUE; \ |
| 221 | if (__object__->IsOutOfMemoryFailure()) { \ |
| 222 | v8::internal::V8::FatalProcessOutOfMemory("CALL_AND_RETRY_0"); \ |
| 223 | } \ |
| 224 | if (!__object__->IsRetryAfterGC()) return RETURN_EMPTY; \ |
| 225 | if (!Heap::CollectGarbage( \ |
| 226 | Failure::cast(__object__)->requested(), \ |
| 227 | Failure::cast(__object__)->allocation_space())) { \ |
| 228 | v8::internal::V8::FatalProcessOutOfMemory("CALL_AND_RETRY_1"); \ |
| 229 | return RETURN_EMPTY; \ |
| 230 | } \ |
| 231 | __object__ = FUNCTION_CALL; \ |
| 232 | if (!__object__->IsFailure()) return RETURN_VALUE; \ |
| 233 | if (__object__->IsOutOfMemoryFailure()) { \ |
| 234 | v8::internal::V8::FatalProcessOutOfMemory("CALL_AND_RETRY_2"); \ |
| 235 | } \ |
| 236 | if (!__object__->IsRetryAfterGC()) return RETURN_EMPTY; \ |
| 237 | Counters::gc_last_resort_from_handles.Increment(); \ |
| 238 | Heap::CollectAllGarbage(); \ |
| 239 | { \ |
| 240 | AlwaysAllocateScope __scope__; \ |
| 241 | __object__ = FUNCTION_CALL; \ |
| 242 | } \ |
| 243 | if (!__object__->IsFailure()) return RETURN_VALUE; \ |
| 244 | if (__object__->IsOutOfMemoryFailure()) { \ |
| 245 | /* TODO(1181417): Fix this. */ \ |
| 246 | v8::internal::V8::FatalProcessOutOfMemory("CALL_AND_RETRY_3"); \ |
| 247 | } \ |
| 248 | ASSERT(!__object__->IsRetryAfterGC()); \ |
| 249 | return RETURN_EMPTY; \ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 250 | } while (false) |
| 251 | |
| 252 | |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame^] | 253 | #define CALL_HEAP_FUNCTION(FUNCTION_CALL, TYPE) \ |
| 254 | CALL_AND_RETRY(FUNCTION_CALL, \ |
| 255 | Handle<TYPE>(TYPE::cast(__object__)), \ |
| 256 | Handle<TYPE>()) |
| 257 | |
| 258 | |
| 259 | #define CALL_HEAP_FUNCTION_VOID(FUNCTION_CALL) \ |
| 260 | CALL_AND_RETRY(FUNCTION_CALL, , ) |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 261 | |
| 262 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 263 | #ifdef DEBUG |
| 264 | |
| 265 | inline bool Heap::allow_allocation(bool new_state) { |
| 266 | bool old = allocation_allowed_; |
| 267 | allocation_allowed_ = new_state; |
| 268 | return old; |
| 269 | } |
| 270 | |
| 271 | #endif |
| 272 | |
| 273 | |
| 274 | } } // namespace v8::internal |
| 275 | |
| 276 | #endif // V8_HEAP_INL_H_ |