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