Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1188)

Side by Side Diff: src/heap/spaces.cc

Issue 1772733002: [heap] Move to two-level free-list (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Rebase again Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2011 the V8 project authors. All rights reserved. 1 // Copyright 2011 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "src/heap/spaces.h" 5 #include "src/heap/spaces.h"
6 6
7 #include "src/base/bits.h" 7 #include "src/base/bits.h"
8 #include "src/base/platform/platform.h" 8 #include "src/base/platform/platform.h"
9 #include "src/full-codegen/full-codegen.h" 9 #include "src/full-codegen/full-codegen.h"
10 #include "src/heap/slot-set.h" 10 #include "src/heap/slot-set.h"
(...skipping 1013 matching lines...) Expand 10 before | Expand all | Expand 10 after
1024 void PagedSpace::TearDown() { 1024 void PagedSpace::TearDown() {
1025 PageIterator iterator(this); 1025 PageIterator iterator(this);
1026 while (iterator.has_next()) { 1026 while (iterator.has_next()) {
1027 heap()->isolate()->memory_allocator()->Free(iterator.next()); 1027 heap()->isolate()->memory_allocator()->Free(iterator.next());
1028 } 1028 }
1029 anchor_.set_next_page(&anchor_); 1029 anchor_.set_next_page(&anchor_);
1030 anchor_.set_prev_page(&anchor_); 1030 anchor_.set_prev_page(&anchor_);
1031 accounting_stats_.Clear(); 1031 accounting_stats_.Clear();
1032 } 1032 }
1033 1033
1034
1035 void PagedSpace::AddMemory(Address start, intptr_t size) {
1036 accounting_stats_.ExpandSpace(static_cast<int>(size));
1037 Free(start, static_cast<int>(size));
1038 }
1039
1040
1041 void PagedSpace::RefillFreeList() { 1034 void PagedSpace::RefillFreeList() {
1042 MarkCompactCollector* collector = heap()->mark_compact_collector(); 1035 // Any PagedSpace might invoke RefillFreeList. We filter all but our old
1043 FreeList* free_list = nullptr; 1036 // generation spaces out.
1044 if (this == heap()->old_space()) { 1037 if (identity() != OLD_SPACE && identity() != CODE_SPACE &&
1045 free_list = collector->free_list_old_space().get(); 1038 identity() != MAP_SPACE) {
1046 } else if (this == heap()->code_space()) {
1047 free_list = collector->free_list_code_space().get();
1048 } else if (this == heap()->map_space()) {
1049 free_list = collector->free_list_map_space().get();
1050 } else {
1051 // Any PagedSpace might invoke RefillFreeList. We filter all but our old
1052 // generation spaces out.
1053 return; 1039 return;
1054 } 1040 }
1055 DCHECK(free_list != nullptr); 1041 MarkCompactCollector* collector = heap()->mark_compact_collector();
1056 intptr_t added = free_list_.Concatenate(free_list); 1042 List<Page*>* swept_pages = collector->swept_pages(identity());
1043 intptr_t added = 0;
1044 {
1045 base::LockGuard<base::Mutex> guard(collector->swept_pages_mutex());
1046 for (Page* p : *swept_pages) {
1047 // Only during compaction, pages can actually change ownership. This is
1048 // safe because there exists no other competing action on the page links
1049 // during compaction.
1050 if (is_local() && (p->owner() != this)) {
1051 base::LockGuard<base::Mutex> guard(
1052 reinterpret_cast<PagedSpace*>(p->owner())->mutex());
1053 p->Unlink();
1054 p->set_owner(this);
1055 p->InsertAfter(anchor_.prev_page());
1056 }
1057
1058 p->ForAllFreeListCategories([this, &added](FreeListCategory* category) {
1059 added += category->available();
1060 category->Relink();
1061 });
1062 added += p->wasted_memory();
1063 }
1064 swept_pages->Rewind(0);
1065 }
1057 accounting_stats_.IncreaseCapacity(added); 1066 accounting_stats_.IncreaseCapacity(added);
1058 } 1067 }
1059 1068
1060 1069 void PagedSpace::MergeCompactionSpace(CompactionSpace* other) {
1061 void CompactionSpace::RefillFreeList() {
1062 MarkCompactCollector* collector = heap()->mark_compact_collector();
1063 FreeList* free_list = nullptr;
1064 if (identity() == OLD_SPACE) {
1065 free_list = collector->free_list_old_space().get();
1066 } else if (identity() == CODE_SPACE) {
1067 free_list = collector->free_list_code_space().get();
1068 } else {
1069 // Compaction spaces only represent old or code space.
1070 UNREACHABLE();
1071 }
1072 DCHECK(free_list != nullptr);
1073 intptr_t refilled = 0;
1074 while (refilled < kCompactionMemoryWanted) {
1075 FreeSpace* node =
1076 free_list->TryRemoveMemory(kCompactionMemoryWanted - refilled);
1077 if (node == nullptr) return;
1078 refilled += node->size();
1079 AddMemory(node->address(), node->size());
1080 }
1081 }
1082
1083 void PagedSpace::MoveOverFreeMemory(PagedSpace* other) {
1084 DCHECK(identity() == other->identity()); 1070 DCHECK(identity() == other->identity());
1085 // Destroy the linear allocation space of {other}. This is needed to
1086 // (a) not waste the memory and
1087 // (b) keep the rest of the chunk in an iterable state (filler is needed).
1088 other->EmptyAllocationInfo();
1089
1090 // Move over the free list. Concatenate makes sure that the source free list
1091 // gets properly reset after moving over all nodes.
1092 intptr_t added = free_list_.Concatenate(other->free_list());
1093
1094 // Moved memory is not recorded as allocated memory, but rather increases and
1095 // decreases capacity of the corresponding spaces.
1096 other->accounting_stats_.DecreaseCapacity(added);
1097 accounting_stats_.IncreaseCapacity(added);
1098 }
1099
1100
1101 void PagedSpace::MergeCompactionSpace(CompactionSpace* other) {
1102 // Unmerged fields: 1071 // Unmerged fields:
1103 // area_size_ 1072 // area_size_
1104 // anchor_ 1073 // anchor_
1105 1074
1106 MoveOverFreeMemory(other); 1075 other->EmptyAllocationInfo();
1107 1076
1108 // Update and clear accounting statistics. 1077 // Update and clear accounting statistics.
1109 accounting_stats_.Merge(other->accounting_stats_); 1078 accounting_stats_.Merge(other->accounting_stats_);
1110 other->accounting_stats_.Clear(); 1079 other->accounting_stats_.Clear();
1111 1080
1112 // The linear allocation area of {other} should be destroyed now. 1081 // The linear allocation area of {other} should be destroyed now.
1113 DCHECK(other->top() == nullptr); 1082 DCHECK(other->top() == nullptr);
1114 DCHECK(other->limit() == nullptr); 1083 DCHECK(other->limit() == nullptr);
1115 1084
1116 AccountCommitted(other->CommittedMemory()); 1085 AccountCommitted(other->CommittedMemory());
1117 1086
1118 // Move over pages. 1087 // Move over pages.
1119 PageIterator it(other); 1088 PageIterator it(other);
1120 Page* p = nullptr; 1089 Page* p = nullptr;
1121 while (it.has_next()) { 1090 while (it.has_next()) {
1122 p = it.next(); 1091 p = it.next();
1092
1093 // Relinking requires the category to be unlinked.
1094 p->ForAllFreeListCategories([this](FreeListCategory* category) {
1095 category->owner()->RemoveCategory(category);
1096 });
1097
1123 p->Unlink(); 1098 p->Unlink();
1124 p->set_owner(this); 1099 p->set_owner(this);
1125 p->InsertAfter(anchor_.prev_page()); 1100 p->InsertAfter(anchor_.prev_page());
1101
1102 p->ForAllFreeListCategories(
1103 [this](FreeListCategory* category) { category->Relink(); });
1126 } 1104 }
1127 } 1105 }
1128 1106
1129 1107
1130 size_t PagedSpace::CommittedPhysicalMemory() { 1108 size_t PagedSpace::CommittedPhysicalMemory() {
1131 if (!base::VirtualMemory::HasLazyCommits()) return CommittedMemory(); 1109 if (!base::VirtualMemory::HasLazyCommits()) return CommittedMemory();
1132 MemoryChunk::UpdateHighWaterMark(allocation_info_.top()); 1110 MemoryChunk::UpdateHighWaterMark(allocation_info_.top());
1133 size_t size = 0; 1111 size_t size = 0;
1134 PageIterator it(this); 1112 PageIterator it(this);
1135 while (it.has_next()) { 1113 while (it.has_next()) {
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
1222 Page* page = page_iterator.next(); 1200 Page* page = page_iterator.next();
1223 page->ResetFreeListStatistics(); 1201 page->ResetFreeListStatistics();
1224 } 1202 }
1225 } 1203 }
1226 1204
1227 1205
1228 void PagedSpace::IncreaseCapacity(int size) { 1206 void PagedSpace::IncreaseCapacity(int size) {
1229 accounting_stats_.ExpandSpace(size); 1207 accounting_stats_.ExpandSpace(size);
1230 } 1208 }
1231 1209
1210 void PagedSpace::ReleasePage(Page* page) {
1211 DCHECK_EQ(page->LiveBytes(), 0);
1212 DCHECK_EQ(AreaSize(), page->area_size());
1213 DCHECK_EQ(page->owner(), this);
1232 1214
1233 void PagedSpace::ReleasePage(Page* page, bool evict_free_list_items) { 1215 free_list_.EvictFreeListItems(page);
1234 DCHECK(page->LiveBytes() == 0);
1235 DCHECK(AreaSize() == page->area_size());
1236
1237 if (evict_free_list_items) {
1238 intptr_t size = free_list_.EvictFreeListItems(page);
1239 accounting_stats_.AllocateBytes(size);
1240 DCHECK_EQ(AreaSize(), static_cast<int>(size));
1241 }
1242
1243 DCHECK(!free_list_.ContainsPageFreeListItems(page)); 1216 DCHECK(!free_list_.ContainsPageFreeListItems(page));
1244 1217
1245 if (Page::FromAllocationTop(allocation_info_.top()) == page) { 1218 if (Page::FromAllocationTop(allocation_info_.top()) == page) {
1246 allocation_info_.Reset(nullptr, nullptr); 1219 allocation_info_.Reset(nullptr, nullptr);
1247 } 1220 }
1248 1221
1249 // If page is still in a list, unlink it from that list. 1222 // If page is still in a list, unlink it from that list.
1250 if (page->next_chunk() != NULL) { 1223 if (page->next_chunk() != NULL) {
1251 DCHECK(page->prev_chunk() != NULL); 1224 DCHECK(page->prev_chunk() != NULL);
1252 page->Unlink(); 1225 page->Unlink();
1253 } 1226 }
1254 1227
1255 AccountUncommitted(static_cast<intptr_t>(page->size())); 1228 AccountUncommitted(static_cast<intptr_t>(page->size()));
1256 heap()->QueueMemoryChunkForFree(page); 1229 heap()->QueueMemoryChunkForFree(page);
1257 1230
1258 DCHECK(Capacity() > 0); 1231 DCHECK(Capacity() > 0);
1259 accounting_stats_.ShrinkSpace(AreaSize()); 1232 accounting_stats_.ShrinkSpace(AreaSize());
1260 } 1233 }
1261 1234
1262
1263 #ifdef DEBUG 1235 #ifdef DEBUG
1264 void PagedSpace::Print() {} 1236 void PagedSpace::Print() {}
1265 #endif 1237 #endif
1266 1238
1267 #ifdef VERIFY_HEAP 1239 #ifdef VERIFY_HEAP
1268 void PagedSpace::Verify(ObjectVisitor* visitor) { 1240 void PagedSpace::Verify(ObjectVisitor* visitor) {
1269 bool allocation_pointer_found_in_space = 1241 bool allocation_pointer_found_in_space =
1270 (allocation_info_.top() == allocation_info_.limit()); 1242 (allocation_info_.top() == allocation_info_.limit());
1271 PageIterator page_iterator(this); 1243 PageIterator page_iterator(this);
1272 while (page_iterator.has_next()) { 1244 while (page_iterator.has_next()) {
(...skipping 885 matching lines...) Expand 10 before | Expand all | Expand 10 after
2158 if (from_space_.is_committed()) { 2130 if (from_space_.is_committed()) {
2159 size += from_space_.CommittedPhysicalMemory(); 2131 size += from_space_.CommittedPhysicalMemory();
2160 } 2132 }
2161 return size; 2133 return size;
2162 } 2134 }
2163 2135
2164 2136
2165 // ----------------------------------------------------------------------------- 2137 // -----------------------------------------------------------------------------
2166 // Free lists for old object spaces implementation 2138 // Free lists for old object spaces implementation
2167 2139
2168 intptr_t FreeListCategory::Concatenate(FreeListCategory* category) {
2169 intptr_t free_bytes = 0;
2170 if (category->top() != NULL) {
2171 DCHECK(category->end_ != NULL);
2172 free_bytes = category->available();
2173 if (end_ == NULL) {
2174 end_ = category->end();
2175 } else {
2176 category->end()->set_next(top());
2177 }
2178 set_top(category->top());
2179 available_ += category->available();
2180 category->Reset();
2181 }
2182 return free_bytes;
2183 }
2184
2185 2140
2186 void FreeListCategory::Reset() { 2141 void FreeListCategory::Reset() {
2187 set_top(nullptr); 2142 set_top(nullptr);
2188 set_end(nullptr); 2143 set_prev(nullptr);
2144 set_next(nullptr);
2189 available_ = 0; 2145 available_ = 0;
2190 } 2146 }
2191 2147
2148 FreeSpace* FreeListCategory::PickNodeFromList(int* node_size) {
2149 DCHECK(page()->CanAllocate());
2192 2150
2193 intptr_t FreeListCategory::EvictFreeListItemsInList(Page* p) {
2194 intptr_t sum = 0;
2195 FreeSpace* prev_node = nullptr;
2196 for (FreeSpace* cur_node = top(); cur_node != nullptr;
2197 cur_node = cur_node->next()) {
2198 Page* page_for_node = Page::FromAddress(cur_node->address());
2199 if (page_for_node == p) {
2200 // FreeSpace node on eviction page found, unlink it.
2201 int size = cur_node->size();
2202 sum += size;
2203 DCHECK((prev_node != nullptr) || (top() == cur_node));
2204 if (cur_node == top()) {
2205 set_top(cur_node->next());
2206 }
2207 if (cur_node == end()) {
2208 set_end(prev_node);
2209 }
2210 if (prev_node != nullptr) {
2211 prev_node->set_next(cur_node->next());
2212 }
2213 continue;
2214 }
2215 prev_node = cur_node;
2216 }
2217 p->add_available_in_free_list(-sum);
2218 available_ -= sum;
2219 return sum;
2220 }
2221
2222
2223 bool FreeListCategory::ContainsPageFreeListItemsInList(Page* p) {
2224 FreeSpace* node = top();
2225 while (node != NULL) {
2226 if (Page::FromAddress(node->address()) == p) return true;
2227 node = node->next();
2228 }
2229 return false;
2230 }
2231
2232
2233 FreeSpace* FreeListCategory::PickNodeFromList(int* node_size) {
2234 FreeSpace* node = top(); 2151 FreeSpace* node = top();
2235 if (node == nullptr) return nullptr; 2152 if (node == nullptr) return nullptr;
2236 2153 set_top(node->next());
2237 Page* page = Page::FromAddress(node->address()); 2154 *node_size = node->Size();
2238 while ((node != nullptr) && !page->CanAllocate()) { 2155 available_ -= *node_size;
2239 available_ -= node->size();
2240 page->add_available_in_free_list(-(node->Size()));
2241 node = node->next();
2242 }
2243
2244 if (node != nullptr) {
2245 set_top(node->next());
2246 *node_size = node->Size();
2247 available_ -= *node_size;
2248 } else {
2249 set_top(nullptr);
2250 }
2251
2252 if (top() == nullptr) {
2253 set_end(nullptr);
2254 }
2255
2256 return node; 2156 return node;
2257 } 2157 }
2258 2158
2159 FreeSpace* FreeListCategory::TryPickNodeFromList(int minimum_size,
2160 int* node_size) {
2161 DCHECK(page()->CanAllocate());
2259 2162
2260 FreeSpace* FreeListCategory::PickNodeFromList(int size_in_bytes,
2261 int* node_size) {
2262 FreeSpace* node = PickNodeFromList(node_size); 2163 FreeSpace* node = PickNodeFromList(node_size);
2263 if ((node != nullptr) && (*node_size < size_in_bytes)) { 2164 if ((node != nullptr) && (*node_size < minimum_size)) {
2264 Free(node, *node_size); 2165 Free(node, *node_size);
2265 *node_size = 0; 2166 *node_size = 0;
2266 return nullptr; 2167 return nullptr;
2267 } 2168 }
2268 return node; 2169 return node;
2269 } 2170 }
2270 2171
2172 FreeSpace* FreeListCategory::SearchForNodeInList(int minimum_size,
2173 int* node_size) {
2174 DCHECK(page()->CanAllocate());
2271 2175
2272 FreeSpace* FreeListCategory::SearchForNodeInList(int size_in_bytes,
2273 int* node_size) {
2274 FreeSpace* prev_non_evac_node = nullptr; 2176 FreeSpace* prev_non_evac_node = nullptr;
2275 for (FreeSpace* cur_node = top(); cur_node != nullptr; 2177 for (FreeSpace* cur_node = top(); cur_node != nullptr;
2276 cur_node = cur_node->next()) { 2178 cur_node = cur_node->next()) {
2277 int size = cur_node->size(); 2179 int size = cur_node->size();
2278 Page* page_for_node = Page::FromAddress(cur_node->address()); 2180 if (size >= minimum_size) {
2279
2280 if ((size >= size_in_bytes) || !page_for_node->CanAllocate()) {
2281 // The node is either large enough or contained in an evacuation
2282 // candidate. In both cases we need to unlink it from the list.
2283 available_ -= size; 2181 available_ -= size;
2284 if (cur_node == top()) { 2182 if (cur_node == top()) {
2285 set_top(cur_node->next()); 2183 set_top(cur_node->next());
2286 } 2184 }
2287 if (cur_node == end()) {
2288 set_end(prev_non_evac_node);
2289 }
2290 if (prev_non_evac_node != nullptr) { 2185 if (prev_non_evac_node != nullptr) {
2291 prev_non_evac_node->set_next(cur_node->next()); 2186 prev_non_evac_node->set_next(cur_node->next());
2292 } 2187 }
2293 // For evacuation candidates we continue.
2294 if (!page_for_node->CanAllocate()) {
2295 page_for_node->add_available_in_free_list(-size);
2296 continue;
2297 }
2298 // Otherwise we have a large enough node and can return.
2299 *node_size = size; 2188 *node_size = size;
2300 return cur_node; 2189 return cur_node;
2301 } 2190 }
2302 2191
2303 prev_non_evac_node = cur_node; 2192 prev_non_evac_node = cur_node;
2304 } 2193 }
2305 return nullptr; 2194 return nullptr;
2306 } 2195 }
2307 2196
2197 bool FreeListCategory::Free(FreeSpace* free_space, int size_in_bytes,
2198 bool keep_local) {
2199 if (!page()->CanAllocate()) return false;
2308 2200
2309 void FreeListCategory::Free(FreeSpace* free_space, int size_in_bytes) {
2310 free_space->set_next(top()); 2201 free_space->set_next(top());
2311 set_top(free_space); 2202 set_top(free_space);
2312 if (end_ == NULL) { 2203 available_ += size_in_bytes;
2313 end_ = free_space; 2204 if (!keep_local && prev() == nullptr && next() == nullptr) {
2205 owner()->AddCategory(this);
2314 } 2206 }
2315 available_ += size_in_bytes; 2207 return true;
2316 } 2208 }
2317 2209
2318 2210
2319 void FreeListCategory::RepairFreeList(Heap* heap) { 2211 void FreeListCategory::RepairFreeList(Heap* heap) {
2320 FreeSpace* n = top(); 2212 FreeSpace* n = top();
2321 while (n != NULL) { 2213 while (n != NULL) {
2322 Map** map_location = reinterpret_cast<Map**>(n->address()); 2214 Map** map_location = reinterpret_cast<Map**>(n->address());
2323 if (*map_location == NULL) { 2215 if (*map_location == NULL) {
2324 *map_location = heap->free_space_map(); 2216 *map_location = heap->free_space_map();
2325 } else { 2217 } else {
2326 DCHECK(*map_location == heap->free_space_map()); 2218 DCHECK(*map_location == heap->free_space_map());
2327 } 2219 }
2328 n = n->next(); 2220 n = n->next();
2329 } 2221 }
2330 } 2222 }
2331 2223
2224 void FreeListCategory::Relink() {
2225 DCHECK(!is_linked());
2226 owner()->AddCategory(this);
2227 }
2228
2229 void FreeListCategory::Invalidate() {
2230 page()->add_available_in_free_list(-available());
2231 Reset();
2232 type_ = kInvalidCategory;
2233 }
2234
2332 FreeList::FreeList(PagedSpace* owner) : owner_(owner), wasted_bytes_(0) { 2235 FreeList::FreeList(PagedSpace* owner) : owner_(owner), wasted_bytes_(0) {
2333 for (int i = kFirstCategory; i < kNumberOfCategories; i++) { 2236 for (int i = kFirstCategory; i < kNumberOfCategories; i++) {
2334 category_[i].Initialize(this, static_cast<FreeListCategoryType>(i)); 2237 categories_[i] = nullptr;
2335 } 2238 }
2336 Reset(); 2239 Reset();
2337 } 2240 }
2338 2241
2339 2242
2340 intptr_t FreeList::Concatenate(FreeList* other) { 2243 void FreeList::Reset() {
2341 intptr_t usable_bytes = 0; 2244 ForAllFreeListCategories(
2342 intptr_t wasted_bytes = 0; 2245 [](FreeListCategory* category) { category->Reset(); });
2343
2344 // This is safe (not going to deadlock) since Concatenate operations
2345 // are never performed on the same free lists at the same time in
2346 // reverse order. Furthermore, we only lock if the PagedSpace containing
2347 // the free list is know to be globally available, i.e., not local.
2348 if (!owner()->is_local()) mutex_.Lock();
2349 if (!other->owner()->is_local()) other->mutex()->Lock();
2350
2351 wasted_bytes = other->wasted_bytes_;
2352 wasted_bytes_ += wasted_bytes;
2353 other->wasted_bytes_ = 0;
2354
2355 for (int i = kFirstCategory; i < kNumberOfCategories; i++) { 2246 for (int i = kFirstCategory; i < kNumberOfCategories; i++) {
2356 usable_bytes += category_[i].Concatenate( 2247 categories_[i] = nullptr;
2357 other->GetFreeListCategory(static_cast<FreeListCategoryType>(i)));
2358 }
2359
2360 if (!other->owner()->is_local()) other->mutex()->Unlock();
2361 if (!owner()->is_local()) mutex_.Unlock();
2362 return usable_bytes + wasted_bytes;
2363 }
2364
2365
2366 void FreeList::Reset() {
2367 for (int i = kFirstCategory; i < kNumberOfCategories; i++) {
2368 category_[i].Reset();
2369 } 2248 }
2370 ResetStats(); 2249 ResetStats();
2371 } 2250 }
2372 2251
2373 2252 int FreeList::Free(Address start, int size_in_bytes, bool keep_local) {
2374 int FreeList::Free(Address start, int size_in_bytes) {
2375 if (size_in_bytes == 0) return 0; 2253 if (size_in_bytes == 0) return 0;
2376 2254
2377 owner()->heap()->CreateFillerObjectAt(start, size_in_bytes, 2255 owner()->heap()->CreateFillerObjectAt(start, size_in_bytes,
2378 ClearRecordedSlots::kNo); 2256 ClearRecordedSlots::kNo);
2379 2257
2380 Page* page = Page::FromAddress(start); 2258 Page* page = Page::FromAddress(start);
2381 2259
2382 // Blocks have to be a minimum size to hold free list items. 2260 // Blocks have to be a minimum size to hold free list items.
2383 if (size_in_bytes < kMinBlockSize) { 2261 if (size_in_bytes < kMinBlockSize) {
2384 page->add_wasted_memory(size_in_bytes); 2262 page->add_wasted_memory(size_in_bytes);
2385 wasted_bytes_ += size_in_bytes; 2263 wasted_bytes_.Increment(size_in_bytes);
2386 return size_in_bytes; 2264 return size_in_bytes;
2387 } 2265 }
2388 2266
2389 FreeSpace* free_space = FreeSpace::cast(HeapObject::FromAddress(start)); 2267 FreeSpace* free_space = FreeSpace::cast(HeapObject::FromAddress(start));
2390 // Insert other blocks at the head of a free list of the appropriate 2268 // Insert other blocks at the head of a free list of the appropriate
2391 // magnitude. 2269 // magnitude.
2392 FreeListCategoryType type = SelectFreeListCategoryType(size_in_bytes); 2270 FreeListCategoryType type = SelectFreeListCategoryType(size_in_bytes);
2393 category_[type].Free(free_space, size_in_bytes); 2271 if (page->free_list_category(type)->Free(free_space, size_in_bytes,
2394 page->add_available_in_free_list(size_in_bytes); 2272 keep_local)) {
2395 2273 page->add_available_in_free_list(size_in_bytes);
2396 DCHECK(IsVeryLong() || Available() == SumFreeLists()); 2274 }
2397 return 0; 2275 return 0;
2398 } 2276 }
2399 2277
2400 2278 FreeSpace* FreeList::FindNodeIn(FreeListCategoryType type, int* node_size) {
2401 FreeSpace* FreeList::FindNodeIn(FreeListCategoryType category, int* node_size) { 2279 FreeListCategoryIterator it(this, type);
2402 FreeSpace* node = GetFreeListCategory(category)->PickNodeFromList(node_size); 2280 FreeSpace* node = nullptr;
2403 if (node != nullptr) { 2281 while (it.HasNext()) {
2404 Page::FromAddress(node->address()) 2282 FreeListCategory* current = it.Next();
2405 ->add_available_in_free_list(-(*node_size)); 2283 node = current->PickNodeFromList(node_size);
2406 DCHECK(IsVeryLong() || Available() == SumFreeLists()); 2284 if (node != nullptr) {
2285 Page::FromAddress(node->address())
2286 ->add_available_in_free_list(-(*node_size));
2287 DCHECK(IsVeryLong() || Available() == SumFreeLists());
2288 return node;
2289 }
2407 } 2290 }
2408 return node; 2291 return node;
2409 } 2292 }
2410 2293
2294 FreeSpace* FreeList::FindNodeIn(FreeListCategoryType type, int* node_size,
2295 int minimum_size) {
2296 FreeListCategoryIterator it(this, type);
2297 FreeSpace* node = nullptr;
2298 while (it.HasNext()) {
2299 FreeListCategory* current = it.Next();
2300 node = current->TryPickNodeFromList(minimum_size, node_size);
2301 if (node != nullptr) {
2302 Page::FromAddress(node->address())
2303 ->add_available_in_free_list(-(*node_size));
2304 DCHECK(IsVeryLong() || Available() == SumFreeLists());
2305 return node;
2306 }
2307 }
2308 return node;
2309 }
2310
2311 FreeSpace* FreeList::SearchForNodeInList(FreeListCategoryType type,
2312 int* node_size, int minimum_size) {
2313 FreeListCategoryIterator it(this, type);
2314 FreeSpace* node = nullptr;
2315 while (it.HasNext()) {
2316 FreeListCategory* current = it.Next();
2317 node = current->SearchForNodeInList(minimum_size, node_size);
2318 if (node != nullptr) {
2319 Page::FromAddress(node->address())
2320 ->add_available_in_free_list(-(*node_size));
2321 DCHECK(IsVeryLong() || Available() == SumFreeLists());
2322 return node;
2323 }
2324 }
2325 return node;
2326 }
2411 2327
2412 FreeSpace* FreeList::FindNodeFor(int size_in_bytes, int* node_size) { 2328 FreeSpace* FreeList::FindNodeFor(int size_in_bytes, int* node_size) {
2413 FreeSpace* node = nullptr; 2329 FreeSpace* node = nullptr;
2414 Page* page = nullptr; 2330 Page* page = nullptr;
2415 2331
2416 // First try the allocation fast path: try to allocate the minimum element 2332 // First try the allocation fast path: try to allocate the minimum element
2417 // size of a free list category. This operation is constant time. 2333 // size of a free list category. This operation is constant time.
2418 FreeListCategoryType type = 2334 FreeListCategoryType type =
2419 SelectFastAllocationFreeListCategoryType(size_in_bytes); 2335 SelectFastAllocationFreeListCategoryType(size_in_bytes);
2420 for (int i = type; i < kHuge; i++) { 2336 for (int i = type; i < kHuge; i++) {
2421 node = FindNodeIn(static_cast<FreeListCategoryType>(i), node_size); 2337 node = FindNodeIn(static_cast<FreeListCategoryType>(i), node_size);
2422 if (node != nullptr) return node; 2338 if (node != nullptr) return node;
2423 } 2339 }
2424 2340
2425 // Next search the huge list for free list nodes. This takes linear time in 2341 // Next search the huge list for free list nodes. This takes linear time in
2426 // the number of huge elements. 2342 // the number of huge elements.
2427 node = category_[kHuge].SearchForNodeInList(size_in_bytes, node_size); 2343 node = SearchForNodeInList(kHuge, node_size, size_in_bytes);
2428 if (node != nullptr) { 2344 if (node != nullptr) {
2429 page = Page::FromAddress(node->address());
2430 page->add_available_in_free_list(-(*node_size));
2431 DCHECK(IsVeryLong() || Available() == SumFreeLists()); 2345 DCHECK(IsVeryLong() || Available() == SumFreeLists());
2432 return node; 2346 return node;
2433 } 2347 }
2434 2348
2435 // We need a huge block of memory, but we didn't find anything in the huge 2349 // We need a huge block of memory, but we didn't find anything in the huge
2436 // list. 2350 // list.
2437 if (type == kHuge) return nullptr; 2351 if (type == kHuge) return nullptr;
2438 2352
2439 // Now search the best fitting free list for a node that has at least the 2353 // Now search the best fitting free list for a node that has at least the
2440 // requested size. 2354 // requested size.
2441 type = SelectFreeListCategoryType(size_in_bytes); 2355 type = SelectFreeListCategoryType(size_in_bytes);
2442 node = category_[type].PickNodeFromList(size_in_bytes, node_size); 2356 node = FindNodeIn(type, node_size, size_in_bytes);
2443 if (node != nullptr) { 2357 if (node != nullptr) {
2444 DCHECK(size_in_bytes <= *node_size); 2358 DCHECK(size_in_bytes <= *node_size);
2445 page = Page::FromAddress(node->address()); 2359 page = Page::FromAddress(node->address());
2446 page->add_available_in_free_list(-(*node_size)); 2360 page->add_available_in_free_list(-(*node_size));
2447 } 2361 }
2448 2362
2449 DCHECK(IsVeryLong() || Available() == SumFreeLists()); 2363 DCHECK(IsVeryLong() || Available() == SumFreeLists());
2450 return node; 2364 return node;
2451 } 2365 }
2452 2366
2453
2454 FreeSpace* FreeList::TryRemoveMemory(intptr_t hint_size_in_bytes) {
2455 hint_size_in_bytes = RoundDown(hint_size_in_bytes, kPointerSize);
2456 base::LockGuard<base::Mutex> guard(&mutex_);
2457 FreeSpace* node = nullptr;
2458 int node_size = 0;
2459 // Try to find a node that fits exactly.
2460 node = FindNodeFor(static_cast<int>(hint_size_in_bytes), &node_size);
2461 // If no node could be found get as much memory as possible.
2462 if (node == nullptr) node = FindNodeIn(kHuge, &node_size);
2463 if (node == nullptr) node = FindNodeIn(kLarge, &node_size);
2464 if (node != nullptr) {
2465 // We round up the size to (kMinBlockSize + kPointerSize) to (a) have a
2466 // size larger then the minimum size required for FreeSpace, and (b) to get
2467 // a block that can actually be freed into some FreeList later on.
2468 if (hint_size_in_bytes <= kMinBlockSize) {
2469 hint_size_in_bytes = kMinBlockSize + kPointerSize;
2470 }
2471 // Give back left overs that were not required by {size_in_bytes}.
2472 intptr_t left_over = node_size - hint_size_in_bytes;
2473
2474 // Do not bother to return anything below {kMinBlockSize} as it would be
2475 // immediately discarded anyways.
2476 if (left_over > kMinBlockSize) {
2477 Free(node->address() + hint_size_in_bytes, static_cast<int>(left_over));
2478 node->set_size(static_cast<int>(hint_size_in_bytes));
2479 }
2480 }
2481 return node;
2482 }
2483
2484
2485 // Allocation on the old space free list. If it succeeds then a new linear 2367 // Allocation on the old space free list. If it succeeds then a new linear
2486 // allocation space has been set up with the top and limit of the space. If 2368 // allocation space has been set up with the top and limit of the space. If
2487 // the allocation fails then NULL is returned, and the caller can perform a GC 2369 // the allocation fails then NULL is returned, and the caller can perform a GC
2488 // or allocate a new page before retrying. 2370 // or allocate a new page before retrying.
2489 HeapObject* FreeList::Allocate(int size_in_bytes) { 2371 HeapObject* FreeList::Allocate(int size_in_bytes) {
2490 DCHECK(0 < size_in_bytes); 2372 DCHECK(0 < size_in_bytes);
2491 DCHECK(size_in_bytes <= kMaxBlockSize); 2373 DCHECK(size_in_bytes <= kMaxBlockSize);
2492 DCHECK(IsAligned(size_in_bytes, kPointerSize)); 2374 DCHECK(IsAligned(size_in_bytes, kPointerSize));
2493 // Don't free list allocate if there is linear space available. 2375 // Don't free list allocate if there is linear space available.
2494 DCHECK(owner_->limit() - owner_->top() < size_in_bytes); 2376 DCHECK(owner_->limit() - owner_->top() < size_in_bytes);
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
2548 } else if (bytes_left > 0) { 2430 } else if (bytes_left > 0) {
2549 // Normally we give the rest of the node to the allocator as its new 2431 // Normally we give the rest of the node to the allocator as its new
2550 // linear allocation area. 2432 // linear allocation area.
2551 owner_->SetTopAndLimit(new_node->address() + size_in_bytes, 2433 owner_->SetTopAndLimit(new_node->address() + size_in_bytes,
2552 new_node->address() + new_node_size); 2434 new_node->address() + new_node_size);
2553 } 2435 }
2554 2436
2555 return new_node; 2437 return new_node;
2556 } 2438 }
2557 2439
2558 2440 intptr_t FreeList::EvictFreeListItems(Page* page) {
2559 intptr_t FreeList::EvictFreeListItems(Page* p) { 2441 intptr_t sum = 0;
2560 intptr_t sum = category_[kHuge].EvictFreeListItemsInList(p); 2442 page->ForAllFreeListCategories(
2561 if (sum < p->area_size()) { 2443 [this, &sum, page](FreeListCategory* category) {
2562 for (int i = kFirstCategory; i <= kLarge; i++) { 2444 if (category->owner() == this && category->is_linked()) {
2563 sum += category_[i].EvictFreeListItemsInList(p); 2445 sum += category->available();
2564 } 2446 RemoveCategory(category);
2565 } 2447 category->Invalidate();
2448 }
2449 });
2566 return sum; 2450 return sum;
2567 } 2451 }
2568 2452
2453 bool FreeList::ContainsPageFreeListItems(Page* page) {
2454 bool contained = false;
2455 page->ForAllFreeListCategories(
2456 [this, &contained](FreeListCategory* category) {
2457 if (category->owner() == this && category->is_linked()) {
2458 contained = true;
2459 }
2460 });
2461 return contained;
2462 }
2569 2463
2570 bool FreeList::ContainsPageFreeListItems(Page* p) { 2464 void FreeList::RepairLists(Heap* heap) {
2571 for (int i = kFirstCategory; i < kNumberOfCategories; i++) { 2465 ForAllFreeListCategories(
2572 if (category_[i].EvictFreeListItemsInList(p)) { 2466 [heap](FreeListCategory* category) { category->RepairFreeList(heap); });
2573 return true; 2467 }
2574 } 2468
2469 bool FreeList::AddCategory(FreeListCategory* category) {
2470 FreeListCategoryType type = category->type_;
2471 FreeListCategory* top = categories_[type];
2472
2473 if (category->is_empty()) return false;
2474 if (top == category) return false;
2475
2476 // Common double-linked list insertion.
2477 if (top != nullptr) {
2478 top->set_prev(category);
2575 } 2479 }
2576 return false; 2480 category->set_next(top);
2481 categories_[type] = category;
2482 return true;
2483 }
2484
2485 void FreeList::RemoveCategory(FreeListCategory* category) {
2486 FreeListCategoryType type = category->type_;
2487 FreeListCategory* top = categories_[type];
2488
2489 // Common double-linked list removal.
2490 if (top == category) {
2491 categories_[type] = category->next();
2492 }
2493 if (category->prev() != nullptr) {
2494 category->prev()->set_next(category->next());
2495 }
2496 if (category->next() != nullptr) {
2497 category->next()->set_prev(category->prev());
2498 }
2499 category->set_next(nullptr);
2500 category->set_prev(nullptr);
2501 }
2502
2503 void FreeList::PrintCategories(FreeListCategoryType type) {
2504 FreeListCategoryIterator it(this, type);
2505 PrintF("FreeList[%p, top=%p, %d] ", this, categories_[type], type);
2506 while (it.HasNext()) {
2507 FreeListCategory* current = it.Next();
2508 PrintF("%p -> ", current);
2509 }
2510 PrintF("null\n");
2577 } 2511 }
2578 2512
2579 2513
2580 void FreeList::RepairLists(Heap* heap) {
2581 for (int i = kFirstCategory; i < kNumberOfCategories; i++) {
2582 category_[i].RepairFreeList(heap);
2583 }
2584 }
2585
2586
2587 #ifdef DEBUG 2514 #ifdef DEBUG
2588 intptr_t FreeListCategory::SumFreeList() { 2515 intptr_t FreeListCategory::SumFreeList() {
2589 intptr_t sum = 0; 2516 intptr_t sum = 0;
2590 FreeSpace* cur = top(); 2517 FreeSpace* cur = top();
2591 while (cur != NULL) { 2518 while (cur != NULL) {
2592 DCHECK(cur->map() == cur->GetHeap()->root(Heap::kFreeSpaceMapRootIndex)); 2519 DCHECK(cur->map() == cur->GetHeap()->root(Heap::kFreeSpaceMapRootIndex));
2593 sum += cur->nobarrier_size(); 2520 sum += cur->nobarrier_size();
2594 cur = cur->next(); 2521 cur = cur->next();
2595 } 2522 }
2596 return sum; 2523 return sum;
(...skipping 11 matching lines...) Expand all
2608 return length; 2535 return length;
2609 } 2536 }
2610 2537
2611 2538
2612 bool FreeListCategory::IsVeryLong() { 2539 bool FreeListCategory::IsVeryLong() {
2613 return FreeListLength() == kVeryLongFreeList; 2540 return FreeListLength() == kVeryLongFreeList;
2614 } 2541 }
2615 2542
2616 2543
2617 bool FreeList::IsVeryLong() { 2544 bool FreeList::IsVeryLong() {
2618 for (int i = kFirstCategory; i < kNumberOfCategories; i++) { 2545 bool is_very_long = false;
2619 if (category_[i].IsVeryLong()) { 2546 ForAllFreeListCategories([&is_very_long](FreeListCategory* category) {
2620 return true; 2547 if (category->IsVeryLong()) is_very_long = true;
2621 } 2548 });
2622 } 2549 return is_very_long;
2623 return false;
2624 } 2550 }
2625 2551
2626 2552
2627 // This can take a very long time because it is linear in the number of entries 2553 // This can take a very long time because it is linear in the number of entries
2628 // on the free list, so it should not be called if FreeListLength returns 2554 // on the free list, so it should not be called if FreeListLength returns
2629 // kVeryLongFreeList. 2555 // kVeryLongFreeList.
2630 intptr_t FreeList::SumFreeLists() { 2556 intptr_t FreeList::SumFreeLists() {
2631 intptr_t sum = 0; 2557 intptr_t sum = 0;
2632 for (int i = kFirstCategory; i < kNumberOfCategories; i++) { 2558 ForAllFreeListCategories(
2633 sum += category_[i].SumFreeList(); 2559 [&sum](FreeListCategory* category) { sum += category->SumFreeList(); });
2634 }
2635 return sum; 2560 return sum;
2636 } 2561 }
2637 #endif 2562 #endif
2638 2563
2639 2564
2640 // ----------------------------------------------------------------------------- 2565 // -----------------------------------------------------------------------------
2641 // OldSpace implementation 2566 // OldSpace implementation
2642 2567
2643 void PagedSpace::PrepareForMarkCompact() { 2568 void PagedSpace::PrepareForMarkCompact() {
2644 // We don't have a linear allocation area while sweeping. It will be restored 2569 // We don't have a linear allocation area while sweeping. It will be restored
(...skipping 597 matching lines...) Expand 10 before | Expand all | Expand 10 after
3242 object->ShortPrint(); 3167 object->ShortPrint();
3243 PrintF("\n"); 3168 PrintF("\n");
3244 } 3169 }
3245 printf(" --------------------------------------\n"); 3170 printf(" --------------------------------------\n");
3246 printf(" Marked: %x, LiveCount: %x\n", mark_size, LiveBytes()); 3171 printf(" Marked: %x, LiveCount: %x\n", mark_size, LiveBytes());
3247 } 3172 }
3248 3173
3249 #endif // DEBUG 3174 #endif // DEBUG
3250 } // namespace internal 3175 } // namespace internal
3251 } // namespace v8 3176 } // namespace v8
OLDNEW
« src/heap/spaces.h ('K') | « src/heap/spaces.h ('k') | src/heap/spaces-inl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698