Finalize thread, orphan heap
| 1435 | |
| 1436 | //! Finalize thread, orphan heap |
| 1437 | void |
| 1438 | rpmalloc_thread_finalize(void) { |
| 1439 | heap_t* heap = _memory_thread_heap; |
| 1440 | if (!heap) |
| 1441 | return; |
| 1442 | |
| 1443 | atomic_add32(&_memory_active_heaps, -1); |
| 1444 | |
| 1445 | _memory_deallocate_deferred(heap, 0); |
| 1446 | |
| 1447 | //Release thread cache spans back to global cache |
| 1448 | for (size_t iclass = 0; iclass < SPAN_CLASS_COUNT; ++iclass) { |
| 1449 | const size_t page_count = (iclass + 1) * SPAN_CLASS_GRANULARITY; |
| 1450 | span_t* span = heap->span_cache[iclass]; |
| 1451 | while (span) { |
| 1452 | if (span->data.list_size > MIN_SPAN_CACHE_RELEASE) { |
| 1453 | count_t list_size = 1; |
| 1454 | span_t* next = span->next_span; |
| 1455 | span_t* last = span; |
| 1456 | while (list_size < MIN_SPAN_CACHE_RELEASE) { |
| 1457 | last = next; |
| 1458 | next = next->next_span; |
| 1459 | ++list_size; |
| 1460 | } |
| 1461 | last->next_span = 0; //Terminate list |
| 1462 | next->data.list_size = span->data.list_size - list_size; |
| 1463 | _memory_global_cache_insert(span, list_size, page_count); |
| 1464 | span = next; |
| 1465 | } |
| 1466 | else { |
| 1467 | _memory_global_cache_insert(span, span->data.list_size, page_count); |
| 1468 | span = 0; |
| 1469 | } |
| 1470 | } |
| 1471 | heap->span_cache[iclass] = 0; |
| 1472 | } |
| 1473 | |
| 1474 | for (size_t iclass = 0; iclass < LARGE_CLASS_COUNT; ++iclass) { |
| 1475 | const size_t span_count = iclass + 1; |
| 1476 | span_t* span = heap->large_cache[iclass]; |
| 1477 | while (span) { |
| 1478 | if (span->data.list_size > MIN_SPAN_CACHE_RELEASE) { |
| 1479 | count_t list_size = 1; |
| 1480 | span_t* next = span->next_span; |
| 1481 | span_t* last = span; |
| 1482 | while (list_size < MIN_SPAN_CACHE_RELEASE) { |
| 1483 | last = next; |
| 1484 | next = next->next_span; |
| 1485 | ++list_size; |
| 1486 | } |
| 1487 | last->next_span = 0; //Terminate list |
| 1488 | next->data.list_size = span->data.list_size - list_size; |
| 1489 | _memory_global_cache_large_insert(span, list_size, span_count); |
| 1490 | span = next; |
| 1491 | } |
| 1492 | else { |
| 1493 | _memory_global_cache_large_insert(span, span->data.list_size, span_count); |
| 1494 | span = 0; |
no test coverage detected