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