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