Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1 | // Copyright 2011 the V8 project authors. All rights reserved. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +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_SPACES_H_ |
| 29 | #define V8_SPACES_H_ |
| 30 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 31 | #include "allocation.h" |
| 32 | #include "list.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 33 | #include "log.h" |
| 34 | |
| 35 | namespace v8 { |
| 36 | namespace internal { |
| 37 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 38 | class Isolate; |
| 39 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 40 | // ----------------------------------------------------------------------------- |
| 41 | // Heap structures: |
| 42 | // |
| 43 | // A JS heap consists of a young generation, an old generation, and a large |
| 44 | // object space. The young generation is divided into two semispaces. A |
| 45 | // scavenger implements Cheney's copying algorithm. The old generation is |
| 46 | // separated into a map space and an old object space. The map space contains |
| 47 | // all (and only) map objects, the rest of old objects go into the old space. |
| 48 | // The old generation is collected by a mark-sweep-compact collector. |
| 49 | // |
| 50 | // The semispaces of the young generation are contiguous. The old and map |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 51 | // spaces consists of a list of pages. A page has a page header and an object |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 52 | // area. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 53 | // |
| 54 | // There is a separate large object space for objects larger than |
| 55 | // Page::kMaxHeapObjectSize, so that they do not have to move during |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 56 | // collection. The large object space is paged. Pages in large object space |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 57 | // may be larger than the page size. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 58 | // |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 59 | // A store-buffer based write barrier is used to keep track of intergenerational |
| 60 | // references. See store-buffer.h. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 61 | // |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 62 | // During scavenges and mark-sweep collections we sometimes (after a store |
| 63 | // buffer overflow) iterate intergenerational pointers without decoding heap |
| 64 | // object maps so if the page belongs to old pointer space or large object |
| 65 | // space it is essential to guarantee that the page does not contain any |
| 66 | // garbage pointers to new space: every pointer aligned word which satisfies |
| 67 | // the Heap::InNewSpace() predicate must be a pointer to a live heap object in |
| 68 | // new space. Thus objects in old pointer and large object spaces should have a |
| 69 | // special layout (e.g. no bare integer fields). This requirement does not |
| 70 | // apply to map space which is iterated in a special fashion. However we still |
| 71 | // require pointer fields of dead maps to be cleaned. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 72 | // |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 73 | // To enable lazy cleaning of old space pages we can mark chunks of the page |
| 74 | // as being garbage. Garbage sections are marked with a special map. These |
| 75 | // sections are skipped when scanning the page, even if we are otherwise |
| 76 | // scanning without regard for object boundaries. Garbage sections are chained |
| 77 | // together to form a free list after a GC. Garbage sections created outside |
| 78 | // of GCs by object trunctation etc. may not be in the free list chain. Very |
| 79 | // small free spaces are ignored, they need only be cleaned of bogus pointers |
| 80 | // into new space. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 81 | // |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 82 | // Each page may have up to one special garbage section. The start of this |
| 83 | // section is denoted by the top field in the space. The end of the section |
| 84 | // is denoted by the limit field in the space. This special garbage section |
| 85 | // is not marked with a free space map in the data. The point of this section |
| 86 | // is to enable linear allocation without having to constantly update the byte |
| 87 | // array every time the top field is updated and a new object is created. The |
| 88 | // special garbage section is not in the chain of garbage sections. |
| 89 | // |
| 90 | // Since the top and limit fields are in the space, not the page, only one page |
| 91 | // has a special garbage section, and if the top and limit are equal then there |
| 92 | // is no special garbage section. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 93 | |
| 94 | // Some assertion macros used in the debugging mode. |
| 95 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 96 | #define ASSERT_PAGE_ALIGNED(address) \ |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 97 | ASSERT((OffsetFrom(address) & Page::kPageAlignmentMask) == 0) |
| 98 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 99 | #define ASSERT_OBJECT_ALIGNED(address) \ |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 100 | ASSERT((OffsetFrom(address) & kObjectAlignmentMask) == 0) |
| 101 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 102 | #define ASSERT_MAP_ALIGNED(address) \ |
| 103 | ASSERT((OffsetFrom(address) & kMapAlignmentMask) == 0) |
| 104 | |
| 105 | #define ASSERT_OBJECT_SIZE(size) \ |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 106 | ASSERT((0 < size) && (size <= Page::kMaxNonCodeHeapObjectSize)) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 107 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 108 | #define ASSERT_PAGE_OFFSET(offset) \ |
| 109 | ASSERT((Page::kObjectStartOffset <= offset) \ |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 110 | && (offset <= Page::kPageSize)) |
| 111 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 112 | #define ASSERT_MAP_PAGE_INDEX(index) \ |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 113 | ASSERT((0 <= index) && (index <= MapSpace::kMaxMapPageIndex)) |
| 114 | |
| 115 | |
| 116 | class PagedSpace; |
| 117 | class MemoryAllocator; |
| 118 | class AllocationInfo; |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 119 | class Space; |
| 120 | class FreeList; |
| 121 | class MemoryChunk; |
| 122 | |
| 123 | class MarkBit { |
| 124 | public: |
| 125 | typedef uint32_t CellType; |
| 126 | |
| 127 | inline MarkBit(CellType* cell, CellType mask, bool data_only) |
| 128 | : cell_(cell), mask_(mask), data_only_(data_only) { } |
| 129 | |
| 130 | inline CellType* cell() { return cell_; } |
| 131 | inline CellType mask() { return mask_; } |
| 132 | |
| 133 | #ifdef DEBUG |
| 134 | bool operator==(const MarkBit& other) { |
| 135 | return cell_ == other.cell_ && mask_ == other.mask_; |
| 136 | } |
| 137 | #endif |
| 138 | |
| 139 | inline void Set() { *cell_ |= mask_; } |
| 140 | inline bool Get() { return (*cell_ & mask_) != 0; } |
| 141 | inline void Clear() { *cell_ &= ~mask_; } |
| 142 | |
| 143 | inline bool data_only() { return data_only_; } |
| 144 | |
| 145 | inline MarkBit Next() { |
| 146 | CellType new_mask = mask_ << 1; |
| 147 | if (new_mask == 0) { |
| 148 | return MarkBit(cell_ + 1, 1, data_only_); |
| 149 | } else { |
| 150 | return MarkBit(cell_, new_mask, data_only_); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | private: |
| 155 | CellType* cell_; |
| 156 | CellType mask_; |
| 157 | // This boolean indicates that the object is in a data-only space with no |
| 158 | // pointers. This enables some optimizations when marking. |
| 159 | // It is expected that this field is inlined and turned into control flow |
| 160 | // at the place where the MarkBit object is created. |
| 161 | bool data_only_; |
| 162 | }; |
| 163 | |
| 164 | |
| 165 | // Bitmap is a sequence of cells each containing fixed number of bits. |
| 166 | class Bitmap { |
| 167 | public: |
| 168 | static const uint32_t kBitsPerCell = 32; |
| 169 | static const uint32_t kBitsPerCellLog2 = 5; |
| 170 | static const uint32_t kBitIndexMask = kBitsPerCell - 1; |
| 171 | static const uint32_t kBytesPerCell = kBitsPerCell / kBitsPerByte; |
| 172 | static const uint32_t kBytesPerCellLog2 = kBitsPerCellLog2 - kBitsPerByteLog2; |
| 173 | |
| 174 | static const size_t kLength = |
| 175 | (1 << kPageSizeBits) >> (kPointerSizeLog2); |
| 176 | |
| 177 | static const size_t kSize = |
| 178 | (1 << kPageSizeBits) >> (kPointerSizeLog2 + kBitsPerByteLog2); |
| 179 | |
| 180 | |
| 181 | static int CellsForLength(int length) { |
| 182 | return (length + kBitsPerCell - 1) >> kBitsPerCellLog2; |
| 183 | } |
| 184 | |
| 185 | int CellsCount() { |
| 186 | return CellsForLength(kLength); |
| 187 | } |
| 188 | |
| 189 | static int SizeFor(int cells_count) { |
| 190 | return sizeof(MarkBit::CellType) * cells_count; |
| 191 | } |
| 192 | |
| 193 | INLINE(static uint32_t IndexToCell(uint32_t index)) { |
| 194 | return index >> kBitsPerCellLog2; |
| 195 | } |
| 196 | |
| 197 | INLINE(static uint32_t CellToIndex(uint32_t index)) { |
| 198 | return index << kBitsPerCellLog2; |
| 199 | } |
| 200 | |
| 201 | INLINE(static uint32_t CellAlignIndex(uint32_t index)) { |
| 202 | return (index + kBitIndexMask) & ~kBitIndexMask; |
| 203 | } |
| 204 | |
| 205 | INLINE(MarkBit::CellType* cells()) { |
| 206 | return reinterpret_cast<MarkBit::CellType*>(this); |
| 207 | } |
| 208 | |
| 209 | INLINE(Address address()) { |
| 210 | return reinterpret_cast<Address>(this); |
| 211 | } |
| 212 | |
| 213 | INLINE(static Bitmap* FromAddress(Address addr)) { |
| 214 | return reinterpret_cast<Bitmap*>(addr); |
| 215 | } |
| 216 | |
| 217 | inline MarkBit MarkBitFromIndex(uint32_t index, bool data_only = false) { |
| 218 | MarkBit::CellType mask = 1 << (index & kBitIndexMask); |
| 219 | MarkBit::CellType* cell = this->cells() + (index >> kBitsPerCellLog2); |
| 220 | return MarkBit(cell, mask, data_only); |
| 221 | } |
| 222 | |
| 223 | static inline void Clear(MemoryChunk* chunk); |
| 224 | |
| 225 | static void PrintWord(uint32_t word, uint32_t himask = 0) { |
| 226 | for (uint32_t mask = 1; mask != 0; mask <<= 1) { |
| 227 | if ((mask & himask) != 0) PrintF("["); |
| 228 | PrintF((mask & word) ? "1" : "0"); |
| 229 | if ((mask & himask) != 0) PrintF("]"); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | class CellPrinter { |
| 234 | public: |
| 235 | CellPrinter() : seq_start(0), seq_type(0), seq_length(0) { } |
| 236 | |
| 237 | void Print(uint32_t pos, uint32_t cell) { |
| 238 | if (cell == seq_type) { |
| 239 | seq_length++; |
| 240 | return; |
| 241 | } |
| 242 | |
| 243 | Flush(); |
| 244 | |
| 245 | if (IsSeq(cell)) { |
| 246 | seq_start = pos; |
| 247 | seq_length = 0; |
| 248 | seq_type = cell; |
| 249 | return; |
| 250 | } |
| 251 | |
| 252 | PrintF("%d: ", pos); |
| 253 | PrintWord(cell); |
| 254 | PrintF("\n"); |
| 255 | } |
| 256 | |
| 257 | void Flush() { |
| 258 | if (seq_length > 0) { |
| 259 | PrintF("%d: %dx%d\n", |
| 260 | seq_start, |
| 261 | seq_type == 0 ? 0 : 1, |
| 262 | seq_length * kBitsPerCell); |
| 263 | seq_length = 0; |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | static bool IsSeq(uint32_t cell) { return cell == 0 || cell == 0xFFFFFFFF; } |
| 268 | |
| 269 | private: |
| 270 | uint32_t seq_start; |
| 271 | uint32_t seq_type; |
| 272 | uint32_t seq_length; |
| 273 | }; |
| 274 | |
| 275 | void Print() { |
| 276 | CellPrinter printer; |
| 277 | for (int i = 0; i < CellsCount(); i++) { |
| 278 | printer.Print(i, cells()[i]); |
| 279 | } |
| 280 | printer.Flush(); |
| 281 | PrintF("\n"); |
| 282 | } |
| 283 | |
| 284 | bool IsClean() { |
| 285 | for (int i = 0; i < CellsCount(); i++) { |
| 286 | if (cells()[i] != 0) return false; |
| 287 | } |
| 288 | return true; |
| 289 | } |
| 290 | }; |
| 291 | |
| 292 | |
| 293 | class SkipList; |
| 294 | class SlotsBuffer; |
| 295 | |
| 296 | // MemoryChunk represents a memory region owned by a specific space. |
| 297 | // It is divided into the header and the body. Chunk start is always |
| 298 | // 1MB aligned. Start of the body is aligned so it can accomodate |
| 299 | // any heap object. |
| 300 | class MemoryChunk { |
| 301 | public: |
| 302 | // Only works if the pointer is in the first kPageSize of the MemoryChunk. |
| 303 | static MemoryChunk* FromAddress(Address a) { |
| 304 | return reinterpret_cast<MemoryChunk*>(OffsetFrom(a) & ~kAlignmentMask); |
| 305 | } |
| 306 | |
| 307 | // Only works for addresses in pointer spaces, not data or code spaces. |
| 308 | static inline MemoryChunk* FromAnyPointerAddress(Address addr); |
| 309 | |
| 310 | Address address() { return reinterpret_cast<Address>(this); } |
| 311 | |
| 312 | bool is_valid() { return address() != NULL; } |
| 313 | |
| 314 | MemoryChunk* next_chunk() const { return next_chunk_; } |
| 315 | MemoryChunk* prev_chunk() const { return prev_chunk_; } |
| 316 | |
| 317 | void set_next_chunk(MemoryChunk* next) { next_chunk_ = next; } |
| 318 | void set_prev_chunk(MemoryChunk* prev) { prev_chunk_ = prev; } |
| 319 | |
| 320 | Space* owner() const { |
| 321 | if ((reinterpret_cast<intptr_t>(owner_) & kFailureTagMask) == |
| 322 | kFailureTag) { |
| 323 | return reinterpret_cast<Space*>(owner_ - kFailureTag); |
| 324 | } else { |
| 325 | return NULL; |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | void set_owner(Space* space) { |
| 330 | ASSERT((reinterpret_cast<intptr_t>(space) & kFailureTagMask) == 0); |
| 331 | owner_ = reinterpret_cast<Address>(space) + kFailureTag; |
| 332 | ASSERT((reinterpret_cast<intptr_t>(owner_) & kFailureTagMask) == |
| 333 | kFailureTag); |
| 334 | } |
| 335 | |
| 336 | VirtualMemory* reserved_memory() { |
| 337 | return &reservation_; |
| 338 | } |
| 339 | |
| 340 | void InitializeReservedMemory() { |
| 341 | reservation_.Reset(); |
| 342 | } |
| 343 | |
| 344 | void set_reserved_memory(VirtualMemory* reservation) { |
| 345 | ASSERT_NOT_NULL(reservation); |
| 346 | reservation_.TakeControl(reservation); |
| 347 | } |
| 348 | |
| 349 | bool scan_on_scavenge() { return IsFlagSet(SCAN_ON_SCAVENGE); } |
| 350 | void initialize_scan_on_scavenge(bool scan) { |
| 351 | if (scan) { |
| 352 | SetFlag(SCAN_ON_SCAVENGE); |
| 353 | } else { |
| 354 | ClearFlag(SCAN_ON_SCAVENGE); |
| 355 | } |
| 356 | } |
| 357 | inline void set_scan_on_scavenge(bool scan); |
| 358 | |
| 359 | int store_buffer_counter() { return store_buffer_counter_; } |
| 360 | void set_store_buffer_counter(int counter) { |
| 361 | store_buffer_counter_ = counter; |
| 362 | } |
| 363 | |
| 364 | bool Contains(Address addr) { |
| 365 | return addr >= area_start() && addr < area_end(); |
| 366 | } |
| 367 | |
| 368 | // Checks whether addr can be a limit of addresses in this page. |
| 369 | // It's a limit if it's in the page, or if it's just after the |
| 370 | // last byte of the page. |
| 371 | bool ContainsLimit(Address addr) { |
| 372 | return addr >= area_start() && addr <= area_end(); |
| 373 | } |
| 374 | |
| 375 | enum MemoryChunkFlags { |
| 376 | IS_EXECUTABLE, |
| 377 | ABOUT_TO_BE_FREED, |
| 378 | POINTERS_TO_HERE_ARE_INTERESTING, |
| 379 | POINTERS_FROM_HERE_ARE_INTERESTING, |
| 380 | SCAN_ON_SCAVENGE, |
| 381 | IN_FROM_SPACE, // Mutually exclusive with IN_TO_SPACE. |
| 382 | IN_TO_SPACE, // All pages in new space has one of these two set. |
| 383 | NEW_SPACE_BELOW_AGE_MARK, |
| 384 | CONTAINS_ONLY_DATA, |
| 385 | EVACUATION_CANDIDATE, |
| 386 | RESCAN_ON_EVACUATION, |
| 387 | |
| 388 | // Pages swept precisely can be iterated, hitting only the live objects. |
| 389 | // Whereas those swept conservatively cannot be iterated over. Both flags |
| 390 | // indicate that marking bits have been cleared by the sweeper, otherwise |
| 391 | // marking bits are still intact. |
| 392 | WAS_SWEPT_PRECISELY, |
| 393 | WAS_SWEPT_CONSERVATIVELY, |
| 394 | |
| 395 | // Last flag, keep at bottom. |
| 396 | NUM_MEMORY_CHUNK_FLAGS |
| 397 | }; |
| 398 | |
| 399 | |
| 400 | static const int kPointersToHereAreInterestingMask = |
| 401 | 1 << POINTERS_TO_HERE_ARE_INTERESTING; |
| 402 | |
| 403 | static const int kPointersFromHereAreInterestingMask = |
| 404 | 1 << POINTERS_FROM_HERE_ARE_INTERESTING; |
| 405 | |
| 406 | static const int kEvacuationCandidateMask = |
| 407 | 1 << EVACUATION_CANDIDATE; |
| 408 | |
| 409 | static const int kSkipEvacuationSlotsRecordingMask = |
| 410 | (1 << EVACUATION_CANDIDATE) | |
| 411 | (1 << RESCAN_ON_EVACUATION) | |
| 412 | (1 << IN_FROM_SPACE) | |
| 413 | (1 << IN_TO_SPACE); |
| 414 | |
| 415 | |
| 416 | void SetFlag(int flag) { |
| 417 | flags_ |= static_cast<uintptr_t>(1) << flag; |
| 418 | } |
| 419 | |
| 420 | void ClearFlag(int flag) { |
| 421 | flags_ &= ~(static_cast<uintptr_t>(1) << flag); |
| 422 | } |
| 423 | |
| 424 | void SetFlagTo(int flag, bool value) { |
| 425 | if (value) { |
| 426 | SetFlag(flag); |
| 427 | } else { |
| 428 | ClearFlag(flag); |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | bool IsFlagSet(int flag) { |
| 433 | return (flags_ & (static_cast<uintptr_t>(1) << flag)) != 0; |
| 434 | } |
| 435 | |
| 436 | // Set or clear multiple flags at a time. The flags in the mask |
| 437 | // are set to the value in "flags", the rest retain the current value |
| 438 | // in flags_. |
| 439 | void SetFlags(intptr_t flags, intptr_t mask) { |
| 440 | flags_ = (flags_ & ~mask) | (flags & mask); |
| 441 | } |
| 442 | |
| 443 | // Return all current flags. |
| 444 | intptr_t GetFlags() { return flags_; } |
| 445 | |
| 446 | // Manage live byte count (count of bytes known to be live, |
| 447 | // because they are marked black). |
| 448 | void ResetLiveBytes() { |
| 449 | if (FLAG_gc_verbose) { |
| 450 | PrintF("ResetLiveBytes:%p:%x->0\n", |
| 451 | static_cast<void*>(this), live_byte_count_); |
| 452 | } |
| 453 | live_byte_count_ = 0; |
| 454 | } |
| 455 | void IncrementLiveBytes(int by) { |
| 456 | if (FLAG_gc_verbose) { |
| 457 | printf("UpdateLiveBytes:%p:%x%c=%x->%x\n", |
| 458 | static_cast<void*>(this), live_byte_count_, |
| 459 | ((by < 0) ? '-' : '+'), ((by < 0) ? -by : by), |
| 460 | live_byte_count_ + by); |
| 461 | } |
| 462 | live_byte_count_ += by; |
| 463 | ASSERT_LE(static_cast<unsigned>(live_byte_count_), size_); |
| 464 | } |
| 465 | int LiveBytes() { |
| 466 | ASSERT(static_cast<unsigned>(live_byte_count_) <= size_); |
| 467 | return live_byte_count_; |
| 468 | } |
| 469 | static void IncrementLiveBytes(Address address, int by) { |
| 470 | MemoryChunk::FromAddress(address)->IncrementLiveBytes(by); |
| 471 | } |
| 472 | |
| 473 | static const intptr_t kAlignment = |
| 474 | (static_cast<uintptr_t>(1) << kPageSizeBits); |
| 475 | |
| 476 | static const intptr_t kAlignmentMask = kAlignment - 1; |
| 477 | |
| 478 | static const intptr_t kSizeOffset = kPointerSize + kPointerSize; |
| 479 | |
| 480 | static const intptr_t kLiveBytesOffset = |
| 481 | kSizeOffset + kPointerSize + kPointerSize + kPointerSize + |
| 482 | kPointerSize + kPointerSize + |
| 483 | kPointerSize + kPointerSize + kPointerSize + kIntSize; |
| 484 | |
| 485 | static const size_t kSlotsBufferOffset = kLiveBytesOffset + kIntSize; |
| 486 | |
| 487 | static const size_t kHeaderSize = |
| 488 | kSlotsBufferOffset + kPointerSize + kPointerSize; |
| 489 | |
| 490 | static const int kBodyOffset = |
| 491 | CODE_POINTER_ALIGN(MAP_POINTER_ALIGN(kHeaderSize + Bitmap::kSize)); |
| 492 | |
| 493 | // The start offset of the object area in a page. Aligned to both maps and |
| 494 | // code alignment to be suitable for both. Also aligned to 32 words because |
| 495 | // the marking bitmap is arranged in 32 bit chunks. |
| 496 | static const int kObjectStartAlignment = 32 * kPointerSize; |
| 497 | static const int kObjectStartOffset = kBodyOffset - 1 + |
| 498 | (kObjectStartAlignment - (kBodyOffset - 1) % kObjectStartAlignment); |
| 499 | |
| 500 | size_t size() const { return size_; } |
| 501 | |
| 502 | void set_size(size_t size) { |
| 503 | size_ = size; |
| 504 | } |
| 505 | |
| 506 | Executability executable() { |
| 507 | return IsFlagSet(IS_EXECUTABLE) ? EXECUTABLE : NOT_EXECUTABLE; |
| 508 | } |
| 509 | |
| 510 | bool ContainsOnlyData() { |
| 511 | return IsFlagSet(CONTAINS_ONLY_DATA); |
| 512 | } |
| 513 | |
| 514 | bool InNewSpace() { |
| 515 | return (flags_ & ((1 << IN_FROM_SPACE) | (1 << IN_TO_SPACE))) != 0; |
| 516 | } |
| 517 | |
| 518 | bool InToSpace() { |
| 519 | return IsFlagSet(IN_TO_SPACE); |
| 520 | } |
| 521 | |
| 522 | bool InFromSpace() { |
| 523 | return IsFlagSet(IN_FROM_SPACE); |
| 524 | } |
| 525 | |
| 526 | // --------------------------------------------------------------------- |
| 527 | // Markbits support |
| 528 | |
| 529 | inline Bitmap* markbits() { |
| 530 | return Bitmap::FromAddress(address() + kHeaderSize); |
| 531 | } |
| 532 | |
| 533 | void PrintMarkbits() { markbits()->Print(); } |
| 534 | |
| 535 | inline uint32_t AddressToMarkbitIndex(Address addr) { |
| 536 | return static_cast<uint32_t>(addr - this->address()) >> kPointerSizeLog2; |
| 537 | } |
| 538 | |
| 539 | inline static uint32_t FastAddressToMarkbitIndex(Address addr) { |
| 540 | const intptr_t offset = |
| 541 | reinterpret_cast<intptr_t>(addr) & kAlignmentMask; |
| 542 | |
| 543 | return static_cast<uint32_t>(offset) >> kPointerSizeLog2; |
| 544 | } |
| 545 | |
| 546 | inline Address MarkbitIndexToAddress(uint32_t index) { |
| 547 | return this->address() + (index << kPointerSizeLog2); |
| 548 | } |
| 549 | |
| 550 | void InsertAfter(MemoryChunk* other); |
| 551 | void Unlink(); |
| 552 | |
| 553 | inline Heap* heap() { return heap_; } |
| 554 | |
| 555 | static const int kFlagsOffset = kPointerSize * 3; |
| 556 | |
| 557 | bool IsEvacuationCandidate() { return IsFlagSet(EVACUATION_CANDIDATE); } |
| 558 | |
| 559 | bool ShouldSkipEvacuationSlotRecording() { |
| 560 | return (flags_ & kSkipEvacuationSlotsRecordingMask) != 0; |
| 561 | } |
| 562 | |
| 563 | inline SkipList* skip_list() { |
| 564 | return skip_list_; |
| 565 | } |
| 566 | |
| 567 | inline void set_skip_list(SkipList* skip_list) { |
| 568 | skip_list_ = skip_list; |
| 569 | } |
| 570 | |
| 571 | inline SlotsBuffer* slots_buffer() { |
| 572 | return slots_buffer_; |
| 573 | } |
| 574 | |
| 575 | inline SlotsBuffer** slots_buffer_address() { |
| 576 | return &slots_buffer_; |
| 577 | } |
| 578 | |
| 579 | void MarkEvacuationCandidate() { |
| 580 | ASSERT(slots_buffer_ == NULL); |
| 581 | SetFlag(EVACUATION_CANDIDATE); |
| 582 | } |
| 583 | |
| 584 | void ClearEvacuationCandidate() { |
| 585 | ASSERT(slots_buffer_ == NULL); |
| 586 | ClearFlag(EVACUATION_CANDIDATE); |
| 587 | } |
| 588 | |
| 589 | Address area_start() { return area_start_; } |
| 590 | Address area_end() { return area_end_; } |
| 591 | int area_size() { |
| 592 | return static_cast<int>(area_end() - area_start()); |
| 593 | } |
| 594 | |
| 595 | protected: |
| 596 | MemoryChunk* next_chunk_; |
| 597 | MemoryChunk* prev_chunk_; |
| 598 | size_t size_; |
| 599 | intptr_t flags_; |
| 600 | |
| 601 | // Start and end of allocatable memory on this chunk. |
| 602 | Address area_start_; |
| 603 | Address area_end_; |
| 604 | |
| 605 | // If the chunk needs to remember its memory reservation, it is stored here. |
| 606 | VirtualMemory reservation_; |
| 607 | // The identity of the owning space. This is tagged as a failure pointer, but |
| 608 | // no failure can be in an object, so this can be distinguished from any entry |
| 609 | // in a fixed array. |
| 610 | Address owner_; |
| 611 | Heap* heap_; |
| 612 | // Used by the store buffer to keep track of which pages to mark scan-on- |
| 613 | // scavenge. |
| 614 | int store_buffer_counter_; |
| 615 | // Count of bytes marked black on page. |
| 616 | int live_byte_count_; |
| 617 | SlotsBuffer* slots_buffer_; |
| 618 | SkipList* skip_list_; |
| 619 | |
| 620 | static MemoryChunk* Initialize(Heap* heap, |
| 621 | Address base, |
| 622 | size_t size, |
| 623 | Address area_start, |
| 624 | Address area_end, |
| 625 | Executability executable, |
| 626 | Space* owner); |
| 627 | |
| 628 | friend class MemoryAllocator; |
| 629 | }; |
| 630 | |
| 631 | STATIC_CHECK(sizeof(MemoryChunk) <= MemoryChunk::kHeaderSize); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 632 | |
| 633 | // ----------------------------------------------------------------------------- |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 634 | // A page is a memory chunk of a size 1MB. Large object pages may be larger. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 635 | // |
| 636 | // The only way to get a page pointer is by calling factory methods: |
| 637 | // Page* p = Page::FromAddress(addr); or |
| 638 | // Page* p = Page::FromAllocationTop(top); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 639 | class Page : public MemoryChunk { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 640 | public: |
| 641 | // Returns the page containing a given address. The address ranges |
| 642 | // from [page_addr .. page_addr + kPageSize[ |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 643 | // This only works if the object is in fact in a page. See also MemoryChunk:: |
| 644 | // FromAddress() and FromAnyAddress(). |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 645 | INLINE(static Page* FromAddress(Address a)) { |
| 646 | return reinterpret_cast<Page*>(OffsetFrom(a) & ~kPageAlignmentMask); |
| 647 | } |
| 648 | |
| 649 | // Returns the page containing an allocation top. Because an allocation |
| 650 | // top address can be the upper bound of the page, we need to subtract |
| 651 | // it with kPointerSize first. The address ranges from |
| 652 | // [page_addr + kObjectStartOffset .. page_addr + kPageSize]. |
| 653 | INLINE(static Page* FromAllocationTop(Address top)) { |
| 654 | Page* p = FromAddress(top - kPointerSize); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 655 | return p; |
| 656 | } |
| 657 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 658 | // Returns the next page in the chain of pages owned by a space. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 659 | inline Page* next_page(); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 660 | inline Page* prev_page(); |
| 661 | inline void set_next_page(Page* page); |
| 662 | inline void set_prev_page(Page* page); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 663 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 664 | // Checks whether an address is page aligned. |
| 665 | static bool IsAlignedToPageSize(Address a) { |
| 666 | return 0 == (OffsetFrom(a) & kPageAlignmentMask); |
| 667 | } |
| 668 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 669 | // Returns the offset of a given address to this page. |
| 670 | INLINE(int Offset(Address a)) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 671 | int offset = static_cast<int>(a - address()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 672 | return offset; |
| 673 | } |
| 674 | |
| 675 | // Returns the address for a given offset to the this page. |
| 676 | Address OffsetToAddress(int offset) { |
| 677 | ASSERT_PAGE_OFFSET(offset); |
| 678 | return address() + offset; |
| 679 | } |
| 680 | |
| 681 | // --------------------------------------------------------------------- |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 682 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 683 | // Page size in bytes. This must be a multiple of the OS page size. |
| 684 | static const int kPageSize = 1 << kPageSizeBits; |
| 685 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 686 | // Object area size in bytes. |
| 687 | static const int kNonCodeObjectAreaSize = kPageSize - kObjectStartOffset; |
| 688 | |
| 689 | // Maximum object size that fits in a page. |
| 690 | static const int kMaxNonCodeHeapObjectSize = kNonCodeObjectAreaSize; |
| 691 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 692 | // Page size mask. |
| 693 | static const intptr_t kPageAlignmentMask = (1 << kPageSizeBits) - 1; |
| 694 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 695 | inline void ClearGCFields(); |
| 696 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 697 | static inline Page* Initialize(Heap* heap, |
| 698 | MemoryChunk* chunk, |
| 699 | Executability executable, |
| 700 | PagedSpace* owner); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 701 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 702 | void InitializeAsAnchor(PagedSpace* owner); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 703 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 704 | bool WasSweptPrecisely() { return IsFlagSet(WAS_SWEPT_PRECISELY); } |
| 705 | bool WasSweptConservatively() { return IsFlagSet(WAS_SWEPT_CONSERVATIVELY); } |
| 706 | bool WasSwept() { return WasSweptPrecisely() || WasSweptConservatively(); } |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 707 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 708 | void MarkSweptPrecisely() { SetFlag(WAS_SWEPT_PRECISELY); } |
| 709 | void MarkSweptConservatively() { SetFlag(WAS_SWEPT_CONSERVATIVELY); } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 710 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 711 | void ClearSweptPrecisely() { ClearFlag(WAS_SWEPT_PRECISELY); } |
| 712 | void ClearSweptConservatively() { ClearFlag(WAS_SWEPT_CONSERVATIVELY); } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 713 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 714 | #ifdef DEBUG |
| 715 | void Print(); |
| 716 | #endif // DEBUG |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 717 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 718 | friend class MemoryAllocator; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 719 | }; |
| 720 | |
| 721 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 722 | STATIC_CHECK(sizeof(Page) <= MemoryChunk::kHeaderSize); |
| 723 | |
| 724 | |
| 725 | class LargePage : public MemoryChunk { |
| 726 | public: |
| 727 | HeapObject* GetObject() { |
| 728 | return HeapObject::FromAddress(area_start()); |
| 729 | } |
| 730 | |
| 731 | inline LargePage* next_page() const { |
| 732 | return static_cast<LargePage*>(next_chunk()); |
| 733 | } |
| 734 | |
| 735 | inline void set_next_page(LargePage* page) { |
| 736 | set_next_chunk(page); |
| 737 | } |
| 738 | private: |
| 739 | static inline LargePage* Initialize(Heap* heap, MemoryChunk* chunk); |
| 740 | |
| 741 | friend class MemoryAllocator; |
| 742 | }; |
| 743 | |
| 744 | STATIC_CHECK(sizeof(LargePage) <= MemoryChunk::kHeaderSize); |
| 745 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 746 | // ---------------------------------------------------------------------------- |
| 747 | // Space is the abstract superclass for all allocation spaces. |
| 748 | class Space : public Malloced { |
| 749 | public: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 750 | Space(Heap* heap, AllocationSpace id, Executability executable) |
| 751 | : heap_(heap), id_(id), executable_(executable) {} |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 752 | |
| 753 | virtual ~Space() {} |
| 754 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 755 | Heap* heap() const { return heap_; } |
| 756 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 757 | // Does the space need executable memory? |
| 758 | Executability executable() { return executable_; } |
| 759 | |
| 760 | // Identity used in error reporting. |
| 761 | AllocationSpace identity() { return id_; } |
| 762 | |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 763 | // Returns allocated size. |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 764 | virtual intptr_t Size() = 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 765 | |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 766 | // Returns size of objects. Can differ from the allocated size |
| 767 | // (e.g. see LargeObjectSpace). |
| 768 | virtual intptr_t SizeOfObjects() { return Size(); } |
| 769 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 770 | virtual int RoundSizeDownToObjectAlignment(int size) { |
| 771 | if (id_ == CODE_SPACE) { |
| 772 | return RoundDown(size, kCodeAlignment); |
| 773 | } else { |
| 774 | return RoundDown(size, kPointerSize); |
| 775 | } |
| 776 | } |
| 777 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 778 | #ifdef DEBUG |
| 779 | virtual void Print() = 0; |
| 780 | #endif |
| 781 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 782 | // After calling this we can allocate a certain number of bytes using only |
| 783 | // linear allocation (with a LinearAllocationScope and an AlwaysAllocateScope) |
| 784 | // without using freelists or causing a GC. This is used by partial |
| 785 | // snapshots. It returns true of space was reserved or false if a GC is |
| 786 | // needed. For paged spaces the space requested must include the space wasted |
| 787 | // at the end of each when allocating linearly. |
| 788 | virtual bool ReserveSpace(int bytes) = 0; |
| 789 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 790 | private: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 791 | Heap* heap_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 792 | AllocationSpace id_; |
| 793 | Executability executable_; |
| 794 | }; |
| 795 | |
| 796 | |
| 797 | // ---------------------------------------------------------------------------- |
| 798 | // All heap objects containing executable code (code objects) must be allocated |
| 799 | // from a 2 GB range of memory, so that they can call each other using 32-bit |
| 800 | // displacements. This happens automatically on 32-bit platforms, where 32-bit |
| 801 | // displacements cover the entire 4GB virtual address space. On 64-bit |
| 802 | // platforms, we support this using the CodeRange object, which reserves and |
| 803 | // manages a range of virtual memory. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 804 | class CodeRange { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 805 | public: |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 806 | explicit CodeRange(Isolate* isolate); |
| 807 | ~CodeRange() { TearDown(); } |
| 808 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 809 | // Reserves a range of virtual memory, but does not commit any of it. |
| 810 | // Can only be called once, at heap initialization time. |
| 811 | // Returns false on failure. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 812 | bool Setup(const size_t requested_size); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 813 | |
| 814 | // Frees the range of virtual memory, and frees the data structures used to |
| 815 | // manage it. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 816 | void TearDown(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 817 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 818 | bool exists() { return this != NULL && code_range_ != NULL; } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 819 | bool contains(Address address) { |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 820 | if (this == NULL || code_range_ == NULL) return false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 821 | Address start = static_cast<Address>(code_range_->address()); |
| 822 | return start <= address && address < start + code_range_->size(); |
| 823 | } |
| 824 | |
| 825 | // Allocates a chunk of memory from the large-object portion of |
| 826 | // the code range. On platforms with no separate code range, should |
| 827 | // not be called. |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 828 | MUST_USE_RESULT Address AllocateRawMemory(const size_t requested, |
| 829 | size_t* allocated); |
| 830 | void FreeRawMemory(Address buf, size_t length); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 831 | |
| 832 | private: |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 833 | Isolate* isolate_; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 834 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 835 | // The reserved range of virtual memory that all code objects are put in. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 836 | VirtualMemory* code_range_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 837 | // Plain old data class, just a struct plus a constructor. |
| 838 | class FreeBlock { |
| 839 | public: |
| 840 | FreeBlock(Address start_arg, size_t size_arg) |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 841 | : start(start_arg), size(size_arg) { |
| 842 | ASSERT(IsAddressAligned(start, MemoryChunk::kAlignment)); |
| 843 | ASSERT(size >= static_cast<size_t>(Page::kPageSize)); |
| 844 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 845 | FreeBlock(void* start_arg, size_t size_arg) |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 846 | : start(static_cast<Address>(start_arg)), size(size_arg) { |
| 847 | ASSERT(IsAddressAligned(start, MemoryChunk::kAlignment)); |
| 848 | ASSERT(size >= static_cast<size_t>(Page::kPageSize)); |
| 849 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 850 | |
| 851 | Address start; |
| 852 | size_t size; |
| 853 | }; |
| 854 | |
| 855 | // Freed blocks of memory are added to the free list. When the allocation |
| 856 | // list is exhausted, the free list is sorted and merged to make the new |
| 857 | // allocation list. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 858 | List<FreeBlock> free_list_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 859 | // Memory is allocated from the free blocks on the allocation list. |
| 860 | // The block at current_allocation_block_index_ is the current block. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 861 | List<FreeBlock> allocation_list_; |
| 862 | int current_allocation_block_index_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 863 | |
| 864 | // Finds a block on the allocation list that contains at least the |
| 865 | // requested amount of memory. If none is found, sorts and merges |
| 866 | // the existing free memory blocks, and searches again. |
| 867 | // If none can be found, terminates V8 with FatalProcessOutOfMemory. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 868 | void GetNextAllocationBlock(size_t requested); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 869 | // Compares the start addresses of two free blocks. |
| 870 | static int CompareFreeBlockAddress(const FreeBlock* left, |
| 871 | const FreeBlock* right); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 872 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 873 | DISALLOW_COPY_AND_ASSIGN(CodeRange); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 874 | }; |
| 875 | |
| 876 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 877 | class SkipList { |
| 878 | public: |
| 879 | SkipList() { |
| 880 | Clear(); |
| 881 | } |
| 882 | |
| 883 | void Clear() { |
| 884 | for (int idx = 0; idx < kSize; idx++) { |
| 885 | starts_[idx] = reinterpret_cast<Address>(-1); |
| 886 | } |
| 887 | } |
| 888 | |
| 889 | Address StartFor(Address addr) { |
| 890 | return starts_[RegionNumber(addr)]; |
| 891 | } |
| 892 | |
| 893 | void AddObject(Address addr, int size) { |
| 894 | int start_region = RegionNumber(addr); |
| 895 | int end_region = RegionNumber(addr + size - kPointerSize); |
| 896 | for (int idx = start_region; idx <= end_region; idx++) { |
| 897 | if (starts_[idx] > addr) starts_[idx] = addr; |
| 898 | } |
| 899 | } |
| 900 | |
| 901 | static inline int RegionNumber(Address addr) { |
| 902 | return (OffsetFrom(addr) & Page::kPageAlignmentMask) >> kRegionSizeLog2; |
| 903 | } |
| 904 | |
| 905 | static void Update(Address addr, int size) { |
| 906 | Page* page = Page::FromAddress(addr); |
| 907 | SkipList* list = page->skip_list(); |
| 908 | if (list == NULL) { |
| 909 | list = new SkipList(); |
| 910 | page->set_skip_list(list); |
| 911 | } |
| 912 | |
| 913 | list->AddObject(addr, size); |
| 914 | } |
| 915 | |
| 916 | private: |
| 917 | static const int kRegionSizeLog2 = 13; |
| 918 | static const int kRegionSize = 1 << kRegionSizeLog2; |
| 919 | static const int kSize = Page::kPageSize / kRegionSize; |
| 920 | |
| 921 | STATIC_ASSERT(Page::kPageSize % kRegionSize == 0); |
| 922 | |
| 923 | Address starts_[kSize]; |
| 924 | }; |
| 925 | |
| 926 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 927 | // ---------------------------------------------------------------------------- |
| 928 | // A space acquires chunks of memory from the operating system. The memory |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 929 | // allocator allocated and deallocates pages for the paged heap spaces and large |
| 930 | // pages for large object space. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 931 | // |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 932 | // Each space has to manage it's own pages. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 933 | // |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 934 | class MemoryAllocator { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 935 | public: |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 936 | explicit MemoryAllocator(Isolate* isolate); |
| 937 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 938 | // Initializes its internal bookkeeping structures. |
Russell Brenner | 90bac25 | 2010-11-18 13:33:46 -0800 | [diff] [blame] | 939 | // Max capacity of the total space and executable memory limit. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 940 | bool Setup(intptr_t max_capacity, intptr_t capacity_executable); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 941 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 942 | void TearDown(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 943 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 944 | Page* AllocatePage(PagedSpace* owner, Executability executable); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 945 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 946 | LargePage* AllocateLargePage(intptr_t object_size, |
| 947 | Executability executable, |
| 948 | Space* owner); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 949 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 950 | void Free(MemoryChunk* chunk); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 951 | |
| 952 | // Returns the maximum available bytes of heaps. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 953 | intptr_t Available() { return capacity_ < size_ ? 0 : capacity_ - size_; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 954 | |
| 955 | // Returns allocated spaces in bytes. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 956 | intptr_t Size() { return size_; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 957 | |
Russell Brenner | 90bac25 | 2010-11-18 13:33:46 -0800 | [diff] [blame] | 958 | // Returns the maximum available executable bytes of heaps. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 959 | intptr_t AvailableExecutable() { |
Russell Brenner | 90bac25 | 2010-11-18 13:33:46 -0800 | [diff] [blame] | 960 | if (capacity_executable_ < size_executable_) return 0; |
| 961 | return capacity_executable_ - size_executable_; |
| 962 | } |
| 963 | |
Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 964 | // Returns allocated executable spaces in bytes. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 965 | intptr_t SizeExecutable() { return size_executable_; } |
Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 966 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 967 | // Returns maximum available bytes that the old space can have. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 968 | intptr_t MaxAvailable() { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 969 | return (Available() / Page::kPageSize) * Page::kMaxNonCodeHeapObjectSize; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 970 | } |
| 971 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 972 | #ifdef DEBUG |
| 973 | // Reports statistic info of the space. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 974 | void ReportStatistics(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 975 | #endif |
| 976 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 977 | MemoryChunk* AllocateChunk(intptr_t body_size, |
| 978 | Executability executable, |
| 979 | Space* space); |
| 980 | |
| 981 | Address ReserveAlignedMemory(size_t requested, |
| 982 | size_t alignment, |
| 983 | VirtualMemory* controller); |
| 984 | Address AllocateAlignedMemory(size_t requested, |
| 985 | size_t alignment, |
| 986 | Executability executable, |
| 987 | VirtualMemory* controller); |
| 988 | |
| 989 | void FreeMemory(VirtualMemory* reservation, Executability executable); |
| 990 | void FreeMemory(Address addr, size_t size, Executability executable); |
| 991 | |
| 992 | // Commit a contiguous block of memory from the initial chunk. Assumes that |
| 993 | // the address is not NULL, the size is greater than zero, and that the |
| 994 | // block is contained in the initial chunk. Returns true if it succeeded |
| 995 | // and false otherwise. |
| 996 | bool CommitBlock(Address start, size_t size, Executability executable); |
| 997 | |
| 998 | // Uncommit a contiguous block of memory [start..(start+size)[. |
| 999 | // start is not NULL, the size is greater than zero, and the |
| 1000 | // block is contained in the initial chunk. Returns true if it succeeded |
| 1001 | // and false otherwise. |
| 1002 | bool UncommitBlock(Address start, size_t size); |
| 1003 | |
| 1004 | // Zaps a contiguous block of memory [start..(start+size)[ thus |
| 1005 | // filling it up with a recognizable non-NULL bit pattern. |
| 1006 | void ZapBlock(Address start, size_t size); |
| 1007 | |
| 1008 | void PerformAllocationCallback(ObjectSpace space, |
| 1009 | AllocationAction action, |
| 1010 | size_t size); |
| 1011 | |
| 1012 | void AddMemoryAllocationCallback(MemoryAllocationCallback callback, |
| 1013 | ObjectSpace space, |
| 1014 | AllocationAction action); |
| 1015 | |
| 1016 | void RemoveMemoryAllocationCallback( |
| 1017 | MemoryAllocationCallback callback); |
| 1018 | |
| 1019 | bool MemoryAllocationCallbackRegistered( |
| 1020 | MemoryAllocationCallback callback); |
| 1021 | |
| 1022 | static int CodePageGuardStartOffset(); |
| 1023 | |
| 1024 | static int CodePageGuardSize(); |
| 1025 | |
| 1026 | static int CodePageAreaStartOffset(); |
| 1027 | |
| 1028 | static int CodePageAreaEndOffset(); |
| 1029 | |
| 1030 | static int CodePageAreaSize() { |
| 1031 | return CodePageAreaEndOffset() - CodePageAreaStartOffset(); |
| 1032 | } |
| 1033 | |
| 1034 | static bool CommitCodePage(VirtualMemory* vm, Address start, size_t size); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1035 | |
| 1036 | private: |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 1037 | Isolate* isolate_; |
| 1038 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1039 | // Maximum space size in bytes. |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1040 | size_t capacity_; |
Russell Brenner | 90bac25 | 2010-11-18 13:33:46 -0800 | [diff] [blame] | 1041 | // Maximum subset of capacity_ that can be executable |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1042 | size_t capacity_executable_; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1043 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1044 | // Allocated space size in bytes. |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1045 | size_t size_; |
Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 1046 | // Allocated executable space size in bytes. |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1047 | size_t size_executable_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1048 | |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 1049 | struct MemoryAllocationCallbackRegistration { |
| 1050 | MemoryAllocationCallbackRegistration(MemoryAllocationCallback callback, |
| 1051 | ObjectSpace space, |
| 1052 | AllocationAction action) |
| 1053 | : callback(callback), space(space), action(action) { |
| 1054 | } |
| 1055 | MemoryAllocationCallback callback; |
| 1056 | ObjectSpace space; |
| 1057 | AllocationAction action; |
| 1058 | }; |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1059 | |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 1060 | // A List of callback that are triggered when memory is allocated or free'd |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1061 | List<MemoryAllocationCallbackRegistration> |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 1062 | memory_allocation_callbacks_; |
| 1063 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1064 | // Initializes pages in a chunk. Returns the first page address. |
| 1065 | // This function and GetChunkId() are provided for the mark-compact |
| 1066 | // collector to rebuild page headers in the from space, which is |
| 1067 | // used as a marking stack and its page headers are destroyed. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1068 | Page* InitializePagesInChunk(int chunk_id, int pages_in_chunk, |
| 1069 | PagedSpace* owner); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1070 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1071 | DISALLOW_IMPLICIT_CONSTRUCTORS(MemoryAllocator); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1072 | }; |
| 1073 | |
| 1074 | |
| 1075 | // ----------------------------------------------------------------------------- |
| 1076 | // Interface for heap object iterator to be implemented by all object space |
| 1077 | // object iterators. |
| 1078 | // |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 1079 | // NOTE: The space specific object iterators also implements the own next() |
| 1080 | // method which is used to avoid using virtual functions |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1081 | // iterating a specific space. |
| 1082 | |
| 1083 | class ObjectIterator : public Malloced { |
| 1084 | public: |
| 1085 | virtual ~ObjectIterator() { } |
| 1086 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1087 | virtual HeapObject* next_object() = 0; |
| 1088 | }; |
| 1089 | |
| 1090 | |
| 1091 | // ----------------------------------------------------------------------------- |
| 1092 | // Heap object iterator in new/old/map spaces. |
| 1093 | // |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1094 | // A HeapObjectIterator iterates objects from the bottom of the given space |
| 1095 | // to its top or from the bottom of the given page to its top. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1096 | // |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1097 | // If objects are allocated in the page during iteration the iterator may |
| 1098 | // or may not iterate over those objects. The caller must create a new |
| 1099 | // iterator in order to be sure to visit these new objects. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1100 | class HeapObjectIterator: public ObjectIterator { |
| 1101 | public: |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1102 | // Creates a new object iterator in a given space. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1103 | // If the size function is not given, the iterator calls the default |
| 1104 | // Object::Size(). |
| 1105 | explicit HeapObjectIterator(PagedSpace* space); |
| 1106 | HeapObjectIterator(PagedSpace* space, HeapObjectCallback size_func); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 1107 | HeapObjectIterator(Page* page, HeapObjectCallback size_func); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1108 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1109 | // Advance to the next object, skipping free spaces and other fillers and |
| 1110 | // skipping the special garbage section of which there is one per space. |
| 1111 | // Returns NULL when the iteration has ended. |
| 1112 | inline HeapObject* Next() { |
| 1113 | do { |
| 1114 | HeapObject* next_obj = FromCurrentPage(); |
| 1115 | if (next_obj != NULL) return next_obj; |
| 1116 | } while (AdvanceToNextPage()); |
| 1117 | return NULL; |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 1118 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1119 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1120 | virtual HeapObject* next_object() { |
| 1121 | return Next(); |
| 1122 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1123 | |
| 1124 | private: |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1125 | enum PageMode { kOnePageOnly, kAllPagesInSpace }; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1126 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1127 | Address cur_addr_; // Current iteration point. |
| 1128 | Address cur_end_; // End iteration point. |
| 1129 | HeapObjectCallback size_func_; // Size function or NULL. |
| 1130 | PagedSpace* space_; |
| 1131 | PageMode page_mode_; |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 1132 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1133 | // Fast (inlined) path of next(). |
| 1134 | inline HeapObject* FromCurrentPage(); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 1135 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1136 | // Slow path of next(), goes into the next page. Returns false if the |
| 1137 | // iteration has ended. |
| 1138 | bool AdvanceToNextPage(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1139 | |
| 1140 | // Initializes fields. |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1141 | inline void Initialize(PagedSpace* owner, |
| 1142 | Address start, |
| 1143 | Address end, |
| 1144 | PageMode mode, |
| 1145 | HeapObjectCallback size_func); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1146 | }; |
| 1147 | |
| 1148 | |
| 1149 | // ----------------------------------------------------------------------------- |
| 1150 | // A PageIterator iterates the pages in a paged space. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1151 | |
| 1152 | class PageIterator BASE_EMBEDDED { |
| 1153 | public: |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1154 | explicit inline PageIterator(PagedSpace* space); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1155 | |
| 1156 | inline bool has_next(); |
| 1157 | inline Page* next(); |
| 1158 | |
| 1159 | private: |
| 1160 | PagedSpace* space_; |
| 1161 | Page* prev_page_; // Previous page returned. |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1162 | // Next page that will be returned. Cached here so that we can use this |
| 1163 | // iterator for operations that deallocate pages. |
| 1164 | Page* next_page_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1165 | }; |
| 1166 | |
| 1167 | |
| 1168 | // ----------------------------------------------------------------------------- |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1169 | // A space has a circular list of pages. The next page can be accessed via |
| 1170 | // Page::next_page() call. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1171 | |
| 1172 | // An abstraction of allocation and relocation pointers in a page-structured |
| 1173 | // space. |
| 1174 | class AllocationInfo { |
| 1175 | public: |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1176 | AllocationInfo() : top(NULL), limit(NULL) { |
| 1177 | } |
| 1178 | |
| 1179 | Address top; // Current allocation top. |
| 1180 | Address limit; // Current allocation limit. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1181 | |
| 1182 | #ifdef DEBUG |
| 1183 | bool VerifyPagedAllocation() { |
| 1184 | return (Page::FromAllocationTop(top) == Page::FromAllocationTop(limit)) |
| 1185 | && (top <= limit); |
| 1186 | } |
| 1187 | #endif |
| 1188 | }; |
| 1189 | |
| 1190 | |
| 1191 | // An abstraction of the accounting statistics of a page-structured space. |
| 1192 | // The 'capacity' of a space is the number of object-area bytes (ie, not |
| 1193 | // including page bookkeeping structures) currently in the space. The 'size' |
| 1194 | // of a space is the number of allocated bytes, the 'waste' in the space is |
| 1195 | // the number of bytes that are not allocated and not available to |
| 1196 | // allocation without reorganizing the space via a GC (eg, small blocks due |
| 1197 | // to internal fragmentation, top of page areas in map space), and the bytes |
| 1198 | // 'available' is the number of unallocated bytes that are not waste. The |
| 1199 | // capacity is the sum of size, waste, and available. |
| 1200 | // |
| 1201 | // The stats are only set by functions that ensure they stay balanced. These |
| 1202 | // functions increase or decrease one of the non-capacity stats in |
| 1203 | // conjunction with capacity, or else they always balance increases and |
| 1204 | // decreases to the non-capacity stats. |
| 1205 | class AllocationStats BASE_EMBEDDED { |
| 1206 | public: |
| 1207 | AllocationStats() { Clear(); } |
| 1208 | |
| 1209 | // Zero out all the allocation statistics (ie, no capacity). |
| 1210 | void Clear() { |
| 1211 | capacity_ = 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1212 | size_ = 0; |
| 1213 | waste_ = 0; |
| 1214 | } |
| 1215 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1216 | void ClearSizeWaste() { |
| 1217 | size_ = capacity_; |
| 1218 | waste_ = 0; |
| 1219 | } |
| 1220 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1221 | // Reset the allocation statistics (ie, available = capacity with no |
| 1222 | // wasted or allocated bytes). |
| 1223 | void Reset() { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1224 | size_ = 0; |
| 1225 | waste_ = 0; |
| 1226 | } |
| 1227 | |
| 1228 | // Accessors for the allocation statistics. |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 1229 | intptr_t Capacity() { return capacity_; } |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 1230 | intptr_t Size() { return size_; } |
| 1231 | intptr_t Waste() { return waste_; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1232 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1233 | // Grow the space by adding available bytes. They are initially marked as |
| 1234 | // being in use (part of the size), but will normally be immediately freed, |
| 1235 | // putting them on the free list and removing them from size_. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1236 | void ExpandSpace(int size_in_bytes) { |
| 1237 | capacity_ += size_in_bytes; |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1238 | size_ += size_in_bytes; |
| 1239 | ASSERT(size_ >= 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1240 | } |
| 1241 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1242 | // Shrink the space by removing available bytes. Since shrinking is done |
| 1243 | // during sweeping, bytes have been marked as being in use (part of the size) |
| 1244 | // and are hereby freed. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1245 | void ShrinkSpace(int size_in_bytes) { |
| 1246 | capacity_ -= size_in_bytes; |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1247 | size_ -= size_in_bytes; |
| 1248 | ASSERT(size_ >= 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1249 | } |
| 1250 | |
| 1251 | // Allocate from available bytes (available -> size). |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 1252 | void AllocateBytes(intptr_t size_in_bytes) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1253 | size_ += size_in_bytes; |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1254 | ASSERT(size_ >= 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1255 | } |
| 1256 | |
| 1257 | // Free allocated bytes, making them available (size -> available). |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 1258 | void DeallocateBytes(intptr_t size_in_bytes) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1259 | size_ -= size_in_bytes; |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1260 | ASSERT(size_ >= 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1261 | } |
| 1262 | |
| 1263 | // Waste free bytes (available -> waste). |
| 1264 | void WasteBytes(int size_in_bytes) { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1265 | size_ -= size_in_bytes; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1266 | waste_ += size_in_bytes; |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1267 | ASSERT(size_ >= 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1268 | } |
| 1269 | |
| 1270 | private: |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 1271 | intptr_t capacity_; |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 1272 | intptr_t size_; |
| 1273 | intptr_t waste_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1274 | }; |
| 1275 | |
| 1276 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1277 | // ----------------------------------------------------------------------------- |
| 1278 | // Free lists for old object spaces |
| 1279 | // |
| 1280 | // Free-list nodes are free blocks in the heap. They look like heap objects |
| 1281 | // (free-list node pointers have the heap object tag, and they have a map like |
| 1282 | // a heap object). They have a size and a next pointer. The next pointer is |
| 1283 | // the raw address of the next free list node (or NULL). |
| 1284 | class FreeListNode: public HeapObject { |
| 1285 | public: |
| 1286 | // Obtain a free-list node from a raw address. This is not a cast because |
| 1287 | // it does not check nor require that the first word at the address is a map |
| 1288 | // pointer. |
| 1289 | static FreeListNode* FromAddress(Address address) { |
| 1290 | return reinterpret_cast<FreeListNode*>(HeapObject::FromAddress(address)); |
| 1291 | } |
| 1292 | |
| 1293 | static inline bool IsFreeListNode(HeapObject* object); |
| 1294 | |
| 1295 | // Set the size in bytes, which can be read with HeapObject::Size(). This |
| 1296 | // function also writes a map to the first word of the block so that it |
| 1297 | // looks like a heap object to the garbage collector and heap iteration |
| 1298 | // functions. |
| 1299 | void set_size(Heap* heap, int size_in_bytes); |
| 1300 | |
| 1301 | // Accessors for the next field. |
| 1302 | inline FreeListNode* next(); |
| 1303 | inline FreeListNode** next_address(); |
| 1304 | inline void set_next(FreeListNode* next); |
| 1305 | |
| 1306 | inline void Zap(); |
| 1307 | |
| 1308 | private: |
| 1309 | static const int kNextOffset = POINTER_SIZE_ALIGN(FreeSpace::kHeaderSize); |
| 1310 | |
| 1311 | DISALLOW_IMPLICIT_CONSTRUCTORS(FreeListNode); |
| 1312 | }; |
| 1313 | |
| 1314 | |
| 1315 | // The free list for the old space. The free list is organized in such a way |
| 1316 | // as to encourage objects allocated around the same time to be near each |
| 1317 | // other. The normal way to allocate is intended to be by bumping a 'top' |
| 1318 | // pointer until it hits a 'limit' pointer. When the limit is hit we need to |
| 1319 | // find a new space to allocate from. This is done with the free list, which |
| 1320 | // is divided up into rough categories to cut down on waste. Having finer |
| 1321 | // categories would scatter allocation more. |
| 1322 | |
| 1323 | // The old space free list is organized in categories. |
| 1324 | // 1-31 words: Such small free areas are discarded for efficiency reasons. |
| 1325 | // They can be reclaimed by the compactor. However the distance between top |
| 1326 | // and limit may be this small. |
| 1327 | // 32-255 words: There is a list of spaces this large. It is used for top and |
| 1328 | // limit when the object we need to allocate is 1-31 words in size. These |
| 1329 | // spaces are called small. |
| 1330 | // 256-2047 words: There is a list of spaces this large. It is used for top and |
| 1331 | // limit when the object we need to allocate is 32-255 words in size. These |
| 1332 | // spaces are called medium. |
| 1333 | // 1048-16383 words: There is a list of spaces this large. It is used for top |
| 1334 | // and limit when the object we need to allocate is 256-2047 words in size. |
| 1335 | // These spaces are call large. |
| 1336 | // At least 16384 words. This list is for objects of 2048 words or larger. |
| 1337 | // Empty pages are added to this list. These spaces are called huge. |
| 1338 | class FreeList BASE_EMBEDDED { |
| 1339 | public: |
| 1340 | explicit FreeList(PagedSpace* owner); |
| 1341 | |
| 1342 | // Clear the free list. |
| 1343 | void Reset(); |
| 1344 | |
| 1345 | // Return the number of bytes available on the free list. |
| 1346 | intptr_t available() { return available_; } |
| 1347 | |
| 1348 | // Place a node on the free list. The block of size 'size_in_bytes' |
| 1349 | // starting at 'start' is placed on the free list. The return value is the |
| 1350 | // number of bytes that have been lost due to internal fragmentation by |
| 1351 | // freeing the block. Bookkeeping information will be written to the block, |
| 1352 | // ie, its contents will be destroyed. The start address should be word |
| 1353 | // aligned, and the size should be a non-zero multiple of the word size. |
| 1354 | int Free(Address start, int size_in_bytes); |
| 1355 | |
| 1356 | // Allocate a block of size 'size_in_bytes' from the free list. The block |
| 1357 | // is unitialized. A failure is returned if no block is available. The |
| 1358 | // number of bytes lost to fragmentation is returned in the output parameter |
| 1359 | // 'wasted_bytes'. The size should be a non-zero multiple of the word size. |
| 1360 | MUST_USE_RESULT HeapObject* Allocate(int size_in_bytes); |
| 1361 | |
| 1362 | #ifdef DEBUG |
| 1363 | void Zap(); |
| 1364 | static intptr_t SumFreeList(FreeListNode* node); |
| 1365 | static int FreeListLength(FreeListNode* cur); |
| 1366 | intptr_t SumFreeLists(); |
| 1367 | bool IsVeryLong(); |
| 1368 | #endif |
| 1369 | |
| 1370 | struct SizeStats { |
| 1371 | intptr_t Total() { |
| 1372 | return small_size_ + medium_size_ + large_size_ + huge_size_; |
| 1373 | } |
| 1374 | |
| 1375 | intptr_t small_size_; |
| 1376 | intptr_t medium_size_; |
| 1377 | intptr_t large_size_; |
| 1378 | intptr_t huge_size_; |
| 1379 | }; |
| 1380 | |
| 1381 | void CountFreeListItems(Page* p, SizeStats* sizes); |
| 1382 | |
| 1383 | intptr_t EvictFreeListItems(Page* p); |
| 1384 | |
| 1385 | private: |
| 1386 | // The size range of blocks, in bytes. |
| 1387 | static const int kMinBlockSize = 3 * kPointerSize; |
| 1388 | static const int kMaxBlockSize = Page::kMaxNonCodeHeapObjectSize; |
| 1389 | |
| 1390 | FreeListNode* PickNodeFromList(FreeListNode** list, int* node_size); |
| 1391 | |
| 1392 | FreeListNode* FindNodeFor(int size_in_bytes, int* node_size); |
| 1393 | |
| 1394 | PagedSpace* owner_; |
| 1395 | Heap* heap_; |
| 1396 | |
| 1397 | // Total available bytes in all blocks on this free list. |
| 1398 | int available_; |
| 1399 | |
| 1400 | static const int kSmallListMin = 0x20 * kPointerSize; |
| 1401 | static const int kSmallListMax = 0xff * kPointerSize; |
| 1402 | static const int kMediumListMax = 0x7ff * kPointerSize; |
| 1403 | static const int kLargeListMax = 0x3fff * kPointerSize; |
| 1404 | static const int kSmallAllocationMax = kSmallListMin - kPointerSize; |
| 1405 | static const int kMediumAllocationMax = kSmallListMax; |
| 1406 | static const int kLargeAllocationMax = kMediumListMax; |
| 1407 | FreeListNode* small_list_; |
| 1408 | FreeListNode* medium_list_; |
| 1409 | FreeListNode* large_list_; |
| 1410 | FreeListNode* huge_list_; |
| 1411 | |
| 1412 | DISALLOW_IMPLICIT_CONSTRUCTORS(FreeList); |
| 1413 | }; |
| 1414 | |
| 1415 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1416 | class PagedSpace : public Space { |
| 1417 | public: |
| 1418 | // Creates a space with a maximum capacity, and an id. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1419 | PagedSpace(Heap* heap, |
| 1420 | intptr_t max_capacity, |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 1421 | AllocationSpace id, |
| 1422 | Executability executable); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1423 | |
| 1424 | virtual ~PagedSpace() {} |
| 1425 | |
| 1426 | // Set up the space using the given address range of virtual memory (from |
| 1427 | // the memory allocator's initial chunk) if possible. If the block of |
| 1428 | // addresses is not big enough to contain a single page-aligned page, a |
| 1429 | // fresh chunk will be allocated. |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1430 | bool Setup(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1431 | |
| 1432 | // Returns true if the space has been successfully set up and not |
| 1433 | // subsequently torn down. |
| 1434 | bool HasBeenSetup(); |
| 1435 | |
| 1436 | // Cleans up the space, frees all pages in this space except those belonging |
| 1437 | // to the initial chunk, uncommits addresses in the initial chunk. |
| 1438 | void TearDown(); |
| 1439 | |
| 1440 | // Checks whether an object/address is in this space. |
| 1441 | inline bool Contains(Address a); |
| 1442 | bool Contains(HeapObject* o) { return Contains(o->address()); } |
| 1443 | |
| 1444 | // Given an address occupied by a live object, return that object if it is |
| 1445 | // in this space, or Failure::Exception() if it is not. The implementation |
| 1446 | // iterates over objects in the page containing the address, the cost is |
| 1447 | // linear in the number of objects in the page. It may be slow. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1448 | MUST_USE_RESULT MaybeObject* FindObject(Address addr); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1449 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1450 | // Prepares for a mark-compact GC. |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1451 | virtual void PrepareForMarkCompact(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1452 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1453 | // Current capacity without growing (Size() + Available()). |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 1454 | intptr_t Capacity() { return accounting_stats_.Capacity(); } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1455 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 1456 | // Total amount of memory committed for this space. For paged |
| 1457 | // spaces this equals the capacity. |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 1458 | intptr_t CommittedMemory() { return Capacity(); } |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 1459 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1460 | // Sets the capacity, the available space and the wasted space to zero. |
| 1461 | // The stats are rebuilt during sweeping by adding each page to the |
| 1462 | // capacity and the size when it is encountered. As free spaces are |
| 1463 | // discovered during the sweeping they are subtracted from the size and added |
| 1464 | // to the available and wasted totals. |
| 1465 | void ClearStats() { |
| 1466 | accounting_stats_.ClearSizeWaste(); |
| 1467 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1468 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1469 | // Available bytes without growing. These are the bytes on the free list. |
| 1470 | // The bytes in the linear allocation area are not included in this total |
| 1471 | // because updating the stats would slow down allocation. New pages are |
| 1472 | // immediately added to the free list so they show up here. |
| 1473 | intptr_t Available() { return free_list_.available(); } |
| 1474 | |
| 1475 | // Allocated bytes in this space. Garbage bytes that were not found due to |
| 1476 | // lazy sweeping are counted as being allocated! The bytes in the current |
| 1477 | // linear allocation area (between top and limit) are also counted here. |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 1478 | virtual intptr_t Size() { return accounting_stats_.Size(); } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1479 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1480 | // As size, but the bytes in the current linear allocation area are not |
| 1481 | // included. |
| 1482 | virtual intptr_t SizeOfObjects() { return Size() - (limit() - top()); } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1483 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1484 | // Wasted bytes in this space. These are just the bytes that were thrown away |
| 1485 | // due to being too small to use for allocation. They do not include the |
| 1486 | // free bytes that were not found at all due to lazy sweeping. |
| 1487 | virtual intptr_t Waste() { return accounting_stats_.Waste(); } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1488 | |
| 1489 | // Returns the allocation pointer in this space. |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1490 | Address top() { |
| 1491 | return allocation_info_.top; |
| 1492 | } |
| 1493 | Address limit() { return allocation_info_.limit; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1494 | |
| 1495 | // Allocate the requested number of bytes in the space if possible, return a |
| 1496 | // failure object if not. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1497 | MUST_USE_RESULT inline MaybeObject* AllocateRaw(int size_in_bytes); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1498 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1499 | virtual bool ReserveSpace(int bytes); |
| 1500 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1501 | // Give a block of memory to the space's free list. It might be added to |
| 1502 | // the free list or accounted as waste. |
| 1503 | // If add_to_freelist is false then just accounting stats are updated and |
| 1504 | // no attempt to add area to free list is made. |
| 1505 | int Free(Address start, int size_in_bytes) { |
| 1506 | int wasted = free_list_.Free(start, size_in_bytes); |
| 1507 | accounting_stats_.DeallocateBytes(size_in_bytes - wasted); |
| 1508 | return size_in_bytes - wasted; |
| 1509 | } |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 1510 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1511 | // Set space allocation info. |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1512 | void SetTop(Address top, Address limit) { |
| 1513 | ASSERT(top == limit || |
| 1514 | Page::FromAddress(top) == Page::FromAddress(limit - 1)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1515 | allocation_info_.top = top; |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1516 | allocation_info_.limit = limit; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1517 | } |
| 1518 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1519 | void Allocate(int bytes) { |
| 1520 | accounting_stats_.AllocateBytes(bytes); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1521 | } |
| 1522 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1523 | void IncreaseCapacity(int size) { |
| 1524 | accounting_stats_.ExpandSpace(size); |
| 1525 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1526 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1527 | // Releases an unused page and shrinks the space. |
| 1528 | void ReleasePage(Page* page); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1529 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1530 | // Releases all of the unused pages. |
| 1531 | void ReleaseAllUnusedPages(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1532 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1533 | // The dummy page that anchors the linked list of pages. |
| 1534 | Page* anchor() { return &anchor_; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1535 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1536 | #ifdef DEBUG |
| 1537 | // Print meta info and objects in this space. |
| 1538 | virtual void Print(); |
| 1539 | |
| 1540 | // Verify integrity of this space. |
| 1541 | virtual void Verify(ObjectVisitor* visitor); |
| 1542 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1543 | // Reports statistics for the space |
| 1544 | void ReportStatistics(); |
| 1545 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1546 | // Overridden by subclasses to verify space-specific object |
| 1547 | // properties (e.g., only maps or free-list nodes are in map space). |
| 1548 | virtual void VerifyObject(HeapObject* obj) {} |
| 1549 | |
| 1550 | // Report code object related statistics |
| 1551 | void CollectCodeStatistics(); |
| 1552 | static void ReportCodeStatistics(); |
| 1553 | static void ResetCodeStatistics(); |
| 1554 | #endif |
| 1555 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1556 | bool was_swept_conservatively() { return was_swept_conservatively_; } |
| 1557 | void set_was_swept_conservatively(bool b) { was_swept_conservatively_ = b; } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1558 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1559 | // Evacuation candidates are swept by evacuator. Needs to return a valid |
| 1560 | // result before _and_ after evacuation has finished. |
| 1561 | static bool ShouldBeSweptLazily(Page* p) { |
| 1562 | return !p->IsEvacuationCandidate() && |
| 1563 | !p->IsFlagSet(Page::RESCAN_ON_EVACUATION) && |
| 1564 | !p->WasSweptPrecisely(); |
| 1565 | } |
| 1566 | |
| 1567 | void SetPagesToSweep(Page* first) { |
| 1568 | if (first == &anchor_) first = NULL; |
| 1569 | first_unswept_page_ = first; |
| 1570 | } |
| 1571 | |
| 1572 | bool AdvanceSweeper(intptr_t bytes_to_sweep); |
| 1573 | |
| 1574 | bool IsSweepingComplete() { |
| 1575 | return !first_unswept_page_->is_valid(); |
| 1576 | } |
| 1577 | |
| 1578 | Page* FirstPage() { return anchor_.next_page(); } |
| 1579 | Page* LastPage() { return anchor_.prev_page(); } |
| 1580 | |
| 1581 | // Returns zero for pages that have so little fragmentation that it is not |
| 1582 | // worth defragmenting them. Otherwise a positive integer that gives an |
| 1583 | // estimate of fragmentation on an arbitrary scale. |
| 1584 | int Fragmentation(Page* p) { |
| 1585 | FreeList::SizeStats sizes; |
| 1586 | free_list_.CountFreeListItems(p, &sizes); |
| 1587 | |
| 1588 | intptr_t ratio; |
| 1589 | intptr_t ratio_threshold; |
| 1590 | if (identity() == CODE_SPACE) { |
| 1591 | ratio = (sizes.medium_size_ * 10 + sizes.large_size_ * 2) * 100 / |
| 1592 | AreaSize(); |
| 1593 | ratio_threshold = 10; |
| 1594 | } else { |
| 1595 | ratio = (sizes.small_size_ * 5 + sizes.medium_size_) * 100 / |
| 1596 | AreaSize(); |
| 1597 | ratio_threshold = 15; |
| 1598 | } |
| 1599 | |
| 1600 | if (FLAG_trace_fragmentation) { |
| 1601 | PrintF("%p [%d]: %d (%.2f%%) %d (%.2f%%) %d (%.2f%%) %d (%.2f%%) %s\n", |
| 1602 | reinterpret_cast<void*>(p), |
| 1603 | identity(), |
| 1604 | static_cast<int>(sizes.small_size_), |
| 1605 | static_cast<double>(sizes.small_size_ * 100) / |
| 1606 | AreaSize(), |
| 1607 | static_cast<int>(sizes.medium_size_), |
| 1608 | static_cast<double>(sizes.medium_size_ * 100) / |
| 1609 | AreaSize(), |
| 1610 | static_cast<int>(sizes.large_size_), |
| 1611 | static_cast<double>(sizes.large_size_ * 100) / |
| 1612 | AreaSize(), |
| 1613 | static_cast<int>(sizes.huge_size_), |
| 1614 | static_cast<double>(sizes.huge_size_ * 100) / |
| 1615 | AreaSize(), |
| 1616 | (ratio > ratio_threshold) ? "[fragmented]" : ""); |
| 1617 | } |
| 1618 | |
| 1619 | if (FLAG_always_compact && sizes.Total() != AreaSize()) { |
| 1620 | return 1; |
| 1621 | } |
| 1622 | if (ratio <= ratio_threshold) return 0; // Not fragmented. |
| 1623 | |
| 1624 | return static_cast<int>(ratio - ratio_threshold); |
| 1625 | } |
| 1626 | |
| 1627 | void EvictEvacuationCandidatesFromFreeLists(); |
| 1628 | |
| 1629 | bool CanExpand(); |
| 1630 | |
| 1631 | // Returns the number of total pages in this space. |
| 1632 | int CountTotalPages(); |
| 1633 | |
| 1634 | // Return size of allocatable area on a page in this space. |
| 1635 | inline int AreaSize() { |
| 1636 | return area_size_; |
| 1637 | } |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 1638 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1639 | protected: |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1640 | int area_size_; |
| 1641 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1642 | // Maximum capacity of this space. |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 1643 | intptr_t max_capacity_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1644 | |
| 1645 | // Accounting information for this space. |
| 1646 | AllocationStats accounting_stats_; |
| 1647 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1648 | // The dummy page that anchors the double linked list of pages. |
| 1649 | Page anchor_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1650 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1651 | // The space's free list. |
| 1652 | FreeList free_list_; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1653 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1654 | // Normal allocation information. |
| 1655 | AllocationInfo allocation_info_; |
| 1656 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1657 | // Bytes of each page that cannot be allocated. Possibly non-zero |
| 1658 | // for pages in spaces with only fixed-size objects. Always zero |
| 1659 | // for pages in spaces with variable sized objects (those pages are |
| 1660 | // padded with free-list nodes). |
| 1661 | int page_extra_; |
| 1662 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1663 | bool was_swept_conservatively_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1664 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1665 | Page* first_unswept_page_; |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 1666 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1667 | // Expands the space by allocating a fixed number of pages. Returns false if |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1668 | // it cannot allocate requested number of pages from OS. |
| 1669 | bool Expand(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1670 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1671 | // Generic fast case allocation function that tries linear allocation at the |
| 1672 | // address denoted by top in allocation_info_. |
| 1673 | inline HeapObject* AllocateLinearly(int size_in_bytes); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1674 | |
| 1675 | // Slow path of AllocateRaw. This function is space-dependent. |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1676 | MUST_USE_RESULT virtual HeapObject* SlowAllocateRaw(int size_in_bytes); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1677 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1678 | friend class PageIterator; |
| 1679 | }; |
| 1680 | |
| 1681 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1682 | class NumberAndSizeInfo BASE_EMBEDDED { |
| 1683 | public: |
| 1684 | NumberAndSizeInfo() : number_(0), bytes_(0) {} |
| 1685 | |
| 1686 | int number() const { return number_; } |
| 1687 | void increment_number(int num) { number_ += num; } |
| 1688 | |
| 1689 | int bytes() const { return bytes_; } |
| 1690 | void increment_bytes(int size) { bytes_ += size; } |
| 1691 | |
| 1692 | void clear() { |
| 1693 | number_ = 0; |
| 1694 | bytes_ = 0; |
| 1695 | } |
| 1696 | |
| 1697 | private: |
| 1698 | int number_; |
| 1699 | int bytes_; |
| 1700 | }; |
| 1701 | |
| 1702 | |
| 1703 | // HistogramInfo class for recording a single "bar" of a histogram. This |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1704 | // class is used for collecting statistics to print to the log file. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1705 | class HistogramInfo: public NumberAndSizeInfo { |
| 1706 | public: |
| 1707 | HistogramInfo() : NumberAndSizeInfo() {} |
| 1708 | |
| 1709 | const char* name() { return name_; } |
| 1710 | void set_name(const char* name) { name_ = name; } |
| 1711 | |
| 1712 | private: |
| 1713 | const char* name_; |
| 1714 | }; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1715 | |
| 1716 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1717 | enum SemiSpaceId { |
| 1718 | kFromSpace = 0, |
| 1719 | kToSpace = 1 |
| 1720 | }; |
| 1721 | |
| 1722 | |
| 1723 | class SemiSpace; |
| 1724 | |
| 1725 | |
| 1726 | class NewSpacePage : public MemoryChunk { |
| 1727 | public: |
| 1728 | // GC related flags copied from from-space to to-space when |
| 1729 | // flipping semispaces. |
| 1730 | static const intptr_t kCopyOnFlipFlagsMask = |
| 1731 | (1 << MemoryChunk::POINTERS_TO_HERE_ARE_INTERESTING) | |
| 1732 | (1 << MemoryChunk::POINTERS_FROM_HERE_ARE_INTERESTING) | |
| 1733 | (1 << MemoryChunk::SCAN_ON_SCAVENGE); |
| 1734 | |
| 1735 | static const int kAreaSize = Page::kNonCodeObjectAreaSize; |
| 1736 | |
| 1737 | inline NewSpacePage* next_page() const { |
| 1738 | return static_cast<NewSpacePage*>(next_chunk()); |
| 1739 | } |
| 1740 | |
| 1741 | inline void set_next_page(NewSpacePage* page) { |
| 1742 | set_next_chunk(page); |
| 1743 | } |
| 1744 | |
| 1745 | inline NewSpacePage* prev_page() const { |
| 1746 | return static_cast<NewSpacePage*>(prev_chunk()); |
| 1747 | } |
| 1748 | |
| 1749 | inline void set_prev_page(NewSpacePage* page) { |
| 1750 | set_prev_chunk(page); |
| 1751 | } |
| 1752 | |
| 1753 | SemiSpace* semi_space() { |
| 1754 | return reinterpret_cast<SemiSpace*>(owner()); |
| 1755 | } |
| 1756 | |
| 1757 | bool is_anchor() { return !this->InNewSpace(); } |
| 1758 | |
| 1759 | static bool IsAtStart(Address addr) { |
| 1760 | return (reinterpret_cast<intptr_t>(addr) & Page::kPageAlignmentMask) |
| 1761 | == kObjectStartOffset; |
| 1762 | } |
| 1763 | |
| 1764 | static bool IsAtEnd(Address addr) { |
| 1765 | return (reinterpret_cast<intptr_t>(addr) & Page::kPageAlignmentMask) == 0; |
| 1766 | } |
| 1767 | |
| 1768 | Address address() { |
| 1769 | return reinterpret_cast<Address>(this); |
| 1770 | } |
| 1771 | |
| 1772 | // Finds the NewSpacePage containg the given address. |
| 1773 | static inline NewSpacePage* FromAddress(Address address_in_page) { |
| 1774 | Address page_start = |
| 1775 | reinterpret_cast<Address>(reinterpret_cast<uintptr_t>(address_in_page) & |
| 1776 | ~Page::kPageAlignmentMask); |
| 1777 | NewSpacePage* page = reinterpret_cast<NewSpacePage*>(page_start); |
| 1778 | return page; |
| 1779 | } |
| 1780 | |
| 1781 | // Find the page for a limit address. A limit address is either an address |
| 1782 | // inside a page, or the address right after the last byte of a page. |
| 1783 | static inline NewSpacePage* FromLimit(Address address_limit) { |
| 1784 | return NewSpacePage::FromAddress(address_limit - 1); |
| 1785 | } |
| 1786 | |
| 1787 | private: |
| 1788 | // Create a NewSpacePage object that is only used as anchor |
| 1789 | // for the doubly-linked list of real pages. |
| 1790 | explicit NewSpacePage(SemiSpace* owner) { |
| 1791 | InitializeAsAnchor(owner); |
| 1792 | } |
| 1793 | |
| 1794 | static NewSpacePage* Initialize(Heap* heap, |
| 1795 | Address start, |
| 1796 | SemiSpace* semi_space); |
| 1797 | |
| 1798 | // Intialize a fake NewSpacePage used as sentinel at the ends |
| 1799 | // of a doubly-linked list of real NewSpacePages. |
| 1800 | // Only uses the prev/next links, and sets flags to not be in new-space. |
| 1801 | void InitializeAsAnchor(SemiSpace* owner); |
| 1802 | |
| 1803 | friend class SemiSpace; |
| 1804 | friend class SemiSpaceIterator; |
| 1805 | }; |
| 1806 | |
| 1807 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1808 | // ----------------------------------------------------------------------------- |
| 1809 | // SemiSpace in young generation |
| 1810 | // |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1811 | // A semispace is a contiguous chunk of memory holding page-like memory |
| 1812 | // chunks. The mark-compact collector uses the memory of the first page in |
| 1813 | // the from space as a marking stack when tracing live objects. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1814 | |
| 1815 | class SemiSpace : public Space { |
| 1816 | public: |
| 1817 | // Constructor. |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1818 | SemiSpace(Heap* heap, SemiSpaceId semispace) |
| 1819 | : Space(heap, NEW_SPACE, NOT_EXECUTABLE), |
| 1820 | start_(NULL), |
| 1821 | age_mark_(NULL), |
| 1822 | id_(semispace), |
| 1823 | anchor_(this), |
| 1824 | current_page_(NULL) { } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1825 | |
| 1826 | // Sets up the semispace using the given chunk. |
| 1827 | bool Setup(Address start, int initial_capacity, int maximum_capacity); |
| 1828 | |
| 1829 | // Tear down the space. Heap memory was not allocated by the space, so it |
| 1830 | // is not deallocated here. |
| 1831 | void TearDown(); |
| 1832 | |
| 1833 | // True if the space has been set up but not torn down. |
| 1834 | bool HasBeenSetup() { return start_ != NULL; } |
| 1835 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1836 | // Grow the semispace to the new capacity. The new capacity |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1837 | // requested must be larger than the current capacity and less than |
| 1838 | // the maximum capacity. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1839 | bool GrowTo(int new_capacity); |
| 1840 | |
| 1841 | // Shrinks the semispace to the new capacity. The new capacity |
| 1842 | // requested must be more than the amount of used memory in the |
| 1843 | // semispace and less than the current capacity. |
| 1844 | bool ShrinkTo(int new_capacity); |
| 1845 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1846 | // Returns the start address of the first page of the space. |
| 1847 | Address space_start() { |
| 1848 | ASSERT(anchor_.next_page() != &anchor_); |
| 1849 | return anchor_.next_page()->area_start(); |
| 1850 | } |
| 1851 | |
| 1852 | // Returns the start address of the current page of the space. |
| 1853 | Address page_low() { |
| 1854 | return current_page_->area_start(); |
| 1855 | } |
| 1856 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1857 | // Returns one past the end address of the space. |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1858 | Address space_end() { |
| 1859 | return anchor_.prev_page()->area_end(); |
| 1860 | } |
| 1861 | |
| 1862 | // Returns one past the end address of the current page of the space. |
| 1863 | Address page_high() { |
| 1864 | return current_page_->area_end(); |
| 1865 | } |
| 1866 | |
| 1867 | bool AdvancePage() { |
| 1868 | NewSpacePage* next_page = current_page_->next_page(); |
| 1869 | if (next_page == anchor()) return false; |
| 1870 | current_page_ = next_page; |
| 1871 | return true; |
| 1872 | } |
| 1873 | |
| 1874 | // Resets the space to using the first page. |
| 1875 | void Reset(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1876 | |
| 1877 | // Age mark accessors. |
| 1878 | Address age_mark() { return age_mark_; } |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1879 | void set_age_mark(Address mark); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1880 | |
| 1881 | // True if the address is in the address range of this semispace (not |
| 1882 | // necessarily below the allocation pointer). |
| 1883 | bool Contains(Address a) { |
| 1884 | return (reinterpret_cast<uintptr_t>(a) & address_mask_) |
| 1885 | == reinterpret_cast<uintptr_t>(start_); |
| 1886 | } |
| 1887 | |
| 1888 | // True if the object is a heap object in the address range of this |
| 1889 | // semispace (not necessarily below the allocation pointer). |
| 1890 | bool Contains(Object* o) { |
| 1891 | return (reinterpret_cast<uintptr_t>(o) & object_mask_) == object_expected_; |
| 1892 | } |
| 1893 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1894 | // If we don't have these here then SemiSpace will be abstract. However |
| 1895 | // they should never be called. |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 1896 | virtual intptr_t Size() { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1897 | UNREACHABLE(); |
| 1898 | return 0; |
| 1899 | } |
| 1900 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1901 | virtual bool ReserveSpace(int bytes) { |
| 1902 | UNREACHABLE(); |
| 1903 | return false; |
| 1904 | } |
| 1905 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1906 | bool is_committed() { return committed_; } |
| 1907 | bool Commit(); |
| 1908 | bool Uncommit(); |
| 1909 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1910 | NewSpacePage* first_page() { return anchor_.next_page(); } |
| 1911 | NewSpacePage* current_page() { return current_page_; } |
| 1912 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1913 | #ifdef DEBUG |
| 1914 | virtual void Print(); |
| 1915 | virtual void Verify(); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1916 | // Validate a range of of addresses in a SemiSpace. |
| 1917 | // The "from" address must be on a page prior to the "to" address, |
| 1918 | // in the linked page order, or it must be earlier on the same page. |
| 1919 | static void AssertValidRange(Address from, Address to); |
| 1920 | #else |
| 1921 | // Do nothing. |
| 1922 | inline static void AssertValidRange(Address from, Address to) {} |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1923 | #endif |
| 1924 | |
| 1925 | // Returns the current capacity of the semi space. |
| 1926 | int Capacity() { return capacity_; } |
| 1927 | |
| 1928 | // Returns the maximum capacity of the semi space. |
| 1929 | int MaximumCapacity() { return maximum_capacity_; } |
| 1930 | |
| 1931 | // Returns the initial capacity of the semi space. |
| 1932 | int InitialCapacity() { return initial_capacity_; } |
| 1933 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1934 | SemiSpaceId id() { return id_; } |
| 1935 | |
| 1936 | static void Swap(SemiSpace* from, SemiSpace* to); |
| 1937 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1938 | private: |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1939 | // Flips the semispace between being from-space and to-space. |
| 1940 | // Copies the flags into the masked positions on all pages in the space. |
| 1941 | void FlipPages(intptr_t flags, intptr_t flag_mask); |
| 1942 | |
| 1943 | NewSpacePage* anchor() { return &anchor_; } |
| 1944 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1945 | // The current and maximum capacity of the space. |
| 1946 | int capacity_; |
| 1947 | int maximum_capacity_; |
| 1948 | int initial_capacity_; |
| 1949 | |
| 1950 | // The start address of the space. |
| 1951 | Address start_; |
| 1952 | // Used to govern object promotion during mark-compact collection. |
| 1953 | Address age_mark_; |
| 1954 | |
| 1955 | // Masks and comparison values to test for containment in this semispace. |
| 1956 | uintptr_t address_mask_; |
| 1957 | uintptr_t object_mask_; |
| 1958 | uintptr_t object_expected_; |
| 1959 | |
| 1960 | bool committed_; |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1961 | SemiSpaceId id_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1962 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1963 | NewSpacePage anchor_; |
| 1964 | NewSpacePage* current_page_; |
| 1965 | |
| 1966 | friend class SemiSpaceIterator; |
| 1967 | friend class NewSpacePageIterator; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1968 | public: |
| 1969 | TRACK_MEMORY("SemiSpace") |
| 1970 | }; |
| 1971 | |
| 1972 | |
| 1973 | // A SemiSpaceIterator is an ObjectIterator that iterates over the active |
| 1974 | // semispace of the heap's new space. It iterates over the objects in the |
| 1975 | // semispace from a given start address (defaulting to the bottom of the |
| 1976 | // semispace) to the top of the semispace. New objects allocated after the |
| 1977 | // iterator is created are not iterated. |
| 1978 | class SemiSpaceIterator : public ObjectIterator { |
| 1979 | public: |
| 1980 | // Create an iterator over the objects in the given space. If no start |
| 1981 | // address is given, the iterator starts from the bottom of the space. If |
| 1982 | // no size function is given, the iterator calls Object::Size(). |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1983 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1984 | // Iterate over all of allocated to-space. |
| 1985 | explicit SemiSpaceIterator(NewSpace* space); |
| 1986 | // Iterate over all of allocated to-space, with a custome size function. |
| 1987 | SemiSpaceIterator(NewSpace* space, HeapObjectCallback size_func); |
| 1988 | // Iterate over part of allocated to-space, from start to the end |
| 1989 | // of allocation. |
| 1990 | SemiSpaceIterator(NewSpace* space, Address start); |
| 1991 | // Iterate from one address to another in the same semi-space. |
| 1992 | SemiSpaceIterator(Address from, Address to); |
| 1993 | |
| 1994 | HeapObject* Next() { |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 1995 | if (current_ == limit_) return NULL; |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1996 | if (NewSpacePage::IsAtEnd(current_)) { |
| 1997 | NewSpacePage* page = NewSpacePage::FromLimit(current_); |
| 1998 | page = page->next_page(); |
| 1999 | ASSERT(!page->is_anchor()); |
| 2000 | current_ = page->area_start(); |
| 2001 | if (current_ == limit_) return NULL; |
| 2002 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2003 | |
| 2004 | HeapObject* object = HeapObject::FromAddress(current_); |
| 2005 | int size = (size_func_ == NULL) ? object->Size() : size_func_(object); |
| 2006 | |
| 2007 | current_ += size; |
| 2008 | return object; |
| 2009 | } |
| 2010 | |
| 2011 | // Implementation of the ObjectIterator functions. |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 2012 | virtual HeapObject* next_object() { return Next(); } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2013 | |
| 2014 | private: |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 2015 | void Initialize(Address start, |
| 2016 | Address end, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2017 | HeapObjectCallback size_func); |
| 2018 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2019 | // The current iteration point. |
| 2020 | Address current_; |
| 2021 | // The end of iteration. |
| 2022 | Address limit_; |
| 2023 | // The callback function. |
| 2024 | HeapObjectCallback size_func_; |
| 2025 | }; |
| 2026 | |
| 2027 | |
| 2028 | // ----------------------------------------------------------------------------- |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 2029 | // A PageIterator iterates the pages in a semi-space. |
| 2030 | class NewSpacePageIterator BASE_EMBEDDED { |
| 2031 | public: |
| 2032 | // Make an iterator that runs over all pages in to-space. |
| 2033 | explicit inline NewSpacePageIterator(NewSpace* space); |
| 2034 | |
| 2035 | // Make an iterator that runs over all pages in the given semispace, |
| 2036 | // even those not used in allocation. |
| 2037 | explicit inline NewSpacePageIterator(SemiSpace* space); |
| 2038 | |
| 2039 | // Make iterator that iterates from the page containing start |
| 2040 | // to the page that contains limit in the same semispace. |
| 2041 | inline NewSpacePageIterator(Address start, Address limit); |
| 2042 | |
| 2043 | inline bool has_next(); |
| 2044 | inline NewSpacePage* next(); |
| 2045 | |
| 2046 | private: |
| 2047 | NewSpacePage* prev_page_; // Previous page returned. |
| 2048 | // Next page that will be returned. Cached here so that we can use this |
| 2049 | // iterator for operations that deallocate pages. |
| 2050 | NewSpacePage* next_page_; |
| 2051 | // Last page returned. |
| 2052 | NewSpacePage* last_page_; |
| 2053 | }; |
| 2054 | |
| 2055 | |
| 2056 | // ----------------------------------------------------------------------------- |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2057 | // The young generation space. |
| 2058 | // |
| 2059 | // The new space consists of a contiguous pair of semispaces. It simply |
| 2060 | // forwards most functions to the appropriate semispace. |
| 2061 | |
| 2062 | class NewSpace : public Space { |
| 2063 | public: |
| 2064 | // Constructor. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2065 | explicit NewSpace(Heap* heap) |
| 2066 | : Space(heap, NEW_SPACE, NOT_EXECUTABLE), |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 2067 | to_space_(heap, kToSpace), |
| 2068 | from_space_(heap, kFromSpace), |
| 2069 | reservation_(), |
| 2070 | inline_allocation_limit_step_(0) {} |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2071 | |
| 2072 | // Sets up the new space using the given chunk. |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 2073 | bool Setup(int reserved_semispace_size_, int max_semispace_size); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2074 | |
| 2075 | // Tears down the space. Heap memory was not allocated by the space, so it |
| 2076 | // is not deallocated here. |
| 2077 | void TearDown(); |
| 2078 | |
| 2079 | // True if the space has been set up but not torn down. |
| 2080 | bool HasBeenSetup() { |
| 2081 | return to_space_.HasBeenSetup() && from_space_.HasBeenSetup(); |
| 2082 | } |
| 2083 | |
| 2084 | // Flip the pair of spaces. |
| 2085 | void Flip(); |
| 2086 | |
| 2087 | // Grow the capacity of the semispaces. Assumes that they are not at |
| 2088 | // their maximum capacity. |
| 2089 | void Grow(); |
| 2090 | |
| 2091 | // Shrink the capacity of the semispaces. |
| 2092 | void Shrink(); |
| 2093 | |
| 2094 | // True if the address or object lies in the address range of either |
| 2095 | // semispace (not necessarily below the allocation pointer). |
| 2096 | bool Contains(Address a) { |
| 2097 | return (reinterpret_cast<uintptr_t>(a) & address_mask_) |
| 2098 | == reinterpret_cast<uintptr_t>(start_); |
| 2099 | } |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 2100 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2101 | bool Contains(Object* o) { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 2102 | Address a = reinterpret_cast<Address>(o); |
| 2103 | return (reinterpret_cast<uintptr_t>(a) & object_mask_) == object_expected_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2104 | } |
| 2105 | |
| 2106 | // Return the allocated bytes in the active semispace. |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 2107 | virtual intptr_t Size() { |
| 2108 | return pages_used_ * NewSpacePage::kAreaSize + |
| 2109 | static_cast<int>(top() - to_space_.page_low()); |
| 2110 | } |
| 2111 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 2112 | // The same, but returning an int. We have to have the one that returns |
| 2113 | // intptr_t because it is inherited, but if we know we are dealing with the |
| 2114 | // new space, which can't get as big as the other spaces then this is useful: |
| 2115 | int SizeAsInt() { return static_cast<int>(Size()); } |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2116 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2117 | // Return the current capacity of a semispace. |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 2118 | intptr_t EffectiveCapacity() { |
| 2119 | SLOW_ASSERT(to_space_.Capacity() == from_space_.Capacity()); |
| 2120 | return (to_space_.Capacity() / Page::kPageSize) * NewSpacePage::kAreaSize; |
| 2121 | } |
| 2122 | |
| 2123 | // Return the current capacity of a semispace. |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 2124 | intptr_t Capacity() { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2125 | ASSERT(to_space_.Capacity() == from_space_.Capacity()); |
| 2126 | return to_space_.Capacity(); |
| 2127 | } |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2128 | |
| 2129 | // Return the total amount of memory committed for new space. |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 2130 | intptr_t CommittedMemory() { |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2131 | if (from_space_.is_committed()) return 2 * Capacity(); |
| 2132 | return Capacity(); |
| 2133 | } |
| 2134 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 2135 | // Return the available bytes without growing. |
| 2136 | intptr_t Available() { |
| 2137 | return Capacity() - Size(); |
| 2138 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2139 | |
| 2140 | // Return the maximum capacity of a semispace. |
| 2141 | int MaximumCapacity() { |
| 2142 | ASSERT(to_space_.MaximumCapacity() == from_space_.MaximumCapacity()); |
| 2143 | return to_space_.MaximumCapacity(); |
| 2144 | } |
| 2145 | |
| 2146 | // Returns the initial capacity of a semispace. |
| 2147 | int InitialCapacity() { |
| 2148 | ASSERT(to_space_.InitialCapacity() == from_space_.InitialCapacity()); |
| 2149 | return to_space_.InitialCapacity(); |
| 2150 | } |
| 2151 | |
| 2152 | // Return the address of the allocation pointer in the active semispace. |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 2153 | Address top() { |
| 2154 | ASSERT(to_space_.current_page()->ContainsLimit(allocation_info_.top)); |
| 2155 | return allocation_info_.top; |
| 2156 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2157 | // Return the address of the first object in the active semispace. |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 2158 | Address bottom() { return to_space_.space_start(); } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2159 | |
| 2160 | // Get the age mark of the inactive semispace. |
| 2161 | Address age_mark() { return from_space_.age_mark(); } |
| 2162 | // Set the age mark in the active semispace. |
| 2163 | void set_age_mark(Address mark) { to_space_.set_age_mark(mark); } |
| 2164 | |
| 2165 | // The start address of the space and a bit mask. Anding an address in the |
| 2166 | // new space with the mask will result in the start address. |
| 2167 | Address start() { return start_; } |
| 2168 | uintptr_t mask() { return address_mask_; } |
| 2169 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 2170 | INLINE(uint32_t AddressToMarkbitIndex(Address addr)) { |
| 2171 | ASSERT(Contains(addr)); |
| 2172 | ASSERT(IsAligned(OffsetFrom(addr), kPointerSize) || |
| 2173 | IsAligned(OffsetFrom(addr) - 1, kPointerSize)); |
| 2174 | return static_cast<uint32_t>(addr - start_) >> kPointerSizeLog2; |
| 2175 | } |
| 2176 | |
| 2177 | INLINE(Address MarkbitIndexToAddress(uint32_t index)) { |
| 2178 | return reinterpret_cast<Address>(index << kPointerSizeLog2); |
| 2179 | } |
| 2180 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2181 | // The allocation top and limit addresses. |
| 2182 | Address* allocation_top_address() { return &allocation_info_.top; } |
| 2183 | Address* allocation_limit_address() { return &allocation_info_.limit; } |
| 2184 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 2185 | MUST_USE_RESULT INLINE(MaybeObject* AllocateRaw(int size_in_bytes)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2186 | |
| 2187 | // Reset the allocation pointer to the beginning of the active semispace. |
| 2188 | void ResetAllocationInfo(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2189 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 2190 | void LowerInlineAllocationLimit(intptr_t step) { |
| 2191 | inline_allocation_limit_step_ = step; |
| 2192 | if (step == 0) { |
| 2193 | allocation_info_.limit = to_space_.page_high(); |
| 2194 | } else { |
| 2195 | allocation_info_.limit = Min( |
| 2196 | allocation_info_.top + inline_allocation_limit_step_, |
| 2197 | allocation_info_.limit); |
| 2198 | } |
| 2199 | top_on_previous_step_ = allocation_info_.top; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2200 | } |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 2201 | |
| 2202 | // Get the extent of the inactive semispace (for use as a marking stack, |
| 2203 | // or to zap it). Notice: space-addresses are not necessarily on the |
| 2204 | // same page, so FromSpaceStart() might be above FromSpaceEnd(). |
| 2205 | Address FromSpacePageLow() { return from_space_.page_low(); } |
| 2206 | Address FromSpacePageHigh() { return from_space_.page_high(); } |
| 2207 | Address FromSpaceStart() { return from_space_.space_start(); } |
| 2208 | Address FromSpaceEnd() { return from_space_.space_end(); } |
| 2209 | |
| 2210 | // Get the extent of the active semispace's pages' memory. |
| 2211 | Address ToSpaceStart() { return to_space_.space_start(); } |
| 2212 | Address ToSpaceEnd() { return to_space_.space_end(); } |
| 2213 | |
| 2214 | inline bool ToSpaceContains(Address address) { |
| 2215 | return to_space_.Contains(address); |
| 2216 | } |
| 2217 | inline bool FromSpaceContains(Address address) { |
| 2218 | return from_space_.Contains(address); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2219 | } |
| 2220 | |
| 2221 | // True if the object is a heap object in the address range of the |
| 2222 | // respective semispace (not necessarily below the allocation pointer of the |
| 2223 | // semispace). |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 2224 | inline bool ToSpaceContains(Object* o) { return to_space_.Contains(o); } |
| 2225 | inline bool FromSpaceContains(Object* o) { return from_space_.Contains(o); } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2226 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 2227 | // Try to switch the active semispace to a new, empty, page. |
| 2228 | // Returns false if this isn't possible or reasonable (i.e., there |
| 2229 | // are no pages, or the current page is already empty), or true |
| 2230 | // if successful. |
| 2231 | bool AddFreshPage(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2232 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2233 | virtual bool ReserveSpace(int bytes); |
| 2234 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2235 | // Resizes a sequential string which must be the most recent thing that was |
| 2236 | // allocated in new space. |
| 2237 | template <typename StringType> |
| 2238 | inline void ShrinkStringAtAllocationBoundary(String* string, int len); |
| 2239 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2240 | #ifdef DEBUG |
| 2241 | // Verify the active semispace. |
| 2242 | virtual void Verify(); |
| 2243 | // Print the active semispace. |
| 2244 | virtual void Print() { to_space_.Print(); } |
| 2245 | #endif |
| 2246 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2247 | // Iterates the active semispace to collect statistics. |
| 2248 | void CollectStatistics(); |
| 2249 | // Reports previously collected statistics of the active semispace. |
| 2250 | void ReportStatistics(); |
| 2251 | // Clears previously collected statistics. |
| 2252 | void ClearHistograms(); |
| 2253 | |
| 2254 | // Record the allocation or promotion of a heap object. Note that we don't |
| 2255 | // record every single allocation, but only those that happen in the |
| 2256 | // to space during a scavenge GC. |
| 2257 | void RecordAllocation(HeapObject* obj); |
| 2258 | void RecordPromotion(HeapObject* obj); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2259 | |
| 2260 | // Return whether the operation succeded. |
| 2261 | bool CommitFromSpaceIfNeeded() { |
| 2262 | if (from_space_.is_committed()) return true; |
| 2263 | return from_space_.Commit(); |
| 2264 | } |
| 2265 | |
| 2266 | bool UncommitFromSpace() { |
| 2267 | if (!from_space_.is_committed()) return true; |
| 2268 | return from_space_.Uncommit(); |
| 2269 | } |
| 2270 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 2271 | inline intptr_t inline_allocation_limit_step() { |
| 2272 | return inline_allocation_limit_step_; |
| 2273 | } |
| 2274 | |
| 2275 | SemiSpace* active_space() { return &to_space_; } |
| 2276 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2277 | private: |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 2278 | // Update allocation info to match the current to-space page. |
| 2279 | void UpdateAllocationInfo(); |
| 2280 | |
| 2281 | Address chunk_base_; |
| 2282 | uintptr_t chunk_size_; |
| 2283 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2284 | // The semispaces. |
| 2285 | SemiSpace to_space_; |
| 2286 | SemiSpace from_space_; |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 2287 | VirtualMemory reservation_; |
| 2288 | int pages_used_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2289 | |
| 2290 | // Start address and bit mask for containment testing. |
| 2291 | Address start_; |
| 2292 | uintptr_t address_mask_; |
| 2293 | uintptr_t object_mask_; |
| 2294 | uintptr_t object_expected_; |
| 2295 | |
| 2296 | // Allocation pointer and limit for normal allocation and allocation during |
| 2297 | // mark-compact collection. |
| 2298 | AllocationInfo allocation_info_; |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 2299 | |
| 2300 | // When incremental marking is active we will set allocation_info_.limit |
| 2301 | // to be lower than actual limit and then will gradually increase it |
| 2302 | // in steps to guarantee that we do incremental marking steps even |
| 2303 | // when all allocation is performed from inlined generated code. |
| 2304 | intptr_t inline_allocation_limit_step_; |
| 2305 | |
| 2306 | Address top_on_previous_step_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2307 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2308 | HistogramInfo* allocated_histogram_; |
| 2309 | HistogramInfo* promoted_histogram_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2310 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 2311 | MUST_USE_RESULT MaybeObject* SlowAllocateRaw(int size_in_bytes); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2312 | |
| 2313 | friend class SemiSpaceIterator; |
| 2314 | |
| 2315 | public: |
| 2316 | TRACK_MEMORY("NewSpace") |
| 2317 | }; |
| 2318 | |
| 2319 | |
| 2320 | // ----------------------------------------------------------------------------- |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2321 | // Old object space (excluding map objects) |
| 2322 | |
| 2323 | class OldSpace : public PagedSpace { |
| 2324 | public: |
| 2325 | // Creates an old space object with a given maximum capacity. |
| 2326 | // The constructor does not allocate pages from OS. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2327 | OldSpace(Heap* heap, |
| 2328 | intptr_t max_capacity, |
| 2329 | AllocationSpace id, |
| 2330 | Executability executable) |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 2331 | : PagedSpace(heap, max_capacity, id, executable) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2332 | page_extra_ = 0; |
| 2333 | } |
| 2334 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2335 | // The limit of allocation for a page in this space. |
| 2336 | virtual Address PageAllocationLimit(Page* page) { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 2337 | return page->area_end(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2338 | } |
| 2339 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2340 | public: |
| 2341 | TRACK_MEMORY("OldSpace") |
| 2342 | }; |
| 2343 | |
| 2344 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 2345 | // For contiguous spaces, top should be in the space (or at the end) and limit |
| 2346 | // should be the end of the space. |
| 2347 | #define ASSERT_SEMISPACE_ALLOCATION_INFO(info, space) \ |
| 2348 | SLOW_ASSERT((space).page_low() <= (info).top \ |
| 2349 | && (info).top <= (space).page_high() \ |
| 2350 | && (info).limit <= (space).page_high()) |
| 2351 | |
| 2352 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2353 | // ----------------------------------------------------------------------------- |
| 2354 | // Old space for objects of a fixed size |
| 2355 | |
| 2356 | class FixedSpace : public PagedSpace { |
| 2357 | public: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2358 | FixedSpace(Heap* heap, |
| 2359 | intptr_t max_capacity, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2360 | AllocationSpace id, |
| 2361 | int object_size_in_bytes, |
| 2362 | const char* name) |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2363 | : PagedSpace(heap, max_capacity, id, NOT_EXECUTABLE), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2364 | object_size_in_bytes_(object_size_in_bytes), |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 2365 | name_(name) { |
| 2366 | page_extra_ = Page::kNonCodeObjectAreaSize % object_size_in_bytes; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2367 | } |
| 2368 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2369 | // The limit of allocation for a page in this space. |
| 2370 | virtual Address PageAllocationLimit(Page* page) { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 2371 | return page->area_end() - page_extra_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2372 | } |
| 2373 | |
| 2374 | int object_size_in_bytes() { return object_size_in_bytes_; } |
| 2375 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2376 | // Prepares for a mark-compact GC. |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 2377 | virtual void PrepareForMarkCompact(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2378 | |
| 2379 | protected: |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2380 | void ResetFreeList() { |
| 2381 | free_list_.Reset(); |
| 2382 | } |
| 2383 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2384 | private: |
| 2385 | // The size of objects in this space. |
| 2386 | int object_size_in_bytes_; |
| 2387 | |
| 2388 | // The name of this space. |
| 2389 | const char* name_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2390 | }; |
| 2391 | |
| 2392 | |
| 2393 | // ----------------------------------------------------------------------------- |
| 2394 | // Old space for all map objects |
| 2395 | |
| 2396 | class MapSpace : public FixedSpace { |
| 2397 | public: |
| 2398 | // Creates a map space object with a maximum capacity. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2399 | MapSpace(Heap* heap, |
| 2400 | intptr_t max_capacity, |
| 2401 | int max_map_space_pages, |
| 2402 | AllocationSpace id) |
| 2403 | : FixedSpace(heap, max_capacity, id, Map::kSize, "map"), |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 2404 | max_map_space_pages_(max_map_space_pages) { |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 2405 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2406 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2407 | // Given an index, returns the page address. |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 2408 | // TODO(1600): this limit is artifical just to keep code compilable |
| 2409 | static const int kMaxMapPageIndex = 1 << 16; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2410 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 2411 | virtual int RoundSizeDownToObjectAlignment(int size) { |
| 2412 | if (IsPowerOf2(Map::kSize)) { |
| 2413 | return RoundDown(size, Map::kSize); |
| 2414 | } else { |
| 2415 | return (size / Map::kSize) * Map::kSize; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2416 | } |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2417 | } |
| 2418 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2419 | protected: |
| 2420 | #ifdef DEBUG |
| 2421 | virtual void VerifyObject(HeapObject* obj); |
| 2422 | #endif |
| 2423 | |
| 2424 | private: |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 2425 | static const int kMapsPerPage = Page::kNonCodeObjectAreaSize / Map::kSize; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2426 | |
| 2427 | // Do map space compaction if there is a page gap. |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 2428 | int CompactionThreshold() { |
| 2429 | return kMapsPerPage * (max_map_space_pages_ - 1); |
| 2430 | } |
| 2431 | |
| 2432 | const int max_map_space_pages_; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2433 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2434 | public: |
| 2435 | TRACK_MEMORY("MapSpace") |
| 2436 | }; |
| 2437 | |
| 2438 | |
| 2439 | // ----------------------------------------------------------------------------- |
| 2440 | // Old space for all global object property cell objects |
| 2441 | |
| 2442 | class CellSpace : public FixedSpace { |
| 2443 | public: |
| 2444 | // Creates a property cell space object with a maximum capacity. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2445 | CellSpace(Heap* heap, intptr_t max_capacity, AllocationSpace id) |
| 2446 | : FixedSpace(heap, max_capacity, id, JSGlobalPropertyCell::kSize, "cell") |
| 2447 | {} |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2448 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 2449 | virtual int RoundSizeDownToObjectAlignment(int size) { |
| 2450 | if (IsPowerOf2(JSGlobalPropertyCell::kSize)) { |
| 2451 | return RoundDown(size, JSGlobalPropertyCell::kSize); |
| 2452 | } else { |
| 2453 | return (size / JSGlobalPropertyCell::kSize) * JSGlobalPropertyCell::kSize; |
| 2454 | } |
| 2455 | } |
| 2456 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2457 | protected: |
| 2458 | #ifdef DEBUG |
| 2459 | virtual void VerifyObject(HeapObject* obj); |
| 2460 | #endif |
| 2461 | |
| 2462 | public: |
| 2463 | TRACK_MEMORY("CellSpace") |
| 2464 | }; |
| 2465 | |
| 2466 | |
| 2467 | // ----------------------------------------------------------------------------- |
| 2468 | // Large objects ( > Page::kMaxHeapObjectSize ) are allocated and managed by |
| 2469 | // the large object space. A large object is allocated from OS heap with |
| 2470 | // extra padding bytes (Page::kPageSize + Page::kObjectStartOffset). |
| 2471 | // A large object always starts at Page::kObjectStartOffset to a page. |
| 2472 | // Large objects do not move during garbage collections. |
| 2473 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2474 | class LargeObjectSpace : public Space { |
| 2475 | public: |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 2476 | LargeObjectSpace(Heap* heap, intptr_t max_capacity, AllocationSpace id); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2477 | virtual ~LargeObjectSpace() {} |
| 2478 | |
| 2479 | // Initializes internal data structures. |
| 2480 | bool Setup(); |
| 2481 | |
| 2482 | // Releases internal resources, frees objects in this space. |
| 2483 | void TearDown(); |
| 2484 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 2485 | static intptr_t ObjectSizeFor(intptr_t chunk_size) { |
| 2486 | if (chunk_size <= (Page::kPageSize + Page::kObjectStartOffset)) return 0; |
| 2487 | return chunk_size - Page::kPageSize - Page::kObjectStartOffset; |
| 2488 | } |
| 2489 | |
| 2490 | // Shared implementation of AllocateRaw, AllocateRawCode and |
| 2491 | // AllocateRawFixedArray. |
| 2492 | MUST_USE_RESULT MaybeObject* AllocateRaw(int object_size, |
| 2493 | Executability executable); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2494 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2495 | // Available bytes for objects in this space. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2496 | inline intptr_t Available(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2497 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 2498 | virtual intptr_t Size() { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2499 | return size_; |
| 2500 | } |
| 2501 | |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 2502 | virtual intptr_t SizeOfObjects() { |
| 2503 | return objects_size_; |
| 2504 | } |
| 2505 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2506 | int PageCount() { |
| 2507 | return page_count_; |
| 2508 | } |
| 2509 | |
| 2510 | // Finds an object for a given address, returns Failure::Exception() |
| 2511 | // if it is not found. The function iterates through all objects in this |
| 2512 | // space, may be slow. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2513 | MaybeObject* FindObject(Address a); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2514 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 2515 | // Finds a large object page containing the given pc, returns NULL |
| 2516 | // if such a page doesn't exist. |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 2517 | LargePage* FindPageContainingPc(Address pc); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2518 | |
| 2519 | // Frees unmarked objects. |
| 2520 | void FreeUnmarkedObjects(); |
| 2521 | |
| 2522 | // Checks whether a heap object is in this space; O(1). |
| 2523 | bool Contains(HeapObject* obj); |
| 2524 | |
| 2525 | // Checks whether the space is empty. |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 2526 | bool IsEmpty() { return first_page_ == NULL; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2527 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2528 | // See the comments for ReserveSpace in the Space class. This has to be |
| 2529 | // called after ReserveSpace has been called on the paged spaces, since they |
| 2530 | // may use some memory, leaving less for large objects. |
| 2531 | virtual bool ReserveSpace(int bytes); |
| 2532 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 2533 | LargePage* first_page() { return first_page_; } |
| 2534 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2535 | #ifdef DEBUG |
| 2536 | virtual void Verify(); |
| 2537 | virtual void Print(); |
| 2538 | void ReportStatistics(); |
| 2539 | void CollectCodeStatistics(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2540 | #endif |
| 2541 | // Checks whether an address is in the object area in this space. It |
| 2542 | // iterates all objects in the space. May be slow. |
| 2543 | bool SlowContains(Address addr) { return !FindObject(addr)->IsFailure(); } |
| 2544 | |
| 2545 | private: |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 2546 | intptr_t max_capacity_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2547 | // The head of the linked list of large object chunks. |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 2548 | LargePage* first_page_; |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 2549 | intptr_t size_; // allocated bytes |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2550 | int page_count_; // number of chunks |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 2551 | intptr_t objects_size_; // size of objects |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2552 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2553 | friend class LargeObjectIterator; |
| 2554 | |
| 2555 | public: |
| 2556 | TRACK_MEMORY("LargeObjectSpace") |
| 2557 | }; |
| 2558 | |
| 2559 | |
| 2560 | class LargeObjectIterator: public ObjectIterator { |
| 2561 | public: |
| 2562 | explicit LargeObjectIterator(LargeObjectSpace* space); |
| 2563 | LargeObjectIterator(LargeObjectSpace* space, HeapObjectCallback size_func); |
| 2564 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 2565 | HeapObject* Next(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2566 | |
| 2567 | // implementation of ObjectIterator. |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 2568 | virtual HeapObject* next_object() { return Next(); } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2569 | |
| 2570 | private: |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 2571 | LargePage* current_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2572 | HeapObjectCallback size_func_; |
| 2573 | }; |
| 2574 | |
| 2575 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 2576 | // Iterates over the chunks (pages and large object pages) that can contain |
| 2577 | // pointers to new space. |
| 2578 | class PointerChunkIterator BASE_EMBEDDED { |
| 2579 | public: |
| 2580 | inline explicit PointerChunkIterator(Heap* heap); |
| 2581 | |
| 2582 | // Return NULL when the iterator is done. |
| 2583 | MemoryChunk* next() { |
| 2584 | switch (state_) { |
| 2585 | case kOldPointerState: { |
| 2586 | if (old_pointer_iterator_.has_next()) { |
| 2587 | return old_pointer_iterator_.next(); |
| 2588 | } |
| 2589 | state_ = kMapState; |
| 2590 | // Fall through. |
| 2591 | } |
| 2592 | case kMapState: { |
| 2593 | if (map_iterator_.has_next()) { |
| 2594 | return map_iterator_.next(); |
| 2595 | } |
| 2596 | state_ = kLargeObjectState; |
| 2597 | // Fall through. |
| 2598 | } |
| 2599 | case kLargeObjectState: { |
| 2600 | HeapObject* heap_object; |
| 2601 | do { |
| 2602 | heap_object = lo_iterator_.Next(); |
| 2603 | if (heap_object == NULL) { |
| 2604 | state_ = kFinishedState; |
| 2605 | return NULL; |
| 2606 | } |
| 2607 | // Fixed arrays are the only pointer-containing objects in large |
| 2608 | // object space. |
| 2609 | } while (!heap_object->IsFixedArray()); |
| 2610 | MemoryChunk* answer = MemoryChunk::FromAddress(heap_object->address()); |
| 2611 | return answer; |
| 2612 | } |
| 2613 | case kFinishedState: |
| 2614 | return NULL; |
| 2615 | default: |
| 2616 | break; |
| 2617 | } |
| 2618 | UNREACHABLE(); |
| 2619 | return NULL; |
| 2620 | } |
| 2621 | |
| 2622 | |
| 2623 | private: |
| 2624 | enum State { |
| 2625 | kOldPointerState, |
| 2626 | kMapState, |
| 2627 | kLargeObjectState, |
| 2628 | kFinishedState |
| 2629 | }; |
| 2630 | State state_; |
| 2631 | PageIterator old_pointer_iterator_; |
| 2632 | PageIterator map_iterator_; |
| 2633 | LargeObjectIterator lo_iterator_; |
| 2634 | }; |
| 2635 | |
| 2636 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2637 | #ifdef DEBUG |
| 2638 | struct CommentStatistic { |
| 2639 | const char* comment; |
| 2640 | int size; |
| 2641 | int count; |
| 2642 | void Clear() { |
| 2643 | comment = NULL; |
| 2644 | size = 0; |
| 2645 | count = 0; |
| 2646 | } |
| 2647 | // Must be small, since an iteration is used for lookup. |
| 2648 | static const int kMaxComments = 64; |
| 2649 | }; |
| 2650 | #endif |
| 2651 | |
| 2652 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2653 | } } // namespace v8::internal |
| 2654 | |
| 2655 | #endif // V8_SPACES_H_ |