fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 1 | // Copyright 2011 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | #ifndef V8_SPACES_H_ |
| 29 | #define V8_SPACES_H_ |
| 30 | |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 31 | #include "allocation.h" |
fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 32 | #include "list.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 33 | #include "log.h" |
| 34 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 35 | namespace v8 { |
| 36 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 37 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 38 | class Isolate; |
| 39 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 40 | // ----------------------------------------------------------------------------- |
| 41 | // Heap structures: |
| 42 | // |
| 43 | // A JS heap consists of a young generation, an old generation, and a large |
| 44 | // object space. The young generation is divided into two semispaces. A |
| 45 | // scavenger implements Cheney's copying algorithm. The old generation is |
| 46 | // separated into a map space and an old object space. The map space contains |
| 47 | // all (and only) map objects, the rest of old objects go into the old space. |
| 48 | // The old generation is collected by a mark-sweep-compact collector. |
| 49 | // |
| 50 | // The semispaces of the young generation are contiguous. The old and map |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 51 | // spaces consists of a list of pages. A page has a page header and an object |
| 52 | // area. A page size is deliberately chosen as 8K bytes. |
| 53 | // The first word of a page is an opaque page header that has the |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 54 | // address of the next page and its ownership information. The second word may |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 55 | // have the allocation top address of this page. Heap objects are aligned to the |
| 56 | // pointer size. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 57 | // |
| 58 | // There is a separate large object space for objects larger than |
| 59 | // Page::kMaxHeapObjectSize, so that they do not have to move during |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 60 | // collection. The large object space is paged. Pages in large object space |
| 61 | // may be larger than 8K. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 62 | // |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 63 | // A card marking write barrier is used to keep track of intergenerational |
| 64 | // references. Old space pages are divided into regions of Page::kRegionSize |
| 65 | // size. Each region has a corresponding dirty bit in the page header which is |
| 66 | // set if the region might contain pointers to new space. For details about |
| 67 | // dirty bits encoding see comments in the Page::GetRegionNumberForAddress() |
| 68 | // method body. |
| 69 | // |
| 70 | // During scavenges and mark-sweep collections we iterate intergenerational |
| 71 | // pointers without decoding heap object maps so if the page belongs to old |
| 72 | // pointer space or large object space it is essential to guarantee that |
| 73 | // the page does not contain any garbage pointers to new space: every pointer |
| 74 | // aligned word which satisfies the Heap::InNewSpace() predicate must be a |
| 75 | // pointer to a live heap object in new space. Thus objects in old pointer |
| 76 | // and large object spaces should have a special layout (e.g. no bare integer |
| 77 | // fields). This requirement does not apply to map space which is iterated in |
| 78 | // a special fashion. However we still require pointer fields of dead maps to |
| 79 | // be cleaned. |
| 80 | // |
| 81 | // To enable lazy cleaning of old space pages we use a notion of allocation |
| 82 | // watermark. Every pointer under watermark is considered to be well formed. |
| 83 | // Page allocation watermark is not necessarily equal to page allocation top but |
| 84 | // all alive objects on page should reside under allocation watermark. |
| 85 | // During scavenge allocation watermark might be bumped and invalid pointers |
| 86 | // might appear below it. To avoid following them we store a valid watermark |
| 87 | // into special field in the page header and set a page WATERMARK_INVALIDATED |
| 88 | // flag. For details see comments in the Page::SetAllocationWatermark() method |
| 89 | // body. |
| 90 | // |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 91 | |
| 92 | // Some assertion macros used in the debugging mode. |
| 93 | |
sgjesse@chromium.org | 846fb74 | 2009-12-18 08:56:33 +0000 | [diff] [blame] | 94 | #define ASSERT_PAGE_ALIGNED(address) \ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 95 | ASSERT((OffsetFrom(address) & Page::kPageAlignmentMask) == 0) |
| 96 | |
sgjesse@chromium.org | 846fb74 | 2009-12-18 08:56:33 +0000 | [diff] [blame] | 97 | #define ASSERT_OBJECT_ALIGNED(address) \ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 98 | ASSERT((OffsetFrom(address) & kObjectAlignmentMask) == 0) |
| 99 | |
sgjesse@chromium.org | 846fb74 | 2009-12-18 08:56:33 +0000 | [diff] [blame] | 100 | #define ASSERT_MAP_ALIGNED(address) \ |
| 101 | ASSERT((OffsetFrom(address) & kMapAlignmentMask) == 0) |
| 102 | |
| 103 | #define ASSERT_OBJECT_SIZE(size) \ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 104 | ASSERT((0 < size) && (size <= Page::kMaxHeapObjectSize)) |
| 105 | |
sgjesse@chromium.org | 846fb74 | 2009-12-18 08:56:33 +0000 | [diff] [blame] | 106 | #define ASSERT_PAGE_OFFSET(offset) \ |
| 107 | ASSERT((Page::kObjectStartOffset <= offset) \ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 108 | && (offset <= Page::kPageSize)) |
| 109 | |
sgjesse@chromium.org | 846fb74 | 2009-12-18 08:56:33 +0000 | [diff] [blame] | 110 | #define ASSERT_MAP_PAGE_INDEX(index) \ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 111 | ASSERT((0 <= index) && (index <= MapSpace::kMaxMapPageIndex)) |
| 112 | |
| 113 | |
| 114 | class PagedSpace; |
| 115 | class MemoryAllocator; |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 116 | class AllocationInfo; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 117 | |
| 118 | // ----------------------------------------------------------------------------- |
| 119 | // A page normally has 8K bytes. Large object pages may be larger. A page |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 120 | // address is always aligned to the 8K page size. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 121 | // |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 122 | // Each page starts with a header of Page::kPageHeaderSize size which contains |
| 123 | // bookkeeping data. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 124 | // |
| 125 | // The mark-compact collector transforms a map pointer into a page index and a |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 126 | // page offset. The exact encoding is described in the comments for |
sgjesse@chromium.org | 846fb74 | 2009-12-18 08:56:33 +0000 | [diff] [blame] | 127 | // class MapWord in objects.h. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 128 | // |
| 129 | // The only way to get a page pointer is by calling factory methods: |
| 130 | // Page* p = Page::FromAddress(addr); or |
| 131 | // Page* p = Page::FromAllocationTop(top); |
| 132 | class Page { |
| 133 | public: |
| 134 | // Returns the page containing a given address. The address ranges |
| 135 | // from [page_addr .. page_addr + kPageSize[ |
| 136 | // |
| 137 | // Note that this function only works for addresses in normal paged |
sgjesse@chromium.org | b9d7da1 | 2009-08-05 08:38:10 +0000 | [diff] [blame] | 138 | // spaces and addresses in the first 8K of large object pages (i.e., |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 139 | // the start of large objects but not necessarily derived pointers |
| 140 | // within them). |
| 141 | INLINE(static Page* FromAddress(Address a)) { |
| 142 | return reinterpret_cast<Page*>(OffsetFrom(a) & ~kPageAlignmentMask); |
| 143 | } |
| 144 | |
| 145 | // Returns the page containing an allocation top. Because an allocation |
| 146 | // top address can be the upper bound of the page, we need to subtract |
| 147 | // it with kPointerSize first. The address ranges from |
| 148 | // [page_addr + kObjectStartOffset .. page_addr + kPageSize]. |
| 149 | INLINE(static Page* FromAllocationTop(Address top)) { |
| 150 | Page* p = FromAddress(top - kPointerSize); |
| 151 | ASSERT_PAGE_OFFSET(p->Offset(top)); |
| 152 | return p; |
| 153 | } |
| 154 | |
| 155 | // Returns the start address of this page. |
| 156 | Address address() { return reinterpret_cast<Address>(this); } |
| 157 | |
| 158 | // Checks whether this is a valid page address. |
| 159 | bool is_valid() { return address() != NULL; } |
| 160 | |
| 161 | // Returns the next page of this page. |
| 162 | inline Page* next_page(); |
| 163 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 164 | // Return the end of allocation in this page. Undefined for unused pages. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 165 | inline Address AllocationTop(); |
| 166 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 167 | // Return the allocation watermark for the page. |
| 168 | // For old space pages it is guaranteed that the area under the watermark |
| 169 | // does not contain any garbage pointers to new space. |
| 170 | inline Address AllocationWatermark(); |
| 171 | |
| 172 | // Return the allocation watermark offset from the beginning of the page. |
| 173 | inline uint32_t AllocationWatermarkOffset(); |
| 174 | |
| 175 | inline void SetAllocationWatermark(Address allocation_watermark); |
| 176 | |
| 177 | inline void SetCachedAllocationWatermark(Address allocation_watermark); |
| 178 | inline Address CachedAllocationWatermark(); |
| 179 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 180 | // Returns the start address of the object area in this page. |
| 181 | Address ObjectAreaStart() { return address() + kObjectStartOffset; } |
| 182 | |
| 183 | // Returns the end address (exclusive) of the object area in this page. |
| 184 | Address ObjectAreaEnd() { return address() + Page::kPageSize; } |
| 185 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 186 | // Checks whether an address is page aligned. |
| 187 | static bool IsAlignedToPageSize(Address a) { |
| 188 | return 0 == (OffsetFrom(a) & kPageAlignmentMask); |
| 189 | } |
| 190 | |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 191 | // True if this page was in use before current compaction started. |
| 192 | // Result is valid only for pages owned by paged spaces and |
| 193 | // only after PagedSpace::PrepareForMarkCompact was called. |
| 194 | inline bool WasInUseBeforeMC(); |
| 195 | |
| 196 | inline void SetWasInUseBeforeMC(bool was_in_use); |
| 197 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 198 | // True if this page is a large object page. |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 199 | inline bool IsLargeObjectPage(); |
| 200 | |
| 201 | inline void SetIsLargeObjectPage(bool is_large_object_page); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 202 | |
erik.corry@gmail.com | 145eff5 | 2010-08-23 11:36:18 +0000 | [diff] [blame] | 203 | inline bool IsPageExecutable(); |
| 204 | |
| 205 | inline void SetIsPageExecutable(bool is_page_executable); |
| 206 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 207 | // Returns the offset of a given address to this page. |
| 208 | INLINE(int Offset(Address a)) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 209 | int offset = static_cast<int>(a - address()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 210 | ASSERT_PAGE_OFFSET(offset); |
| 211 | return offset; |
| 212 | } |
| 213 | |
| 214 | // Returns the address for a given offset to the this page. |
| 215 | Address OffsetToAddress(int offset) { |
| 216 | ASSERT_PAGE_OFFSET(offset); |
| 217 | return address() + offset; |
| 218 | } |
| 219 | |
| 220 | // --------------------------------------------------------------------- |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 221 | // Card marking support |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 222 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 223 | static const uint32_t kAllRegionsCleanMarks = 0x0; |
| 224 | static const uint32_t kAllRegionsDirtyMarks = 0xFFFFFFFF; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 225 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 226 | inline uint32_t GetRegionMarks(); |
| 227 | inline void SetRegionMarks(uint32_t dirty); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 228 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 229 | inline uint32_t GetRegionMaskForAddress(Address addr); |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 230 | inline uint32_t GetRegionMaskForSpan(Address start, int length_in_bytes); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 231 | inline int GetRegionNumberForAddress(Address addr); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 232 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 233 | inline void MarkRegionDirty(Address addr); |
| 234 | inline bool IsRegionDirty(Address addr); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 235 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 236 | inline void ClearRegionMarks(Address start, |
| 237 | Address end, |
| 238 | bool reaches_limit); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 239 | |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 240 | // Page size in bytes. This must be a multiple of the OS page size. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 241 | static const int kPageSize = 1 << kPageSizeBits; |
| 242 | |
| 243 | // Page size mask. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 244 | static const intptr_t kPageAlignmentMask = (1 << kPageSizeBits) - 1; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 245 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 246 | static const int kPageHeaderSize = kPointerSize + kPointerSize + kIntSize + |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 247 | kIntSize + kPointerSize + kPointerSize; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 248 | |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 249 | // The start offset of the object area in a page. Aligned to both maps and |
| 250 | // code alignment to be suitable for both. |
| 251 | static const int kObjectStartOffset = |
| 252 | CODE_POINTER_ALIGN(MAP_POINTER_ALIGN(kPageHeaderSize)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 253 | |
| 254 | // Object area size in bytes. |
| 255 | static const int kObjectAreaSize = kPageSize - kObjectStartOffset; |
| 256 | |
| 257 | // Maximum object size that fits in a page. |
| 258 | static const int kMaxHeapObjectSize = kObjectAreaSize; |
| 259 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 260 | static const int kDirtyFlagOffset = 2 * kPointerSize; |
| 261 | static const int kRegionSizeLog2 = 8; |
| 262 | static const int kRegionSize = 1 << kRegionSizeLog2; |
| 263 | static const intptr_t kRegionAlignmentMask = (kRegionSize - 1); |
| 264 | |
| 265 | STATIC_CHECK(kRegionSize == kPageSize / kBitsPerInt); |
| 266 | |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 267 | enum PageFlag { |
erik.corry@gmail.com | 145eff5 | 2010-08-23 11:36:18 +0000 | [diff] [blame] | 268 | IS_NORMAL_PAGE = 0, |
| 269 | WAS_IN_USE_BEFORE_MC, |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 270 | |
| 271 | // Page allocation watermark was bumped by preallocation during scavenge. |
| 272 | // Correct watermark can be retrieved by CachedAllocationWatermark() method |
erik.corry@gmail.com | 145eff5 | 2010-08-23 11:36:18 +0000 | [diff] [blame] | 273 | WATERMARK_INVALIDATED, |
| 274 | IS_EXECUTABLE, |
| 275 | NUM_PAGE_FLAGS // Must be last |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 276 | }; |
erik.corry@gmail.com | 145eff5 | 2010-08-23 11:36:18 +0000 | [diff] [blame] | 277 | static const int kPageFlagMask = (1 << NUM_PAGE_FLAGS) - 1; |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 278 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 279 | // To avoid an additional WATERMARK_INVALIDATED flag clearing pass during |
| 280 | // scavenge we just invalidate the watermark on each old space page after |
| 281 | // processing it. And then we flip the meaning of the WATERMARK_INVALIDATED |
| 282 | // flag at the beginning of the next scavenge and each page becomes marked as |
| 283 | // having a valid watermark. |
| 284 | // |
| 285 | // The following invariant must hold for pages in old pointer and map spaces: |
| 286 | // If page is in use then page is marked as having invalid watermark at |
| 287 | // the beginning and at the end of any GC. |
| 288 | // |
| 289 | // This invariant guarantees that after flipping flag meaning at the |
| 290 | // beginning of scavenge all pages in use will be marked as having valid |
| 291 | // watermark. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 292 | static inline void FlipMeaningOfInvalidatedWatermarkFlag(Heap* heap); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 293 | |
| 294 | // Returns true if the page allocation watermark was not altered during |
| 295 | // scavenge. |
| 296 | inline bool IsWatermarkValid(); |
| 297 | |
| 298 | inline void InvalidateWatermark(bool value); |
| 299 | |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 300 | inline bool GetPageFlag(PageFlag flag); |
| 301 | inline void SetPageFlag(PageFlag flag, bool value); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 302 | inline void ClearPageFlags(); |
| 303 | |
| 304 | inline void ClearGCFields(); |
| 305 | |
erik.corry@gmail.com | 145eff5 | 2010-08-23 11:36:18 +0000 | [diff] [blame] | 306 | static const int kAllocationWatermarkOffsetShift = WATERMARK_INVALIDATED + 1; |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 307 | static const int kAllocationWatermarkOffsetBits = kPageSizeBits + 1; |
| 308 | static const uint32_t kAllocationWatermarkOffsetMask = |
| 309 | ((1 << kAllocationWatermarkOffsetBits) - 1) << |
| 310 | kAllocationWatermarkOffsetShift; |
| 311 | |
| 312 | static const uint32_t kFlagsMask = |
| 313 | ((1 << kAllocationWatermarkOffsetShift) - 1); |
| 314 | |
| 315 | STATIC_CHECK(kBitsPerInt - kAllocationWatermarkOffsetShift >= |
| 316 | kAllocationWatermarkOffsetBits); |
| 317 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 318 | //--------------------------------------------------------------------------- |
| 319 | // Page header description. |
| 320 | // |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 321 | // If a page is not in the large object space, the first word, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 322 | // opaque_header, encodes the next page address (aligned to kPageSize 8K) |
| 323 | // and the chunk number (0 ~ 8K-1). Only MemoryAllocator should use |
| 324 | // opaque_header. The value range of the opaque_header is [0..kPageSize[, |
| 325 | // or [next_page_start, next_page_end[. It cannot point to a valid address |
| 326 | // in the current page. If a page is in the large object space, the first |
| 327 | // word *may* (if the page start and large object chunk start are the |
| 328 | // same) contain the address of the next large object chunk. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 329 | intptr_t opaque_header; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 330 | |
| 331 | // If the page is not in the large object space, the low-order bit of the |
| 332 | // second word is set. If the page is in the large object space, the |
| 333 | // second word *may* (if the page start and large object chunk start are |
| 334 | // the same) contain the large object chunk size. In either case, the |
| 335 | // low-order bit for large object pages will be cleared. |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 336 | // For normal pages this word is used to store page flags and |
| 337 | // offset of allocation top. |
| 338 | intptr_t flags_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 339 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 340 | // This field contains dirty marks for regions covering the page. Only dirty |
| 341 | // regions might contain intergenerational references. |
| 342 | // Only 32 dirty marks are supported so for large object pages several regions |
| 343 | // might be mapped to a single dirty mark. |
| 344 | uint32_t dirty_regions_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 345 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 346 | // The index of the page in its owner space. |
| 347 | int mc_page_index; |
| 348 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 349 | // During mark-compact collections this field contains the forwarding address |
| 350 | // of the first live object in this page. |
| 351 | // During scavenge collection this field is used to store allocation watermark |
| 352 | // if it is altered during scavenge. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 353 | Address mc_first_forwarded; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 354 | |
| 355 | Heap* heap_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 356 | }; |
| 357 | |
| 358 | |
| 359 | // ---------------------------------------------------------------------------- |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 360 | // Space is the abstract superclass for all allocation spaces. |
| 361 | class Space : public Malloced { |
| 362 | public: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 363 | Space(Heap* heap, AllocationSpace id, Executability executable) |
| 364 | : heap_(heap), id_(id), executable_(executable) {} |
kasperl@chromium.org | f5aa837 | 2009-03-24 14:47:14 +0000 | [diff] [blame] | 365 | |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 366 | virtual ~Space() {} |
kasperl@chromium.org | f5aa837 | 2009-03-24 14:47:14 +0000 | [diff] [blame] | 367 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 368 | Heap* heap() const { return heap_; } |
| 369 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 370 | // Does the space need executable memory? |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 371 | Executability executable() { return executable_; } |
kasperl@chromium.org | f5aa837 | 2009-03-24 14:47:14 +0000 | [diff] [blame] | 372 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 373 | // Identity used in error reporting. |
| 374 | AllocationSpace identity() { return id_; } |
kasperl@chromium.org | f5aa837 | 2009-03-24 14:47:14 +0000 | [diff] [blame] | 375 | |
erik.corry@gmail.com | 4a6c327 | 2010-11-18 12:04:40 +0000 | [diff] [blame] | 376 | // Returns allocated size. |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 377 | virtual intptr_t Size() = 0; |
kasperl@chromium.org | f5aa837 | 2009-03-24 14:47:14 +0000 | [diff] [blame] | 378 | |
erik.corry@gmail.com | 4a6c327 | 2010-11-18 12:04:40 +0000 | [diff] [blame] | 379 | // Returns size of objects. Can differ from the allocated size |
| 380 | // (e.g. see LargeObjectSpace). |
| 381 | virtual intptr_t SizeOfObjects() { return Size(); } |
| 382 | |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 383 | #ifdef DEBUG |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 384 | virtual void Print() = 0; |
| 385 | #endif |
kasperl@chromium.org | f5aa837 | 2009-03-24 14:47:14 +0000 | [diff] [blame] | 386 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 387 | // After calling this we can allocate a certain number of bytes using only |
| 388 | // linear allocation (with a LinearAllocationScope and an AlwaysAllocateScope) |
| 389 | // without using freelists or causing a GC. This is used by partial |
| 390 | // snapshots. It returns true of space was reserved or false if a GC is |
| 391 | // needed. For paged spaces the space requested must include the space wasted |
| 392 | // at the end of each when allocating linearly. |
| 393 | virtual bool ReserveSpace(int bytes) = 0; |
| 394 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 395 | private: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 396 | Heap* heap_; |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 397 | AllocationSpace id_; |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 398 | Executability executable_; |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 399 | }; |
| 400 | |
| 401 | |
| 402 | // ---------------------------------------------------------------------------- |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 403 | // All heap objects containing executable code (code objects) must be allocated |
| 404 | // from a 2 GB range of memory, so that they can call each other using 32-bit |
| 405 | // displacements. This happens automatically on 32-bit platforms, where 32-bit |
| 406 | // displacements cover the entire 4GB virtual address space. On 64-bit |
| 407 | // platforms, we support this using the CodeRange object, which reserves and |
| 408 | // manages a range of virtual memory. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 409 | class CodeRange { |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 410 | public: |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 411 | explicit CodeRange(Isolate* isolate); |
| 412 | |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 413 | // Reserves a range of virtual memory, but does not commit any of it. |
| 414 | // Can only be called once, at heap initialization time. |
| 415 | // Returns false on failure. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 416 | bool Setup(const size_t requested_size); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 417 | |
| 418 | // Frees the range of virtual memory, and frees the data structures used to |
| 419 | // manage it. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 420 | void TearDown(); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 421 | |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 422 | bool exists() { return this != NULL && code_range_ != NULL; } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 423 | bool contains(Address address) { |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 424 | if (this == NULL || code_range_ == NULL) return false; |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 425 | Address start = static_cast<Address>(code_range_->address()); |
| 426 | return start <= address && address < start + code_range_->size(); |
| 427 | } |
| 428 | |
| 429 | // Allocates a chunk of memory from the large-object portion of |
| 430 | // the code range. On platforms with no separate code range, should |
| 431 | // not be called. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 432 | MUST_USE_RESULT void* AllocateRawMemory(const size_t requested, |
| 433 | size_t* allocated); |
| 434 | void FreeRawMemory(void* buf, size_t length); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 435 | |
| 436 | private: |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 437 | Isolate* isolate_; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 438 | |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 439 | // The reserved range of virtual memory that all code objects are put in. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 440 | VirtualMemory* code_range_; |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 441 | // Plain old data class, just a struct plus a constructor. |
| 442 | class FreeBlock { |
| 443 | public: |
| 444 | FreeBlock(Address start_arg, size_t size_arg) |
| 445 | : start(start_arg), size(size_arg) {} |
| 446 | FreeBlock(void* start_arg, size_t size_arg) |
| 447 | : start(static_cast<Address>(start_arg)), size(size_arg) {} |
| 448 | |
| 449 | Address start; |
| 450 | size_t size; |
| 451 | }; |
| 452 | |
| 453 | // Freed blocks of memory are added to the free list. When the allocation |
| 454 | // list is exhausted, the free list is sorted and merged to make the new |
| 455 | // allocation list. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 456 | List<FreeBlock> free_list_; |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 457 | // Memory is allocated from the free blocks on the allocation list. |
| 458 | // The block at current_allocation_block_index_ is the current block. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 459 | List<FreeBlock> allocation_list_; |
| 460 | int current_allocation_block_index_; |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 461 | |
| 462 | // Finds a block on the allocation list that contains at least the |
| 463 | // requested amount of memory. If none is found, sorts and merges |
| 464 | // the existing free memory blocks, and searches again. |
| 465 | // If none can be found, terminates V8 with FatalProcessOutOfMemory. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 466 | void GetNextAllocationBlock(size_t requested); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 467 | // Compares the start addresses of two free blocks. |
| 468 | static int CompareFreeBlockAddress(const FreeBlock* left, |
| 469 | const FreeBlock* right); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 470 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 471 | DISALLOW_COPY_AND_ASSIGN(CodeRange); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 472 | }; |
| 473 | |
| 474 | |
| 475 | // ---------------------------------------------------------------------------- |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 476 | // A space acquires chunks of memory from the operating system. The memory |
| 477 | // allocator manages chunks for the paged heap spaces (old space and map |
| 478 | // space). A paged chunk consists of pages. Pages in a chunk have contiguous |
| 479 | // addresses and are linked as a list. |
| 480 | // |
| 481 | // The allocator keeps an initial chunk which is used for the new space. The |
| 482 | // leftover regions of the initial chunk are used for the initial chunks of |
| 483 | // old space and map space if they are big enough to hold at least one page. |
| 484 | // The allocator assumes that there is one old space and one map space, each |
| 485 | // expands the space by allocating kPagesPerChunk pages except the last |
| 486 | // expansion (before running out of space). The first chunk may contain fewer |
| 487 | // than kPagesPerChunk pages as well. |
| 488 | // |
| 489 | // The memory allocator also allocates chunks for the large object space, but |
| 490 | // they are managed by the space itself. The new space does not expand. |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 491 | // |
| 492 | // The fact that pages for paged spaces are allocated and deallocated in chunks |
| 493 | // induces a constraint on the order of pages in a linked lists. We say that |
| 494 | // pages are linked in the chunk-order if and only if every two consecutive |
| 495 | // pages from the same chunk are consecutive in the linked list. |
| 496 | // |
| 497 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 498 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 499 | class MemoryAllocator { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 500 | public: |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 501 | explicit MemoryAllocator(Isolate* isolate); |
| 502 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 503 | // Initializes its internal bookkeeping structures. |
ager@chromium.org | 01fe7df | 2010-11-10 11:59:11 +0000 | [diff] [blame] | 504 | // Max capacity of the total space and executable memory limit. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 505 | bool Setup(intptr_t max_capacity, intptr_t capacity_executable); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 506 | |
| 507 | // Deletes valid chunks. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 508 | void TearDown(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 509 | |
| 510 | // Reserves an initial address range of virtual memory to be split between |
| 511 | // the two new space semispaces, the old space, and the map space. The |
| 512 | // memory is not yet committed or assigned to spaces and split into pages. |
| 513 | // The initial chunk is unmapped when the memory allocator is torn down. |
| 514 | // This function should only be called when there is not already a reserved |
| 515 | // initial chunk (initial_chunk_ should be NULL). It returns the start |
| 516 | // address of the initial chunk if successful, with the side effect of |
| 517 | // setting the initial chunk, or else NULL if unsuccessful and leaves the |
| 518 | // initial chunk NULL. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 519 | void* ReserveInitialChunk(const size_t requested); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 520 | |
| 521 | // Commits pages from an as-yet-unmanaged block of virtual memory into a |
| 522 | // paged space. The block should be part of the initial chunk reserved via |
| 523 | // a call to ReserveInitialChunk. The number of pages is always returned in |
| 524 | // the output parameter num_pages. This function assumes that the start |
| 525 | // address is non-null and that it is big enough to hold at least one |
| 526 | // page-aligned page. The call always succeeds, and num_pages is always |
| 527 | // greater than zero. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 528 | Page* CommitPages(Address start, size_t size, PagedSpace* owner, |
| 529 | int* num_pages); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 530 | |
| 531 | // Commit a contiguous block of memory from the initial chunk. Assumes that |
| 532 | // the address is not NULL, the size is greater than zero, and that the |
| 533 | // block is contained in the initial chunk. Returns true if it succeeded |
| 534 | // and false otherwise. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 535 | bool CommitBlock(Address start, size_t size, Executability executable); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 536 | |
ager@chromium.org | add848f | 2009-08-13 12:44:13 +0000 | [diff] [blame] | 537 | // Uncommit a contiguous block of memory [start..(start+size)[. |
| 538 | // start is not NULL, the size is greater than zero, and the |
| 539 | // block is contained in the initial chunk. Returns true if it succeeded |
| 540 | // and false otherwise. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 541 | bool UncommitBlock(Address start, size_t size); |
ager@chromium.org | add848f | 2009-08-13 12:44:13 +0000 | [diff] [blame] | 542 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 543 | // Zaps a contiguous block of memory [start..(start+size)[ thus |
| 544 | // filling it up with a recognizable non-NULL bit pattern. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 545 | void ZapBlock(Address start, size_t size); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 546 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 547 | // Attempts to allocate the requested (non-zero) number of pages from the |
| 548 | // OS. Fewer pages might be allocated than requested. If it fails to |
| 549 | // allocate memory for the OS or cannot allocate a single page, this |
| 550 | // function returns an invalid page pointer (NULL). The caller must check |
| 551 | // whether the returned page is valid (by calling Page::is_valid()). It is |
| 552 | // guaranteed that allocated pages have contiguous addresses. The actual |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 553 | // number of allocated pages is returned in the output parameter |
| 554 | // allocated_pages. If the PagedSpace owner is executable and there is |
| 555 | // a code range, the pages are allocated from the code range. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 556 | Page* AllocatePages(int requested_pages, int* allocated_pages, |
| 557 | PagedSpace* owner); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 558 | |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 559 | // Frees pages from a given page and after. Requires pages to be |
| 560 | // linked in chunk-order (see comment for class). |
| 561 | // If 'p' is the first page of a chunk, pages from 'p' are freed |
| 562 | // and this function returns an invalid page pointer. |
| 563 | // Otherwise, the function searches a page after 'p' that is |
| 564 | // the first page of a chunk. Pages after the found page |
| 565 | // are freed and the function returns 'p'. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 566 | Page* FreePages(Page* p); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 567 | |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 568 | // Frees all pages owned by given space. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 569 | void FreeAllPages(PagedSpace* space); |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 570 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 571 | // Allocates and frees raw memory of certain size. |
| 572 | // These are just thin wrappers around OS::Allocate and OS::Free, |
| 573 | // but keep track of allocated bytes as part of heap. |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 574 | // If the flag is EXECUTABLE and a code range exists, the requested |
| 575 | // memory is allocated from the code range. If a code range exists |
| 576 | // and the freed memory is in it, the code range manages the freed memory. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 577 | MUST_USE_RESULT void* AllocateRawMemory(const size_t requested, |
| 578 | size_t* allocated, |
| 579 | Executability executable); |
| 580 | void FreeRawMemory(void* buf, |
| 581 | size_t length, |
| 582 | Executability executable); |
| 583 | void PerformAllocationCallback(ObjectSpace space, |
| 584 | AllocationAction action, |
| 585 | size_t size); |
kmillikin@chromium.org | 3cdd9e1 | 2010-09-06 11:39:48 +0000 | [diff] [blame] | 586 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 587 | void AddMemoryAllocationCallback(MemoryAllocationCallback callback, |
| 588 | ObjectSpace space, |
| 589 | AllocationAction action); |
| 590 | void RemoveMemoryAllocationCallback(MemoryAllocationCallback callback); |
| 591 | bool MemoryAllocationCallbackRegistered(MemoryAllocationCallback callback); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 592 | |
| 593 | // Returns the maximum available bytes of heaps. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 594 | intptr_t Available() { return capacity_ < size_ ? 0 : capacity_ - size_; } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 595 | |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 596 | // Returns allocated spaces in bytes. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 597 | intptr_t Size() { return size_; } |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 598 | |
ager@chromium.org | 01fe7df | 2010-11-10 11:59:11 +0000 | [diff] [blame] | 599 | // Returns the maximum available executable bytes of heaps. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 600 | intptr_t AvailableExecutable() { |
ager@chromium.org | 01fe7df | 2010-11-10 11:59:11 +0000 | [diff] [blame] | 601 | if (capacity_executable_ < size_executable_) return 0; |
| 602 | return capacity_executable_ - size_executable_; |
| 603 | } |
| 604 | |
erik.corry@gmail.com | 145eff5 | 2010-08-23 11:36:18 +0000 | [diff] [blame] | 605 | // Returns allocated executable spaces in bytes. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 606 | intptr_t SizeExecutable() { return size_executable_; } |
erik.corry@gmail.com | 145eff5 | 2010-08-23 11:36:18 +0000 | [diff] [blame] | 607 | |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 608 | // Returns maximum available bytes that the old space can have. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 609 | intptr_t MaxAvailable() { |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 610 | return (Available() / Page::kPageSize) * Page::kObjectAreaSize; |
| 611 | } |
| 612 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 613 | // Links two pages. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 614 | inline void SetNextPage(Page* prev, Page* next); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 615 | |
| 616 | // Returns the next page of a given page. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 617 | inline Page* GetNextPage(Page* p); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 618 | |
| 619 | // Checks whether a page belongs to a space. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 620 | inline bool IsPageInSpace(Page* p, PagedSpace* space); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 621 | |
| 622 | // Returns the space that owns the given page. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 623 | inline PagedSpace* PageOwner(Page* page); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 624 | |
| 625 | // Finds the first/last page in the same chunk as a given page. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 626 | Page* FindFirstPageInSameChunk(Page* p); |
| 627 | Page* FindLastPageInSameChunk(Page* p); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 628 | |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 629 | // Relinks list of pages owned by space to make it chunk-ordered. |
| 630 | // Returns new first and last pages of space. |
| 631 | // Also returns last page in relinked list which has WasInUsedBeforeMC |
| 632 | // flag set. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 633 | void RelinkPageListInChunkOrder(PagedSpace* space, |
| 634 | Page** first_page, |
| 635 | Page** last_page, |
| 636 | Page** last_page_in_use); |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 637 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 638 | #ifdef DEBUG |
| 639 | // Reports statistic info of the space. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 640 | void ReportStatistics(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 641 | #endif |
| 642 | |
| 643 | // Due to encoding limitation, we can only have 8K chunks. |
sgjesse@chromium.org | 846fb74 | 2009-12-18 08:56:33 +0000 | [diff] [blame] | 644 | static const int kMaxNofChunks = 1 << kPageSizeBits; |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 645 | // If a chunk has at least 16 pages, the maximum heap size is about |
| 646 | // 8K * 8K * 16 = 1G bytes. |
| 647 | #ifdef V8_TARGET_ARCH_X64 |
| 648 | static const int kPagesPerChunk = 32; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 649 | // On 64 bit the chunk table consists of 4 levels of 4096-entry tables. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 650 | static const int kChunkTableLevels = 4; |
| 651 | static const int kChunkTableBitsPerLevel = 12; |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 652 | #else |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 653 | static const int kPagesPerChunk = 16; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 654 | // On 32 bit the chunk table consists of 2 levels of 256-entry tables. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 655 | static const int kChunkTableLevels = 2; |
| 656 | static const int kChunkTableBitsPerLevel = 8; |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 657 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 658 | |
| 659 | private: |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 660 | static const int kChunkSize = kPagesPerChunk * Page::kPageSize; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 661 | |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 662 | Isolate* isolate_; |
| 663 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 664 | // Maximum space size in bytes. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 665 | intptr_t capacity_; |
ager@chromium.org | 01fe7df | 2010-11-10 11:59:11 +0000 | [diff] [blame] | 666 | // Maximum subset of capacity_ that can be executable |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 667 | intptr_t capacity_executable_; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 668 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 669 | // Allocated space size in bytes. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 670 | intptr_t size_; |
| 671 | |
erik.corry@gmail.com | 145eff5 | 2010-08-23 11:36:18 +0000 | [diff] [blame] | 672 | // Allocated executable space size in bytes. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 673 | intptr_t size_executable_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 674 | |
kmillikin@chromium.org | 3cdd9e1 | 2010-09-06 11:39:48 +0000 | [diff] [blame] | 675 | struct MemoryAllocationCallbackRegistration { |
| 676 | MemoryAllocationCallbackRegistration(MemoryAllocationCallback callback, |
| 677 | ObjectSpace space, |
| 678 | AllocationAction action) |
| 679 | : callback(callback), space(space), action(action) { |
| 680 | } |
| 681 | MemoryAllocationCallback callback; |
| 682 | ObjectSpace space; |
| 683 | AllocationAction action; |
| 684 | }; |
| 685 | // A List of callback that are triggered when memory is allocated or free'd |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 686 | List<MemoryAllocationCallbackRegistration> |
kmillikin@chromium.org | 3cdd9e1 | 2010-09-06 11:39:48 +0000 | [diff] [blame] | 687 | memory_allocation_callbacks_; |
| 688 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 689 | // The initial chunk of virtual memory. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 690 | VirtualMemory* initial_chunk_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 691 | |
| 692 | // Allocated chunk info: chunk start address, chunk size, and owning space. |
| 693 | class ChunkInfo BASE_EMBEDDED { |
| 694 | public: |
kmillikin@chromium.org | 3cdd9e1 | 2010-09-06 11:39:48 +0000 | [diff] [blame] | 695 | ChunkInfo() : address_(NULL), |
| 696 | size_(0), |
| 697 | owner_(NULL), |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 698 | executable_(NOT_EXECUTABLE), |
| 699 | owner_identity_(FIRST_SPACE) {} |
kmillikin@chromium.org | 3cdd9e1 | 2010-09-06 11:39:48 +0000 | [diff] [blame] | 700 | inline void init(Address a, size_t s, PagedSpace* o); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 701 | Address address() { return address_; } |
| 702 | size_t size() { return size_; } |
| 703 | PagedSpace* owner() { return owner_; } |
kmillikin@chromium.org | 3cdd9e1 | 2010-09-06 11:39:48 +0000 | [diff] [blame] | 704 | // We save executability of the owner to allow using it |
| 705 | // when collecting stats after the owner has been destroyed. |
| 706 | Executability executable() const { return executable_; } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 707 | AllocationSpace owner_identity() const { return owner_identity_; } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 708 | |
| 709 | private: |
| 710 | Address address_; |
| 711 | size_t size_; |
| 712 | PagedSpace* owner_; |
kmillikin@chromium.org | 3cdd9e1 | 2010-09-06 11:39:48 +0000 | [diff] [blame] | 713 | Executability executable_; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 714 | AllocationSpace owner_identity_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 715 | }; |
| 716 | |
| 717 | // Chunks_, free_chunk_ids_ and top_ act as a stack of free chunk ids. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 718 | List<ChunkInfo> chunks_; |
| 719 | List<int> free_chunk_ids_; |
| 720 | int max_nof_chunks_; |
| 721 | int top_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 722 | |
| 723 | // Push/pop a free chunk id onto/from the stack. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 724 | void Push(int free_chunk_id); |
| 725 | int Pop(); |
| 726 | bool OutOfChunkIds() { return top_ == 0; } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 727 | |
| 728 | // Frees a chunk. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 729 | void DeleteChunk(int chunk_id); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 730 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 731 | // Basic check whether a chunk id is in the valid range. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 732 | inline bool IsValidChunkId(int chunk_id); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 733 | |
| 734 | // Checks whether a chunk id identifies an allocated chunk. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 735 | inline bool IsValidChunk(int chunk_id); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 736 | |
| 737 | // Returns the chunk id that a page belongs to. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 738 | inline int GetChunkId(Page* p); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 739 | |
kasperl@chromium.org | f5aa837 | 2009-03-24 14:47:14 +0000 | [diff] [blame] | 740 | // True if the address lies in the initial chunk. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 741 | inline bool InInitialChunk(Address address); |
kasperl@chromium.org | f5aa837 | 2009-03-24 14:47:14 +0000 | [diff] [blame] | 742 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 743 | // Initializes pages in a chunk. Returns the first page address. |
| 744 | // This function and GetChunkId() are provided for the mark-compact |
| 745 | // collector to rebuild page headers in the from space, which is |
| 746 | // used as a marking stack and its page headers are destroyed. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 747 | Page* InitializePagesInChunk(int chunk_id, int pages_in_chunk, |
| 748 | PagedSpace* owner); |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 749 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 750 | Page* RelinkPagesInChunk(int chunk_id, |
| 751 | Address chunk_start, |
| 752 | size_t chunk_size, |
| 753 | Page* prev, |
| 754 | Page** last_page_in_use); |
| 755 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 756 | DISALLOW_COPY_AND_ASSIGN(MemoryAllocator); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 757 | }; |
| 758 | |
| 759 | |
| 760 | // ----------------------------------------------------------------------------- |
| 761 | // Interface for heap object iterator to be implemented by all object space |
| 762 | // object iterators. |
| 763 | // |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 764 | // NOTE: The space specific object iterators also implements the own next() |
| 765 | // method which is used to avoid using virtual functions |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 766 | // iterating a specific space. |
| 767 | |
| 768 | class ObjectIterator : public Malloced { |
| 769 | public: |
| 770 | virtual ~ObjectIterator() { } |
| 771 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 772 | virtual HeapObject* next_object() = 0; |
| 773 | }; |
| 774 | |
| 775 | |
| 776 | // ----------------------------------------------------------------------------- |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 777 | // Heap object iterator in new/old/map spaces. |
| 778 | // |
| 779 | // A HeapObjectIterator iterates objects from a given address to the |
| 780 | // top of a space. The given address must be below the current |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 781 | // allocation pointer (space top). There are some caveats. |
| 782 | // |
| 783 | // (1) If the space top changes upward during iteration (because of |
| 784 | // allocating new objects), the iterator does not iterate objects |
| 785 | // above the original space top. The caller must create a new |
| 786 | // iterator starting from the old top in order to visit these new |
| 787 | // objects. |
| 788 | // |
| 789 | // (2) If new objects are allocated below the original allocation top |
| 790 | // (e.g., free-list allocation in paged spaces), the new objects |
| 791 | // may or may not be iterated depending on their position with |
| 792 | // respect to the current point of iteration. |
| 793 | // |
| 794 | // (3) The space top should not change downward during iteration, |
| 795 | // otherwise the iterator will return not-necessarily-valid |
| 796 | // objects. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 797 | |
| 798 | class HeapObjectIterator: public ObjectIterator { |
| 799 | public: |
| 800 | // Creates a new object iterator in a given space. If a start |
| 801 | // address is not given, the iterator starts from the space bottom. |
| 802 | // If the size function is not given, the iterator calls the default |
| 803 | // Object::Size(). |
| 804 | explicit HeapObjectIterator(PagedSpace* space); |
| 805 | HeapObjectIterator(PagedSpace* space, HeapObjectCallback size_func); |
| 806 | HeapObjectIterator(PagedSpace* space, Address start); |
| 807 | HeapObjectIterator(PagedSpace* space, |
| 808 | Address start, |
| 809 | HeapObjectCallback size_func); |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 810 | HeapObjectIterator(Page* page, HeapObjectCallback size_func); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 811 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 812 | inline HeapObject* next() { |
| 813 | return (cur_addr_ < cur_limit_) ? FromCurrentPage() : FromNextPage(); |
| 814 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 815 | |
| 816 | // implementation of ObjectIterator. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 817 | virtual HeapObject* next_object() { return next(); } |
| 818 | |
| 819 | private: |
| 820 | Address cur_addr_; // current iteration point |
| 821 | Address end_addr_; // end iteration point |
| 822 | Address cur_limit_; // current page limit |
| 823 | HeapObjectCallback size_func_; // size function |
| 824 | Page* end_page_; // caches the page of the end address |
| 825 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 826 | HeapObject* FromCurrentPage() { |
| 827 | ASSERT(cur_addr_ < cur_limit_); |
| 828 | |
| 829 | HeapObject* obj = HeapObject::FromAddress(cur_addr_); |
| 830 | int obj_size = (size_func_ == NULL) ? obj->Size() : size_func_(obj); |
| 831 | ASSERT_OBJECT_SIZE(obj_size); |
| 832 | |
| 833 | cur_addr_ += obj_size; |
| 834 | ASSERT(cur_addr_ <= cur_limit_); |
| 835 | |
| 836 | return obj; |
| 837 | } |
| 838 | |
| 839 | // Slow path of next, goes into the next page. |
| 840 | HeapObject* FromNextPage(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 841 | |
| 842 | // Initializes fields. |
| 843 | void Initialize(Address start, Address end, HeapObjectCallback size_func); |
| 844 | |
| 845 | #ifdef DEBUG |
| 846 | // Verifies whether fields have valid values. |
| 847 | void Verify(); |
| 848 | #endif |
| 849 | }; |
| 850 | |
| 851 | |
| 852 | // ----------------------------------------------------------------------------- |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 853 | // A PageIterator iterates the pages in a paged space. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 854 | // |
| 855 | // The PageIterator class provides three modes for iterating pages in a space: |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 856 | // PAGES_IN_USE iterates pages containing allocated objects. |
| 857 | // PAGES_USED_BY_MC iterates pages that hold relocated objects during a |
| 858 | // mark-compact collection. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 859 | // ALL_PAGES iterates all pages in the space. |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 860 | // |
| 861 | // There are some caveats. |
| 862 | // |
| 863 | // (1) If the space expands during iteration, new pages will not be |
| 864 | // returned by the iterator in any mode. |
| 865 | // |
| 866 | // (2) If new objects are allocated during iteration, they will appear |
| 867 | // in pages returned by the iterator. Allocation may cause the |
| 868 | // allocation pointer or MC allocation pointer in the last page to |
| 869 | // change between constructing the iterator and iterating the last |
| 870 | // page. |
| 871 | // |
| 872 | // (3) The space should not shrink during iteration, otherwise the |
| 873 | // iterator will return deallocated pages. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 874 | |
| 875 | class PageIterator BASE_EMBEDDED { |
| 876 | public: |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 877 | enum Mode { |
| 878 | PAGES_IN_USE, |
| 879 | PAGES_USED_BY_MC, |
| 880 | ALL_PAGES |
| 881 | }; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 882 | |
| 883 | PageIterator(PagedSpace* space, Mode mode); |
| 884 | |
| 885 | inline bool has_next(); |
| 886 | inline Page* next(); |
| 887 | |
| 888 | private: |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 889 | PagedSpace* space_; |
| 890 | Page* prev_page_; // Previous page returned. |
| 891 | Page* stop_page_; // Page to stop at (last page returned by the iterator). |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 892 | }; |
| 893 | |
| 894 | |
| 895 | // ----------------------------------------------------------------------------- |
| 896 | // A space has a list of pages. The next page can be accessed via |
| 897 | // Page::next_page() call. The next page of the last page is an |
| 898 | // invalid page pointer. A space can expand and shrink dynamically. |
| 899 | |
| 900 | // An abstraction of allocation and relocation pointers in a page-structured |
| 901 | // space. |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 902 | class AllocationInfo { |
| 903 | public: |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 904 | Address top; // current allocation top |
| 905 | Address limit; // current allocation limit |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 906 | |
| 907 | #ifdef DEBUG |
| 908 | bool VerifyPagedAllocation() { |
| 909 | return (Page::FromAllocationTop(top) == Page::FromAllocationTop(limit)) |
| 910 | && (top <= limit); |
| 911 | } |
| 912 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 913 | }; |
| 914 | |
| 915 | |
| 916 | // An abstraction of the accounting statistics of a page-structured space. |
| 917 | // The 'capacity' of a space is the number of object-area bytes (ie, not |
| 918 | // including page bookkeeping structures) currently in the space. The 'size' |
| 919 | // of a space is the number of allocated bytes, the 'waste' in the space is |
| 920 | // the number of bytes that are not allocated and not available to |
| 921 | // allocation without reorganizing the space via a GC (eg, small blocks due |
| 922 | // to internal fragmentation, top of page areas in map space), and the bytes |
| 923 | // 'available' is the number of unallocated bytes that are not waste. The |
| 924 | // capacity is the sum of size, waste, and available. |
| 925 | // |
| 926 | // The stats are only set by functions that ensure they stay balanced. These |
| 927 | // functions increase or decrease one of the non-capacity stats in |
| 928 | // conjunction with capacity, or else they always balance increases and |
| 929 | // decreases to the non-capacity stats. |
| 930 | class AllocationStats BASE_EMBEDDED { |
| 931 | public: |
| 932 | AllocationStats() { Clear(); } |
| 933 | |
| 934 | // Zero out all the allocation statistics (ie, no capacity). |
| 935 | void Clear() { |
| 936 | capacity_ = 0; |
| 937 | available_ = 0; |
| 938 | size_ = 0; |
| 939 | waste_ = 0; |
| 940 | } |
| 941 | |
| 942 | // Reset the allocation statistics (ie, available = capacity with no |
| 943 | // wasted or allocated bytes). |
| 944 | void Reset() { |
| 945 | available_ = capacity_; |
| 946 | size_ = 0; |
| 947 | waste_ = 0; |
| 948 | } |
| 949 | |
| 950 | // Accessors for the allocation statistics. |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 951 | intptr_t Capacity() { return capacity_; } |
| 952 | intptr_t Available() { return available_; } |
| 953 | intptr_t Size() { return size_; } |
| 954 | intptr_t Waste() { return waste_; } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 955 | |
| 956 | // Grow the space by adding available bytes. |
| 957 | void ExpandSpace(int size_in_bytes) { |
| 958 | capacity_ += size_in_bytes; |
| 959 | available_ += size_in_bytes; |
| 960 | } |
| 961 | |
| 962 | // Shrink the space by removing available bytes. |
| 963 | void ShrinkSpace(int size_in_bytes) { |
| 964 | capacity_ -= size_in_bytes; |
| 965 | available_ -= size_in_bytes; |
| 966 | } |
| 967 | |
| 968 | // Allocate from available bytes (available -> size). |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 969 | void AllocateBytes(intptr_t size_in_bytes) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 970 | available_ -= size_in_bytes; |
| 971 | size_ += size_in_bytes; |
| 972 | } |
| 973 | |
| 974 | // Free allocated bytes, making them available (size -> available). |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 975 | void DeallocateBytes(intptr_t size_in_bytes) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 976 | size_ -= size_in_bytes; |
| 977 | available_ += size_in_bytes; |
| 978 | } |
| 979 | |
| 980 | // Waste free bytes (available -> waste). |
| 981 | void WasteBytes(int size_in_bytes) { |
| 982 | available_ -= size_in_bytes; |
| 983 | waste_ += size_in_bytes; |
| 984 | } |
| 985 | |
| 986 | // Consider the wasted bytes to be allocated, as they contain filler |
| 987 | // objects (waste -> size). |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 988 | void FillWastedBytes(intptr_t size_in_bytes) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 989 | waste_ -= size_in_bytes; |
| 990 | size_ += size_in_bytes; |
| 991 | } |
| 992 | |
| 993 | private: |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 994 | intptr_t capacity_; |
| 995 | intptr_t available_; |
| 996 | intptr_t size_; |
| 997 | intptr_t waste_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 998 | }; |
| 999 | |
| 1000 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1001 | class PagedSpace : public Space { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1002 | public: |
| 1003 | // Creates a space with a maximum capacity, and an id. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1004 | PagedSpace(Heap* heap, |
| 1005 | intptr_t max_capacity, |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 1006 | AllocationSpace id, |
| 1007 | Executability executable); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1008 | |
| 1009 | virtual ~PagedSpace() {} |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1010 | |
| 1011 | // Set up the space using the given address range of virtual memory (from |
| 1012 | // the memory allocator's initial chunk) if possible. If the block of |
| 1013 | // addresses is not big enough to contain a single page-aligned page, a |
| 1014 | // fresh chunk will be allocated. |
| 1015 | bool Setup(Address start, size_t size); |
| 1016 | |
| 1017 | // Returns true if the space has been successfully set up and not |
| 1018 | // subsequently torn down. |
| 1019 | bool HasBeenSetup(); |
| 1020 | |
| 1021 | // Cleans up the space, frees all pages in this space except those belonging |
| 1022 | // to the initial chunk, uncommits addresses in the initial chunk. |
| 1023 | void TearDown(); |
| 1024 | |
| 1025 | // Checks whether an object/address is in this space. |
| 1026 | inline bool Contains(Address a); |
| 1027 | bool Contains(HeapObject* o) { return Contains(o->address()); } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1028 | // Never crashes even if a is not a valid pointer. |
| 1029 | inline bool SafeContains(Address a); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1030 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1031 | // Given an address occupied by a live object, return that object if it is |
| 1032 | // in this space, or Failure::Exception() if it is not. The implementation |
| 1033 | // iterates over objects in the page containing the address, the cost is |
| 1034 | // linear in the number of objects in the page. It may be slow. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1035 | MUST_USE_RESULT MaybeObject* FindObject(Address addr); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1036 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1037 | // Checks whether page is currently in use by this space. |
| 1038 | bool IsUsed(Page* page); |
| 1039 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 1040 | void MarkAllPagesClean(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1041 | |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 1042 | // Prepares for a mark-compact GC. |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 1043 | virtual void PrepareForMarkCompact(bool will_compact); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 1044 | |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 1045 | // The top of allocation in a page in this space. Undefined if page is unused. |
| 1046 | Address PageAllocationTop(Page* page) { |
| 1047 | return page == TopPageOf(allocation_info_) ? top() |
| 1048 | : PageAllocationLimit(page); |
| 1049 | } |
| 1050 | |
| 1051 | // The limit of allocation for a page in this space. |
| 1052 | virtual Address PageAllocationLimit(Page* page) = 0; |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 1053 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 1054 | void FlushTopPageWatermark() { |
| 1055 | AllocationTopPage()->SetCachedAllocationWatermark(top()); |
| 1056 | AllocationTopPage()->InvalidateWatermark(true); |
| 1057 | } |
| 1058 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1059 | // Current capacity without growing (Size() + Available() + Waste()). |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 1060 | intptr_t Capacity() { return accounting_stats_.Capacity(); } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1061 | |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 1062 | // Total amount of memory committed for this space. For paged |
| 1063 | // spaces this equals the capacity. |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 1064 | intptr_t CommittedMemory() { return Capacity(); } |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 1065 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1066 | // Available bytes without growing. |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 1067 | intptr_t Available() { return accounting_stats_.Available(); } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1068 | |
| 1069 | // Allocated bytes in this space. |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 1070 | virtual intptr_t Size() { return accounting_stats_.Size(); } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1071 | |
| 1072 | // Wasted bytes due to fragmentation and not recoverable until the |
| 1073 | // next GC of this space. |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 1074 | intptr_t Waste() { return accounting_stats_.Waste(); } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1075 | |
| 1076 | // Returns the address of the first object in this space. |
| 1077 | Address bottom() { return first_page_->ObjectAreaStart(); } |
| 1078 | |
| 1079 | // Returns the allocation pointer in this space. |
| 1080 | Address top() { return allocation_info_.top; } |
| 1081 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1082 | // Allocate the requested number of bytes in the space if possible, return a |
| 1083 | // failure object if not. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1084 | MUST_USE_RESULT inline MaybeObject* AllocateRaw(int size_in_bytes); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1085 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1086 | // Allocate the requested number of bytes for relocation during mark-compact |
| 1087 | // collection. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1088 | MUST_USE_RESULT inline MaybeObject* MCAllocateRaw(int size_in_bytes); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1089 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 1090 | virtual bool ReserveSpace(int bytes); |
| 1091 | |
| 1092 | // Used by ReserveSpace. |
| 1093 | virtual void PutRestOfCurrentPageOnFreeList(Page* current_page) = 0; |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1094 | |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 1095 | // Free all pages in range from prev (exclusive) to last (inclusive). |
| 1096 | // Freed pages are moved to the end of page list. |
| 1097 | void FreePages(Page* prev, Page* last); |
| 1098 | |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 1099 | // Deallocates a block. |
| 1100 | virtual void DeallocateBlock(Address start, |
| 1101 | int size_in_bytes, |
| 1102 | bool add_to_freelist) = 0; |
| 1103 | |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 1104 | // Set space allocation info. |
| 1105 | void SetTop(Address top) { |
| 1106 | allocation_info_.top = top; |
| 1107 | allocation_info_.limit = PageAllocationLimit(Page::FromAllocationTop(top)); |
| 1108 | } |
| 1109 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1110 | // --------------------------------------------------------------------------- |
| 1111 | // Mark-compact collection support functions |
| 1112 | |
| 1113 | // Set the relocation point to the beginning of the space. |
| 1114 | void MCResetRelocationInfo(); |
| 1115 | |
| 1116 | // Writes relocation info to the top page. |
| 1117 | void MCWriteRelocationInfoToPage() { |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 1118 | TopPageOf(mc_forwarding_info_)-> |
| 1119 | SetAllocationWatermark(mc_forwarding_info_.top); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1120 | } |
| 1121 | |
| 1122 | // Computes the offset of a given address in this space to the beginning |
| 1123 | // of the space. |
| 1124 | int MCSpaceOffsetForAddress(Address addr); |
| 1125 | |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 1126 | // Updates the allocation pointer to the relocation top after a mark-compact |
| 1127 | // collection. |
| 1128 | virtual void MCCommitRelocationInfo() = 0; |
| 1129 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1130 | // Releases half of unused pages. |
| 1131 | void Shrink(); |
| 1132 | |
| 1133 | // Ensures that the capacity is at least 'capacity'. Returns false on failure. |
| 1134 | bool EnsureCapacity(int capacity); |
| 1135 | |
| 1136 | #ifdef DEBUG |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1137 | // Print meta info and objects in this space. |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 1138 | virtual void Print(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1139 | |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 1140 | // Verify integrity of this space. |
| 1141 | virtual void Verify(ObjectVisitor* visitor); |
| 1142 | |
| 1143 | // Overridden by subclasses to verify space-specific object |
| 1144 | // properties (e.g., only maps or free-list nodes are in map space). |
| 1145 | virtual void VerifyObject(HeapObject* obj) {} |
| 1146 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1147 | // Report code object related statistics |
| 1148 | void CollectCodeStatistics(); |
| 1149 | static void ReportCodeStatistics(); |
| 1150 | static void ResetCodeStatistics(); |
| 1151 | #endif |
| 1152 | |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 1153 | // Returns the page of the allocation pointer. |
| 1154 | Page* AllocationTopPage() { return TopPageOf(allocation_info_); } |
| 1155 | |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 1156 | void RelinkPageListInChunkOrder(bool deallocate_blocks); |
| 1157 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1158 | protected: |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1159 | // Maximum capacity of this space. |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 1160 | intptr_t max_capacity_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1161 | |
| 1162 | // Accounting information for this space. |
| 1163 | AllocationStats accounting_stats_; |
| 1164 | |
| 1165 | // The first page in this space. |
| 1166 | Page* first_page_; |
| 1167 | |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 1168 | // The last page in this space. Initially set in Setup, updated in |
| 1169 | // Expand and Shrink. |
| 1170 | Page* last_page_; |
| 1171 | |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 1172 | // True if pages owned by this space are linked in chunk-order. |
| 1173 | // See comment for class MemoryAllocator for definition of chunk-order. |
| 1174 | bool page_list_is_chunk_ordered_; |
| 1175 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1176 | // Normal allocation information. |
| 1177 | AllocationInfo allocation_info_; |
| 1178 | |
| 1179 | // Relocation information during mark-compact collections. |
| 1180 | AllocationInfo mc_forwarding_info_; |
| 1181 | |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 1182 | // Bytes of each page that cannot be allocated. Possibly non-zero |
| 1183 | // for pages in spaces with only fixed-size objects. Always zero |
| 1184 | // for pages in spaces with variable sized objects (those pages are |
| 1185 | // padded with free-list nodes). |
| 1186 | int page_extra_; |
| 1187 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1188 | // Sets allocation pointer to a page bottom. |
| 1189 | static void SetAllocationInfo(AllocationInfo* alloc_info, Page* p); |
| 1190 | |
| 1191 | // Returns the top page specified by an allocation info structure. |
| 1192 | static Page* TopPageOf(AllocationInfo alloc_info) { |
| 1193 | return Page::FromAllocationTop(alloc_info.limit); |
| 1194 | } |
| 1195 | |
kasperl@chromium.org | eac059f | 2010-01-25 11:02:06 +0000 | [diff] [blame] | 1196 | int CountPagesToTop() { |
| 1197 | Page* p = Page::FromAllocationTop(allocation_info_.top); |
| 1198 | PageIterator it(this, PageIterator::ALL_PAGES); |
| 1199 | int counter = 1; |
| 1200 | while (it.has_next()) { |
| 1201 | if (it.next() == p) return counter; |
| 1202 | counter++; |
| 1203 | } |
| 1204 | UNREACHABLE(); |
| 1205 | return -1; |
| 1206 | } |
| 1207 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1208 | // Expands the space by allocating a fixed number of pages. Returns false if |
| 1209 | // it cannot allocate requested number of pages from OS. Newly allocated |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 1210 | // pages are append to the last_page; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1211 | bool Expand(Page* last_page); |
| 1212 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1213 | // Generic fast case allocation function that tries linear allocation in |
| 1214 | // the top page of 'alloc_info'. Returns NULL on failure. |
| 1215 | inline HeapObject* AllocateLinearly(AllocationInfo* alloc_info, |
| 1216 | int size_in_bytes); |
| 1217 | |
| 1218 | // During normal allocation or deserialization, roll to the next page in |
| 1219 | // the space (there is assumed to be one) and allocate there. This |
| 1220 | // function is space-dependent. |
| 1221 | virtual HeapObject* AllocateInNextPage(Page* current_page, |
| 1222 | int size_in_bytes) = 0; |
| 1223 | |
| 1224 | // Slow path of AllocateRaw. This function is space-dependent. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1225 | MUST_USE_RESULT virtual HeapObject* SlowAllocateRaw(int size_in_bytes) = 0; |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1226 | |
| 1227 | // Slow path of MCAllocateRaw. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1228 | MUST_USE_RESULT HeapObject* SlowMCAllocateRaw(int size_in_bytes); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1229 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1230 | #ifdef DEBUG |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 1231 | // Returns the number of total pages in this space. |
| 1232 | int CountTotalPages(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1233 | #endif |
| 1234 | private: |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1235 | |
| 1236 | // Returns a pointer to the page of the relocation pointer. |
| 1237 | Page* MCRelocationTopPage() { return TopPageOf(mc_forwarding_info_); } |
| 1238 | |
kasperl@chromium.org | f5aa837 | 2009-03-24 14:47:14 +0000 | [diff] [blame] | 1239 | friend class PageIterator; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1240 | }; |
| 1241 | |
| 1242 | |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 1243 | class NumberAndSizeInfo BASE_EMBEDDED { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1244 | public: |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 1245 | NumberAndSizeInfo() : number_(0), bytes_(0) {} |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1246 | |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 1247 | int number() const { return number_; } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1248 | void increment_number(int num) { number_ += num; } |
| 1249 | |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 1250 | int bytes() const { return bytes_; } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1251 | void increment_bytes(int size) { bytes_ += size; } |
| 1252 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1253 | void clear() { |
| 1254 | number_ = 0; |
| 1255 | bytes_ = 0; |
| 1256 | } |
| 1257 | |
| 1258 | private: |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1259 | int number_; |
| 1260 | int bytes_; |
| 1261 | }; |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 1262 | |
| 1263 | |
| 1264 | // HistogramInfo class for recording a single "bar" of a histogram. This |
whesse@chromium.org | 030d38e | 2011-07-13 13:23:34 +0000 | [diff] [blame] | 1265 | // class is used for collecting statistics to print to the log file. |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 1266 | class HistogramInfo: public NumberAndSizeInfo { |
| 1267 | public: |
| 1268 | HistogramInfo() : NumberAndSizeInfo() {} |
| 1269 | |
| 1270 | const char* name() { return name_; } |
| 1271 | void set_name(const char* name) { name_ = name; } |
| 1272 | |
| 1273 | private: |
| 1274 | const char* name_; |
| 1275 | }; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1276 | |
| 1277 | |
| 1278 | // ----------------------------------------------------------------------------- |
| 1279 | // SemiSpace in young generation |
| 1280 | // |
| 1281 | // A semispace is a contiguous chunk of memory. The mark-compact collector |
| 1282 | // uses the memory in the from space as a marking stack when tracing live |
| 1283 | // objects. |
| 1284 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1285 | class SemiSpace : public Space { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1286 | public: |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1287 | // Constructor. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1288 | explicit SemiSpace(Heap* heap) : Space(heap, NEW_SPACE, NOT_EXECUTABLE) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1289 | start_ = NULL; |
| 1290 | age_mark_ = NULL; |
| 1291 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1292 | |
| 1293 | // Sets up the semispace using the given chunk. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1294 | bool Setup(Address start, int initial_capacity, int maximum_capacity); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1295 | |
| 1296 | // Tear down the space. Heap memory was not allocated by the space, so it |
| 1297 | // is not deallocated here. |
| 1298 | void TearDown(); |
| 1299 | |
| 1300 | // True if the space has been set up but not torn down. |
| 1301 | bool HasBeenSetup() { return start_ != NULL; } |
| 1302 | |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 1303 | // Grow the size of the semispace by committing extra virtual memory. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1304 | // Assumes that the caller has checked that the semispace has not reached |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 1305 | // its maximum capacity (and thus there is space available in the reserved |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1306 | // address range to grow). |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 1307 | bool Grow(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1308 | |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 1309 | // Grow the semispace to the new capacity. The new capacity |
| 1310 | // requested must be larger than the current capacity. |
| 1311 | bool GrowTo(int new_capacity); |
| 1312 | |
| 1313 | // Shrinks the semispace to the new capacity. The new capacity |
| 1314 | // requested must be more than the amount of used memory in the |
| 1315 | // semispace and less than the current capacity. |
| 1316 | bool ShrinkTo(int new_capacity); |
| 1317 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1318 | // Returns the start address of the space. |
| 1319 | Address low() { return start_; } |
| 1320 | // Returns one past the end address of the space. |
| 1321 | Address high() { return low() + capacity_; } |
| 1322 | |
| 1323 | // Age mark accessors. |
| 1324 | Address age_mark() { return age_mark_; } |
| 1325 | void set_age_mark(Address mark) { age_mark_ = mark; } |
| 1326 | |
| 1327 | // True if the address is in the address range of this semispace (not |
| 1328 | // necessarily below the allocation pointer). |
| 1329 | bool Contains(Address a) { |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 1330 | return (reinterpret_cast<uintptr_t>(a) & address_mask_) |
| 1331 | == reinterpret_cast<uintptr_t>(start_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1332 | } |
| 1333 | |
| 1334 | // True if the object is a heap object in the address range of this |
| 1335 | // semispace (not necessarily below the allocation pointer). |
| 1336 | bool Contains(Object* o) { |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 1337 | return (reinterpret_cast<uintptr_t>(o) & object_mask_) == object_expected_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1338 | } |
| 1339 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 1340 | // The offset of an address from the beginning of the space. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1341 | int SpaceOffsetForAddress(Address addr) { |
| 1342 | return static_cast<int>(addr - low()); |
| 1343 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1344 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 1345 | // If we don't have these here then SemiSpace will be abstract. However |
| 1346 | // they should never be called. |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 1347 | virtual intptr_t Size() { |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 1348 | UNREACHABLE(); |
| 1349 | return 0; |
| 1350 | } |
| 1351 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 1352 | virtual bool ReserveSpace(int bytes) { |
| 1353 | UNREACHABLE(); |
| 1354 | return false; |
| 1355 | } |
| 1356 | |
ager@chromium.org | add848f | 2009-08-13 12:44:13 +0000 | [diff] [blame] | 1357 | bool is_committed() { return committed_; } |
| 1358 | bool Commit(); |
| 1359 | bool Uncommit(); |
| 1360 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1361 | #ifdef DEBUG |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 1362 | virtual void Print(); |
| 1363 | virtual void Verify(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1364 | #endif |
| 1365 | |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 1366 | // Returns the current capacity of the semi space. |
| 1367 | int Capacity() { return capacity_; } |
| 1368 | |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 1369 | // Returns the maximum capacity of the semi space. |
| 1370 | int MaximumCapacity() { return maximum_capacity_; } |
| 1371 | |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 1372 | // Returns the initial capacity of the semi space. |
| 1373 | int InitialCapacity() { return initial_capacity_; } |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 1374 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1375 | private: |
| 1376 | // The current and maximum capacity of the space. |
| 1377 | int capacity_; |
| 1378 | int maximum_capacity_; |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 1379 | int initial_capacity_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1380 | |
| 1381 | // The start address of the space. |
| 1382 | Address start_; |
| 1383 | // Used to govern object promotion during mark-compact collection. |
| 1384 | Address age_mark_; |
| 1385 | |
| 1386 | // Masks and comparison values to test for containment in this semispace. |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 1387 | uintptr_t address_mask_; |
| 1388 | uintptr_t object_mask_; |
| 1389 | uintptr_t object_expected_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1390 | |
ager@chromium.org | add848f | 2009-08-13 12:44:13 +0000 | [diff] [blame] | 1391 | bool committed_; |
| 1392 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1393 | public: |
| 1394 | TRACK_MEMORY("SemiSpace") |
| 1395 | }; |
| 1396 | |
| 1397 | |
| 1398 | // A SemiSpaceIterator is an ObjectIterator that iterates over the active |
| 1399 | // semispace of the heap's new space. It iterates over the objects in the |
| 1400 | // semispace from a given start address (defaulting to the bottom of the |
| 1401 | // semispace) to the top of the semispace. New objects allocated after the |
| 1402 | // iterator is created are not iterated. |
| 1403 | class SemiSpaceIterator : public ObjectIterator { |
| 1404 | public: |
| 1405 | // Create an iterator over the objects in the given space. If no start |
| 1406 | // address is given, the iterator starts from the bottom of the space. If |
| 1407 | // no size function is given, the iterator calls Object::Size(). |
| 1408 | explicit SemiSpaceIterator(NewSpace* space); |
| 1409 | SemiSpaceIterator(NewSpace* space, HeapObjectCallback size_func); |
| 1410 | SemiSpaceIterator(NewSpace* space, Address start); |
| 1411 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1412 | HeapObject* next() { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1413 | if (current_ == limit_) return NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1414 | |
| 1415 | HeapObject* object = HeapObject::FromAddress(current_); |
| 1416 | int size = (size_func_ == NULL) ? object->Size() : size_func_(object); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1417 | |
| 1418 | current_ += size; |
| 1419 | return object; |
| 1420 | } |
| 1421 | |
| 1422 | // Implementation of the ObjectIterator functions. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1423 | virtual HeapObject* next_object() { return next(); } |
| 1424 | |
| 1425 | private: |
| 1426 | void Initialize(NewSpace* space, Address start, Address end, |
| 1427 | HeapObjectCallback size_func); |
| 1428 | |
| 1429 | // The semispace. |
| 1430 | SemiSpace* space_; |
| 1431 | // The current iteration point. |
| 1432 | Address current_; |
| 1433 | // The end of iteration. |
| 1434 | Address limit_; |
| 1435 | // The callback function. |
| 1436 | HeapObjectCallback size_func_; |
| 1437 | }; |
| 1438 | |
| 1439 | |
| 1440 | // ----------------------------------------------------------------------------- |
| 1441 | // The young generation space. |
| 1442 | // |
| 1443 | // The new space consists of a contiguous pair of semispaces. It simply |
| 1444 | // forwards most functions to the appropriate semispace. |
| 1445 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1446 | class NewSpace : public Space { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1447 | public: |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1448 | // Constructor. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1449 | explicit NewSpace(Heap* heap) |
| 1450 | : Space(heap, NEW_SPACE, NOT_EXECUTABLE), |
| 1451 | to_space_(heap), |
| 1452 | from_space_(heap) {} |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1453 | |
| 1454 | // Sets up the new space using the given chunk. |
| 1455 | bool Setup(Address start, int size); |
| 1456 | |
| 1457 | // Tears down the space. Heap memory was not allocated by the space, so it |
| 1458 | // is not deallocated here. |
| 1459 | void TearDown(); |
| 1460 | |
| 1461 | // True if the space has been set up but not torn down. |
| 1462 | bool HasBeenSetup() { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1463 | return to_space_.HasBeenSetup() && from_space_.HasBeenSetup(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1464 | } |
| 1465 | |
| 1466 | // Flip the pair of spaces. |
| 1467 | void Flip(); |
| 1468 | |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 1469 | // Grow the capacity of the semispaces. Assumes that they are not at |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 1470 | // their maximum capacity. |
| 1471 | void Grow(); |
| 1472 | |
| 1473 | // Shrink the capacity of the semispaces. |
| 1474 | void Shrink(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1475 | |
| 1476 | // True if the address or object lies in the address range of either |
| 1477 | // semispace (not necessarily below the allocation pointer). |
| 1478 | bool Contains(Address a) { |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 1479 | return (reinterpret_cast<uintptr_t>(a) & address_mask_) |
| 1480 | == reinterpret_cast<uintptr_t>(start_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1481 | } |
| 1482 | bool Contains(Object* o) { |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 1483 | return (reinterpret_cast<uintptr_t>(o) & object_mask_) == object_expected_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1484 | } |
| 1485 | |
| 1486 | // Return the allocated bytes in the active semispace. |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 1487 | virtual intptr_t Size() { return static_cast<int>(top() - bottom()); } |
| 1488 | // The same, but returning an int. We have to have the one that returns |
| 1489 | // intptr_t because it is inherited, but if we know we are dealing with the |
| 1490 | // new space, which can't get as big as the other spaces then this is useful: |
| 1491 | int SizeAsInt() { return static_cast<int>(Size()); } |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 1492 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1493 | // Return the current capacity of a semispace. |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 1494 | intptr_t Capacity() { |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 1495 | ASSERT(to_space_.Capacity() == from_space_.Capacity()); |
| 1496 | return to_space_.Capacity(); |
| 1497 | } |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 1498 | |
| 1499 | // Return the total amount of memory committed for new space. |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 1500 | intptr_t CommittedMemory() { |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 1501 | if (from_space_.is_committed()) return 2 * Capacity(); |
| 1502 | return Capacity(); |
| 1503 | } |
| 1504 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1505 | // Return the available bytes without growing in the active semispace. |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 1506 | intptr_t Available() { return Capacity() - Size(); } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1507 | |
| 1508 | // Return the maximum capacity of a semispace. |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 1509 | int MaximumCapacity() { |
| 1510 | ASSERT(to_space_.MaximumCapacity() == from_space_.MaximumCapacity()); |
| 1511 | return to_space_.MaximumCapacity(); |
| 1512 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1513 | |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 1514 | // Returns the initial capacity of a semispace. |
| 1515 | int InitialCapacity() { |
| 1516 | ASSERT(to_space_.InitialCapacity() == from_space_.InitialCapacity()); |
| 1517 | return to_space_.InitialCapacity(); |
| 1518 | } |
| 1519 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1520 | // Return the address of the allocation pointer in the active semispace. |
| 1521 | Address top() { return allocation_info_.top; } |
| 1522 | // Return the address of the first object in the active semispace. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1523 | Address bottom() { return to_space_.low(); } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1524 | |
| 1525 | // Get the age mark of the inactive semispace. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1526 | Address age_mark() { return from_space_.age_mark(); } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1527 | // Set the age mark in the active semispace. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1528 | void set_age_mark(Address mark) { to_space_.set_age_mark(mark); } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1529 | |
| 1530 | // The start address of the space and a bit mask. Anding an address in the |
| 1531 | // new space with the mask will result in the start address. |
| 1532 | Address start() { return start_; } |
sgjesse@chromium.org | b9d7da1 | 2009-08-05 08:38:10 +0000 | [diff] [blame] | 1533 | uintptr_t mask() { return address_mask_; } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1534 | |
| 1535 | // The allocation top and limit addresses. |
| 1536 | Address* allocation_top_address() { return &allocation_info_.top; } |
| 1537 | Address* allocation_limit_address() { return &allocation_info_.limit; } |
| 1538 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1539 | MUST_USE_RESULT MaybeObject* AllocateRaw(int size_in_bytes) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1540 | return AllocateRawInternal(size_in_bytes, &allocation_info_); |
| 1541 | } |
| 1542 | |
| 1543 | // Allocate the requested number of bytes for relocation during mark-compact |
| 1544 | // collection. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1545 | MUST_USE_RESULT MaybeObject* MCAllocateRaw(int size_in_bytes) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1546 | return AllocateRawInternal(size_in_bytes, &mc_forwarding_info_); |
| 1547 | } |
| 1548 | |
| 1549 | // Reset the allocation pointer to the beginning of the active semispace. |
| 1550 | void ResetAllocationInfo(); |
| 1551 | // Reset the reloction pointer to the bottom of the inactive semispace in |
| 1552 | // preparation for mark-compact collection. |
| 1553 | void MCResetRelocationInfo(); |
| 1554 | // Update the allocation pointer in the active semispace after a |
| 1555 | // mark-compact collection. |
| 1556 | void MCCommitRelocationInfo(); |
| 1557 | |
| 1558 | // Get the extent of the inactive semispace (for use as a marking stack). |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1559 | Address FromSpaceLow() { return from_space_.low(); } |
| 1560 | Address FromSpaceHigh() { return from_space_.high(); } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1561 | |
| 1562 | // Get the extent of the active semispace (to sweep newly copied objects |
| 1563 | // during a scavenge collection). |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1564 | Address ToSpaceLow() { return to_space_.low(); } |
| 1565 | Address ToSpaceHigh() { return to_space_.high(); } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1566 | |
| 1567 | // Offsets from the beginning of the semispaces. |
| 1568 | int ToSpaceOffsetForAddress(Address a) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1569 | return to_space_.SpaceOffsetForAddress(a); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1570 | } |
| 1571 | int FromSpaceOffsetForAddress(Address a) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1572 | return from_space_.SpaceOffsetForAddress(a); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1573 | } |
| 1574 | |
| 1575 | // True if the object is a heap object in the address range of the |
| 1576 | // respective semispace (not necessarily below the allocation pointer of the |
| 1577 | // semispace). |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1578 | bool ToSpaceContains(Object* o) { return to_space_.Contains(o); } |
| 1579 | bool FromSpaceContains(Object* o) { return from_space_.Contains(o); } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1580 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1581 | bool ToSpaceContains(Address a) { return to_space_.Contains(a); } |
| 1582 | bool FromSpaceContains(Address a) { return from_space_.Contains(a); } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1583 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 1584 | virtual bool ReserveSpace(int bytes); |
| 1585 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1586 | // Resizes a sequential string which must be the most recent thing that was |
| 1587 | // allocated in new space. |
| 1588 | template <typename StringType> |
| 1589 | inline void ShrinkStringAtAllocationBoundary(String* string, int len); |
| 1590 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1591 | #ifdef DEBUG |
| 1592 | // Verify the active semispace. |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 1593 | virtual void Verify(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1594 | // Print the active semispace. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1595 | virtual void Print() { to_space_.Print(); } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1596 | #endif |
| 1597 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1598 | // Iterates the active semispace to collect statistics. |
| 1599 | void CollectStatistics(); |
| 1600 | // Reports previously collected statistics of the active semispace. |
| 1601 | void ReportStatistics(); |
| 1602 | // Clears previously collected statistics. |
| 1603 | void ClearHistograms(); |
| 1604 | |
| 1605 | // Record the allocation or promotion of a heap object. Note that we don't |
| 1606 | // record every single allocation, but only those that happen in the |
| 1607 | // to space during a scavenge GC. |
| 1608 | void RecordAllocation(HeapObject* obj); |
| 1609 | void RecordPromotion(HeapObject* obj); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1610 | |
ager@chromium.org | add848f | 2009-08-13 12:44:13 +0000 | [diff] [blame] | 1611 | // Return whether the operation succeded. |
| 1612 | bool CommitFromSpaceIfNeeded() { |
| 1613 | if (from_space_.is_committed()) return true; |
| 1614 | return from_space_.Commit(); |
| 1615 | } |
| 1616 | |
| 1617 | bool UncommitFromSpace() { |
| 1618 | if (!from_space_.is_committed()) return true; |
| 1619 | return from_space_.Uncommit(); |
| 1620 | } |
| 1621 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1622 | private: |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1623 | // The semispaces. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1624 | SemiSpace to_space_; |
| 1625 | SemiSpace from_space_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1626 | |
| 1627 | // Start address and bit mask for containment testing. |
| 1628 | Address start_; |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 1629 | uintptr_t address_mask_; |
| 1630 | uintptr_t object_mask_; |
| 1631 | uintptr_t object_expected_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1632 | |
| 1633 | // Allocation pointer and limit for normal allocation and allocation during |
| 1634 | // mark-compact collection. |
| 1635 | AllocationInfo allocation_info_; |
| 1636 | AllocationInfo mc_forwarding_info_; |
| 1637 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1638 | HistogramInfo* allocated_histogram_; |
| 1639 | HistogramInfo* promoted_histogram_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1640 | |
| 1641 | // Implementation of AllocateRaw and MCAllocateRaw. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1642 | MUST_USE_RESULT inline MaybeObject* AllocateRawInternal( |
| 1643 | int size_in_bytes, |
| 1644 | AllocationInfo* alloc_info); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1645 | |
| 1646 | friend class SemiSpaceIterator; |
| 1647 | |
| 1648 | public: |
| 1649 | TRACK_MEMORY("NewSpace") |
| 1650 | }; |
| 1651 | |
| 1652 | |
| 1653 | // ----------------------------------------------------------------------------- |
| 1654 | // Free lists for old object spaces |
| 1655 | // |
| 1656 | // Free-list nodes are free blocks in the heap. They look like heap objects |
| 1657 | // (free-list node pointers have the heap object tag, and they have a map like |
| 1658 | // a heap object). They have a size and a next pointer. The next pointer is |
| 1659 | // the raw address of the next free list node (or NULL). |
| 1660 | class FreeListNode: public HeapObject { |
| 1661 | public: |
| 1662 | // Obtain a free-list node from a raw address. This is not a cast because |
| 1663 | // it does not check nor require that the first word at the address is a map |
| 1664 | // pointer. |
| 1665 | static FreeListNode* FromAddress(Address address) { |
| 1666 | return reinterpret_cast<FreeListNode*>(HeapObject::FromAddress(address)); |
| 1667 | } |
| 1668 | |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 1669 | static inline bool IsFreeListNode(HeapObject* object); |
| 1670 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1671 | // Set the size in bytes, which can be read with HeapObject::Size(). This |
| 1672 | // function also writes a map to the first word of the block so that it |
| 1673 | // looks like a heap object to the garbage collector and heap iteration |
| 1674 | // functions. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1675 | void set_size(Heap* heap, int size_in_bytes); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1676 | |
| 1677 | // Accessors for the next field. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1678 | inline Address next(Heap* heap); |
| 1679 | inline void set_next(Heap* heap, Address next); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1680 | |
| 1681 | private: |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1682 | static const int kNextOffset = POINTER_SIZE_ALIGN(ByteArray::kHeaderSize); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1683 | |
| 1684 | DISALLOW_IMPLICIT_CONSTRUCTORS(FreeListNode); |
| 1685 | }; |
| 1686 | |
| 1687 | |
| 1688 | // The free list for the old space. |
| 1689 | class OldSpaceFreeList BASE_EMBEDDED { |
| 1690 | public: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1691 | OldSpaceFreeList(Heap* heap, AllocationSpace owner); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1692 | |
| 1693 | // Clear the free list. |
| 1694 | void Reset(); |
| 1695 | |
| 1696 | // Return the number of bytes available on the free list. |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 1697 | intptr_t available() { return available_; } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1698 | |
| 1699 | // Place a node on the free list. The block of size 'size_in_bytes' |
| 1700 | // starting at 'start' is placed on the free list. The return value is the |
| 1701 | // number of bytes that have been lost due to internal fragmentation by |
| 1702 | // freeing the block. Bookkeeping information will be written to the block, |
| 1703 | // ie, its contents will be destroyed. The start address should be word |
| 1704 | // aligned, and the size should be a non-zero multiple of the word size. |
| 1705 | int Free(Address start, int size_in_bytes); |
| 1706 | |
| 1707 | // Allocate a block of size 'size_in_bytes' from the free list. The block |
| 1708 | // is unitialized. A failure is returned if no block is available. The |
| 1709 | // number of bytes lost to fragmentation is returned in the output parameter |
| 1710 | // 'wasted_bytes'. The size should be a non-zero multiple of the word size. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1711 | MUST_USE_RESULT MaybeObject* Allocate(int size_in_bytes, int* wasted_bytes); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1712 | |
erik.corry@gmail.com | 4a6c327 | 2010-11-18 12:04:40 +0000 | [diff] [blame] | 1713 | void MarkNodes(); |
| 1714 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1715 | private: |
| 1716 | // The size range of blocks, in bytes. (Smaller allocations are allowed, but |
| 1717 | // will always result in waste.) |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 1718 | static const int kMinBlockSize = 2 * kPointerSize; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1719 | static const int kMaxBlockSize = Page::kMaxHeapObjectSize; |
| 1720 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1721 | Heap* heap_; |
| 1722 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1723 | // The identity of the owning space, for building allocation Failure |
| 1724 | // objects. |
| 1725 | AllocationSpace owner_; |
| 1726 | |
| 1727 | // Total available bytes in all blocks on this free list. |
| 1728 | int available_; |
| 1729 | |
| 1730 | // Blocks are put on exact free lists in an array, indexed by size in words. |
| 1731 | // The available sizes are kept in an increasingly ordered list. Entries |
| 1732 | // corresponding to sizes < kMinBlockSize always have an empty free list |
| 1733 | // (but index kHead is used for the head of the size list). |
| 1734 | struct SizeNode { |
| 1735 | // Address of the head FreeListNode of the implied block size or NULL. |
| 1736 | Address head_node_; |
| 1737 | // Size (words) of the next larger available size if head_node_ != NULL. |
| 1738 | int next_size_; |
| 1739 | }; |
| 1740 | static const int kFreeListsLength = kMaxBlockSize / kPointerSize + 1; |
| 1741 | SizeNode free_[kFreeListsLength]; |
| 1742 | |
| 1743 | // Sentinel elements for the size list. Real elements are in ]kHead..kEnd[. |
| 1744 | static const int kHead = kMinBlockSize / kPointerSize - 1; |
| 1745 | static const int kEnd = kMaxInt; |
| 1746 | |
| 1747 | // We keep a "finger" in the size list to speed up a common pattern: |
| 1748 | // repeated requests for the same or increasing sizes. |
| 1749 | int finger_; |
| 1750 | |
| 1751 | // Starting from *prev, find and return the smallest size >= index (words), |
| 1752 | // or kEnd. Update *prev to be the largest size < index, or kHead. |
| 1753 | int FindSize(int index, int* prev) { |
| 1754 | int cur = free_[*prev].next_size_; |
| 1755 | while (cur < index) { |
| 1756 | *prev = cur; |
| 1757 | cur = free_[cur].next_size_; |
| 1758 | } |
| 1759 | return cur; |
| 1760 | } |
| 1761 | |
| 1762 | // Remove an existing element from the size list. |
| 1763 | void RemoveSize(int index) { |
| 1764 | int prev = kHead; |
| 1765 | int cur = FindSize(index, &prev); |
| 1766 | ASSERT(cur == index); |
| 1767 | free_[prev].next_size_ = free_[cur].next_size_; |
| 1768 | finger_ = prev; |
| 1769 | } |
| 1770 | |
| 1771 | // Insert a new element into the size list. |
| 1772 | void InsertSize(int index) { |
| 1773 | int prev = kHead; |
| 1774 | int cur = FindSize(index, &prev); |
| 1775 | ASSERT(cur != index); |
| 1776 | free_[prev].next_size_ = index; |
| 1777 | free_[index].next_size_ = cur; |
| 1778 | } |
| 1779 | |
| 1780 | // The size list is not updated during a sequence of calls to Free, but is |
| 1781 | // rebuilt before the next allocation. |
| 1782 | void RebuildSizeList(); |
| 1783 | bool needs_rebuild_; |
| 1784 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1785 | #ifdef DEBUG |
| 1786 | // Does this free list contain a free block located at the address of 'node'? |
| 1787 | bool Contains(FreeListNode* node); |
| 1788 | #endif |
| 1789 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1790 | DISALLOW_COPY_AND_ASSIGN(OldSpaceFreeList); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1791 | }; |
| 1792 | |
| 1793 | |
| 1794 | // The free list for the map space. |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 1795 | class FixedSizeFreeList BASE_EMBEDDED { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1796 | public: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1797 | FixedSizeFreeList(Heap* heap, AllocationSpace owner, int object_size); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1798 | |
| 1799 | // Clear the free list. |
| 1800 | void Reset(); |
| 1801 | |
| 1802 | // Return the number of bytes available on the free list. |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 1803 | intptr_t available() { return available_; } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1804 | |
| 1805 | // Place a node on the free list. The block starting at 'start' (assumed to |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 1806 | // have size object_size_) is placed on the free list. Bookkeeping |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1807 | // information will be written to the block, ie, its contents will be |
| 1808 | // destroyed. The start address should be word aligned. |
| 1809 | void Free(Address start); |
| 1810 | |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 1811 | // Allocate a fixed sized block from the free list. The block is unitialized. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1812 | // A failure is returned if no block is available. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1813 | MUST_USE_RESULT MaybeObject* Allocate(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1814 | |
erik.corry@gmail.com | 4a6c327 | 2010-11-18 12:04:40 +0000 | [diff] [blame] | 1815 | void MarkNodes(); |
| 1816 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1817 | private: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1818 | |
| 1819 | Heap* heap_; |
| 1820 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1821 | // Available bytes on the free list. |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 1822 | intptr_t available_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1823 | |
| 1824 | // The head of the free list. |
| 1825 | Address head_; |
| 1826 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 1827 | // The tail of the free list. |
| 1828 | Address tail_; |
| 1829 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1830 | // The identity of the owning space, for building allocation Failure |
| 1831 | // objects. |
| 1832 | AllocationSpace owner_; |
| 1833 | |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 1834 | // The size of the objects in this space. |
| 1835 | int object_size_; |
| 1836 | |
| 1837 | DISALLOW_COPY_AND_ASSIGN(FixedSizeFreeList); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1838 | }; |
| 1839 | |
| 1840 | |
| 1841 | // ----------------------------------------------------------------------------- |
| 1842 | // Old object space (excluding map objects) |
| 1843 | |
| 1844 | class OldSpace : public PagedSpace { |
| 1845 | public: |
| 1846 | // Creates an old space object with a given maximum capacity. |
| 1847 | // The constructor does not allocate pages from OS. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1848 | OldSpace(Heap* heap, |
| 1849 | intptr_t max_capacity, |
| 1850 | AllocationSpace id, |
| 1851 | Executability executable) |
| 1852 | : PagedSpace(heap, max_capacity, id, executable), |
| 1853 | free_list_(heap, id) { |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 1854 | page_extra_ = 0; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1855 | } |
| 1856 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1857 | // The bytes available on the free list (ie, not above the linear allocation |
| 1858 | // pointer). |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 1859 | intptr_t AvailableFree() { return free_list_.available(); } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1860 | |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 1861 | // The limit of allocation for a page in this space. |
| 1862 | virtual Address PageAllocationLimit(Page* page) { |
| 1863 | return page->ObjectAreaEnd(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1864 | } |
| 1865 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1866 | // Give a block of memory to the space's free list. It might be added to |
| 1867 | // the free list or accounted as waste. |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 1868 | // If add_to_freelist is false then just accounting stats are updated and |
| 1869 | // no attempt to add area to free list is made. |
| 1870 | void Free(Address start, int size_in_bytes, bool add_to_freelist) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1871 | accounting_stats_.DeallocateBytes(size_in_bytes); |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 1872 | |
| 1873 | if (add_to_freelist) { |
| 1874 | int wasted_bytes = free_list_.Free(start, size_in_bytes); |
| 1875 | accounting_stats_.WasteBytes(wasted_bytes); |
| 1876 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1877 | } |
| 1878 | |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 1879 | virtual void DeallocateBlock(Address start, |
| 1880 | int size_in_bytes, |
| 1881 | bool add_to_freelist); |
| 1882 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1883 | // Prepare for full garbage collection. Resets the relocation pointer and |
| 1884 | // clears the free list. |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 1885 | virtual void PrepareForMarkCompact(bool will_compact); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1886 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1887 | // Updates the allocation pointer to the relocation top after a mark-compact |
| 1888 | // collection. |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 1889 | virtual void MCCommitRelocationInfo(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1890 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 1891 | virtual void PutRestOfCurrentPageOnFreeList(Page* current_page); |
| 1892 | |
erik.corry@gmail.com | 4a6c327 | 2010-11-18 12:04:40 +0000 | [diff] [blame] | 1893 | void MarkFreeListNodes() { free_list_.MarkNodes(); } |
| 1894 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1895 | #ifdef DEBUG |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1896 | // Reports statistics for the space |
| 1897 | void ReportStatistics(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1898 | #endif |
| 1899 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1900 | protected: |
| 1901 | // Virtual function in the superclass. Slow path of AllocateRaw. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1902 | MUST_USE_RESULT HeapObject* SlowAllocateRaw(int size_in_bytes); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1903 | |
| 1904 | // Virtual function in the superclass. Allocate linearly at the start of |
| 1905 | // the page after current_page (there is assumed to be one). |
| 1906 | HeapObject* AllocateInNextPage(Page* current_page, int size_in_bytes); |
| 1907 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1908 | private: |
| 1909 | // The space's free list. |
| 1910 | OldSpaceFreeList free_list_; |
| 1911 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1912 | public: |
| 1913 | TRACK_MEMORY("OldSpace") |
| 1914 | }; |
| 1915 | |
| 1916 | |
| 1917 | // ----------------------------------------------------------------------------- |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 1918 | // Old space for objects of a fixed size |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1919 | |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 1920 | class FixedSpace : public PagedSpace { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1921 | public: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1922 | FixedSpace(Heap* heap, |
| 1923 | intptr_t max_capacity, |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 1924 | AllocationSpace id, |
| 1925 | int object_size_in_bytes, |
| 1926 | const char* name) |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1927 | : PagedSpace(heap, max_capacity, id, NOT_EXECUTABLE), |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 1928 | object_size_in_bytes_(object_size_in_bytes), |
| 1929 | name_(name), |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1930 | free_list_(heap, id, object_size_in_bytes) { |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 1931 | page_extra_ = Page::kObjectAreaSize % object_size_in_bytes; |
| 1932 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1933 | |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 1934 | // The limit of allocation for a page in this space. |
| 1935 | virtual Address PageAllocationLimit(Page* page) { |
| 1936 | return page->ObjectAreaEnd() - page_extra_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1937 | } |
| 1938 | |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 1939 | int object_size_in_bytes() { return object_size_in_bytes_; } |
| 1940 | |
| 1941 | // Give a fixed sized block of memory to the space's free list. |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 1942 | // If add_to_freelist is false then just accounting stats are updated and |
| 1943 | // no attempt to add area to free list is made. |
| 1944 | void Free(Address start, bool add_to_freelist) { |
| 1945 | if (add_to_freelist) { |
| 1946 | free_list_.Free(start); |
| 1947 | } |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 1948 | accounting_stats_.DeallocateBytes(object_size_in_bytes_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1949 | } |
| 1950 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1951 | // Prepares for a mark-compact GC. |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 1952 | virtual void PrepareForMarkCompact(bool will_compact); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1953 | |
| 1954 | // Updates the allocation pointer to the relocation top after a mark-compact |
| 1955 | // collection. |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 1956 | virtual void MCCommitRelocationInfo(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1957 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 1958 | virtual void PutRestOfCurrentPageOnFreeList(Page* current_page); |
| 1959 | |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 1960 | virtual void DeallocateBlock(Address start, |
| 1961 | int size_in_bytes, |
| 1962 | bool add_to_freelist); |
erik.corry@gmail.com | 4a6c327 | 2010-11-18 12:04:40 +0000 | [diff] [blame] | 1963 | |
| 1964 | void MarkFreeListNodes() { free_list_.MarkNodes(); } |
| 1965 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1966 | #ifdef DEBUG |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1967 | // Reports statistic info of the space |
| 1968 | void ReportStatistics(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1969 | #endif |
| 1970 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1971 | protected: |
| 1972 | // Virtual function in the superclass. Slow path of AllocateRaw. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1973 | MUST_USE_RESULT HeapObject* SlowAllocateRaw(int size_in_bytes); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1974 | |
| 1975 | // Virtual function in the superclass. Allocate linearly at the start of |
| 1976 | // the page after current_page (there is assumed to be one). |
| 1977 | HeapObject* AllocateInNextPage(Page* current_page, int size_in_bytes); |
| 1978 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 1979 | void ResetFreeList() { |
| 1980 | free_list_.Reset(); |
| 1981 | } |
| 1982 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1983 | private: |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 1984 | // The size of objects in this space. |
| 1985 | int object_size_in_bytes_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1986 | |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 1987 | // The name of this space. |
| 1988 | const char* name_; |
| 1989 | |
| 1990 | // The space's free list. |
| 1991 | FixedSizeFreeList free_list_; |
| 1992 | }; |
| 1993 | |
| 1994 | |
| 1995 | // ----------------------------------------------------------------------------- |
| 1996 | // Old space for all map objects |
| 1997 | |
| 1998 | class MapSpace : public FixedSpace { |
| 1999 | public: |
| 2000 | // Creates a map space object with a maximum capacity. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2001 | MapSpace(Heap* heap, |
| 2002 | intptr_t max_capacity, |
| 2003 | int max_map_space_pages, |
| 2004 | AllocationSpace id) |
| 2005 | : FixedSpace(heap, max_capacity, id, Map::kSize, "map"), |
kasperl@chromium.org | eac059f | 2010-01-25 11:02:06 +0000 | [diff] [blame] | 2006 | max_map_space_pages_(max_map_space_pages) { |
| 2007 | ASSERT(max_map_space_pages < kMaxMapPageIndex); |
| 2008 | } |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 2009 | |
| 2010 | // Prepares for a mark-compact GC. |
| 2011 | virtual void PrepareForMarkCompact(bool will_compact); |
| 2012 | |
| 2013 | // Given an index, returns the page address. |
| 2014 | Address PageAddress(int page_index) { return page_addresses_[page_index]; } |
| 2015 | |
kasperl@chromium.org | eac059f | 2010-01-25 11:02:06 +0000 | [diff] [blame] | 2016 | static const int kMaxMapPageIndex = 1 << MapWord::kMapPageIndexBits; |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 2017 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2018 | // Are map pointers encodable into map word? |
| 2019 | bool MapPointersEncodable() { |
| 2020 | if (!FLAG_use_big_map_space) { |
kasperl@chromium.org | eac059f | 2010-01-25 11:02:06 +0000 | [diff] [blame] | 2021 | ASSERT(CountPagesToTop() <= kMaxMapPageIndex); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2022 | return true; |
| 2023 | } |
kasperl@chromium.org | eac059f | 2010-01-25 11:02:06 +0000 | [diff] [blame] | 2024 | return CountPagesToTop() <= max_map_space_pages_; |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2025 | } |
| 2026 | |
| 2027 | // Should be called after forced sweep to find out if map space needs |
| 2028 | // compaction. |
| 2029 | bool NeedsCompaction(int live_maps) { |
kasperl@chromium.org | eac059f | 2010-01-25 11:02:06 +0000 | [diff] [blame] | 2030 | return !MapPointersEncodable() && live_maps <= CompactionThreshold(); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2031 | } |
| 2032 | |
| 2033 | Address TopAfterCompaction(int live_maps) { |
| 2034 | ASSERT(NeedsCompaction(live_maps)); |
| 2035 | |
| 2036 | int pages_left = live_maps / kMapsPerPage; |
| 2037 | PageIterator it(this, PageIterator::ALL_PAGES); |
| 2038 | while (pages_left-- > 0) { |
| 2039 | ASSERT(it.has_next()); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 2040 | it.next()->SetRegionMarks(Page::kAllRegionsCleanMarks); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2041 | } |
| 2042 | ASSERT(it.has_next()); |
| 2043 | Page* top_page = it.next(); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 2044 | top_page->SetRegionMarks(Page::kAllRegionsCleanMarks); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2045 | ASSERT(top_page->is_valid()); |
| 2046 | |
| 2047 | int offset = live_maps % kMapsPerPage * Map::kSize; |
| 2048 | Address top = top_page->ObjectAreaStart() + offset; |
| 2049 | ASSERT(top < top_page->ObjectAreaEnd()); |
| 2050 | ASSERT(Contains(top)); |
| 2051 | |
| 2052 | return top; |
| 2053 | } |
| 2054 | |
| 2055 | void FinishCompaction(Address new_top, int live_maps) { |
| 2056 | Page* top_page = Page::FromAddress(new_top); |
| 2057 | ASSERT(top_page->is_valid()); |
| 2058 | |
| 2059 | SetAllocationInfo(&allocation_info_, top_page); |
| 2060 | allocation_info_.top = new_top; |
| 2061 | |
| 2062 | int new_size = live_maps * Map::kSize; |
| 2063 | accounting_stats_.DeallocateBytes(accounting_stats_.Size()); |
| 2064 | accounting_stats_.AllocateBytes(new_size); |
| 2065 | |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 2066 | // Flush allocation watermarks. |
| 2067 | for (Page* p = first_page_; p != top_page; p = p->next_page()) { |
| 2068 | p->SetAllocationWatermark(p->AllocationTop()); |
| 2069 | } |
| 2070 | top_page->SetAllocationWatermark(new_top); |
| 2071 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2072 | #ifdef DEBUG |
| 2073 | if (FLAG_enable_slow_asserts) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 2074 | intptr_t actual_size = 0; |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2075 | for (Page* p = first_page_; p != top_page; p = p->next_page()) |
| 2076 | actual_size += kMapsPerPage * Map::kSize; |
| 2077 | actual_size += (new_top - top_page->ObjectAreaStart()); |
| 2078 | ASSERT(accounting_stats_.Size() == actual_size); |
| 2079 | } |
| 2080 | #endif |
| 2081 | |
| 2082 | Shrink(); |
| 2083 | ResetFreeList(); |
| 2084 | } |
| 2085 | |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 2086 | protected: |
| 2087 | #ifdef DEBUG |
| 2088 | virtual void VerifyObject(HeapObject* obj); |
| 2089 | #endif |
| 2090 | |
| 2091 | private: |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2092 | static const int kMapsPerPage = Page::kObjectAreaSize / Map::kSize; |
| 2093 | |
| 2094 | // Do map space compaction if there is a page gap. |
kasperl@chromium.org | eac059f | 2010-01-25 11:02:06 +0000 | [diff] [blame] | 2095 | int CompactionThreshold() { |
| 2096 | return kMapsPerPage * (max_map_space_pages_ - 1); |
| 2097 | } |
| 2098 | |
| 2099 | const int max_map_space_pages_; |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2100 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2101 | // An array of page start address in a map space. |
kasperl@chromium.org | eac059f | 2010-01-25 11:02:06 +0000 | [diff] [blame] | 2102 | Address page_addresses_[kMaxMapPageIndex]; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2103 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2104 | public: |
| 2105 | TRACK_MEMORY("MapSpace") |
| 2106 | }; |
| 2107 | |
| 2108 | |
| 2109 | // ----------------------------------------------------------------------------- |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 2110 | // Old space for all global object property cell objects |
| 2111 | |
| 2112 | class CellSpace : public FixedSpace { |
| 2113 | public: |
| 2114 | // Creates a property cell space object with a maximum capacity. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2115 | CellSpace(Heap* heap, intptr_t max_capacity, AllocationSpace id) |
| 2116 | : FixedSpace(heap, max_capacity, id, JSGlobalPropertyCell::kSize, "cell") |
| 2117 | {} |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 2118 | |
| 2119 | protected: |
| 2120 | #ifdef DEBUG |
| 2121 | virtual void VerifyObject(HeapObject* obj); |
| 2122 | #endif |
| 2123 | |
| 2124 | public: |
ager@chromium.org | 4af710e | 2009-09-15 12:20:11 +0000 | [diff] [blame] | 2125 | TRACK_MEMORY("CellSpace") |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 2126 | }; |
| 2127 | |
| 2128 | |
| 2129 | // ----------------------------------------------------------------------------- |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2130 | // Large objects ( > Page::kMaxHeapObjectSize ) are allocated and managed by |
| 2131 | // the large object space. A large object is allocated from OS heap with |
| 2132 | // extra padding bytes (Page::kPageSize + Page::kObjectStartOffset). |
| 2133 | // A large object always starts at Page::kObjectStartOffset to a page. |
| 2134 | // Large objects do not move during garbage collections. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2135 | |
| 2136 | // A LargeObjectChunk holds exactly one large object page with exactly one |
| 2137 | // large object. |
| 2138 | class LargeObjectChunk { |
| 2139 | public: |
| 2140 | // Allocates a new LargeObjectChunk that contains a large object page |
| 2141 | // (Page::kPageSize aligned) that has at least size_in_bytes (for a large |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 2142 | // object) bytes after the object area start of that page. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2143 | static LargeObjectChunk* New(int size_in_bytes, Executability executable); |
| 2144 | |
| 2145 | // Free the memory associated with the chunk. |
| 2146 | inline void Free(Executability executable); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2147 | |
| 2148 | // Interpret a raw address as a large object chunk. |
| 2149 | static LargeObjectChunk* FromAddress(Address address) { |
| 2150 | return reinterpret_cast<LargeObjectChunk*>(address); |
| 2151 | } |
| 2152 | |
| 2153 | // Returns the address of this chunk. |
| 2154 | Address address() { return reinterpret_cast<Address>(this); } |
| 2155 | |
| 2156 | // Accessors for the fields of the chunk. |
| 2157 | LargeObjectChunk* next() { return next_; } |
| 2158 | void set_next(LargeObjectChunk* chunk) { next_ = chunk; } |
erik.corry@gmail.com | 145eff5 | 2010-08-23 11:36:18 +0000 | [diff] [blame] | 2159 | size_t size() { return size_ & ~Page::kPageFlagMask; } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2160 | |
| 2161 | // Compute the start address in the chunk. |
| 2162 | inline Address GetStartAddress(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2163 | |
| 2164 | // Returns the object in this chunk. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2165 | HeapObject* GetObject() { return HeapObject::FromAddress(GetStartAddress()); } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2166 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 2167 | // Given a requested size returns the physical size of a chunk to be |
| 2168 | // allocated. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2169 | static int ChunkSizeFor(int size_in_bytes); |
| 2170 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 2171 | // Given a chunk size, returns the object size it can accommodate. Used by |
| 2172 | // LargeObjectSpace::Available. |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 2173 | static intptr_t ObjectSizeFor(intptr_t chunk_size) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2174 | if (chunk_size <= (Page::kPageSize + Page::kObjectStartOffset)) return 0; |
| 2175 | return chunk_size - Page::kPageSize - Page::kObjectStartOffset; |
| 2176 | } |
| 2177 | |
| 2178 | private: |
| 2179 | // A pointer to the next large object chunk in the space or NULL. |
| 2180 | LargeObjectChunk* next_; |
| 2181 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2182 | // The total size of this chunk. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2183 | size_t size_; |
| 2184 | |
| 2185 | public: |
| 2186 | TRACK_MEMORY("LargeObjectChunk") |
| 2187 | }; |
| 2188 | |
| 2189 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 2190 | class LargeObjectSpace : public Space { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2191 | public: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2192 | LargeObjectSpace(Heap* heap, AllocationSpace id); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 2193 | virtual ~LargeObjectSpace() {} |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2194 | |
| 2195 | // Initializes internal data structures. |
| 2196 | bool Setup(); |
| 2197 | |
| 2198 | // Releases internal resources, frees objects in this space. |
| 2199 | void TearDown(); |
| 2200 | |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 2201 | // Allocates a (non-FixedArray, non-Code) large object. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 2202 | MUST_USE_RESULT MaybeObject* AllocateRaw(int size_in_bytes); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 2203 | // Allocates a large Code object. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 2204 | MUST_USE_RESULT MaybeObject* AllocateRawCode(int size_in_bytes); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2205 | // Allocates a large FixedArray. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 2206 | MUST_USE_RESULT MaybeObject* AllocateRawFixedArray(int size_in_bytes); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2207 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 2208 | // Available bytes for objects in this space. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2209 | inline intptr_t Available(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2210 | |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 2211 | virtual intptr_t Size() { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2212 | return size_; |
| 2213 | } |
| 2214 | |
erik.corry@gmail.com | 4a6c327 | 2010-11-18 12:04:40 +0000 | [diff] [blame] | 2215 | virtual intptr_t SizeOfObjects() { |
| 2216 | return objects_size_; |
| 2217 | } |
| 2218 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2219 | int PageCount() { |
| 2220 | return page_count_; |
| 2221 | } |
| 2222 | |
| 2223 | // Finds an object for a given address, returns Failure::Exception() |
| 2224 | // if it is not found. The function iterates through all objects in this |
| 2225 | // space, may be slow. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 2226 | MaybeObject* FindObject(Address a); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2227 | |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 2228 | // Finds a large object page containing the given pc, returns NULL |
| 2229 | // if such a page doesn't exist. |
| 2230 | LargeObjectChunk* FindChunkContainingPc(Address pc); |
| 2231 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 2232 | // Iterates objects covered by dirty regions. |
| 2233 | void IterateDirtyRegions(ObjectSlotCallback func); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2234 | |
| 2235 | // Frees unmarked objects. |
| 2236 | void FreeUnmarkedObjects(); |
| 2237 | |
| 2238 | // Checks whether a heap object is in this space; O(1). |
| 2239 | bool Contains(HeapObject* obj); |
| 2240 | |
| 2241 | // Checks whether the space is empty. |
| 2242 | bool IsEmpty() { return first_chunk_ == NULL; } |
| 2243 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2244 | // See the comments for ReserveSpace in the Space class. This has to be |
| 2245 | // called after ReserveSpace has been called on the paged spaces, since they |
| 2246 | // may use some memory, leaving less for large objects. |
| 2247 | virtual bool ReserveSpace(int bytes); |
| 2248 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2249 | #ifdef DEBUG |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 2250 | virtual void Verify(); |
| 2251 | virtual void Print(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2252 | void ReportStatistics(); |
| 2253 | void CollectCodeStatistics(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2254 | #endif |
| 2255 | // Checks whether an address is in the object area in this space. It |
| 2256 | // iterates all objects in the space. May be slow. |
| 2257 | bool SlowContains(Address addr) { return !FindObject(addr)->IsFailure(); } |
| 2258 | |
| 2259 | private: |
| 2260 | // The head of the linked list of large object chunks. |
| 2261 | LargeObjectChunk* first_chunk_; |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 2262 | intptr_t size_; // allocated bytes |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2263 | int page_count_; // number of chunks |
erik.corry@gmail.com | 4a6c327 | 2010-11-18 12:04:40 +0000 | [diff] [blame] | 2264 | intptr_t objects_size_; // size of objects |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2265 | |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 2266 | // Shared implementation of AllocateRaw, AllocateRawCode and |
| 2267 | // AllocateRawFixedArray. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 2268 | MUST_USE_RESULT MaybeObject* AllocateRawInternal(int requested_size, |
| 2269 | int object_size, |
| 2270 | Executability executable); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2271 | |
kasperl@chromium.org | f5aa837 | 2009-03-24 14:47:14 +0000 | [diff] [blame] | 2272 | friend class LargeObjectIterator; |
| 2273 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2274 | public: |
| 2275 | TRACK_MEMORY("LargeObjectSpace") |
| 2276 | }; |
| 2277 | |
| 2278 | |
| 2279 | class LargeObjectIterator: public ObjectIterator { |
| 2280 | public: |
| 2281 | explicit LargeObjectIterator(LargeObjectSpace* space); |
| 2282 | LargeObjectIterator(LargeObjectSpace* space, HeapObjectCallback size_func); |
| 2283 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2284 | HeapObject* next(); |
| 2285 | |
| 2286 | // implementation of ObjectIterator. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2287 | virtual HeapObject* next_object() { return next(); } |
| 2288 | |
| 2289 | private: |
| 2290 | LargeObjectChunk* current_; |
| 2291 | HeapObjectCallback size_func_; |
| 2292 | }; |
| 2293 | |
| 2294 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2295 | #ifdef DEBUG |
| 2296 | struct CommentStatistic { |
| 2297 | const char* comment; |
| 2298 | int size; |
| 2299 | int count; |
| 2300 | void Clear() { |
| 2301 | comment = NULL; |
| 2302 | size = 0; |
| 2303 | count = 0; |
| 2304 | } |
| 2305 | // Must be small, since an iteration is used for lookup. |
| 2306 | static const int kMaxComments = 64; |
| 2307 | }; |
| 2308 | #endif |
| 2309 | |
| 2310 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2311 | } } // namespace v8::internal |
| 2312 | |
| 2313 | #endif // V8_SPACES_H_ |