blob: b54ef3acd3a87b96a0942776baf3e7be01509114 [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
34namespace v8 { namespace internal {
35
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000036int Heap::MaxHeapObjectSize() {
37 return Page::kMaxHeapObjectSize;
38}
39
40
ager@chromium.org9258b6b2008-09-11 09:11:10 +000041Object* Heap::AllocateRaw(int size_in_bytes,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000042 AllocationSpace space,
43 AllocationSpace retry_space) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000044 ASSERT(allocation_allowed_ && gc_state_ == NOT_IN_GC);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000045 ASSERT(space != NEW_SPACE ||
46 retry_space == OLD_POINTER_SPACE ||
47 retry_space == OLD_DATA_SPACE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000048#ifdef DEBUG
49 if (FLAG_gc_interval >= 0 &&
50 !disallow_allocation_failure_ &&
51 Heap::allocation_timeout_-- <= 0) {
52 return Failure::RetryAfterGC(size_in_bytes, space);
53 }
54 Counters::objs_since_last_full.Increment();
55 Counters::objs_since_last_young.Increment();
56#endif
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000057 Object* result;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000058 if (NEW_SPACE == space) {
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000059 result = new_space_.AllocateRaw(size_in_bytes);
60 if (always_allocate() && result->IsFailure()) {
61 space = retry_space;
62 } else {
63 return result;
64 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000065 }
66
ager@chromium.org9258b6b2008-09-11 09:11:10 +000067 if (OLD_POINTER_SPACE == space) {
68 result = old_pointer_space_->AllocateRaw(size_in_bytes);
69 } else if (OLD_DATA_SPACE == space) {
70 result = old_data_space_->AllocateRaw(size_in_bytes);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000071 } else if (CODE_SPACE == space) {
72 result = code_space_->AllocateRaw(size_in_bytes);
73 } else if (LO_SPACE == space) {
74 result = lo_space_->AllocateRaw(size_in_bytes);
75 } else {
76 ASSERT(MAP_SPACE == space);
77 result = map_space_->AllocateRaw(size_in_bytes);
78 }
79 if (result->IsFailure()) old_gen_exhausted_ = true;
80 return result;
81}
82
83
84Object* Heap::NumberFromInt32(int32_t value) {
85 if (Smi::IsValid(value)) return Smi::FromInt(value);
86 // Bypass NumberFromDouble to avoid various redundant checks.
87 return AllocateHeapNumber(FastI2D(value));
88}
89
90
91Object* Heap::NumberFromUint32(uint32_t value) {
92 if ((int32_t)value >= 0 && Smi::IsValid((int32_t)value)) {
93 return Smi::FromInt((int32_t)value);
94 }
95 // Bypass NumberFromDouble to avoid various redundant checks.
96 return AllocateHeapNumber(FastUI2D(value));
97}
98
99
100Object* Heap::AllocateRawMap(int size_in_bytes) {
101#ifdef DEBUG
102 Counters::objs_since_last_full.Increment();
103 Counters::objs_since_last_young.Increment();
104#endif
105 Object* result = map_space_->AllocateRaw(size_in_bytes);
106 if (result->IsFailure()) old_gen_exhausted_ = true;
107 return result;
108}
109
110
111bool Heap::InNewSpace(Object* object) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000112 return new_space_.Contains(object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000113}
114
115
116bool Heap::InFromSpace(Object* object) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000117 return new_space_.FromSpaceContains(object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000118}
119
120
121bool Heap::InToSpace(Object* object) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000122 return new_space_.ToSpaceContains(object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000123}
124
125
126bool Heap::ShouldBePromoted(Address old_address, int object_size) {
127 // An object should be promoted if:
128 // - the object has survived a scavenge operation or
129 // - to space is already 25% full.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000130 return old_address < new_space_.age_mark()
131 || (new_space_.Size() + object_size) >= (new_space_.Capacity() >> 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000132}
133
134
135void Heap::RecordWrite(Address address, int offset) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000136 if (new_space_.Contains(address)) return;
137 ASSERT(!new_space_.FromSpaceContains(address));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000138 SLOW_ASSERT(Contains(address + offset));
139 Page::SetRSet(address, offset);
140}
141
142
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000143OldSpace* Heap::TargetSpace(HeapObject* object) {
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000144 InstanceType type = object->map()->instance_type();
145 AllocationSpace space = TargetSpaceId(type);
146 return (space == OLD_POINTER_SPACE)
147 ? old_pointer_space_
148 : old_data_space_;
149}
150
151
152AllocationSpace Heap::TargetSpaceId(InstanceType type) {
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000153 // Heap numbers and sequential strings are promoted to old data space, all
154 // other object types are promoted to old pointer space. We do not use
kasper.lund7276f142008-07-30 08:49:36 +0000155 // object->IsHeapNumber() and object->IsSeqString() because we already
156 // know that object has the heap object tag.
kasper.lund7276f142008-07-30 08:49:36 +0000157 ASSERT((type != CODE_TYPE) && (type != MAP_TYPE));
158 bool has_pointers =
159 type != HEAP_NUMBER_TYPE &&
160 (type >= FIRST_NONSTRING_TYPE ||
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000161 (type & kStringRepresentationMask) != kSeqStringTag);
162 return has_pointers ? OLD_POINTER_SPACE : OLD_DATA_SPACE;
kasper.lund7276f142008-07-30 08:49:36 +0000163}
164
165
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000166void Heap::CopyBlock(Object** dst, Object** src, int byte_size) {
167 ASSERT(IsAligned(byte_size, kPointerSize));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000168
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000169 // Use block copying memcpy if the segment we're copying is
170 // enough to justify the extra call/setup overhead.
171 static const int kBlockCopyLimit = 16 * kPointerSize;
172
173 if (byte_size >= kBlockCopyLimit) {
174 memcpy(dst, src, byte_size);
175 } else {
176 int remaining = byte_size / kPointerSize;
177 do {
178 remaining--;
179 *dst++ = *src++;
180 } while (remaining > 0);
181 }
182}
183
184
185Object* Heap::GetKeyedLookupCache() {
186 if (keyed_lookup_cache()->IsUndefined()) {
187 Object* obj = LookupCache::Allocate(4);
188 if (obj->IsFailure()) return obj;
189 keyed_lookup_cache_ = obj;
190 }
191 return keyed_lookup_cache();
192}
193
194
195void Heap::SetKeyedLookupCache(LookupCache* cache) {
196 keyed_lookup_cache_ = cache;
197}
198
199
200void Heap::ClearKeyedLookupCache() {
201 keyed_lookup_cache_ = undefined_value();
202}
203
204
205#define GC_GREEDY_CHECK() \
206 ASSERT(!FLAG_gc_greedy || v8::internal::Heap::GarbageCollectionGreedyCheck())
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000207
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000208
209// Calls the FUNCTION_CALL function and retries it up to three times
210// to guarantee that any allocations performed during the call will
211// succeed if there's enough memory.
212
213// Warning: Do not use the identifiers __object__ or __scope__ in a
214// call to this macro.
215
216#define CALL_AND_RETRY(FUNCTION_CALL, RETURN_VALUE, RETURN_EMPTY) \
217 do { \
218 GC_GREEDY_CHECK(); \
219 Object* __object__ = FUNCTION_CALL; \
220 if (!__object__->IsFailure()) return RETURN_VALUE; \
221 if (__object__->IsOutOfMemoryFailure()) { \
222 v8::internal::V8::FatalProcessOutOfMemory("CALL_AND_RETRY_0"); \
223 } \
224 if (!__object__->IsRetryAfterGC()) return RETURN_EMPTY; \
225 if (!Heap::CollectGarbage( \
226 Failure::cast(__object__)->requested(), \
227 Failure::cast(__object__)->allocation_space())) { \
228 v8::internal::V8::FatalProcessOutOfMemory("CALL_AND_RETRY_1"); \
229 return RETURN_EMPTY; \
230 } \
231 __object__ = FUNCTION_CALL; \
232 if (!__object__->IsFailure()) return RETURN_VALUE; \
233 if (__object__->IsOutOfMemoryFailure()) { \
234 v8::internal::V8::FatalProcessOutOfMemory("CALL_AND_RETRY_2"); \
235 } \
236 if (!__object__->IsRetryAfterGC()) return RETURN_EMPTY; \
237 Counters::gc_last_resort_from_handles.Increment(); \
238 Heap::CollectAllGarbage(); \
239 { \
240 AlwaysAllocateScope __scope__; \
241 __object__ = FUNCTION_CALL; \
242 } \
243 if (!__object__->IsFailure()) return RETURN_VALUE; \
244 if (__object__->IsOutOfMemoryFailure()) { \
245 /* TODO(1181417): Fix this. */ \
246 v8::internal::V8::FatalProcessOutOfMemory("CALL_AND_RETRY_3"); \
247 } \
248 ASSERT(!__object__->IsRetryAfterGC()); \
249 return RETURN_EMPTY; \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000250 } while (false)
251
252
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000253#define CALL_HEAP_FUNCTION(FUNCTION_CALL, TYPE) \
254 CALL_AND_RETRY(FUNCTION_CALL, \
255 Handle<TYPE>(TYPE::cast(__object__)), \
256 Handle<TYPE>())
257
258
259#define CALL_HEAP_FUNCTION_VOID(FUNCTION_CALL) \
260 CALL_AND_RETRY(FUNCTION_CALL, , )
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000261
262
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000263#ifdef DEBUG
264
265inline bool Heap::allow_allocation(bool new_state) {
266 bool old = allocation_allowed_;
267 allocation_allowed_ = new_state;
268 return old;
269}
270
271#endif
272
273
274} } // namespace v8::internal
275
276#endif // V8_HEAP_INL_H_