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 | |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 122 | // Distinguishable invalid map encodings (for single word and multiple words) |
| 123 | // that indicate free regions. |
| 124 | static const uint32_t kSingleFreeEncoding = 0; |
| 125 | static const uint32_t kMultiFreeEncoding = 1; |
| 126 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 127 | private: |
| 128 | #ifdef DEBUG |
| 129 | enum CollectorState { |
| 130 | IDLE, |
| 131 | PREPARE_GC, |
| 132 | MARK_LIVE_OBJECTS, |
| 133 | SWEEP_SPACES, |
| 134 | ENCODE_FORWARDING_ADDRESSES, |
| 135 | UPDATE_POINTERS, |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 136 | RELOCATE_OBJECTS |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 137 | }; |
| 138 | |
| 139 | // The current stage of the collector. |
| 140 | static CollectorState state_; |
| 141 | #endif |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 142 | |
| 143 | // Global flag that forces a compaction. |
| 144 | static bool force_compaction_; |
| 145 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 146 | // Global flag indicating whether spaces were compacted on the last GC. |
| 147 | static bool compacting_collection_; |
| 148 | |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 149 | // Global flag indicating whether spaces will be compacted on the next GC. |
| 150 | static bool compact_on_next_gc_; |
| 151 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 152 | // The number of objects left marked at the end of the last completed full |
| 153 | // GC (expected to be zero). |
| 154 | static int previous_marked_count_; |
| 155 | |
| 156 | // A pointer to the current stack-allocated GC tracer object during a full |
| 157 | // collection (NULL before and after). |
| 158 | static GCTracer* tracer_; |
| 159 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 160 | // Finishes GC, performs heap verification if enabled. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 161 | static void Finish(); |
| 162 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 163 | // ----------------------------------------------------------------------- |
| 164 | // Phase 1: Marking live objects. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 165 | // |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 166 | // Before: The heap has been prepared for garbage collection by |
| 167 | // MarkCompactCollector::Prepare() and is otherwise in its |
| 168 | // normal state. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 169 | // |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 170 | // After: Live objects are marked and non-live objects are unmarked. |
| 171 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 172 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 173 | friend class RootMarkingVisitor; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 174 | friend class MarkingVisitor; |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 175 | friend class StaticMarkingVisitor; |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 176 | friend class CodeMarkingVisitor; |
| 177 | friend class SharedFunctionInfoMarkingVisitor; |
| 178 | |
| 179 | static void PrepareForCodeFlushing(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 180 | |
| 181 | // Marking operations for objects reachable from roots. |
| 182 | static void MarkLiveObjects(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 183 | |
| 184 | static void MarkUnmarkedObject(HeapObject* obj); |
| 185 | |
| 186 | static inline void MarkObject(HeapObject* obj) { |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 187 | if (!obj->IsMarked()) MarkUnmarkedObject(obj); |
| 188 | } |
| 189 | |
| 190 | static inline void SetMark(HeapObject* obj) { |
| 191 | tracer_->increment_marked_count(); |
| 192 | #ifdef DEBUG |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 193 | UpdateLiveObjectCount(obj); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 194 | #endif |
| 195 | obj->SetMark(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 196 | } |
| 197 | |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 198 | // Creates back pointers for all map transitions, stores them in |
| 199 | // the prototype field. The original prototype pointers are restored |
| 200 | // in ClearNonLiveTransitions(). All JSObject maps |
| 201 | // connected by map transitions have the same prototype object, which |
| 202 | // is why we can use this field temporarily for back pointers. |
| 203 | static void CreateBackPointers(); |
| 204 | |
| 205 | // Mark a Map and its DescriptorArray together, skipping transitions. |
| 206 | static void MarkMapContents(Map* map); |
| 207 | static void MarkDescriptorArray(DescriptorArray* descriptors); |
| 208 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 209 | // Mark the heap roots and all objects reachable from them. |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 210 | static void MarkRoots(RootMarkingVisitor* visitor); |
| 211 | |
| 212 | // Mark the symbol table specially. References to symbols from the |
| 213 | // symbol table are weak. |
| 214 | static void MarkSymbolTable(); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 215 | |
| 216 | // Mark objects in object groups that have at least one object in the |
| 217 | // group marked. |
| 218 | static void MarkObjectGroups(); |
| 219 | |
| 220 | // Mark all objects in an object group with at least one marked |
| 221 | // object, then all objects reachable from marked objects in object |
| 222 | // groups, and repeat. |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 223 | static void ProcessObjectGroups(); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 224 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 225 | // Mark objects reachable (transitively) from objects in the marking stack |
| 226 | // or overflowed in the heap. |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 227 | static void ProcessMarkingStack(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 228 | |
| 229 | // Mark objects reachable (transitively) from objects in the marking |
| 230 | // stack. This function empties the marking stack, but may leave |
| 231 | // overflowed objects in the heap, in which case the marking stack's |
| 232 | // overflow flag will be set. |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 233 | static void EmptyMarkingStack(); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 234 | |
| 235 | // Refill the marking stack with overflowed objects from the heap. This |
| 236 | // function either leaves the marking stack full or clears the overflow |
| 237 | // flag on the marking stack. |
| 238 | static void RefillMarkingStack(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 239 | |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 240 | // Callback function for telling whether the object *p is an unmarked |
| 241 | // heap object. |
| 242 | static bool IsUnmarkedHeapObject(Object** p); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 243 | |
| 244 | #ifdef DEBUG |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 245 | static void UpdateLiveObjectCount(HeapObject* obj); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 246 | #endif |
| 247 | |
| 248 | // We sweep the large object space in the same way whether we are |
| 249 | // compacting or not, because the large object space is never compacted. |
| 250 | static void SweepLargeObjectSpace(); |
| 251 | |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 252 | // Test whether a (possibly marked) object is a Map. |
| 253 | static inline bool SafeIsMap(HeapObject* object); |
| 254 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 255 | // 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] | 256 | // We replace them with a null descriptor, with the same key. |
| 257 | static void ClearNonLiveTransitions(); |
| 258 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 259 | // ----------------------------------------------------------------------- |
| 260 | // Phase 2: Sweeping to clear mark bits and free non-live objects for |
| 261 | // a non-compacting collection, or else computing and encoding |
| 262 | // forwarding addresses for a compacting collection. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 263 | // |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 264 | // Before: Live objects are marked and non-live objects are unmarked. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 265 | // |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 266 | // After: (Non-compacting collection.) Live objects are unmarked, |
| 267 | // non-live regions have been added to their space's free |
| 268 | // list. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 269 | // |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 270 | // After: (Compacting collection.) The forwarding address of live |
| 271 | // objects in the paged spaces is encoded in their map word |
| 272 | // along with their (non-forwarded) map pointer. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 273 | // |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 274 | // The forwarding address of live objects in the new space is |
| 275 | // written to their map word's offset in the inactive |
| 276 | // semispace. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 277 | // |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 278 | // Bookkeeping data is written to the page header of |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 279 | // eached paged-space page that contains live objects after |
| 280 | // compaction: |
| 281 | // |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 282 | // The allocation watermark field is used to track the |
| 283 | // relocation top address, the address of the first word |
| 284 | // after the end of the last live object in the page after |
| 285 | // compaction. |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 286 | // |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 287 | // The Page::mc_page_index field contains the zero-based index of the |
| 288 | // 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] | 289 | // order to encode the map addresses in 21 bits to free 11 |
| 290 | // bits per map word for the forwarding address. |
| 291 | // |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 292 | // The Page::mc_first_forwarded field contains the (nonencoded) |
| 293 | // forwarding address of the first live object in the page. |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 294 | // |
| 295 | // In both the new space and the paged spaces, a linked list |
| 296 | // of live regions is constructructed (linked through |
| 297 | // pointers in the non-live region immediately following each |
| 298 | // live region) to speed further passes of the collector. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 299 | |
| 300 | // Encodes forwarding addresses of objects in compactable parts of the |
| 301 | // heap. |
| 302 | static void EncodeForwardingAddresses(); |
| 303 | |
| 304 | // Encodes the forwarding addresses of objects in new space. |
| 305 | static void EncodeForwardingAddressesInNewSpace(); |
| 306 | |
| 307 | // Function template to encode the forwarding addresses of objects in |
| 308 | // paged spaces, parameterized by allocation and non-live processing |
| 309 | // functions. |
| 310 | template<AllocationFunction Alloc, ProcessNonLiveFunction ProcessNonLive> |
| 311 | static void EncodeForwardingAddressesInPagedSpace(PagedSpace* space); |
| 312 | |
| 313 | // Iterates live objects in a space, passes live objects |
| 314 | // to a callback function which returns the heap size of the object. |
| 315 | // Returns the number of live objects iterated. |
| 316 | static int IterateLiveObjects(NewSpace* space, HeapObjectCallback size_f); |
| 317 | static int IterateLiveObjects(PagedSpace* space, HeapObjectCallback size_f); |
| 318 | |
| 319 | // Iterates the live objects between a range of addresses, returning the |
| 320 | // number of live objects. |
| 321 | static int IterateLiveObjectsInRange(Address start, Address end, |
| 322 | HeapObjectCallback size_func); |
| 323 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 324 | // If we are not compacting the heap, we simply sweep the spaces except |
| 325 | // for the large object space, clearing mark bits and adding unmarked |
| 326 | // regions to each space's free list. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 327 | static void SweepSpaces(); |
| 328 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 329 | // ----------------------------------------------------------------------- |
| 330 | // Phase 3: Updating pointers in live objects. |
| 331 | // |
| 332 | // Before: Same as after phase 2 (compacting collection). |
| 333 | // |
| 334 | // After: All pointers in live objects, including encoded map |
| 335 | // pointers, are updated to point to their target's new |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 336 | // location. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 337 | |
| 338 | friend class UpdatingVisitor; // helper for updating visited objects |
| 339 | |
| 340 | // Updates pointers in all spaces. |
| 341 | static void UpdatePointers(); |
| 342 | |
| 343 | // Updates pointers in an object in new space. |
| 344 | // Returns the heap size of the object. |
| 345 | static int UpdatePointersInNewObject(HeapObject* obj); |
| 346 | |
| 347 | // Updates pointers in an object in old spaces. |
| 348 | // Returns the heap size of the object. |
| 349 | static int UpdatePointersInOldObject(HeapObject* obj); |
| 350 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 351 | // Calculates the forwarding address of an object in an old space. |
| 352 | static Address GetForwardingAddressInOldSpace(HeapObject* obj); |
| 353 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 354 | // ----------------------------------------------------------------------- |
| 355 | // Phase 4: Relocating objects. |
| 356 | // |
| 357 | // Before: Pointers to live objects are updated to point to their |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 358 | // target's new location. |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 359 | // |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 360 | // After: Objects have been moved to their new addresses. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 361 | |
| 362 | // Relocates objects in all spaces. |
| 363 | static void RelocateObjects(); |
| 364 | |
| 365 | // Converts a code object's inline target to addresses, convention from |
| 366 | // address to target happens in the marking phase. |
| 367 | static int ConvertCodeICTargetToAddress(HeapObject* obj); |
| 368 | |
| 369 | // Relocate a map object. |
| 370 | static int RelocateMapObject(HeapObject* obj); |
| 371 | |
| 372 | // Relocates an old object. |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 373 | static int RelocateOldPointerObject(HeapObject* obj); |
| 374 | static int RelocateOldDataObject(HeapObject* obj); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 375 | |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 376 | // Relocate a property cell object. |
| 377 | static int RelocateCellObject(HeapObject* obj); |
| 378 | |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 379 | // Helper function. |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 380 | static inline int RelocateOldNonCodeObject(HeapObject* obj, |
| 381 | PagedSpace* space); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 382 | |
| 383 | // Relocates an object in the code space. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 384 | static int RelocateCodeObject(HeapObject* obj); |
| 385 | |
| 386 | // Copy a new object. |
| 387 | static int RelocateNewObject(HeapObject* obj); |
| 388 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 389 | #ifdef DEBUG |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 390 | // ----------------------------------------------------------------------- |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 391 | // Debugging variables, functions and classes |
| 392 | // Counters used for debugging the marking phase of mark-compact or |
| 393 | // mark-sweep collection. |
| 394 | |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 395 | // Size of live objects in Heap::to_space_. |
| 396 | static int live_young_objects_size_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 397 | |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 398 | // Size of live objects in Heap::old_pointer_space_. |
| 399 | static int live_old_pointer_objects_size_; |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +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_data_space_. |
| 402 | static int live_old_data_objects_size_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 403 | |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 404 | // Size of live objects in Heap::code_space_. |
| 405 | static int live_code_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::map_space_. |
| 408 | static int live_map_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::cell_space_. |
| 411 | static int live_cell_objects_size_; |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 412 | |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 413 | // Size of live objects in Heap::lo_space_. |
| 414 | static int live_lo_objects_size_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 415 | |
| 416 | // Number of live bytes in this collection. |
| 417 | static int live_bytes_; |
| 418 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 419 | friend class MarkObjectVisitor; |
| 420 | static void VisitObject(HeapObject* obj); |
| 421 | |
| 422 | friend class UnmarkObjectVisitor; |
| 423 | static void UnmarkObject(HeapObject* obj); |
| 424 | #endif |
| 425 | }; |
| 426 | |
| 427 | |
| 428 | } } // namespace v8::internal |
| 429 | |
| 430 | #endif // V8_MARK_COMPACT_H_ |