fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +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_HEAP_INL_H_ |
| 29 | #define V8_HEAP_INL_H_ |
| 30 | |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 31 | #include "heap.h" |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 32 | #include "isolate.h" |
fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 33 | #include "list-inl.h" |
| 34 | #include "objects.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 35 | #include "v8-counters.h" |
| 36 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 37 | namespace v8 { |
| 38 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 39 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 40 | void PromotionQueue::insert(HeapObject* target, int size) { |
| 41 | *(--rear_) = reinterpret_cast<intptr_t>(target); |
| 42 | *(--rear_) = size; |
| 43 | // Assert no overflow into live objects. |
| 44 | ASSERT(reinterpret_cast<Address>(rear_) >= HEAP->new_space()->top()); |
| 45 | } |
| 46 | |
| 47 | |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 48 | int Heap::MaxObjectSizeInPagedSpace() { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 49 | return Page::kMaxHeapObjectSize; |
| 50 | } |
| 51 | |
| 52 | |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 53 | MaybeObject* Heap::AllocateStringFromUtf8(Vector<const char> str, |
| 54 | PretenureFlag pretenure) { |
| 55 | // Check for ASCII first since this is the common case. |
| 56 | if (String::IsAscii(str.start(), str.length())) { |
| 57 | // If the string is ASCII, we do not need to convert the characters |
| 58 | // since UTF8 is backwards compatible with ASCII. |
| 59 | return AllocateStringFromAscii(str, pretenure); |
| 60 | } |
| 61 | // Non-ASCII and we need to decode. |
| 62 | return AllocateStringFromUtf8Slow(str, pretenure); |
| 63 | } |
| 64 | |
| 65 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 66 | MaybeObject* Heap::AllocateSymbol(Vector<const char> str, |
| 67 | int chars, |
| 68 | uint32_t hash_field) { |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 69 | unibrow::Utf8InputBuffer<> buffer(str.start(), |
| 70 | static_cast<unsigned>(str.length())); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 71 | return AllocateInternalSymbol(&buffer, chars, hash_field); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 75 | MaybeObject* Heap::AllocateAsciiSymbol(Vector<const char> str, |
| 76 | uint32_t hash_field) { |
| 77 | if (str.length() > SeqAsciiString::kMaxLength) { |
| 78 | return Failure::OutOfMemoryException(); |
| 79 | } |
| 80 | // Compute map and object size. |
| 81 | Map* map = ascii_symbol_map(); |
| 82 | int size = SeqAsciiString::SizeFor(str.length()); |
| 83 | |
| 84 | // Allocate string. |
| 85 | Object* result; |
| 86 | { MaybeObject* maybe_result = (size > MaxObjectSizeInPagedSpace()) |
| 87 | ? lo_space_->AllocateRaw(size) |
| 88 | : old_data_space_->AllocateRaw(size); |
| 89 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 90 | } |
| 91 | |
| 92 | reinterpret_cast<HeapObject*>(result)->set_map(map); |
| 93 | // Set length and hash fields of the allocated string. |
| 94 | String* answer = String::cast(result); |
| 95 | answer->set_length(str.length()); |
| 96 | answer->set_hash_field(hash_field); |
| 97 | |
| 98 | ASSERT_EQ(size, answer->Size()); |
| 99 | |
| 100 | // Fill in the characters. |
| 101 | memcpy(answer->address() + SeqAsciiString::kHeaderSize, |
| 102 | str.start(), str.length()); |
| 103 | |
| 104 | return answer; |
| 105 | } |
| 106 | |
| 107 | |
| 108 | MaybeObject* Heap::AllocateTwoByteSymbol(Vector<const uc16> str, |
| 109 | uint32_t hash_field) { |
| 110 | if (str.length() > SeqTwoByteString::kMaxLength) { |
| 111 | return Failure::OutOfMemoryException(); |
| 112 | } |
| 113 | // Compute map and object size. |
| 114 | Map* map = symbol_map(); |
| 115 | int size = SeqTwoByteString::SizeFor(str.length()); |
| 116 | |
| 117 | // Allocate string. |
| 118 | Object* result; |
| 119 | { MaybeObject* maybe_result = (size > MaxObjectSizeInPagedSpace()) |
| 120 | ? lo_space_->AllocateRaw(size) |
| 121 | : old_data_space_->AllocateRaw(size); |
| 122 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 123 | } |
| 124 | |
| 125 | reinterpret_cast<HeapObject*>(result)->set_map(map); |
| 126 | // Set length and hash fields of the allocated string. |
| 127 | String* answer = String::cast(result); |
| 128 | answer->set_length(str.length()); |
| 129 | answer->set_hash_field(hash_field); |
| 130 | |
| 131 | ASSERT_EQ(size, answer->Size()); |
| 132 | |
| 133 | // Fill in the characters. |
| 134 | memcpy(answer->address() + SeqTwoByteString::kHeaderSize, |
| 135 | str.start(), str.length() * kUC16Size); |
| 136 | |
| 137 | return answer; |
| 138 | } |
| 139 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 140 | MaybeObject* Heap::CopyFixedArray(FixedArray* src) { |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 141 | return CopyFixedArrayWithMap(src, src->map()); |
| 142 | } |
| 143 | |
| 144 | |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 145 | MaybeObject* Heap::CopyFixedDoubleArray(FixedDoubleArray* src) { |
| 146 | return CopyFixedDoubleArrayWithMap(src, src->map()); |
| 147 | } |
| 148 | |
| 149 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 150 | MaybeObject* Heap::AllocateRaw(int size_in_bytes, |
| 151 | AllocationSpace space, |
| 152 | AllocationSpace retry_space) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 153 | ASSERT(allocation_allowed_ && gc_state_ == NOT_IN_GC); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 154 | ASSERT(space != NEW_SPACE || |
| 155 | retry_space == OLD_POINTER_SPACE || |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 156 | retry_space == OLD_DATA_SPACE || |
| 157 | retry_space == LO_SPACE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 158 | #ifdef DEBUG |
| 159 | if (FLAG_gc_interval >= 0 && |
| 160 | !disallow_allocation_failure_ && |
| 161 | Heap::allocation_timeout_-- <= 0) { |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 162 | return Failure::RetryAfterGC(space); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 163 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 164 | isolate_->counters()->objs_since_last_full()->Increment(); |
| 165 | isolate_->counters()->objs_since_last_young()->Increment(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 166 | #endif |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 167 | MaybeObject* result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 168 | if (NEW_SPACE == space) { |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 169 | result = new_space_.AllocateRaw(size_in_bytes); |
| 170 | if (always_allocate() && result->IsFailure()) { |
| 171 | space = retry_space; |
| 172 | } else { |
| 173 | return result; |
| 174 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 175 | } |
| 176 | |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 177 | if (OLD_POINTER_SPACE == space) { |
| 178 | result = old_pointer_space_->AllocateRaw(size_in_bytes); |
| 179 | } else if (OLD_DATA_SPACE == space) { |
| 180 | result = old_data_space_->AllocateRaw(size_in_bytes); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 181 | } else if (CODE_SPACE == space) { |
| 182 | result = code_space_->AllocateRaw(size_in_bytes); |
| 183 | } else if (LO_SPACE == space) { |
| 184 | result = lo_space_->AllocateRaw(size_in_bytes); |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 185 | } else if (CELL_SPACE == space) { |
| 186 | result = cell_space_->AllocateRaw(size_in_bytes); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 187 | } else { |
| 188 | ASSERT(MAP_SPACE == space); |
| 189 | result = map_space_->AllocateRaw(size_in_bytes); |
| 190 | } |
| 191 | if (result->IsFailure()) old_gen_exhausted_ = true; |
| 192 | return result; |
| 193 | } |
| 194 | |
| 195 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 196 | MaybeObject* Heap::NumberFromInt32(int32_t value) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 197 | if (Smi::IsValid(value)) return Smi::FromInt(value); |
| 198 | // Bypass NumberFromDouble to avoid various redundant checks. |
| 199 | return AllocateHeapNumber(FastI2D(value)); |
| 200 | } |
| 201 | |
| 202 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 203 | MaybeObject* Heap::NumberFromUint32(uint32_t value) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 204 | if ((int32_t)value >= 0 && Smi::IsValid((int32_t)value)) { |
| 205 | return Smi::FromInt((int32_t)value); |
| 206 | } |
| 207 | // Bypass NumberFromDouble to avoid various redundant checks. |
| 208 | return AllocateHeapNumber(FastUI2D(value)); |
| 209 | } |
| 210 | |
| 211 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 212 | void Heap::FinalizeExternalString(String* string) { |
| 213 | ASSERT(string->IsExternalString()); |
| 214 | v8::String::ExternalStringResourceBase** resource_addr = |
| 215 | reinterpret_cast<v8::String::ExternalStringResourceBase**>( |
| 216 | reinterpret_cast<byte*>(string) + |
| 217 | ExternalString::kResourceOffset - |
| 218 | kHeapObjectTag); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 219 | |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 220 | // Dispose of the C++ object if it has not already been disposed. |
| 221 | if (*resource_addr != NULL) { |
| 222 | (*resource_addr)->Dispose(); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 223 | } |
| 224 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 225 | // Clear the resource pointer in the string. |
| 226 | *resource_addr = NULL; |
| 227 | } |
| 228 | |
| 229 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 230 | MaybeObject* Heap::AllocateRawMap() { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 231 | #ifdef DEBUG |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 232 | isolate_->counters()->objs_since_last_full()->Increment(); |
| 233 | isolate_->counters()->objs_since_last_young()->Increment(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 234 | #endif |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 235 | MaybeObject* result = map_space_->AllocateRaw(Map::kSize); |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 236 | if (result->IsFailure()) old_gen_exhausted_ = true; |
sgjesse@chromium.org | 846fb74 | 2009-12-18 08:56:33 +0000 | [diff] [blame] | 237 | #ifdef DEBUG |
| 238 | if (!result->IsFailure()) { |
| 239 | // Maps have their own alignment. |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 240 | CHECK((reinterpret_cast<intptr_t>(result) & kMapAlignmentMask) == |
| 241 | static_cast<intptr_t>(kHeapObjectTag)); |
sgjesse@chromium.org | 846fb74 | 2009-12-18 08:56:33 +0000 | [diff] [blame] | 242 | } |
| 243 | #endif |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 244 | return result; |
| 245 | } |
| 246 | |
| 247 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 248 | MaybeObject* Heap::AllocateRawCell() { |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 249 | #ifdef DEBUG |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 250 | isolate_->counters()->objs_since_last_full()->Increment(); |
| 251 | isolate_->counters()->objs_since_last_young()->Increment(); |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 252 | #endif |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 253 | MaybeObject* result = cell_space_->AllocateRaw(JSGlobalPropertyCell::kSize); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 254 | if (result->IsFailure()) old_gen_exhausted_ = true; |
| 255 | return result; |
| 256 | } |
| 257 | |
| 258 | |
| 259 | bool Heap::InNewSpace(Object* object) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 260 | bool result = new_space_.Contains(object); |
| 261 | ASSERT(!result || // Either not in new space |
| 262 | gc_state_ != NOT_IN_GC || // ... or in the middle of GC |
| 263 | InToSpace(object)); // ... or in to-space (where we allocate). |
| 264 | return result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | |
| 268 | bool Heap::InFromSpace(Object* object) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 269 | return new_space_.FromSpaceContains(object); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | |
| 273 | bool Heap::InToSpace(Object* object) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 274 | return new_space_.ToSpaceContains(object); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | |
| 278 | bool Heap::ShouldBePromoted(Address old_address, int object_size) { |
| 279 | // An object should be promoted if: |
| 280 | // - the object has survived a scavenge operation or |
| 281 | // - to space is already 25% full. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 282 | return old_address < new_space_.age_mark() |
| 283 | || (new_space_.Size() + object_size) >= (new_space_.Capacity() >> 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | |
| 287 | void Heap::RecordWrite(Address address, int offset) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 288 | if (new_space_.Contains(address)) return; |
| 289 | ASSERT(!new_space_.FromSpaceContains(address)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 290 | SLOW_ASSERT(Contains(address + offset)); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 291 | Page::FromAddress(address)->MarkRegionDirty(address + offset); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 295 | void Heap::RecordWrites(Address address, int start, int len) { |
| 296 | if (new_space_.Contains(address)) return; |
| 297 | ASSERT(!new_space_.FromSpaceContains(address)); |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 298 | Page* page = Page::FromAddress(address); |
| 299 | page->SetRegionMarks(page->GetRegionMarks() | |
| 300 | page->GetRegionMaskForSpan(address + start, len * kPointerSize)); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 304 | OldSpace* Heap::TargetSpace(HeapObject* object) { |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 305 | InstanceType type = object->map()->instance_type(); |
| 306 | AllocationSpace space = TargetSpaceId(type); |
| 307 | return (space == OLD_POINTER_SPACE) |
| 308 | ? old_pointer_space_ |
| 309 | : old_data_space_; |
| 310 | } |
| 311 | |
| 312 | |
| 313 | AllocationSpace Heap::TargetSpaceId(InstanceType type) { |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 314 | // Heap numbers and sequential strings are promoted to old data space, all |
| 315 | // 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] | 316 | // object->IsHeapNumber() and object->IsSeqString() because we already |
| 317 | // know that object has the heap object tag. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 318 | |
| 319 | // These objects are never allocated in new space. |
| 320 | ASSERT(type != MAP_TYPE); |
| 321 | ASSERT(type != CODE_TYPE); |
| 322 | ASSERT(type != ODDBALL_TYPE); |
| 323 | ASSERT(type != JS_GLOBAL_PROPERTY_CELL_TYPE); |
| 324 | |
| 325 | if (type < FIRST_NONSTRING_TYPE) { |
ricow@chromium.org | 4668a2c | 2011-08-29 10:41:00 +0000 | [diff] [blame] | 326 | // There are four string representations: sequential strings, external |
| 327 | // strings, cons strings, and sliced strings. |
| 328 | // Only the latter two contain non-map-word pointers to heap objects. |
| 329 | return ((type & kIsIndirectStringMask) == kIsIndirectStringTag) |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 330 | ? OLD_POINTER_SPACE |
| 331 | : OLD_DATA_SPACE; |
| 332 | } else { |
| 333 | return (type <= LAST_DATA_TYPE) ? OLD_DATA_SPACE : OLD_POINTER_SPACE; |
| 334 | } |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 338 | void Heap::CopyBlock(Address dst, Address src, int byte_size) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 339 | ASSERT(IsAligned(byte_size, kPointerSize)); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 340 | CopyWords(reinterpret_cast<Object**>(dst), |
| 341 | reinterpret_cast<Object**>(src), |
| 342 | byte_size / kPointerSize); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 343 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 344 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 345 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 346 | void Heap::CopyBlockToOldSpaceAndUpdateRegionMarks(Address dst, |
| 347 | Address src, |
| 348 | int byte_size) { |
| 349 | ASSERT(IsAligned(byte_size, kPointerSize)); |
| 350 | |
| 351 | Page* page = Page::FromAddress(dst); |
| 352 | uint32_t marks = page->GetRegionMarks(); |
| 353 | |
| 354 | for (int remaining = byte_size / kPointerSize; |
| 355 | remaining > 0; |
| 356 | remaining--) { |
| 357 | Memory::Object_at(dst) = Memory::Object_at(src); |
| 358 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 359 | if (InNewSpace(Memory::Object_at(dst))) { |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 360 | marks |= page->GetRegionMaskForAddress(dst); |
| 361 | } |
| 362 | |
| 363 | dst += kPointerSize; |
| 364 | src += kPointerSize; |
| 365 | } |
| 366 | |
| 367 | page->SetRegionMarks(marks); |
| 368 | } |
| 369 | |
| 370 | |
| 371 | void Heap::MoveBlock(Address dst, Address src, int byte_size) { |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 372 | ASSERT(IsAligned(byte_size, kPointerSize)); |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 373 | |
| 374 | int size_in_words = byte_size / kPointerSize; |
| 375 | |
ricow@chromium.org | 9fa0967 | 2011-07-25 11:05:35 +0000 | [diff] [blame] | 376 | if ((dst < src) || (dst >= (src + byte_size))) { |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 377 | Object** src_slot = reinterpret_cast<Object**>(src); |
| 378 | Object** dst_slot = reinterpret_cast<Object**>(dst); |
| 379 | Object** end_slot = src_slot + size_in_words; |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 380 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 381 | while (src_slot != end_slot) { |
| 382 | *dst_slot++ = *src_slot++; |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 383 | } |
| 384 | } else { |
| 385 | memmove(dst, src, byte_size); |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 390 | void Heap::MoveBlockToOldSpaceAndUpdateRegionMarks(Address dst, |
| 391 | Address src, |
| 392 | int byte_size) { |
| 393 | ASSERT(IsAligned(byte_size, kPointerSize)); |
ricow@chromium.org | 9fa0967 | 2011-07-25 11:05:35 +0000 | [diff] [blame] | 394 | ASSERT((dst < src) || (dst >= (src + byte_size))); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 395 | |
| 396 | CopyBlockToOldSpaceAndUpdateRegionMarks(dst, src, byte_size); |
| 397 | } |
| 398 | |
| 399 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 400 | void Heap::ScavengePointer(HeapObject** p) { |
| 401 | ScavengeObject(p, *p); |
| 402 | } |
| 403 | |
| 404 | |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 405 | void Heap::ScavengeObject(HeapObject** p, HeapObject* object) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 406 | ASSERT(HEAP->InFromSpace(object)); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 407 | |
| 408 | // We use the first word (where the map pointer usually is) of a heap |
| 409 | // object to record the forwarding pointer. A forwarding pointer can |
| 410 | // point to an old space, the code space, or the to space of the new |
| 411 | // generation. |
| 412 | MapWord first_word = object->map_word(); |
| 413 | |
| 414 | // If the first word is a forwarding address, the object has already been |
| 415 | // copied. |
| 416 | if (first_word.IsForwardingAddress()) { |
| 417 | *p = first_word.ToForwardingAddress(); |
| 418 | return; |
| 419 | } |
| 420 | |
| 421 | // Call the slow part of scavenge object. |
| 422 | return ScavengeObjectSlow(p, object); |
| 423 | } |
| 424 | |
| 425 | |
whesse@chromium.org | f0ac72d | 2010-11-08 12:47:26 +0000 | [diff] [blame] | 426 | bool Heap::CollectGarbage(AllocationSpace space) { |
| 427 | return CollectGarbage(space, SelectGarbageCollector(space)); |
| 428 | } |
| 429 | |
| 430 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 431 | MaybeObject* Heap::PrepareForCompare(String* str) { |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 432 | // Always flatten small strings and force flattening of long strings |
| 433 | // after we have accumulated a certain amount we failed to flatten. |
| 434 | static const int kMaxAlwaysFlattenLength = 32; |
| 435 | static const int kFlattenLongThreshold = 16*KB; |
| 436 | |
| 437 | const int length = str->length(); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 438 | MaybeObject* obj = str->TryFlatten(); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 439 | if (length <= kMaxAlwaysFlattenLength || |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 440 | unflattened_strings_length_ >= kFlattenLongThreshold) { |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 441 | return obj; |
| 442 | } |
| 443 | if (obj->IsFailure()) { |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 444 | unflattened_strings_length_ += length; |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 445 | } |
| 446 | return str; |
| 447 | } |
| 448 | |
| 449 | |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 450 | int Heap::AdjustAmountOfExternalAllocatedMemory(int change_in_bytes) { |
| 451 | ASSERT(HasBeenSetup()); |
| 452 | int amount = amount_of_external_allocated_memory_ + change_in_bytes; |
| 453 | if (change_in_bytes >= 0) { |
| 454 | // Avoid overflow. |
| 455 | if (amount > amount_of_external_allocated_memory_) { |
| 456 | amount_of_external_allocated_memory_ = amount; |
| 457 | } |
| 458 | int amount_since_last_global_gc = |
| 459 | amount_of_external_allocated_memory_ - |
| 460 | amount_of_external_allocated_memory_at_last_global_gc_; |
| 461 | if (amount_since_last_global_gc > external_allocation_limit_) { |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 462 | CollectAllGarbage(false); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 463 | } |
| 464 | } else { |
| 465 | // Avoid underflow. |
| 466 | if (amount >= 0) { |
| 467 | amount_of_external_allocated_memory_ = amount; |
| 468 | } |
| 469 | } |
| 470 | ASSERT(amount_of_external_allocated_memory_ >= 0); |
| 471 | return amount_of_external_allocated_memory_; |
| 472 | } |
| 473 | |
| 474 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 475 | void Heap::SetLastScriptId(Object* last_script_id) { |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 476 | roots_[kLastScriptIdRootIndex] = last_script_id; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 477 | } |
| 478 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 479 | Isolate* Heap::isolate() { |
| 480 | return reinterpret_cast<Isolate*>(reinterpret_cast<intptr_t>(this) - |
| 481 | reinterpret_cast<size_t>(reinterpret_cast<Isolate*>(4)->heap()) + 4); |
| 482 | } |
| 483 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 484 | |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 485 | #ifdef DEBUG |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 486 | #define GC_GREEDY_CHECK() \ |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 487 | if (FLAG_gc_greedy) HEAP->GarbageCollectionGreedyCheck() |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 488 | #else |
| 489 | #define GC_GREEDY_CHECK() { } |
| 490 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 491 | |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 492 | |
| 493 | // Calls the FUNCTION_CALL function and retries it up to three times |
| 494 | // to guarantee that any allocations performed during the call will |
| 495 | // succeed if there's enough memory. |
| 496 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 497 | // Warning: Do not use the identifiers __object__, __maybe_object__ or |
| 498 | // __scope__ in a call to this macro. |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 499 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 500 | #define CALL_AND_RETRY(ISOLATE, FUNCTION_CALL, RETURN_VALUE, RETURN_EMPTY)\ |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 501 | do { \ |
| 502 | GC_GREEDY_CHECK(); \ |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 503 | MaybeObject* __maybe_object__ = FUNCTION_CALL; \ |
| 504 | Object* __object__ = NULL; \ |
| 505 | if (__maybe_object__->ToObject(&__object__)) RETURN_VALUE; \ |
| 506 | if (__maybe_object__->IsOutOfMemory()) { \ |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 507 | v8::internal::V8::FatalProcessOutOfMemory("CALL_AND_RETRY_0", true);\ |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 508 | } \ |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 509 | if (!__maybe_object__->IsRetryAfterGC()) RETURN_EMPTY; \ |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 510 | ISOLATE->heap()->CollectGarbage(Failure::cast(__maybe_object__)-> \ |
| 511 | allocation_space()); \ |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 512 | __maybe_object__ = FUNCTION_CALL; \ |
| 513 | if (__maybe_object__->ToObject(&__object__)) RETURN_VALUE; \ |
| 514 | if (__maybe_object__->IsOutOfMemory()) { \ |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 515 | v8::internal::V8::FatalProcessOutOfMemory("CALL_AND_RETRY_1", true);\ |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 516 | } \ |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 517 | if (!__maybe_object__->IsRetryAfterGC()) RETURN_EMPTY; \ |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 518 | ISOLATE->counters()->gc_last_resort_from_handles()->Increment(); \ |
| 519 | ISOLATE->heap()->CollectAllAvailableGarbage(); \ |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 520 | { \ |
| 521 | AlwaysAllocateScope __scope__; \ |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 522 | __maybe_object__ = FUNCTION_CALL; \ |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 523 | } \ |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 524 | if (__maybe_object__->ToObject(&__object__)) RETURN_VALUE; \ |
| 525 | if (__maybe_object__->IsOutOfMemory() || \ |
| 526 | __maybe_object__->IsRetryAfterGC()) { \ |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 527 | /* TODO(1181417): Fix this. */ \ |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 528 | v8::internal::V8::FatalProcessOutOfMemory("CALL_AND_RETRY_2", true);\ |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 529 | } \ |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 530 | RETURN_EMPTY; \ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 531 | } while (false) |
| 532 | |
| 533 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 534 | #define CALL_HEAP_FUNCTION(ISOLATE, FUNCTION_CALL, TYPE) \ |
| 535 | CALL_AND_RETRY(ISOLATE, \ |
| 536 | FUNCTION_CALL, \ |
| 537 | return Handle<TYPE>(TYPE::cast(__object__), ISOLATE), \ |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 538 | return Handle<TYPE>()) |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 539 | |
| 540 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 541 | #define CALL_HEAP_FUNCTION_VOID(ISOLATE, FUNCTION_CALL) \ |
| 542 | CALL_AND_RETRY(ISOLATE, FUNCTION_CALL, return, return) |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 543 | |
| 544 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 545 | #ifdef DEBUG |
| 546 | |
| 547 | inline bool Heap::allow_allocation(bool new_state) { |
| 548 | bool old = allocation_allowed_; |
| 549 | allocation_allowed_ = new_state; |
| 550 | return old; |
| 551 | } |
| 552 | |
| 553 | #endif |
| 554 | |
| 555 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 556 | void ExternalStringTable::AddString(String* string) { |
| 557 | ASSERT(string->IsExternalString()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 558 | if (heap_->InNewSpace(string)) { |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 559 | new_space_strings_.Add(string); |
| 560 | } else { |
| 561 | old_space_strings_.Add(string); |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | |
| 566 | void ExternalStringTable::Iterate(ObjectVisitor* v) { |
| 567 | if (!new_space_strings_.is_empty()) { |
| 568 | Object** start = &new_space_strings_[0]; |
| 569 | v->VisitPointers(start, start + new_space_strings_.length()); |
| 570 | } |
| 571 | if (!old_space_strings_.is_empty()) { |
| 572 | Object** start = &old_space_strings_[0]; |
| 573 | v->VisitPointers(start, start + old_space_strings_.length()); |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | |
| 578 | // Verify() is inline to avoid ifdef-s around its calls in release |
| 579 | // mode. |
| 580 | void ExternalStringTable::Verify() { |
| 581 | #ifdef DEBUG |
| 582 | for (int i = 0; i < new_space_strings_.length(); ++i) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 583 | ASSERT(heap_->InNewSpace(new_space_strings_[i])); |
| 584 | ASSERT(new_space_strings_[i] != HEAP->raw_unchecked_null_value()); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 585 | } |
| 586 | for (int i = 0; i < old_space_strings_.length(); ++i) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 587 | ASSERT(!heap_->InNewSpace(old_space_strings_[i])); |
| 588 | ASSERT(old_space_strings_[i] != HEAP->raw_unchecked_null_value()); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 589 | } |
| 590 | #endif |
| 591 | } |
| 592 | |
| 593 | |
| 594 | void ExternalStringTable::AddOldString(String* string) { |
| 595 | ASSERT(string->IsExternalString()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 596 | ASSERT(!heap_->InNewSpace(string)); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 597 | old_space_strings_.Add(string); |
| 598 | } |
| 599 | |
| 600 | |
| 601 | void ExternalStringTable::ShrinkNewStrings(int position) { |
| 602 | new_space_strings_.Rewind(position); |
| 603 | Verify(); |
| 604 | } |
| 605 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 606 | |
| 607 | void Heap::ClearInstanceofCache() { |
| 608 | set_instanceof_cache_function(the_hole_value()); |
| 609 | } |
| 610 | |
| 611 | |
| 612 | Object* Heap::ToBoolean(bool condition) { |
| 613 | return condition ? true_value() : false_value(); |
| 614 | } |
| 615 | |
| 616 | |
| 617 | void Heap::CompletelyClearInstanceofCache() { |
| 618 | set_instanceof_cache_map(the_hole_value()); |
| 619 | set_instanceof_cache_function(the_hole_value()); |
| 620 | } |
| 621 | |
| 622 | |
| 623 | MaybeObject* TranscendentalCache::Get(Type type, double input) { |
| 624 | SubCache* cache = caches_[type]; |
| 625 | if (cache == NULL) { |
| 626 | caches_[type] = cache = new SubCache(type); |
| 627 | } |
| 628 | return cache->Get(input); |
| 629 | } |
| 630 | |
| 631 | |
| 632 | Address TranscendentalCache::cache_array_address() { |
| 633 | return reinterpret_cast<Address>(caches_); |
| 634 | } |
| 635 | |
| 636 | |
| 637 | double TranscendentalCache::SubCache::Calculate(double input) { |
| 638 | switch (type_) { |
| 639 | case ACOS: |
| 640 | return acos(input); |
| 641 | case ASIN: |
| 642 | return asin(input); |
| 643 | case ATAN: |
| 644 | return atan(input); |
| 645 | case COS: |
| 646 | return cos(input); |
| 647 | case EXP: |
| 648 | return exp(input); |
| 649 | case LOG: |
| 650 | return log(input); |
| 651 | case SIN: |
| 652 | return sin(input); |
| 653 | case TAN: |
| 654 | return tan(input); |
| 655 | default: |
| 656 | return 0.0; // Never happens. |
| 657 | } |
| 658 | } |
| 659 | |
| 660 | |
| 661 | MaybeObject* TranscendentalCache::SubCache::Get(double input) { |
| 662 | Converter c; |
| 663 | c.dbl = input; |
| 664 | int hash = Hash(c); |
| 665 | Element e = elements_[hash]; |
| 666 | if (e.in[0] == c.integers[0] && |
| 667 | e.in[1] == c.integers[1]) { |
| 668 | ASSERT(e.output != NULL); |
| 669 | isolate_->counters()->transcendental_cache_hit()->Increment(); |
| 670 | return e.output; |
| 671 | } |
| 672 | double answer = Calculate(input); |
| 673 | isolate_->counters()->transcendental_cache_miss()->Increment(); |
| 674 | Object* heap_number; |
| 675 | { MaybeObject* maybe_heap_number = |
| 676 | isolate_->heap()->AllocateHeapNumber(answer); |
| 677 | if (!maybe_heap_number->ToObject(&heap_number)) return maybe_heap_number; |
| 678 | } |
| 679 | elements_[hash].in[0] = c.integers[0]; |
| 680 | elements_[hash].in[1] = c.integers[1]; |
| 681 | elements_[hash].output = heap_number; |
| 682 | return heap_number; |
| 683 | } |
| 684 | |
| 685 | |
| 686 | Heap* _inline_get_heap_() { |
| 687 | return HEAP; |
| 688 | } |
| 689 | |
| 690 | |
| 691 | void MarkCompactCollector::SetMark(HeapObject* obj) { |
| 692 | tracer_->increment_marked_count(); |
| 693 | #ifdef DEBUG |
| 694 | UpdateLiveObjectCount(obj); |
| 695 | #endif |
| 696 | obj->SetMark(); |
| 697 | } |
| 698 | |
| 699 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 700 | } } // namespace v8::internal |
| 701 | |
| 702 | #endif // V8_HEAP_INL_H_ |