blob: 992d89a079d9321b3b7d250199692b947bb88df1 [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 ||
57 retry_space == OLD_DATA_SPACE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000058#ifdef DEBUG
59 if (FLAG_gc_interval >= 0 &&
60 !disallow_allocation_failure_ &&
61 Heap::allocation_timeout_-- <= 0) {
62 return Failure::RetryAfterGC(size_in_bytes, space);
63 }
64 Counters::objs_since_last_full.Increment();
65 Counters::objs_since_last_young.Increment();
66#endif
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000067 Object* result;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000068 if (NEW_SPACE == space) {
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000069 result = new_space_.AllocateRaw(size_in_bytes);
70 if (always_allocate() && result->IsFailure()) {
71 space = retry_space;
72 } else {
73 return result;
74 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000075 }
76
ager@chromium.org9258b6b2008-09-11 09:11:10 +000077 if (OLD_POINTER_SPACE == space) {
78 result = old_pointer_space_->AllocateRaw(size_in_bytes);
79 } else if (OLD_DATA_SPACE == space) {
80 result = old_data_space_->AllocateRaw(size_in_bytes);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000081 } else if (CODE_SPACE == space) {
82 result = code_space_->AllocateRaw(size_in_bytes);
83 } else if (LO_SPACE == space) {
84 result = lo_space_->AllocateRaw(size_in_bytes);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +000085 } else if (CELL_SPACE == space) {
86 result = cell_space_->AllocateRaw(size_in_bytes);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000087 } else {
88 ASSERT(MAP_SPACE == space);
89 result = map_space_->AllocateRaw(size_in_bytes);
90 }
91 if (result->IsFailure()) old_gen_exhausted_ = true;
92 return result;
93}
94
95
96Object* Heap::NumberFromInt32(int32_t value) {
97 if (Smi::IsValid(value)) return Smi::FromInt(value);
98 // Bypass NumberFromDouble to avoid various redundant checks.
99 return AllocateHeapNumber(FastI2D(value));
100}
101
102
103Object* Heap::NumberFromUint32(uint32_t value) {
104 if ((int32_t)value >= 0 && Smi::IsValid((int32_t)value)) {
105 return Smi::FromInt((int32_t)value);
106 }
107 // Bypass NumberFromDouble to avoid various redundant checks.
108 return AllocateHeapNumber(FastUI2D(value));
109}
110
111
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000112void Heap::FinalizeExternalString(String* string) {
113 ASSERT(string->IsExternalString());
114 v8::String::ExternalStringResourceBase** resource_addr =
115 reinterpret_cast<v8::String::ExternalStringResourceBase**>(
116 reinterpret_cast<byte*>(string) +
117 ExternalString::kResourceOffset -
118 kHeapObjectTag);
119 delete *resource_addr;
120 // Clear the resource pointer in the string.
121 *resource_addr = NULL;
122}
123
124
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000125Object* Heap::AllocateRawMap() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000126#ifdef DEBUG
127 Counters::objs_since_last_full.Increment();
128 Counters::objs_since_last_young.Increment();
129#endif
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000130 Object* result = map_space_->AllocateRaw(Map::kSize);
131 if (result->IsFailure()) old_gen_exhausted_ = true;
132 return result;
133}
134
135
136Object* Heap::AllocateRawCell() {
137#ifdef DEBUG
138 Counters::objs_since_last_full.Increment();
139 Counters::objs_since_last_young.Increment();
140#endif
141 Object* result = cell_space_->AllocateRaw(JSGlobalPropertyCell::kSize);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000142 if (result->IsFailure()) old_gen_exhausted_ = true;
143 return result;
144}
145
146
147bool Heap::InNewSpace(Object* object) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000148 return new_space_.Contains(object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000149}
150
151
152bool Heap::InFromSpace(Object* object) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000153 return new_space_.FromSpaceContains(object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000154}
155
156
157bool Heap::InToSpace(Object* object) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000158 return new_space_.ToSpaceContains(object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000159}
160
161
162bool Heap::ShouldBePromoted(Address old_address, int object_size) {
163 // An object should be promoted if:
164 // - the object has survived a scavenge operation or
165 // - to space is already 25% full.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000166 return old_address < new_space_.age_mark()
167 || (new_space_.Size() + object_size) >= (new_space_.Capacity() >> 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000168}
169
170
171void Heap::RecordWrite(Address address, int offset) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000172 if (new_space_.Contains(address)) return;
173 ASSERT(!new_space_.FromSpaceContains(address));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000174 SLOW_ASSERT(Contains(address + offset));
175 Page::SetRSet(address, offset);
176}
177
178
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000179OldSpace* Heap::TargetSpace(HeapObject* object) {
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000180 InstanceType type = object->map()->instance_type();
181 AllocationSpace space = TargetSpaceId(type);
182 return (space == OLD_POINTER_SPACE)
183 ? old_pointer_space_
184 : old_data_space_;
185}
186
187
188AllocationSpace Heap::TargetSpaceId(InstanceType type) {
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000189 // Heap numbers and sequential strings are promoted to old data space, all
190 // other object types are promoted to old pointer space. We do not use
kasper.lund7276f142008-07-30 08:49:36 +0000191 // object->IsHeapNumber() and object->IsSeqString() because we already
192 // know that object has the heap object tag.
kasper.lund7276f142008-07-30 08:49:36 +0000193 ASSERT((type != CODE_TYPE) && (type != MAP_TYPE));
194 bool has_pointers =
195 type != HEAP_NUMBER_TYPE &&
196 (type >= FIRST_NONSTRING_TYPE ||
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000197 (type & kStringRepresentationMask) != kSeqStringTag);
198 return has_pointers ? OLD_POINTER_SPACE : OLD_DATA_SPACE;
kasper.lund7276f142008-07-30 08:49:36 +0000199}
200
201
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000202void Heap::CopyBlock(Object** dst, Object** src, int byte_size) {
203 ASSERT(IsAligned(byte_size, kPointerSize));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000204
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000205 // Use block copying memcpy if the segment we're copying is
206 // enough to justify the extra call/setup overhead.
207 static const int kBlockCopyLimit = 16 * kPointerSize;
208
209 if (byte_size >= kBlockCopyLimit) {
210 memcpy(dst, src, byte_size);
211 } else {
212 int remaining = byte_size / kPointerSize;
213 do {
214 remaining--;
215 *dst++ = *src++;
216 } while (remaining > 0);
217 }
218}
219
220
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000221void Heap::ScavengeObject(HeapObject** p, HeapObject* object) {
222 ASSERT(InFromSpace(object));
223
224 // We use the first word (where the map pointer usually is) of a heap
225 // object to record the forwarding pointer. A forwarding pointer can
226 // point to an old space, the code space, or the to space of the new
227 // generation.
228 MapWord first_word = object->map_word();
229
230 // If the first word is a forwarding address, the object has already been
231 // copied.
232 if (first_word.IsForwardingAddress()) {
233 *p = first_word.ToForwardingAddress();
234 return;
235 }
236
237 // Call the slow part of scavenge object.
238 return ScavengeObjectSlow(p, object);
239}
240
241
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000242int Heap::AdjustAmountOfExternalAllocatedMemory(int change_in_bytes) {
243 ASSERT(HasBeenSetup());
244 int amount = amount_of_external_allocated_memory_ + change_in_bytes;
245 if (change_in_bytes >= 0) {
246 // Avoid overflow.
247 if (amount > amount_of_external_allocated_memory_) {
248 amount_of_external_allocated_memory_ = amount;
249 }
250 int amount_since_last_global_gc =
251 amount_of_external_allocated_memory_ -
252 amount_of_external_allocated_memory_at_last_global_gc_;
253 if (amount_since_last_global_gc > external_allocation_limit_) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000254 CollectAllGarbage(false);
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000255 }
256 } else {
257 // Avoid underflow.
258 if (amount >= 0) {
259 amount_of_external_allocated_memory_ = amount;
260 }
261 }
262 ASSERT(amount_of_external_allocated_memory_ >= 0);
263 return amount_of_external_allocated_memory_;
264}
265
266
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000267void Heap::SetLastScriptId(Object* last_script_id) {
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000268 roots_[kLastScriptIdRootIndex] = last_script_id;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000269}
270
271
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000272#define GC_GREEDY_CHECK() \
273 ASSERT(!FLAG_gc_greedy || v8::internal::Heap::GarbageCollectionGreedyCheck())
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000274
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000275
276// Calls the FUNCTION_CALL function and retries it up to three times
277// to guarantee that any allocations performed during the call will
278// succeed if there's enough memory.
279
280// Warning: Do not use the identifiers __object__ or __scope__ in a
281// call to this macro.
282
283#define CALL_AND_RETRY(FUNCTION_CALL, RETURN_VALUE, RETURN_EMPTY) \
284 do { \
285 GC_GREEDY_CHECK(); \
286 Object* __object__ = FUNCTION_CALL; \
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000287 if (!__object__->IsFailure()) RETURN_VALUE; \
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000288 if (__object__->IsOutOfMemoryFailure()) { \
289 v8::internal::V8::FatalProcessOutOfMemory("CALL_AND_RETRY_0"); \
290 } \
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000291 if (!__object__->IsRetryAfterGC()) RETURN_EMPTY; \
kasperl@chromium.org58870952008-10-30 14:34:19 +0000292 Heap::CollectGarbage(Failure::cast(__object__)->requested(), \
293 Failure::cast(__object__)->allocation_space()); \
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000294 __object__ = FUNCTION_CALL; \
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000295 if (!__object__->IsFailure()) RETURN_VALUE; \
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000296 if (__object__->IsOutOfMemoryFailure()) { \
kasperl@chromium.org58870952008-10-30 14:34:19 +0000297 v8::internal::V8::FatalProcessOutOfMemory("CALL_AND_RETRY_1"); \
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000298 } \
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000299 if (!__object__->IsRetryAfterGC()) RETURN_EMPTY; \
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000300 Counters::gc_last_resort_from_handles.Increment(); \
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000301 Heap::CollectAllGarbage(false); \
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000302 { \
303 AlwaysAllocateScope __scope__; \
304 __object__ = FUNCTION_CALL; \
305 } \
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000306 if (!__object__->IsFailure()) RETURN_VALUE; \
ager@chromium.org9085a012009-05-11 19:22:57 +0000307 if (__object__->IsOutOfMemoryFailure() || \
308 __object__->IsRetryAfterGC()) { \
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000309 /* TODO(1181417): Fix this. */ \
kasperl@chromium.org58870952008-10-30 14:34:19 +0000310 v8::internal::V8::FatalProcessOutOfMemory("CALL_AND_RETRY_2"); \
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000311 } \
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000312 RETURN_EMPTY; \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000313 } while (false)
314
315
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000316#define CALL_HEAP_FUNCTION(FUNCTION_CALL, TYPE) \
317 CALL_AND_RETRY(FUNCTION_CALL, \
318 return Handle<TYPE>(TYPE::cast(__object__)), \
319 return Handle<TYPE>())
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000320
321
322#define CALL_HEAP_FUNCTION_VOID(FUNCTION_CALL) \
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000323 CALL_AND_RETRY(FUNCTION_CALL, return, return)
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000324
325
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000326#ifdef DEBUG
327
328inline bool Heap::allow_allocation(bool new_state) {
329 bool old = allocation_allowed_;
330 allocation_allowed_ = new_state;
331 return old;
332}
333
334#endif
335
336
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000337void ExternalStringTable::AddString(String* string) {
338 ASSERT(string->IsExternalString());
339 if (Heap::InNewSpace(string)) {
340 new_space_strings_.Add(string);
341 } else {
342 old_space_strings_.Add(string);
343 }
344}
345
346
347void ExternalStringTable::Iterate(ObjectVisitor* v) {
348 if (!new_space_strings_.is_empty()) {
349 Object** start = &new_space_strings_[0];
350 v->VisitPointers(start, start + new_space_strings_.length());
351 }
352 if (!old_space_strings_.is_empty()) {
353 Object** start = &old_space_strings_[0];
354 v->VisitPointers(start, start + old_space_strings_.length());
355 }
356}
357
358
359// Verify() is inline to avoid ifdef-s around its calls in release
360// mode.
361void ExternalStringTable::Verify() {
362#ifdef DEBUG
363 for (int i = 0; i < new_space_strings_.length(); ++i) {
364 ASSERT(Heap::InNewSpace(new_space_strings_[i]));
365 ASSERT(new_space_strings_[i] != Heap::raw_unchecked_null_value());
366 }
367 for (int i = 0; i < old_space_strings_.length(); ++i) {
368 ASSERT(!Heap::InNewSpace(old_space_strings_[i]));
369 ASSERT(old_space_strings_[i] != Heap::raw_unchecked_null_value());
370 }
371#endif
372}
373
374
375void ExternalStringTable::AddOldString(String* string) {
376 ASSERT(string->IsExternalString());
377 ASSERT(!Heap::InNewSpace(string));
378 old_space_strings_.Add(string);
379}
380
381
382void ExternalStringTable::ShrinkNewStrings(int position) {
383 new_space_strings_.Rewind(position);
384 Verify();
385}
386
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000387} } // namespace v8::internal
388
389#endif // V8_HEAP_INL_H_