blob: bf9c535d4f7fb0bc38b6f46b7c3ff7e34741058d [file] [log] [blame]
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001// Copyright 2006-2008 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// 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.org71affb52009-05-26 05:44:31 +000034namespace v8 {
35namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000036
ager@chromium.org5aa501c2009-06-23 07:57:28 +000037int Heap::MaxObjectSizeInPagedSpace() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000038 return Page::kMaxHeapObjectSize;
39}
40
41
ager@chromium.orga74f0da2008-12-03 16:05:52 +000042Object* Heap::AllocateSymbol(Vector<const char> str,
43 int chars,
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +000044 uint32_t hash_field) {
ager@chromium.orga74f0da2008-12-03 16:05:52 +000045 unibrow::Utf8InputBuffer<> buffer(str.start(),
46 static_cast<unsigned>(str.length()));
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +000047 return AllocateInternalSymbol(&buffer, chars, hash_field);
ager@chromium.orga74f0da2008-12-03 16:05:52 +000048}
49
50
ager@chromium.org9258b6b2008-09-11 09:11:10 +000051Object* Heap::AllocateRaw(int size_in_bytes,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000052 AllocationSpace space,
53 AllocationSpace retry_space) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000054 ASSERT(allocation_allowed_ && gc_state_ == NOT_IN_GC);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000055 ASSERT(space != NEW_SPACE ||
56 retry_space == OLD_POINTER_SPACE ||
fschneider@chromium.org0c20e672010-01-14 15:28:53 +000057 retry_space == OLD_DATA_SPACE ||
58 retry_space == LO_SPACE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000059#ifdef DEBUG
60 if (FLAG_gc_interval >= 0 &&
61 !disallow_allocation_failure_ &&
62 Heap::allocation_timeout_-- <= 0) {
63 return Failure::RetryAfterGC(size_in_bytes, space);
64 }
65 Counters::objs_since_last_full.Increment();
66 Counters::objs_since_last_young.Increment();
67#endif
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000068 Object* result;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000069 if (NEW_SPACE == space) {
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000070 result = new_space_.AllocateRaw(size_in_bytes);
71 if (always_allocate() && result->IsFailure()) {
72 space = retry_space;
73 } else {
74 return result;
75 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000076 }
77
ager@chromium.org9258b6b2008-09-11 09:11:10 +000078 if (OLD_POINTER_SPACE == space) {
79 result = old_pointer_space_->AllocateRaw(size_in_bytes);
80 } else if (OLD_DATA_SPACE == space) {
81 result = old_data_space_->AllocateRaw(size_in_bytes);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000082 } else if (CODE_SPACE == space) {
83 result = code_space_->AllocateRaw(size_in_bytes);
84 } else if (LO_SPACE == space) {
85 result = lo_space_->AllocateRaw(size_in_bytes);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +000086 } else if (CELL_SPACE == space) {
87 result = cell_space_->AllocateRaw(size_in_bytes);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000088 } else {
89 ASSERT(MAP_SPACE == space);
90 result = map_space_->AllocateRaw(size_in_bytes);
91 }
92 if (result->IsFailure()) old_gen_exhausted_ = true;
93 return result;
94}
95
96
97Object* Heap::NumberFromInt32(int32_t value) {
98 if (Smi::IsValid(value)) return Smi::FromInt(value);
99 // Bypass NumberFromDouble to avoid various redundant checks.
100 return AllocateHeapNumber(FastI2D(value));
101}
102
103
104Object* Heap::NumberFromUint32(uint32_t value) {
105 if ((int32_t)value >= 0 && Smi::IsValid((int32_t)value)) {
106 return Smi::FromInt((int32_t)value);
107 }
108 // Bypass NumberFromDouble to avoid various redundant checks.
109 return AllocateHeapNumber(FastUI2D(value));
110}
111
112
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000113void Heap::FinalizeExternalString(String* string) {
114 ASSERT(string->IsExternalString());
115 v8::String::ExternalStringResourceBase** resource_addr =
116 reinterpret_cast<v8::String::ExternalStringResourceBase**>(
117 reinterpret_cast<byte*>(string) +
118 ExternalString::kResourceOffset -
119 kHeapObjectTag);
120 delete *resource_addr;
121 // Clear the resource pointer in the string.
122 *resource_addr = NULL;
123}
124
125
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000126Object* Heap::AllocateRawMap() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000127#ifdef DEBUG
128 Counters::objs_since_last_full.Increment();
129 Counters::objs_since_last_young.Increment();
130#endif
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000131 Object* result = map_space_->AllocateRaw(Map::kSize);
132 if (result->IsFailure()) old_gen_exhausted_ = true;
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000133#ifdef DEBUG
134 if (!result->IsFailure()) {
135 // Maps have their own alignment.
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000136 CHECK((reinterpret_cast<intptr_t>(result) & kMapAlignmentMask) ==
137 static_cast<intptr_t>(kHeapObjectTag));
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000138 }
139#endif
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000140 return result;
141}
142
143
144Object* Heap::AllocateRawCell() {
145#ifdef DEBUG
146 Counters::objs_since_last_full.Increment();
147 Counters::objs_since_last_young.Increment();
148#endif
149 Object* result = cell_space_->AllocateRaw(JSGlobalPropertyCell::kSize);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000150 if (result->IsFailure()) old_gen_exhausted_ = true;
151 return result;
152}
153
154
155bool Heap::InNewSpace(Object* object) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000156 bool result = new_space_.Contains(object);
157 ASSERT(!result || // Either not in new space
158 gc_state_ != NOT_IN_GC || // ... or in the middle of GC
159 InToSpace(object)); // ... or in to-space (where we allocate).
160 return result;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000161}
162
163
164bool Heap::InFromSpace(Object* object) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000165 return new_space_.FromSpaceContains(object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000166}
167
168
169bool Heap::InToSpace(Object* object) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000170 return new_space_.ToSpaceContains(object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000171}
172
173
174bool Heap::ShouldBePromoted(Address old_address, int object_size) {
175 // An object should be promoted if:
176 // - the object has survived a scavenge operation or
177 // - to space is already 25% full.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000178 return old_address < new_space_.age_mark()
179 || (new_space_.Size() + object_size) >= (new_space_.Capacity() >> 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000180}
181
182
183void Heap::RecordWrite(Address address, int offset) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000184 if (new_space_.Contains(address)) return;
185 ASSERT(!new_space_.FromSpaceContains(address));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000186 SLOW_ASSERT(Contains(address + offset));
187 Page::SetRSet(address, offset);
188}
189
190
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000191void Heap::RecordWrites(Address address, int start, int len) {
192 if (new_space_.Contains(address)) return;
193 ASSERT(!new_space_.FromSpaceContains(address));
194 for (int offset = start;
195 offset < start + len * kPointerSize;
196 offset += kPointerSize) {
197 SLOW_ASSERT(Contains(address + offset));
198 Page::SetRSet(address, offset);
199 }
200}
201
202
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000203OldSpace* Heap::TargetSpace(HeapObject* object) {
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000204 InstanceType type = object->map()->instance_type();
205 AllocationSpace space = TargetSpaceId(type);
206 return (space == OLD_POINTER_SPACE)
207 ? old_pointer_space_
208 : old_data_space_;
209}
210
211
212AllocationSpace Heap::TargetSpaceId(InstanceType type) {
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000213 // Heap numbers and sequential strings are promoted to old data space, all
214 // other object types are promoted to old pointer space. We do not use
kasper.lund7276f142008-07-30 08:49:36 +0000215 // object->IsHeapNumber() and object->IsSeqString() because we already
216 // know that object has the heap object tag.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000217
218 // These objects are never allocated in new space.
219 ASSERT(type != MAP_TYPE);
220 ASSERT(type != CODE_TYPE);
221 ASSERT(type != ODDBALL_TYPE);
222 ASSERT(type != JS_GLOBAL_PROPERTY_CELL_TYPE);
223
224 if (type < FIRST_NONSTRING_TYPE) {
225 // There are three string representations: sequential strings, cons
226 // strings, and external strings. Only cons strings contain
227 // non-map-word pointers to heap objects.
228 return ((type & kStringRepresentationMask) == kConsStringTag)
229 ? OLD_POINTER_SPACE
230 : OLD_DATA_SPACE;
231 } else {
232 return (type <= LAST_DATA_TYPE) ? OLD_DATA_SPACE : OLD_POINTER_SPACE;
233 }
kasper.lund7276f142008-07-30 08:49:36 +0000234}
235
236
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000237void Heap::CopyBlock(Object** dst, Object** src, int byte_size) {
238 ASSERT(IsAligned(byte_size, kPointerSize));
lrn@chromium.org25156de2010-04-06 13:10:27 +0000239 CopyWords(dst, src, byte_size / kPointerSize);
240}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000241
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000242
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +0000243void Heap::MoveBlock(Object** dst, Object** src, size_t byte_size) {
244 ASSERT(IsAligned<size_t>(byte_size, kPointerSize));
245
246 int size_in_words = byte_size / kPointerSize;
247
248 if ((dst < src) || (dst >= (src + size_in_words))) {
249 ASSERT((dst >= (src + size_in_words)) ||
250 ((OffsetFrom(reinterpret_cast<Address>(src)) -
251 OffsetFrom(reinterpret_cast<Address>(dst))) >= kPointerSize));
252
253 Object** end = src + size_in_words;
254
255 while (src != end) {
256 *dst++ = *src++;
257 }
258 } else {
259 memmove(dst, src, byte_size);
260 }
261}
262
263
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000264void Heap::ScavengeObject(HeapObject** p, HeapObject* object) {
265 ASSERT(InFromSpace(object));
266
267 // We use the first word (where the map pointer usually is) of a heap
268 // object to record the forwarding pointer. A forwarding pointer can
269 // point to an old space, the code space, or the to space of the new
270 // generation.
271 MapWord first_word = object->map_word();
272
273 // If the first word is a forwarding address, the object has already been
274 // copied.
275 if (first_word.IsForwardingAddress()) {
276 *p = first_word.ToForwardingAddress();
277 return;
278 }
279
280 // Call the slow part of scavenge object.
281 return ScavengeObjectSlow(p, object);
282}
283
284
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000285Object* Heap::PrepareForCompare(String* str) {
286 // Always flatten small strings and force flattening of long strings
287 // after we have accumulated a certain amount we failed to flatten.
288 static const int kMaxAlwaysFlattenLength = 32;
289 static const int kFlattenLongThreshold = 16*KB;
290
291 const int length = str->length();
292 Object* obj = str->TryFlatten();
293 if (length <= kMaxAlwaysFlattenLength ||
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000294 unflattened_strings_length_ >= kFlattenLongThreshold) {
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000295 return obj;
296 }
297 if (obj->IsFailure()) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000298 unflattened_strings_length_ += length;
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000299 }
300 return str;
301}
302
303
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000304int Heap::AdjustAmountOfExternalAllocatedMemory(int change_in_bytes) {
305 ASSERT(HasBeenSetup());
306 int amount = amount_of_external_allocated_memory_ + change_in_bytes;
307 if (change_in_bytes >= 0) {
308 // Avoid overflow.
309 if (amount > amount_of_external_allocated_memory_) {
310 amount_of_external_allocated_memory_ = amount;
311 }
312 int amount_since_last_global_gc =
313 amount_of_external_allocated_memory_ -
314 amount_of_external_allocated_memory_at_last_global_gc_;
315 if (amount_since_last_global_gc > external_allocation_limit_) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000316 CollectAllGarbage(false);
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000317 }
318 } else {
319 // Avoid underflow.
320 if (amount >= 0) {
321 amount_of_external_allocated_memory_ = amount;
322 }
323 }
324 ASSERT(amount_of_external_allocated_memory_ >= 0);
325 return amount_of_external_allocated_memory_;
326}
327
328
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000329void Heap::SetLastScriptId(Object* last_script_id) {
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000330 roots_[kLastScriptIdRootIndex] = last_script_id;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000331}
332
333
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000334#define GC_GREEDY_CHECK() \
335 ASSERT(!FLAG_gc_greedy || v8::internal::Heap::GarbageCollectionGreedyCheck())
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000336
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000337
338// Calls the FUNCTION_CALL function and retries it up to three times
339// to guarantee that any allocations performed during the call will
340// succeed if there's enough memory.
341
342// Warning: Do not use the identifiers __object__ or __scope__ in a
343// call to this macro.
344
345#define CALL_AND_RETRY(FUNCTION_CALL, RETURN_VALUE, RETURN_EMPTY) \
346 do { \
347 GC_GREEDY_CHECK(); \
348 Object* __object__ = FUNCTION_CALL; \
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000349 if (!__object__->IsFailure()) RETURN_VALUE; \
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000350 if (__object__->IsOutOfMemoryFailure()) { \
351 v8::internal::V8::FatalProcessOutOfMemory("CALL_AND_RETRY_0"); \
352 } \
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000353 if (!__object__->IsRetryAfterGC()) RETURN_EMPTY; \
kasperl@chromium.org58870952008-10-30 14:34:19 +0000354 Heap::CollectGarbage(Failure::cast(__object__)->requested(), \
355 Failure::cast(__object__)->allocation_space()); \
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000356 __object__ = FUNCTION_CALL; \
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000357 if (!__object__->IsFailure()) RETURN_VALUE; \
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000358 if (__object__->IsOutOfMemoryFailure()) { \
kasperl@chromium.org58870952008-10-30 14:34:19 +0000359 v8::internal::V8::FatalProcessOutOfMemory("CALL_AND_RETRY_1"); \
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000360 } \
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000361 if (!__object__->IsRetryAfterGC()) RETURN_EMPTY; \
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000362 Counters::gc_last_resort_from_handles.Increment(); \
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000363 Heap::CollectAllGarbage(false); \
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000364 { \
365 AlwaysAllocateScope __scope__; \
366 __object__ = FUNCTION_CALL; \
367 } \
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000368 if (!__object__->IsFailure()) RETURN_VALUE; \
ager@chromium.org9085a012009-05-11 19:22:57 +0000369 if (__object__->IsOutOfMemoryFailure() || \
370 __object__->IsRetryAfterGC()) { \
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000371 /* TODO(1181417): Fix this. */ \
kasperl@chromium.org58870952008-10-30 14:34:19 +0000372 v8::internal::V8::FatalProcessOutOfMemory("CALL_AND_RETRY_2"); \
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000373 } \
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000374 RETURN_EMPTY; \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000375 } while (false)
376
377
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000378#define CALL_HEAP_FUNCTION(FUNCTION_CALL, TYPE) \
379 CALL_AND_RETRY(FUNCTION_CALL, \
380 return Handle<TYPE>(TYPE::cast(__object__)), \
381 return Handle<TYPE>())
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000382
383
384#define CALL_HEAP_FUNCTION_VOID(FUNCTION_CALL) \
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000385 CALL_AND_RETRY(FUNCTION_CALL, return, return)
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000386
387
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000388#ifdef DEBUG
389
390inline bool Heap::allow_allocation(bool new_state) {
391 bool old = allocation_allowed_;
392 allocation_allowed_ = new_state;
393 return old;
394}
395
396#endif
397
398
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000399void ExternalStringTable::AddString(String* string) {
400 ASSERT(string->IsExternalString());
401 if (Heap::InNewSpace(string)) {
402 new_space_strings_.Add(string);
403 } else {
404 old_space_strings_.Add(string);
405 }
406}
407
408
409void ExternalStringTable::Iterate(ObjectVisitor* v) {
410 if (!new_space_strings_.is_empty()) {
411 Object** start = &new_space_strings_[0];
412 v->VisitPointers(start, start + new_space_strings_.length());
413 }
414 if (!old_space_strings_.is_empty()) {
415 Object** start = &old_space_strings_[0];
416 v->VisitPointers(start, start + old_space_strings_.length());
417 }
418}
419
420
421// Verify() is inline to avoid ifdef-s around its calls in release
422// mode.
423void ExternalStringTable::Verify() {
424#ifdef DEBUG
425 for (int i = 0; i < new_space_strings_.length(); ++i) {
426 ASSERT(Heap::InNewSpace(new_space_strings_[i]));
427 ASSERT(new_space_strings_[i] != Heap::raw_unchecked_null_value());
428 }
429 for (int i = 0; i < old_space_strings_.length(); ++i) {
430 ASSERT(!Heap::InNewSpace(old_space_strings_[i]));
431 ASSERT(old_space_strings_[i] != Heap::raw_unchecked_null_value());
432 }
433#endif
434}
435
436
437void ExternalStringTable::AddOldString(String* string) {
438 ASSERT(string->IsExternalString());
439 ASSERT(!Heap::InNewSpace(string));
440 old_space_strings_.Add(string);
441}
442
443
444void ExternalStringTable::ShrinkNewStrings(int position) {
445 new_space_strings_.Rewind(position);
446 Verify();
447}
448
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000449} } // namespace v8::internal
450
451#endif // V8_HEAP_INL_H_