blob: 49460cc0716db8937ecbc556fcbcfcb2f4f5c737 [file] [log] [blame]
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001// Copyright 2006-2008 Google Inc. All Rights Reserved.
2// 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
36#ifdef DEBUG
37DECLARE_bool(gc_greedy);
38DECLARE_int(gc_interval);
39#endif
40
41
42int Heap::MaxHeapObjectSize() {
43 return Page::kMaxHeapObjectSize;
44}
45
46
47Object* Heap::AllocateRaw(int size_in_bytes, AllocationSpace space) {
48 ASSERT(allocation_allowed_ && gc_state_ == NOT_IN_GC);
49#ifdef DEBUG
50 if (FLAG_gc_interval >= 0 &&
51 !disallow_allocation_failure_ &&
52 Heap::allocation_timeout_-- <= 0) {
53 return Failure::RetryAfterGC(size_in_bytes, space);
54 }
55 Counters::objs_since_last_full.Increment();
56 Counters::objs_since_last_young.Increment();
57#endif
58 if (NEW_SPACE == space) {
59 return new_space_->AllocateRaw(size_in_bytes);
60 }
61
62 Object* result;
63 if (OLD_SPACE == space) {
64 result = old_space_->AllocateRaw(size_in_bytes);
65 } else if (CODE_SPACE == space) {
66 result = code_space_->AllocateRaw(size_in_bytes);
67 } else if (LO_SPACE == space) {
68 result = lo_space_->AllocateRaw(size_in_bytes);
69 } else {
70 ASSERT(MAP_SPACE == space);
71 result = map_space_->AllocateRaw(size_in_bytes);
72 }
73 if (result->IsFailure()) old_gen_exhausted_ = true;
74 return result;
75}
76
77
kasper.lund7276f142008-07-30 08:49:36 +000078Object* Heap::AllocateForDeserialization(int size_in_bytes,
79 AllocationSpace space) {
80 ASSERT(allocation_allowed_ && gc_state_ == NOT_IN_GC);
81 PagedSpace* where;
82
83 switch (space) {
84 case NEW_SPACE:
85 return new_space_->AllocateRaw(size_in_bytes);
86 case LO_SPACE:
87 return lo_space_->AllocateRaw(size_in_bytes);
88 case OLD_SPACE:
89 where = old_space_;
90 break;
91 case CODE_SPACE:
92 where = code_space_;
93 break;
94 case MAP_SPACE:
95 where = map_space_;
96 break;
97 }
98
99 // Only paged spaces fall through.
100 return where->AllocateForDeserialization(size_in_bytes);
101}
102
103
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000104Object* Heap::NumberFromInt32(int32_t value) {
105 if (Smi::IsValid(value)) return Smi::FromInt(value);
106 // Bypass NumberFromDouble to avoid various redundant checks.
107 return AllocateHeapNumber(FastI2D(value));
108}
109
110
111Object* Heap::NumberFromUint32(uint32_t value) {
112 if ((int32_t)value >= 0 && Smi::IsValid((int32_t)value)) {
113 return Smi::FromInt((int32_t)value);
114 }
115 // Bypass NumberFromDouble to avoid various redundant checks.
116 return AllocateHeapNumber(FastUI2D(value));
117}
118
119
120Object* Heap::AllocateRawMap(int size_in_bytes) {
121#ifdef DEBUG
122 Counters::objs_since_last_full.Increment();
123 Counters::objs_since_last_young.Increment();
124#endif
125 Object* result = map_space_->AllocateRaw(size_in_bytes);
126 if (result->IsFailure()) old_gen_exhausted_ = true;
127 return result;
128}
129
130
131bool Heap::InNewSpace(Object* object) {
132 return new_space_->Contains(object);
133}
134
135
136bool Heap::InFromSpace(Object* object) {
137 return new_space_->FromSpaceContains(object);
138}
139
140
141bool Heap::InToSpace(Object* object) {
142 return new_space_->ToSpaceContains(object);
143}
144
145
146bool Heap::ShouldBePromoted(Address old_address, int object_size) {
147 // An object should be promoted if:
148 // - the object has survived a scavenge operation or
149 // - to space is already 25% full.
150 return old_address < new_space_->age_mark()
151 || (new_space_->Size() + object_size) >= (new_space_->Capacity() >> 2);
152}
153
154
155void Heap::RecordWrite(Address address, int offset) {
156 if (new_space_->Contains(address)) return;
157 ASSERT(!new_space_->FromSpaceContains(address));
158 SLOW_ASSERT(Contains(address + offset));
159 Page::SetRSet(address, offset);
160}
161
162
163Object* Heap::AllocatePropertyStorageForMap(Map* map) {
164 if (map->unused_property_fields() > 0) {
165 return AllocateFixedArray(map->unused_property_fields());
166 }
167 return Heap::empty_fixed_array();
168}
169
170
kasper.lund7276f142008-07-30 08:49:36 +0000171AllocationSpace Heap::TargetSpace(HeapObject* object) {
172 // Heap numbers and sequential strings are promoted to code space, all
173 // other object types are promoted to old space. We do not use
174 // object->IsHeapNumber() and object->IsSeqString() because we already
175 // know that object has the heap object tag.
176 InstanceType type = object->map()->instance_type();
177 ASSERT((type != CODE_TYPE) && (type != MAP_TYPE));
178 bool has_pointers =
179 type != HEAP_NUMBER_TYPE &&
180 (type >= FIRST_NONSTRING_TYPE ||
181 String::cast(object)->representation_tag() != kSeqStringTag);
182 return has_pointers ? OLD_SPACE : CODE_SPACE;
183}
184
185
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000186#define GC_GREEDY_CHECK() \
187 ASSERT(!FLAG_gc_greedy \
188 || v8::internal::Heap::disallow_allocation_failure() \
189 || v8::internal::Heap::CollectGarbage(0, NEW_SPACE))
190
191
192// Do not use the identifier __object__ in a call to this macro.
193//
194// Call the function FUNCTION_CALL. If it fails with a RetryAfterGC
195// failure, call the garbage collector and retry the function. If the
196// garbage collector cannot reclaim the required space or the second
197// call fails with a RetryAfterGC failure, fail with out of memory.
198// If there is any other failure, return a null handle. If either
199// call succeeds, return a handle to the functions return value.
200//
201// Note that this macro always returns or raises a fatal error.
202#define CALL_HEAP_FUNCTION(FUNCTION_CALL, TYPE) \
203 do { \
204 GC_GREEDY_CHECK(); \
205 Object* __object__ = FUNCTION_CALL; \
206 if (__object__->IsFailure()) { \
207 if (__object__->IsRetryAfterGC()) { \
208 if (!Heap::CollectGarbage( \
209 Failure::cast(__object__)->requested(), \
210 Failure::cast(__object__)->allocation_space())) { \
211 /* TODO(1181417): Fix this. */ \
212 v8::internal::V8::FatalProcessOutOfMemory("CALL_HEAP_FUNCTION"); \
213 } \
214 __object__ = FUNCTION_CALL; \
215 if (__object__->IsFailure()) { \
216 if (__object__->IsRetryAfterGC()) { \
217 /* TODO(1181417): Fix this. */ \
218 v8::internal::V8::FatalProcessOutOfMemory("CALL_HEAP_FUNCTION"); \
219 } \
220 return Handle<TYPE>(); \
221 } \
222 } else { \
223 return Handle<TYPE>(); \
224 } \
225 } \
226 return Handle<TYPE>(TYPE::cast(__object__)); \
227 } while (false)
228
229
230#ifdef DEBUG
231
232inline bool Heap::allow_allocation(bool new_state) {
233 bool old = allocation_allowed_;
234 allocation_allowed_ = new_state;
235 return old;
236}
237
238#endif
239
240
241} } // namespace v8::internal
242
243#endif // V8_HEAP_INL_H_