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