ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 1 | // Copyright 2006-2008 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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_MARK_COMPACT_H_ |
| 29 | #define V8_MARK_COMPACT_H_ |
| 30 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 31 | namespace v8 { |
| 32 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 33 | |
| 34 | // Callback function, returns whether an object is alive. The heap size |
| 35 | // of the object is returned in size. It optionally updates the offset |
| 36 | // to the first live object in the page (only used for old and map objects). |
| 37 | typedef bool (*IsAliveFunction)(HeapObject* obj, int* size, int* offset); |
| 38 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 39 | // Forward declarations. |
| 40 | class RootMarkingVisitor; |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 41 | class MarkingVisitor; |
| 42 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 43 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 44 | // ------------------------------------------------------------------------- |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 45 | // Mark-Compact collector |
| 46 | // |
| 47 | // All methods are static. |
| 48 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 49 | class MarkCompactCollector: public AllStatic { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 50 | public: |
| 51 | // Type of functions to compute forwarding addresses of objects in |
| 52 | // compacted spaces. Given an object and its size, return a (non-failure) |
| 53 | // Object* that will be the object after forwarding. There is a separate |
| 54 | // allocation function for each (compactable) space based on the location |
| 55 | // of the object before compaction. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 56 | typedef MaybeObject* (*AllocationFunction)(HeapObject* object, |
| 57 | int object_size); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 58 | |
| 59 | // Type of functions to encode the forwarding address for an object. |
| 60 | // Given the object, its size, and the new (non-failure) object it will be |
| 61 | // forwarded to, encode the forwarding address. For paged spaces, the |
| 62 | // 'offset' input/output parameter contains the offset of the forwarded |
| 63 | // object from the forwarding address of the previous live object in the |
| 64 | // page as input, and is updated to contain the offset to be used for the |
| 65 | // next live object in the same page. For spaces using a different |
| 66 | // encoding (ie, contiguous spaces), the offset parameter is ignored. |
| 67 | typedef void (*EncodingFunction)(HeapObject* old_object, |
| 68 | int object_size, |
| 69 | Object* new_object, |
| 70 | int* offset); |
| 71 | |
| 72 | // Type of functions to process non-live objects. |
| 73 | typedef void (*ProcessNonLiveFunction)(HeapObject* object); |
| 74 | |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 75 | // Set the global force_compaction flag, it must be called before Prepare |
| 76 | // to take effect. |
| 77 | static void SetForceCompaction(bool value) { |
| 78 | force_compaction_ = value; |
| 79 | } |
| 80 | |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 81 | |
| 82 | static void Initialize(); |
| 83 | |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 84 | // Prepares for GC by resetting relocation info in old and map spaces and |
| 85 | // choosing spaces to compact. |
| 86 | static void Prepare(GCTracer* tracer); |
| 87 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 88 | // Performs a global garbage collection. |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 89 | static void CollectGarbage(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 90 | |
| 91 | // True if the last full GC performed heap compaction. |
| 92 | static bool HasCompacted() { return compacting_collection_; } |
| 93 | |
| 94 | // True after the Prepare phase if the compaction is taking place. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 95 | static bool IsCompacting() { |
| 96 | #ifdef DEBUG |
| 97 | // For the purposes of asserts we don't want this to keep returning true |
| 98 | // after the collection is completed. |
| 99 | return state_ != IDLE && compacting_collection_; |
| 100 | #else |
| 101 | return compacting_collection_; |
| 102 | #endif |
| 103 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 104 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 105 | // The count of the number of objects left marked at the end of the last |
| 106 | // completed full GC (expected to be zero). |
| 107 | static int previous_marked_count() { return previous_marked_count_; } |
| 108 | |
| 109 | // During a full GC, there is a stack-allocated GCTracer that is used for |
| 110 | // bookkeeping information. Return a pointer to that tracer. |
| 111 | static GCTracer* tracer() { return tracer_; } |
| 112 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 113 | #ifdef DEBUG |
| 114 | // Checks whether performing mark-compact collection. |
| 115 | static bool in_use() { return state_ > PREPARE_GC; } |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 116 | static bool are_map_pointers_encoded() { return state_ == UPDATE_POINTERS; } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 117 | #endif |
| 118 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 119 | // Determine type of object and emit deletion log event. |
| 120 | static void ReportDeleteIfNeeded(HeapObject* obj); |
| 121 | |
erik.corry@gmail.com | 4a6c327 | 2010-11-18 12:04:40 +0000 | [diff] [blame] | 122 | // Returns size of a possibly marked object. |
| 123 | static int SizeOfMarkedObject(HeapObject* obj); |
| 124 | |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 125 | // Distinguishable invalid map encodings (for single word and multiple words) |
| 126 | // that indicate free regions. |
| 127 | static const uint32_t kSingleFreeEncoding = 0; |
| 128 | static const uint32_t kMultiFreeEncoding = 1; |
| 129 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 130 | private: |
| 131 | #ifdef DEBUG |
| 132 | enum CollectorState { |
| 133 | IDLE, |
| 134 | PREPARE_GC, |
| 135 | MARK_LIVE_OBJECTS, |
| 136 | SWEEP_SPACES, |
| 137 | ENCODE_FORWARDING_ADDRESSES, |
| 138 | UPDATE_POINTERS, |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 139 | RELOCATE_OBJECTS |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 140 | }; |
| 141 | |
| 142 | // The current stage of the collector. |
| 143 | static CollectorState state_; |
| 144 | #endif |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 145 | |
| 146 | // Global flag that forces a compaction. |
| 147 | static bool force_compaction_; |
| 148 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 149 | // Global flag indicating whether spaces were compacted on the last GC. |
| 150 | static bool compacting_collection_; |
| 151 | |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 152 | // Global flag indicating whether spaces will be compacted on the next GC. |
| 153 | static bool compact_on_next_gc_; |
| 154 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 155 | // The number of objects left marked at the end of the last completed full |
| 156 | // GC (expected to be zero). |
| 157 | static int previous_marked_count_; |
| 158 | |
| 159 | // A pointer to the current stack-allocated GC tracer object during a full |
| 160 | // collection (NULL before and after). |
| 161 | static GCTracer* tracer_; |
| 162 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 163 | // Finishes GC, performs heap verification if enabled. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 164 | static void Finish(); |
| 165 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 166 | // ----------------------------------------------------------------------- |
| 167 | // Phase 1: Marking live objects. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 168 | // |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 169 | // Before: The heap has been prepared for garbage collection by |
| 170 | // MarkCompactCollector::Prepare() and is otherwise in its |
| 171 | // normal state. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 172 | // |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 173 | // After: Live objects are marked and non-live objects are unmarked. |
| 174 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 175 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 176 | friend class RootMarkingVisitor; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 177 | friend class MarkingVisitor; |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 178 | friend class StaticMarkingVisitor; |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 179 | friend class CodeMarkingVisitor; |
| 180 | friend class SharedFunctionInfoMarkingVisitor; |
| 181 | |
| 182 | static void PrepareForCodeFlushing(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 183 | |
| 184 | // Marking operations for objects reachable from roots. |
| 185 | static void MarkLiveObjects(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 186 | |
| 187 | static void MarkUnmarkedObject(HeapObject* obj); |
| 188 | |
| 189 | static inline void MarkObject(HeapObject* obj) { |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 190 | if (!obj->IsMarked()) MarkUnmarkedObject(obj); |
| 191 | } |
| 192 | |
| 193 | static inline void SetMark(HeapObject* obj) { |
| 194 | tracer_->increment_marked_count(); |
| 195 | #ifdef DEBUG |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 196 | UpdateLiveObjectCount(obj); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 197 | #endif |
| 198 | obj->SetMark(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 199 | } |
| 200 | |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 201 | // Creates back pointers for all map transitions, stores them in |
| 202 | // the prototype field. The original prototype pointers are restored |
| 203 | // in ClearNonLiveTransitions(). All JSObject maps |
| 204 | // connected by map transitions have the same prototype object, which |
| 205 | // is why we can use this field temporarily for back pointers. |
| 206 | static void CreateBackPointers(); |
| 207 | |
| 208 | // Mark a Map and its DescriptorArray together, skipping transitions. |
| 209 | static void MarkMapContents(Map* map); |
| 210 | static void MarkDescriptorArray(DescriptorArray* descriptors); |
| 211 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 212 | // Mark the heap roots and all objects reachable from them. |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 213 | static void MarkRoots(RootMarkingVisitor* visitor); |
| 214 | |
| 215 | // Mark the symbol table specially. References to symbols from the |
| 216 | // symbol table are weak. |
| 217 | static void MarkSymbolTable(); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 218 | |
| 219 | // Mark objects in object groups that have at least one object in the |
| 220 | // group marked. |
| 221 | static void MarkObjectGroups(); |
| 222 | |
| 223 | // Mark all objects in an object group with at least one marked |
| 224 | // object, then all objects reachable from marked objects in object |
| 225 | // groups, and repeat. |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 226 | static void ProcessObjectGroups(); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 227 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 228 | // Mark objects reachable (transitively) from objects in the marking stack |
| 229 | // or overflowed in the heap. |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 230 | static void ProcessMarkingStack(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 231 | |
| 232 | // Mark objects reachable (transitively) from objects in the marking |
| 233 | // stack. This function empties the marking stack, but may leave |
| 234 | // overflowed objects in the heap, in which case the marking stack's |
| 235 | // overflow flag will be set. |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 236 | static void EmptyMarkingStack(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 237 | |
| 238 | // Refill the marking stack with overflowed objects from the heap. This |
| 239 | // function either leaves the marking stack full or clears the overflow |
| 240 | // flag on the marking stack. |
| 241 | static void RefillMarkingStack(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 242 | |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 243 | // Callback function for telling whether the object *p is an unmarked |
| 244 | // heap object. |
| 245 | static bool IsUnmarkedHeapObject(Object** p); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 246 | |
| 247 | #ifdef DEBUG |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 248 | static void UpdateLiveObjectCount(HeapObject* obj); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 249 | #endif |
| 250 | |
| 251 | // We sweep the large object space in the same way whether we are |
| 252 | // compacting or not, because the large object space is never compacted. |
| 253 | static void SweepLargeObjectSpace(); |
| 254 | |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 255 | // Test whether a (possibly marked) object is a Map. |
| 256 | static inline bool SafeIsMap(HeapObject* object); |
| 257 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 258 | // Map transitions from a live map to a dead map must be killed. |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 259 | // We replace them with a null descriptor, with the same key. |
| 260 | static void ClearNonLiveTransitions(); |
| 261 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 262 | // ----------------------------------------------------------------------- |
| 263 | // Phase 2: Sweeping to clear mark bits and free non-live objects for |
| 264 | // a non-compacting collection, or else computing and encoding |
| 265 | // forwarding addresses for a compacting collection. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 266 | // |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 267 | // Before: Live objects are marked and non-live objects are unmarked. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 268 | // |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 269 | // After: (Non-compacting collection.) Live objects are unmarked, |
| 270 | // non-live regions have been added to their space's free |
| 271 | // list. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 272 | // |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 273 | // After: (Compacting collection.) The forwarding address of live |
| 274 | // objects in the paged spaces is encoded in their map word |
| 275 | // along with their (non-forwarded) map pointer. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 276 | // |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 277 | // The forwarding address of live objects in the new space is |
| 278 | // written to their map word's offset in the inactive |
| 279 | // semispace. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 280 | // |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 281 | // Bookkeeping data is written to the page header of |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 282 | // eached paged-space page that contains live objects after |
| 283 | // compaction: |
| 284 | // |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 285 | // The allocation watermark field is used to track the |
| 286 | // relocation top address, the address of the first word |
| 287 | // after the end of the last live object in the page after |
| 288 | // compaction. |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 289 | // |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 290 | // The Page::mc_page_index field contains the zero-based index of the |
| 291 | // page in its space. This word is only used for map space pages, in |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 292 | // order to encode the map addresses in 21 bits to free 11 |
| 293 | // bits per map word for the forwarding address. |
| 294 | // |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 295 | // The Page::mc_first_forwarded field contains the (nonencoded) |
| 296 | // forwarding address of the first live object in the page. |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 297 | // |
| 298 | // In both the new space and the paged spaces, a linked list |
| 299 | // of live regions is constructructed (linked through |
| 300 | // pointers in the non-live region immediately following each |
| 301 | // live region) to speed further passes of the collector. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 302 | |
| 303 | // Encodes forwarding addresses of objects in compactable parts of the |
| 304 | // heap. |
| 305 | static void EncodeForwardingAddresses(); |
| 306 | |
| 307 | // Encodes the forwarding addresses of objects in new space. |
| 308 | static void EncodeForwardingAddressesInNewSpace(); |
| 309 | |
| 310 | // Function template to encode the forwarding addresses of objects in |
| 311 | // paged spaces, parameterized by allocation and non-live processing |
| 312 | // functions. |
| 313 | template<AllocationFunction Alloc, ProcessNonLiveFunction ProcessNonLive> |
| 314 | static void EncodeForwardingAddressesInPagedSpace(PagedSpace* space); |
| 315 | |
| 316 | // Iterates live objects in a space, passes live objects |
| 317 | // to a callback function which returns the heap size of the object. |
| 318 | // Returns the number of live objects iterated. |
| 319 | static int IterateLiveObjects(NewSpace* space, HeapObjectCallback size_f); |
| 320 | static int IterateLiveObjects(PagedSpace* space, HeapObjectCallback size_f); |
| 321 | |
| 322 | // Iterates the live objects between a range of addresses, returning the |
| 323 | // number of live objects. |
| 324 | static int IterateLiveObjectsInRange(Address start, Address end, |
| 325 | HeapObjectCallback size_func); |
| 326 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 327 | // If we are not compacting the heap, we simply sweep the spaces except |
| 328 | // for the large object space, clearing mark bits and adding unmarked |
| 329 | // regions to each space's free list. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 330 | static void SweepSpaces(); |
| 331 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 332 | // ----------------------------------------------------------------------- |
| 333 | // Phase 3: Updating pointers in live objects. |
| 334 | // |
| 335 | // Before: Same as after phase 2 (compacting collection). |
| 336 | // |
| 337 | // After: All pointers in live objects, including encoded map |
| 338 | // pointers, are updated to point to their target's new |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 339 | // location. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 340 | |
| 341 | friend class UpdatingVisitor; // helper for updating visited objects |
| 342 | |
| 343 | // Updates pointers in all spaces. |
| 344 | static void UpdatePointers(); |
| 345 | |
| 346 | // Updates pointers in an object in new space. |
| 347 | // Returns the heap size of the object. |
| 348 | static int UpdatePointersInNewObject(HeapObject* obj); |
| 349 | |
| 350 | // Updates pointers in an object in old spaces. |
| 351 | // Returns the heap size of the object. |
| 352 | static int UpdatePointersInOldObject(HeapObject* obj); |
| 353 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 354 | // Calculates the forwarding address of an object in an old space. |
| 355 | static Address GetForwardingAddressInOldSpace(HeapObject* obj); |
| 356 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 357 | // ----------------------------------------------------------------------- |
| 358 | // Phase 4: Relocating objects. |
| 359 | // |
| 360 | // Before: Pointers to live objects are updated to point to their |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 361 | // target's new location. |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 362 | // |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 363 | // After: Objects have been moved to their new addresses. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 364 | |
| 365 | // Relocates objects in all spaces. |
| 366 | static void RelocateObjects(); |
| 367 | |
| 368 | // Converts a code object's inline target to addresses, convention from |
| 369 | // address to target happens in the marking phase. |
| 370 | static int ConvertCodeICTargetToAddress(HeapObject* obj); |
| 371 | |
| 372 | // Relocate a map object. |
| 373 | static int RelocateMapObject(HeapObject* obj); |
| 374 | |
| 375 | // Relocates an old object. |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 376 | static int RelocateOldPointerObject(HeapObject* obj); |
| 377 | static int RelocateOldDataObject(HeapObject* obj); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 378 | |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 379 | // Relocate a property cell object. |
| 380 | static int RelocateCellObject(HeapObject* obj); |
| 381 | |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 382 | // Helper function. |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 383 | static inline int RelocateOldNonCodeObject(HeapObject* obj, |
| 384 | PagedSpace* space); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 385 | |
| 386 | // Relocates an object in the code space. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 387 | static int RelocateCodeObject(HeapObject* obj); |
| 388 | |
| 389 | // Copy a new object. |
| 390 | static int RelocateNewObject(HeapObject* obj); |
| 391 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 392 | #ifdef DEBUG |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 393 | // ----------------------------------------------------------------------- |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 394 | // Debugging variables, functions and classes |
| 395 | // Counters used for debugging the marking phase of mark-compact or |
| 396 | // mark-sweep collection. |
| 397 | |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 398 | // Size of live objects in Heap::to_space_. |
| 399 | static int live_young_objects_size_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 400 | |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 401 | // Size of live objects in Heap::old_pointer_space_. |
| 402 | static int live_old_pointer_objects_size_; |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 403 | |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 404 | // Size of live objects in Heap::old_data_space_. |
| 405 | static int live_old_data_objects_size_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 406 | |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 407 | // Size of live objects in Heap::code_space_. |
| 408 | static int live_code_objects_size_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 409 | |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 410 | // Size of live objects in Heap::map_space_. |
| 411 | static int live_map_objects_size_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 412 | |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 413 | // Size of live objects in Heap::cell_space_. |
| 414 | static int live_cell_objects_size_; |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 415 | |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 416 | // Size of live objects in Heap::lo_space_. |
| 417 | static int live_lo_objects_size_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 418 | |
| 419 | // Number of live bytes in this collection. |
| 420 | static int live_bytes_; |
| 421 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 422 | friend class MarkObjectVisitor; |
| 423 | static void VisitObject(HeapObject* obj); |
| 424 | |
| 425 | friend class UnmarkObjectVisitor; |
| 426 | static void UnmarkObject(HeapObject* obj); |
| 427 | #endif |
| 428 | }; |
| 429 | |
| 430 | |
| 431 | } } // namespace v8::internal |
| 432 | |
| 433 | #endif // V8_MARK_COMPACT_H_ |