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