blob: f4b1b27530927fa09c6f6282236c10a77cd32ba1 [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
78Object* Heap::NumberFromInt32(int32_t value) {
79 if (Smi::IsValid(value)) return Smi::FromInt(value);
80 // Bypass NumberFromDouble to avoid various redundant checks.
81 return AllocateHeapNumber(FastI2D(value));
82}
83
84
85Object* Heap::NumberFromUint32(uint32_t value) {
86 if ((int32_t)value >= 0 && Smi::IsValid((int32_t)value)) {
87 return Smi::FromInt((int32_t)value);
88 }
89 // Bypass NumberFromDouble to avoid various redundant checks.
90 return AllocateHeapNumber(FastUI2D(value));
91}
92
93
94Object* Heap::AllocateRawMap(int size_in_bytes) {
95#ifdef DEBUG
96 Counters::objs_since_last_full.Increment();
97 Counters::objs_since_last_young.Increment();
98#endif
99 Object* result = map_space_->AllocateRaw(size_in_bytes);
100 if (result->IsFailure()) old_gen_exhausted_ = true;
101 return result;
102}
103
104
105bool Heap::InNewSpace(Object* object) {
106 return new_space_->Contains(object);
107}
108
109
110bool Heap::InFromSpace(Object* object) {
111 return new_space_->FromSpaceContains(object);
112}
113
114
115bool Heap::InToSpace(Object* object) {
116 return new_space_->ToSpaceContains(object);
117}
118
119
120bool Heap::ShouldBePromoted(Address old_address, int object_size) {
121 // An object should be promoted if:
122 // - the object has survived a scavenge operation or
123 // - to space is already 25% full.
124 return old_address < new_space_->age_mark()
125 || (new_space_->Size() + object_size) >= (new_space_->Capacity() >> 2);
126}
127
128
129void Heap::RecordWrite(Address address, int offset) {
130 if (new_space_->Contains(address)) return;
131 ASSERT(!new_space_->FromSpaceContains(address));
132 SLOW_ASSERT(Contains(address + offset));
133 Page::SetRSet(address, offset);
134}
135
136
137Object* Heap::AllocatePropertyStorageForMap(Map* map) {
138 if (map->unused_property_fields() > 0) {
139 return AllocateFixedArray(map->unused_property_fields());
140 }
141 return Heap::empty_fixed_array();
142}
143
144
145#define GC_GREEDY_CHECK() \
146 ASSERT(!FLAG_gc_greedy \
147 || v8::internal::Heap::disallow_allocation_failure() \
148 || v8::internal::Heap::CollectGarbage(0, NEW_SPACE))
149
150
151// Do not use the identifier __object__ in a call to this macro.
152//
153// Call the function FUNCTION_CALL. If it fails with a RetryAfterGC
154// failure, call the garbage collector and retry the function. If the
155// garbage collector cannot reclaim the required space or the second
156// call fails with a RetryAfterGC failure, fail with out of memory.
157// If there is any other failure, return a null handle. If either
158// call succeeds, return a handle to the functions return value.
159//
160// Note that this macro always returns or raises a fatal error.
161#define CALL_HEAP_FUNCTION(FUNCTION_CALL, TYPE) \
162 do { \
163 GC_GREEDY_CHECK(); \
164 Object* __object__ = FUNCTION_CALL; \
165 if (__object__->IsFailure()) { \
166 if (__object__->IsRetryAfterGC()) { \
167 if (!Heap::CollectGarbage( \
168 Failure::cast(__object__)->requested(), \
169 Failure::cast(__object__)->allocation_space())) { \
170 /* TODO(1181417): Fix this. */ \
171 v8::internal::V8::FatalProcessOutOfMemory("CALL_HEAP_FUNCTION"); \
172 } \
173 __object__ = FUNCTION_CALL; \
174 if (__object__->IsFailure()) { \
175 if (__object__->IsRetryAfterGC()) { \
176 /* TODO(1181417): Fix this. */ \
177 v8::internal::V8::FatalProcessOutOfMemory("CALL_HEAP_FUNCTION"); \
178 } \
179 return Handle<TYPE>(); \
180 } \
181 } else { \
182 return Handle<TYPE>(); \
183 } \
184 } \
185 return Handle<TYPE>(TYPE::cast(__object__)); \
186 } while (false)
187
188
189#ifdef DEBUG
190
191inline bool Heap::allow_allocation(bool new_state) {
192 bool old = allocation_allowed_;
193 allocation_allowed_ = new_state;
194 return old;
195}
196
197#endif
198
199
200} } // namespace v8::internal
201
202#endif // V8_HEAP_INL_H_