blob: f18bf0f61b69b6811cfe9844477993e29d54b71c [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.
136 CHECK((OffsetFrom(result) & kMapAlignmentMask) == kHeapObjectTag);
137 }
138#endif
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000139 return result;
140}
141
142
143Object* Heap::AllocateRawCell() {
144#ifdef DEBUG
145 Counters::objs_since_last_full.Increment();
146 Counters::objs_since_last_young.Increment();
147#endif
148 Object* result = cell_space_->AllocateRaw(JSGlobalPropertyCell::kSize);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000149 if (result->IsFailure()) old_gen_exhausted_ = true;
150 return result;
151}
152
153
154bool Heap::InNewSpace(Object* object) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000155 bool result = new_space_.Contains(object);
156 ASSERT(!result || // Either not in new space
157 gc_state_ != NOT_IN_GC || // ... or in the middle of GC
158 InToSpace(object)); // ... or in to-space (where we allocate).
159 return result;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000160}
161
162
163bool Heap::InFromSpace(Object* object) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000164 return new_space_.FromSpaceContains(object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000165}
166
167
168bool Heap::InToSpace(Object* object) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000169 return new_space_.ToSpaceContains(object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000170}
171
172
173bool Heap::ShouldBePromoted(Address old_address, int object_size) {
174 // An object should be promoted if:
175 // - the object has survived a scavenge operation or
176 // - to space is already 25% full.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000177 return old_address < new_space_.age_mark()
178 || (new_space_.Size() + object_size) >= (new_space_.Capacity() >> 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000179}
180
181
182void Heap::RecordWrite(Address address, int offset) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000183 if (new_space_.Contains(address)) return;
184 ASSERT(!new_space_.FromSpaceContains(address));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000185 SLOW_ASSERT(Contains(address + offset));
186 Page::SetRSet(address, offset);
187}
188
189
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000190OldSpace* Heap::TargetSpace(HeapObject* object) {
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000191 InstanceType type = object->map()->instance_type();
192 AllocationSpace space = TargetSpaceId(type);
193 return (space == OLD_POINTER_SPACE)
194 ? old_pointer_space_
195 : old_data_space_;
196}
197
198
199AllocationSpace Heap::TargetSpaceId(InstanceType type) {
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000200 // Heap numbers and sequential strings are promoted to old data space, all
201 // other object types are promoted to old pointer space. We do not use
kasper.lund7276f142008-07-30 08:49:36 +0000202 // object->IsHeapNumber() and object->IsSeqString() because we already
203 // know that object has the heap object tag.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000204
205 // These objects are never allocated in new space.
206 ASSERT(type != MAP_TYPE);
207 ASSERT(type != CODE_TYPE);
208 ASSERT(type != ODDBALL_TYPE);
209 ASSERT(type != JS_GLOBAL_PROPERTY_CELL_TYPE);
210
211 if (type < FIRST_NONSTRING_TYPE) {
212 // There are three string representations: sequential strings, cons
213 // strings, and external strings. Only cons strings contain
214 // non-map-word pointers to heap objects.
215 return ((type & kStringRepresentationMask) == kConsStringTag)
216 ? OLD_POINTER_SPACE
217 : OLD_DATA_SPACE;
218 } else {
219 return (type <= LAST_DATA_TYPE) ? OLD_DATA_SPACE : OLD_POINTER_SPACE;
220 }
kasper.lund7276f142008-07-30 08:49:36 +0000221}
222
223
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000224void Heap::CopyBlock(Object** dst, Object** src, int byte_size) {
225 ASSERT(IsAligned(byte_size, kPointerSize));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000226
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000227 // Use block copying memcpy if the segment we're copying is
228 // enough to justify the extra call/setup overhead.
229 static const int kBlockCopyLimit = 16 * kPointerSize;
230
231 if (byte_size >= kBlockCopyLimit) {
232 memcpy(dst, src, byte_size);
233 } else {
234 int remaining = byte_size / kPointerSize;
235 do {
236 remaining--;
237 *dst++ = *src++;
238 } while (remaining > 0);
239 }
240}
241
242
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000243void Heap::ScavengeObject(HeapObject** p, HeapObject* object) {
244 ASSERT(InFromSpace(object));
245
246 // We use the first word (where the map pointer usually is) of a heap
247 // object to record the forwarding pointer. A forwarding pointer can
248 // point to an old space, the code space, or the to space of the new
249 // generation.
250 MapWord first_word = object->map_word();
251
252 // If the first word is a forwarding address, the object has already been
253 // copied.
254 if (first_word.IsForwardingAddress()) {
255 *p = first_word.ToForwardingAddress();
256 return;
257 }
258
259 // Call the slow part of scavenge object.
260 return ScavengeObjectSlow(p, object);
261}
262
263
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000264int Heap::AdjustAmountOfExternalAllocatedMemory(int change_in_bytes) {
265 ASSERT(HasBeenSetup());
266 int amount = amount_of_external_allocated_memory_ + change_in_bytes;
267 if (change_in_bytes >= 0) {
268 // Avoid overflow.
269 if (amount > amount_of_external_allocated_memory_) {
270 amount_of_external_allocated_memory_ = amount;
271 }
272 int amount_since_last_global_gc =
273 amount_of_external_allocated_memory_ -
274 amount_of_external_allocated_memory_at_last_global_gc_;
275 if (amount_since_last_global_gc > external_allocation_limit_) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000276 CollectAllGarbage(false);
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000277 }
278 } else {
279 // Avoid underflow.
280 if (amount >= 0) {
281 amount_of_external_allocated_memory_ = amount;
282 }
283 }
284 ASSERT(amount_of_external_allocated_memory_ >= 0);
285 return amount_of_external_allocated_memory_;
286}
287
288
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000289void Heap::SetLastScriptId(Object* last_script_id) {
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000290 roots_[kLastScriptIdRootIndex] = last_script_id;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000291}
292
293
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000294#define GC_GREEDY_CHECK() \
295 ASSERT(!FLAG_gc_greedy || v8::internal::Heap::GarbageCollectionGreedyCheck())
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000296
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000297
298// Calls the FUNCTION_CALL function and retries it up to three times
299// to guarantee that any allocations performed during the call will
300// succeed if there's enough memory.
301
302// Warning: Do not use the identifiers __object__ or __scope__ in a
303// call to this macro.
304
305#define CALL_AND_RETRY(FUNCTION_CALL, RETURN_VALUE, RETURN_EMPTY) \
306 do { \
307 GC_GREEDY_CHECK(); \
308 Object* __object__ = FUNCTION_CALL; \
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000309 if (!__object__->IsFailure()) RETURN_VALUE; \
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000310 if (__object__->IsOutOfMemoryFailure()) { \
311 v8::internal::V8::FatalProcessOutOfMemory("CALL_AND_RETRY_0"); \
312 } \
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000313 if (!__object__->IsRetryAfterGC()) RETURN_EMPTY; \
kasperl@chromium.org58870952008-10-30 14:34:19 +0000314 Heap::CollectGarbage(Failure::cast(__object__)->requested(), \
315 Failure::cast(__object__)->allocation_space()); \
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000316 __object__ = FUNCTION_CALL; \
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000317 if (!__object__->IsFailure()) RETURN_VALUE; \
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000318 if (__object__->IsOutOfMemoryFailure()) { \
kasperl@chromium.org58870952008-10-30 14:34:19 +0000319 v8::internal::V8::FatalProcessOutOfMemory("CALL_AND_RETRY_1"); \
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000320 } \
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000321 if (!__object__->IsRetryAfterGC()) RETURN_EMPTY; \
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000322 Counters::gc_last_resort_from_handles.Increment(); \
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000323 Heap::CollectAllGarbage(false); \
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000324 { \
325 AlwaysAllocateScope __scope__; \
326 __object__ = FUNCTION_CALL; \
327 } \
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000328 if (!__object__->IsFailure()) RETURN_VALUE; \
ager@chromium.org9085a012009-05-11 19:22:57 +0000329 if (__object__->IsOutOfMemoryFailure() || \
330 __object__->IsRetryAfterGC()) { \
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000331 /* TODO(1181417): Fix this. */ \
kasperl@chromium.org58870952008-10-30 14:34:19 +0000332 v8::internal::V8::FatalProcessOutOfMemory("CALL_AND_RETRY_2"); \
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000333 } \
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000334 RETURN_EMPTY; \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000335 } while (false)
336
337
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000338#define CALL_HEAP_FUNCTION(FUNCTION_CALL, TYPE) \
339 CALL_AND_RETRY(FUNCTION_CALL, \
340 return Handle<TYPE>(TYPE::cast(__object__)), \
341 return Handle<TYPE>())
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000342
343
344#define CALL_HEAP_FUNCTION_VOID(FUNCTION_CALL) \
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000345 CALL_AND_RETRY(FUNCTION_CALL, return, return)
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000346
347
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000348#ifdef DEBUG
349
350inline bool Heap::allow_allocation(bool new_state) {
351 bool old = allocation_allowed_;
352 allocation_allowed_ = new_state;
353 return old;
354}
355
356#endif
357
358
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000359void ExternalStringTable::AddString(String* string) {
360 ASSERT(string->IsExternalString());
361 if (Heap::InNewSpace(string)) {
362 new_space_strings_.Add(string);
363 } else {
364 old_space_strings_.Add(string);
365 }
366}
367
368
369void ExternalStringTable::Iterate(ObjectVisitor* v) {
370 if (!new_space_strings_.is_empty()) {
371 Object** start = &new_space_strings_[0];
372 v->VisitPointers(start, start + new_space_strings_.length());
373 }
374 if (!old_space_strings_.is_empty()) {
375 Object** start = &old_space_strings_[0];
376 v->VisitPointers(start, start + old_space_strings_.length());
377 }
378}
379
380
381// Verify() is inline to avoid ifdef-s around its calls in release
382// mode.
383void ExternalStringTable::Verify() {
384#ifdef DEBUG
385 for (int i = 0; i < new_space_strings_.length(); ++i) {
386 ASSERT(Heap::InNewSpace(new_space_strings_[i]));
387 ASSERT(new_space_strings_[i] != Heap::raw_unchecked_null_value());
388 }
389 for (int i = 0; i < old_space_strings_.length(); ++i) {
390 ASSERT(!Heap::InNewSpace(old_space_strings_[i]));
391 ASSERT(old_space_strings_[i] != Heap::raw_unchecked_null_value());
392 }
393#endif
394}
395
396
397void ExternalStringTable::AddOldString(String* string) {
398 ASSERT(string->IsExternalString());
399 ASSERT(!Heap::InNewSpace(string));
400 old_space_strings_.Add(string);
401}
402
403
404void ExternalStringTable::ShrinkNewStrings(int position) {
405 new_space_strings_.Rewind(position);
406 Verify();
407}
408
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000409} } // namespace v8::internal
410
411#endif // V8_HEAP_INL_H_