OLD | NEW |
1 // Copyright 2011 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef V8_HEAP_SPACES_H_ | 5 #ifndef V8_HEAP_SPACES_H_ |
6 #define V8_HEAP_SPACES_H_ | 6 #define V8_HEAP_SPACES_H_ |
7 | 7 |
8 #include "src/allocation.h" | 8 #include "src/allocation.h" |
9 #include "src/atomic-utils.h" | 9 #include "src/atomic-utils.h" |
10 #include "src/base/atomicops.h" | 10 #include "src/base/atomicops.h" |
11 #include "src/base/bits.h" | 11 #include "src/base/bits.h" |
12 #include "src/base/platform/mutex.h" | 12 #include "src/base/platform/mutex.h" |
13 #include "src/flags.h" | 13 #include "src/flags.h" |
14 #include "src/hashmap.h" | 14 #include "src/hashmap.h" |
15 #include "src/list.h" | 15 #include "src/list.h" |
16 #include "src/objects.h" | 16 #include "src/objects.h" |
17 #include "src/utils.h" | 17 #include "src/utils.h" |
18 | 18 |
19 namespace v8 { | 19 namespace v8 { |
20 namespace internal { | 20 namespace internal { |
21 | 21 |
22 class CompactionSpaceCollection; | |
23 class Isolate; | 22 class Isolate; |
24 | 23 |
25 // ----------------------------------------------------------------------------- | 24 // ----------------------------------------------------------------------------- |
26 // Heap structures: | 25 // Heap structures: |
27 // | 26 // |
28 // A JS heap consists of a young generation, an old generation, and a large | 27 // A JS heap consists of a young generation, an old generation, and a large |
29 // object space. The young generation is divided into two semispaces. A | 28 // object space. The young generation is divided into two semispaces. A |
30 // scavenger implements Cheney's copying algorithm. The old generation is | 29 // scavenger implements Cheney's copying algorithm. The old generation is |
31 // separated into a map space and an old object space. The map space contains | 30 // separated into a map space and an old object space. The map space contains |
32 // all (and only) map objects, the rest of old objects go into the old space. | 31 // all (and only) map objects, the rest of old objects go into the old space. |
(...skipping 1687 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1720 return kLargeAllocationMax; | 1719 return kLargeAllocationMax; |
1721 } | 1720 } |
1722 return maximum_freed; | 1721 return maximum_freed; |
1723 } | 1722 } |
1724 | 1723 |
1725 // Allocate a block of size 'size_in_bytes' from the free list. The block | 1724 // Allocate a block of size 'size_in_bytes' from the free list. The block |
1726 // is unitialized. A failure is returned if no block is available. | 1725 // is unitialized. A failure is returned if no block is available. |
1727 // The size should be a non-zero multiple of the word size. | 1726 // The size should be a non-zero multiple of the word size. |
1728 MUST_USE_RESULT HeapObject* Allocate(int size_in_bytes); | 1727 MUST_USE_RESULT HeapObject* Allocate(int size_in_bytes); |
1729 | 1728 |
1730 // The method tries to find a {FreeSpace} node of at least {size_in_bytes} | |
1731 // size in the free list category exactly matching the size. If no suitable | |
1732 // node could be found, the method falls back to retrieving a {FreeSpace} | |
1733 // from the large or huge free list category. | |
1734 // | |
1735 // Can be used concurrently. | |
1736 MUST_USE_RESULT FreeSpace* TryRemoveMemory(intptr_t hint_size_in_bytes); | |
1737 | |
1738 bool IsEmpty() { | 1729 bool IsEmpty() { |
1739 return small_list_.IsEmpty() && medium_list_.IsEmpty() && | 1730 return small_list_.IsEmpty() && medium_list_.IsEmpty() && |
1740 large_list_.IsEmpty() && huge_list_.IsEmpty(); | 1731 large_list_.IsEmpty() && huge_list_.IsEmpty(); |
1741 } | 1732 } |
1742 | 1733 |
1743 #ifdef DEBUG | 1734 #ifdef DEBUG |
1744 void Zap(); | 1735 void Zap(); |
1745 intptr_t SumFreeLists(); | 1736 intptr_t SumFreeLists(); |
1746 bool IsVeryLong(); | 1737 bool IsVeryLong(); |
1747 #endif | 1738 #endif |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1782 return &large_list_; | 1773 return &large_list_; |
1783 case kHuge: | 1774 case kHuge: |
1784 return &huge_list_; | 1775 return &huge_list_; |
1785 default: | 1776 default: |
1786 UNREACHABLE(); | 1777 UNREACHABLE(); |
1787 } | 1778 } |
1788 UNREACHABLE(); | 1779 UNREACHABLE(); |
1789 return nullptr; | 1780 return nullptr; |
1790 } | 1781 } |
1791 | 1782 |
| 1783 |
1792 PagedSpace* owner_; | 1784 PagedSpace* owner_; |
1793 Heap* heap_; | 1785 Heap* heap_; |
1794 base::Mutex mutex_; | 1786 base::Mutex mutex_; |
1795 intptr_t wasted_bytes_; | 1787 intptr_t wasted_bytes_; |
1796 FreeListCategory small_list_; | 1788 FreeListCategory small_list_; |
1797 FreeListCategory medium_list_; | 1789 FreeListCategory medium_list_; |
1798 FreeListCategory large_list_; | 1790 FreeListCategory large_list_; |
1799 FreeListCategory huge_list_; | 1791 FreeListCategory huge_list_; |
1800 | 1792 |
1801 DISALLOW_IMPLICIT_CONSTRUCTORS(FreeList); | 1793 DISALLOW_IMPLICIT_CONSTRUCTORS(FreeList); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1840 | 1832 |
1841 Object* object_; | 1833 Object* object_; |
1842 }; | 1834 }; |
1843 | 1835 |
1844 | 1836 |
1845 STATIC_ASSERT(sizeof(AllocationResult) == kPointerSize); | 1837 STATIC_ASSERT(sizeof(AllocationResult) == kPointerSize); |
1846 | 1838 |
1847 | 1839 |
1848 class PagedSpace : public Space { | 1840 class PagedSpace : public Space { |
1849 public: | 1841 public: |
1850 static const intptr_t kCompactionMemoryWanted = 500 * KB; | |
1851 | |
1852 // Creates a space with an id. | 1842 // Creates a space with an id. |
1853 PagedSpace(Heap* heap, AllocationSpace id, Executability executable); | 1843 PagedSpace(Heap* heap, AllocationSpace id, Executability executable); |
1854 | 1844 |
1855 virtual ~PagedSpace() { TearDown(); } | 1845 virtual ~PagedSpace() { TearDown(); } |
1856 | 1846 |
1857 // Set up the space using the given address range of virtual memory (from | 1847 // Set up the space using the given address range of virtual memory (from |
1858 // the memory allocator's initial chunk) if possible. If the block of | 1848 // the memory allocator's initial chunk) if possible. If the block of |
1859 // addresses is not big enough to contain a single page-aligned page, a | 1849 // addresses is not big enough to contain a single page-aligned page, a |
1860 // fresh chunk will be allocated. | 1850 // fresh chunk will be allocated. |
1861 bool SetUp(); | 1851 bool SetUp(); |
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2043 void EvictEvacuationCandidatesFromFreeLists(); | 2033 void EvictEvacuationCandidatesFromFreeLists(); |
2044 | 2034 |
2045 bool CanExpand(size_t size); | 2035 bool CanExpand(size_t size); |
2046 | 2036 |
2047 // Returns the number of total pages in this space. | 2037 // Returns the number of total pages in this space. |
2048 int CountTotalPages(); | 2038 int CountTotalPages(); |
2049 | 2039 |
2050 // Return size of allocatable area on a page in this space. | 2040 // Return size of allocatable area on a page in this space. |
2051 inline int AreaSize() { return area_size_; } | 2041 inline int AreaSize() { return area_size_; } |
2052 | 2042 |
2053 virtual bool is_local() { return false; } | |
2054 | |
2055 // Merges {other} into the current space. Note that this modifies {other}, | 2043 // Merges {other} into the current space. Note that this modifies {other}, |
2056 // e.g., removes its bump pointer area and resets statistics. | 2044 // e.g., removes its bump pointer area and resets statistics. |
2057 void MergeCompactionSpace(CompactionSpace* other); | 2045 void MergeCompactionSpace(CompactionSpace* other); |
2058 | 2046 |
2059 void DivideUponCompactionSpaces(CompactionSpaceCollection** other, int num, | 2047 void MoveOverFreeMemory(PagedSpace* other); |
2060 intptr_t limit = kCompactionMemoryWanted); | |
2061 | 2048 |
2062 // Refills the free list from the corresponding free list filled by the | 2049 virtual bool is_local() { return false; } |
2063 // sweeper. | |
2064 virtual void RefillFreeList(); | |
2065 | 2050 |
2066 protected: | 2051 protected: |
2067 void AddMemory(Address start, intptr_t size); | |
2068 | |
2069 FreeSpace* TryRemoveMemory(intptr_t size_in_bytes); | |
2070 | |
2071 void MoveOverFreeMemory(PagedSpace* other); | |
2072 | |
2073 // PagedSpaces that should be included in snapshots have different, i.e., | 2052 // PagedSpaces that should be included in snapshots have different, i.e., |
2074 // smaller, initial pages. | 2053 // smaller, initial pages. |
2075 virtual bool snapshotable() { return true; } | 2054 virtual bool snapshotable() { return true; } |
2076 | 2055 |
2077 FreeList* free_list() { return &free_list_; } | 2056 FreeList* free_list() { return &free_list_; } |
2078 | 2057 |
2079 bool HasPages() { return anchor_.next_page() != &anchor_; } | 2058 bool HasPages() { return anchor_.next_page() != &anchor_; } |
2080 | 2059 |
2081 // Cleans up the space, frees all pages in this space except those belonging | 2060 // Cleans up the space, frees all pages in this space except those belonging |
2082 // to the initial chunk, uncommits addresses in the initial chunk. | 2061 // to the initial chunk, uncommits addresses in the initial chunk. |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2123 // The sweeper threads iterate over the list of pointer and data space pages | 2102 // The sweeper threads iterate over the list of pointer and data space pages |
2124 // and sweep these pages concurrently. They will stop sweeping after the | 2103 // and sweep these pages concurrently. They will stop sweeping after the |
2125 // end_of_unswept_pages_ page. | 2104 // end_of_unswept_pages_ page. |
2126 Page* end_of_unswept_pages_; | 2105 Page* end_of_unswept_pages_; |
2127 | 2106 |
2128 // Mutex guarding any concurrent access to the space. | 2107 // Mutex guarding any concurrent access to the space. |
2129 base::Mutex space_mutex_; | 2108 base::Mutex space_mutex_; |
2130 | 2109 |
2131 friend class MarkCompactCollector; | 2110 friend class MarkCompactCollector; |
2132 friend class PageIterator; | 2111 friend class PageIterator; |
2133 | |
2134 // Used in cctest. | |
2135 friend class HeapTester; | |
2136 }; | 2112 }; |
2137 | 2113 |
2138 | 2114 |
2139 class NumberAndSizeInfo BASE_EMBEDDED { | 2115 class NumberAndSizeInfo BASE_EMBEDDED { |
2140 public: | 2116 public: |
2141 NumberAndSizeInfo() : number_(0), bytes_(0) {} | 2117 NumberAndSizeInfo() : number_(0), bytes_(0) {} |
2142 | 2118 |
2143 int number() const { return number_; } | 2119 int number() const { return number_; } |
2144 void increment_number(int num) { number_ += num; } | 2120 void increment_number(int num) { number_ += num; } |
2145 | 2121 |
(...skipping 657 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2803 public: | 2779 public: |
2804 CompactionSpace(Heap* heap, AllocationSpace id, Executability executable) | 2780 CompactionSpace(Heap* heap, AllocationSpace id, Executability executable) |
2805 : PagedSpace(heap, id, executable) {} | 2781 : PagedSpace(heap, id, executable) {} |
2806 | 2782 |
2807 // Adds external memory starting at {start} of {size_in_bytes} to the space. | 2783 // Adds external memory starting at {start} of {size_in_bytes} to the space. |
2808 void AddExternalMemory(Address start, int size_in_bytes) { | 2784 void AddExternalMemory(Address start, int size_in_bytes) { |
2809 IncreaseCapacity(size_in_bytes); | 2785 IncreaseCapacity(size_in_bytes); |
2810 Free(start, size_in_bytes); | 2786 Free(start, size_in_bytes); |
2811 } | 2787 } |
2812 | 2788 |
2813 virtual bool is_local() override { return true; } | 2789 virtual bool is_local() { return true; } |
2814 | |
2815 virtual void RefillFreeList() override; | |
2816 | 2790 |
2817 protected: | 2791 protected: |
2818 // The space is temporary and not included in any snapshots. | 2792 // The space is temporary and not included in any snapshots. |
2819 virtual bool snapshotable() override { return false; } | 2793 virtual bool snapshotable() { return false; } |
2820 }; | 2794 }; |
2821 | 2795 |
2822 | 2796 |
2823 // A collection of |CompactionSpace|s used by a single compaction task. | 2797 // A collection of |CompactionSpace|s used by a single compaction task. |
2824 class CompactionSpaceCollection : public Malloced { | 2798 class CompactionSpaceCollection : public Malloced { |
2825 public: | 2799 public: |
2826 explicit CompactionSpaceCollection(Heap* heap) | 2800 explicit CompactionSpaceCollection(Heap* heap) |
2827 : old_space_(heap, OLD_SPACE, Executability::NOT_EXECUTABLE), | 2801 : old_space_(heap, OLD_SPACE, Executability::NOT_EXECUTABLE), |
2828 code_space_(heap, CODE_SPACE, Executability::EXECUTABLE) {} | 2802 code_space_(heap, CODE_SPACE, Executability::EXECUTABLE) {} |
2829 | 2803 |
(...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3035 count = 0; | 3009 count = 0; |
3036 } | 3010 } |
3037 // Must be small, since an iteration is used for lookup. | 3011 // Must be small, since an iteration is used for lookup. |
3038 static const int kMaxComments = 64; | 3012 static const int kMaxComments = 64; |
3039 }; | 3013 }; |
3040 #endif | 3014 #endif |
3041 } // namespace internal | 3015 } // namespace internal |
3042 } // namespace v8 | 3016 } // namespace v8 |
3043 | 3017 |
3044 #endif // V8_HEAP_SPACES_H_ | 3018 #endif // V8_HEAP_SPACES_H_ |
OLD | NEW |