blob: 8dd09d77d63332070b15e0fe51d78853a9bb7ca7 [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
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000037int Heap::MaxHeapObjectSize() {
38 return Page::kMaxHeapObjectSize;
39}
40
41
ager@chromium.orga74f0da2008-12-03 16:05:52 +000042Object* Heap::AllocateSymbol(Vector<const char> str,
43 int chars,
44 uint32_t length_field) {
ager@chromium.orga74f0da2008-12-03 16:05:52 +000045 unibrow::Utf8InputBuffer<> buffer(str.start(),
46 static_cast<unsigned>(str.length()));
47 return AllocateInternalSymbol(&buffer, chars, length_field);
48}
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);
85 } else {
86 ASSERT(MAP_SPACE == space);
87 result = map_space_->AllocateRaw(size_in_bytes);
88 }
89 if (result->IsFailure()) old_gen_exhausted_ = true;
90 return result;
91}
92
93
94Object* Heap::NumberFromInt32(int32_t value) {
95 if (Smi::IsValid(value)) return Smi::FromInt(value);
96 // Bypass NumberFromDouble to avoid various redundant checks.
97 return AllocateHeapNumber(FastI2D(value));
98}
99
100
101Object* Heap::NumberFromUint32(uint32_t value) {
102 if ((int32_t)value >= 0 && Smi::IsValid((int32_t)value)) {
103 return Smi::FromInt((int32_t)value);
104 }
105 // Bypass NumberFromDouble to avoid various redundant checks.
106 return AllocateHeapNumber(FastUI2D(value));
107}
108
109
110Object* Heap::AllocateRawMap(int size_in_bytes) {
111#ifdef DEBUG
112 Counters::objs_since_last_full.Increment();
113 Counters::objs_since_last_young.Increment();
114#endif
115 Object* result = map_space_->AllocateRaw(size_in_bytes);
116 if (result->IsFailure()) old_gen_exhausted_ = true;
117 return result;
118}
119
120
121bool Heap::InNewSpace(Object* object) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000122 return new_space_.Contains(object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000123}
124
125
126bool Heap::InFromSpace(Object* object) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000127 return new_space_.FromSpaceContains(object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000128}
129
130
131bool Heap::InToSpace(Object* object) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000132 return new_space_.ToSpaceContains(object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000133}
134
135
136bool Heap::ShouldBePromoted(Address old_address, int object_size) {
137 // An object should be promoted if:
138 // - the object has survived a scavenge operation or
139 // - to space is already 25% full.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000140 return old_address < new_space_.age_mark()
141 || (new_space_.Size() + object_size) >= (new_space_.Capacity() >> 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000142}
143
144
145void Heap::RecordWrite(Address address, int offset) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000146 if (new_space_.Contains(address)) return;
147 ASSERT(!new_space_.FromSpaceContains(address));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000148 SLOW_ASSERT(Contains(address + offset));
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000149#ifndef V8_HOST_ARCH_64_BIT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000150 Page::SetRSet(address, offset);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000151#endif // V8_HOST_ARCH_64_BIT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000152}
153
154
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000155OldSpace* Heap::TargetSpace(HeapObject* object) {
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000156 InstanceType type = object->map()->instance_type();
157 AllocationSpace space = TargetSpaceId(type);
158 return (space == OLD_POINTER_SPACE)
159 ? old_pointer_space_
160 : old_data_space_;
161}
162
163
164AllocationSpace Heap::TargetSpaceId(InstanceType type) {
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000165 // Heap numbers and sequential strings are promoted to old data space, all
166 // other object types are promoted to old pointer space. We do not use
kasper.lund7276f142008-07-30 08:49:36 +0000167 // object->IsHeapNumber() and object->IsSeqString() because we already
168 // know that object has the heap object tag.
kasper.lund7276f142008-07-30 08:49:36 +0000169 ASSERT((type != CODE_TYPE) && (type != MAP_TYPE));
170 bool has_pointers =
171 type != HEAP_NUMBER_TYPE &&
172 (type >= FIRST_NONSTRING_TYPE ||
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000173 (type & kStringRepresentationMask) != kSeqStringTag);
174 return has_pointers ? OLD_POINTER_SPACE : OLD_DATA_SPACE;
kasper.lund7276f142008-07-30 08:49:36 +0000175}
176
177
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000178void Heap::CopyBlock(Object** dst, Object** src, int byte_size) {
179 ASSERT(IsAligned(byte_size, kPointerSize));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000180
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000181 // Use block copying memcpy if the segment we're copying is
182 // enough to justify the extra call/setup overhead.
183 static const int kBlockCopyLimit = 16 * kPointerSize;
184
185 if (byte_size >= kBlockCopyLimit) {
186 memcpy(dst, src, byte_size);
187 } else {
188 int remaining = byte_size / kPointerSize;
189 do {
190 remaining--;
191 *dst++ = *src++;
192 } while (remaining > 0);
193 }
194}
195
196
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000197void Heap::ScavengeObject(HeapObject** p, HeapObject* object) {
198 ASSERT(InFromSpace(object));
199
200 // We use the first word (where the map pointer usually is) of a heap
201 // object to record the forwarding pointer. A forwarding pointer can
202 // point to an old space, the code space, or the to space of the new
203 // generation.
204 MapWord first_word = object->map_word();
205
206 // If the first word is a forwarding address, the object has already been
207 // copied.
208 if (first_word.IsForwardingAddress()) {
209 *p = first_word.ToForwardingAddress();
210 return;
211 }
212
213 // Call the slow part of scavenge object.
214 return ScavengeObjectSlow(p, object);
215}
216
217
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000218Object* Heap::GetKeyedLookupCache() {
219 if (keyed_lookup_cache()->IsUndefined()) {
220 Object* obj = LookupCache::Allocate(4);
221 if (obj->IsFailure()) return obj;
222 keyed_lookup_cache_ = obj;
223 }
224 return keyed_lookup_cache();
225}
226
227
228void Heap::SetKeyedLookupCache(LookupCache* cache) {
229 keyed_lookup_cache_ = cache;
230}
231
232
233void Heap::ClearKeyedLookupCache() {
234 keyed_lookup_cache_ = undefined_value();
235}
236
237
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000238void Heap::SetLastScriptId(Object* last_script_id) {
239 last_script_id_ = last_script_id;
240}
241
242
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000243#define GC_GREEDY_CHECK() \
244 ASSERT(!FLAG_gc_greedy || v8::internal::Heap::GarbageCollectionGreedyCheck())
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000245
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000246
247// Calls the FUNCTION_CALL function and retries it up to three times
248// to guarantee that any allocations performed during the call will
249// succeed if there's enough memory.
250
251// Warning: Do not use the identifiers __object__ or __scope__ in a
252// call to this macro.
253
254#define CALL_AND_RETRY(FUNCTION_CALL, RETURN_VALUE, RETURN_EMPTY) \
255 do { \
256 GC_GREEDY_CHECK(); \
257 Object* __object__ = FUNCTION_CALL; \
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000258 if (!__object__->IsFailure()) RETURN_VALUE; \
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000259 if (__object__->IsOutOfMemoryFailure()) { \
260 v8::internal::V8::FatalProcessOutOfMemory("CALL_AND_RETRY_0"); \
261 } \
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000262 if (!__object__->IsRetryAfterGC()) RETURN_EMPTY; \
kasperl@chromium.org58870952008-10-30 14:34:19 +0000263 Heap::CollectGarbage(Failure::cast(__object__)->requested(), \
264 Failure::cast(__object__)->allocation_space()); \
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000265 __object__ = FUNCTION_CALL; \
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000266 if (!__object__->IsFailure()) RETURN_VALUE; \
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000267 if (__object__->IsOutOfMemoryFailure()) { \
kasperl@chromium.org58870952008-10-30 14:34:19 +0000268 v8::internal::V8::FatalProcessOutOfMemory("CALL_AND_RETRY_1"); \
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000269 } \
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000270 if (!__object__->IsRetryAfterGC()) RETURN_EMPTY; \
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000271 Counters::gc_last_resort_from_handles.Increment(); \
272 Heap::CollectAllGarbage(); \
273 { \
274 AlwaysAllocateScope __scope__; \
275 __object__ = FUNCTION_CALL; \
276 } \
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000277 if (!__object__->IsFailure()) RETURN_VALUE; \
ager@chromium.org9085a012009-05-11 19:22:57 +0000278 if (__object__->IsOutOfMemoryFailure() || \
279 __object__->IsRetryAfterGC()) { \
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000280 /* TODO(1181417): Fix this. */ \
kasperl@chromium.org58870952008-10-30 14:34:19 +0000281 v8::internal::V8::FatalProcessOutOfMemory("CALL_AND_RETRY_2"); \
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000282 } \
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000283 RETURN_EMPTY; \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000284 } while (false)
285
286
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000287#define CALL_HEAP_FUNCTION(FUNCTION_CALL, TYPE) \
288 CALL_AND_RETRY(FUNCTION_CALL, \
289 return Handle<TYPE>(TYPE::cast(__object__)), \
290 return Handle<TYPE>())
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000291
292
293#define CALL_HEAP_FUNCTION_VOID(FUNCTION_CALL) \
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000294 CALL_AND_RETRY(FUNCTION_CALL, return, return)
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000295
296
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000297#ifdef DEBUG
298
299inline bool Heap::allow_allocation(bool new_state) {
300 bool old = allocation_allowed_;
301 allocation_allowed_ = new_state;
302 return old;
303}
304
305#endif
306
307
308} } // namespace v8::internal
309
310#endif // V8_HEAP_INL_H_