blob: c4676fd74cf83ac0a5f4191f812e3df2d0f5f90e [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));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000239
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000240 // Use block copying memcpy if the segment we're copying is
241 // enough to justify the extra call/setup overhead.
242 static const int kBlockCopyLimit = 16 * kPointerSize;
243
244 if (byte_size >= kBlockCopyLimit) {
245 memcpy(dst, src, byte_size);
246 } else {
247 int remaining = byte_size / kPointerSize;
248 do {
249 remaining--;
250 *dst++ = *src++;
251 } while (remaining > 0);
252 }
253}
254
255
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000256void Heap::ScavengeObject(HeapObject** p, HeapObject* object) {
257 ASSERT(InFromSpace(object));
258
259 // We use the first word (where the map pointer usually is) of a heap
260 // object to record the forwarding pointer. A forwarding pointer can
261 // point to an old space, the code space, or the to space of the new
262 // generation.
263 MapWord first_word = object->map_word();
264
265 // If the first word is a forwarding address, the object has already been
266 // copied.
267 if (first_word.IsForwardingAddress()) {
268 *p = first_word.ToForwardingAddress();
269 return;
270 }
271
272 // Call the slow part of scavenge object.
273 return ScavengeObjectSlow(p, object);
274}
275
276
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000277Object* Heap::PrepareForCompare(String* str) {
278 // Always flatten small strings and force flattening of long strings
279 // after we have accumulated a certain amount we failed to flatten.
280 static const int kMaxAlwaysFlattenLength = 32;
281 static const int kFlattenLongThreshold = 16*KB;
282
283 const int length = str->length();
284 Object* obj = str->TryFlatten();
285 if (length <= kMaxAlwaysFlattenLength ||
286 unflattended_strings_length_ >= kFlattenLongThreshold) {
287 return obj;
288 }
289 if (obj->IsFailure()) {
290 unflattended_strings_length_ += length;
291 }
292 return str;
293}
294
295
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000296int Heap::AdjustAmountOfExternalAllocatedMemory(int change_in_bytes) {
297 ASSERT(HasBeenSetup());
298 int amount = amount_of_external_allocated_memory_ + change_in_bytes;
299 if (change_in_bytes >= 0) {
300 // Avoid overflow.
301 if (amount > amount_of_external_allocated_memory_) {
302 amount_of_external_allocated_memory_ = amount;
303 }
304 int amount_since_last_global_gc =
305 amount_of_external_allocated_memory_ -
306 amount_of_external_allocated_memory_at_last_global_gc_;
307 if (amount_since_last_global_gc > external_allocation_limit_) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000308 CollectAllGarbage(false);
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000309 }
310 } else {
311 // Avoid underflow.
312 if (amount >= 0) {
313 amount_of_external_allocated_memory_ = amount;
314 }
315 }
316 ASSERT(amount_of_external_allocated_memory_ >= 0);
317 return amount_of_external_allocated_memory_;
318}
319
320
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000321void Heap::SetLastScriptId(Object* last_script_id) {
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000322 roots_[kLastScriptIdRootIndex] = last_script_id;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000323}
324
325
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000326#define GC_GREEDY_CHECK() \
327 ASSERT(!FLAG_gc_greedy || v8::internal::Heap::GarbageCollectionGreedyCheck())
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000328
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000329
330// Calls the FUNCTION_CALL function and retries it up to three times
331// to guarantee that any allocations performed during the call will
332// succeed if there's enough memory.
333
334// Warning: Do not use the identifiers __object__ or __scope__ in a
335// call to this macro.
336
337#define CALL_AND_RETRY(FUNCTION_CALL, RETURN_VALUE, RETURN_EMPTY) \
338 do { \
339 GC_GREEDY_CHECK(); \
340 Object* __object__ = FUNCTION_CALL; \
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000341 if (!__object__->IsFailure()) RETURN_VALUE; \
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000342 if (__object__->IsOutOfMemoryFailure()) { \
343 v8::internal::V8::FatalProcessOutOfMemory("CALL_AND_RETRY_0"); \
344 } \
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000345 if (!__object__->IsRetryAfterGC()) RETURN_EMPTY; \
kasperl@chromium.org58870952008-10-30 14:34:19 +0000346 Heap::CollectGarbage(Failure::cast(__object__)->requested(), \
347 Failure::cast(__object__)->allocation_space()); \
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000348 __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()) { \
kasperl@chromium.org58870952008-10-30 14:34:19 +0000351 v8::internal::V8::FatalProcessOutOfMemory("CALL_AND_RETRY_1"); \
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000352 } \
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000353 if (!__object__->IsRetryAfterGC()) RETURN_EMPTY; \
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000354 Counters::gc_last_resort_from_handles.Increment(); \
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000355 Heap::CollectAllGarbage(false); \
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000356 { \
357 AlwaysAllocateScope __scope__; \
358 __object__ = FUNCTION_CALL; \
359 } \
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000360 if (!__object__->IsFailure()) RETURN_VALUE; \
ager@chromium.org9085a012009-05-11 19:22:57 +0000361 if (__object__->IsOutOfMemoryFailure() || \
362 __object__->IsRetryAfterGC()) { \
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000363 /* TODO(1181417): Fix this. */ \
kasperl@chromium.org58870952008-10-30 14:34:19 +0000364 v8::internal::V8::FatalProcessOutOfMemory("CALL_AND_RETRY_2"); \
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000365 } \
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000366 RETURN_EMPTY; \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000367 } while (false)
368
369
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000370#define CALL_HEAP_FUNCTION(FUNCTION_CALL, TYPE) \
371 CALL_AND_RETRY(FUNCTION_CALL, \
372 return Handle<TYPE>(TYPE::cast(__object__)), \
373 return Handle<TYPE>())
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000374
375
376#define CALL_HEAP_FUNCTION_VOID(FUNCTION_CALL) \
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000377 CALL_AND_RETRY(FUNCTION_CALL, return, return)
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000378
379
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000380#ifdef DEBUG
381
382inline bool Heap::allow_allocation(bool new_state) {
383 bool old = allocation_allowed_;
384 allocation_allowed_ = new_state;
385 return old;
386}
387
388#endif
389
390
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000391void ExternalStringTable::AddString(String* string) {
392 ASSERT(string->IsExternalString());
393 if (Heap::InNewSpace(string)) {
394 new_space_strings_.Add(string);
395 } else {
396 old_space_strings_.Add(string);
397 }
398}
399
400
401void ExternalStringTable::Iterate(ObjectVisitor* v) {
402 if (!new_space_strings_.is_empty()) {
403 Object** start = &new_space_strings_[0];
404 v->VisitPointers(start, start + new_space_strings_.length());
405 }
406 if (!old_space_strings_.is_empty()) {
407 Object** start = &old_space_strings_[0];
408 v->VisitPointers(start, start + old_space_strings_.length());
409 }
410}
411
412
413// Verify() is inline to avoid ifdef-s around its calls in release
414// mode.
415void ExternalStringTable::Verify() {
416#ifdef DEBUG
417 for (int i = 0; i < new_space_strings_.length(); ++i) {
418 ASSERT(Heap::InNewSpace(new_space_strings_[i]));
419 ASSERT(new_space_strings_[i] != Heap::raw_unchecked_null_value());
420 }
421 for (int i = 0; i < old_space_strings_.length(); ++i) {
422 ASSERT(!Heap::InNewSpace(old_space_strings_[i]));
423 ASSERT(old_space_strings_[i] != Heap::raw_unchecked_null_value());
424 }
425#endif
426}
427
428
429void ExternalStringTable::AddOldString(String* string) {
430 ASSERT(string->IsExternalString());
431 ASSERT(!Heap::InNewSpace(string));
432 old_space_strings_.Add(string);
433}
434
435
436void ExternalStringTable::ShrinkNewStrings(int position) {
437 new_space_strings_.Rewind(position);
438 Verify();
439}
440
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000441} } // namespace v8::internal
442
443#endif // V8_HEAP_INL_H_