| 1432 | } |
| 1433 | |
| 1434 | void NULLC::CollectMemory() |
| 1435 | { |
| 1436 | GC_DEBUG_PRINT("%d used memory (%d collectable cap, %d max cap)\r\n", usedMemory, collectableMinimum, globalMemoryLimit); |
| 1437 | |
| 1438 | double time = (double(clock()) / CLOCKS_PER_SEC); |
| 1439 | |
| 1440 | GC::unmanageableBase = (char*)&time; |
| 1441 | |
| 1442 | // All memory blocks are marked with 0 |
| 1443 | MarkMemory(0); |
| 1444 | // Used memory blocks are marked with 1 |
| 1445 | MarkUsedBlocks(); |
| 1446 | |
| 1447 | markTime += (double(clock()) / CLOCKS_PER_SEC) - time; |
| 1448 | time = (double(clock()) / CLOCKS_PER_SEC); |
| 1449 | |
| 1450 | // Globally allocated objects marked with 0 are deleted |
| 1451 | unsigned int unusedBlocks = 0; |
| 1452 | for(unsigned int i = 0; i < globalObjects.size(); i++) |
| 1453 | { |
| 1454 | if(((unsigned int*)globalObjects[i])[1] == 0) |
| 1455 | { |
| 1456 | usedMemory -= *(unsigned int*)globalObjects[i]; |
| 1457 | NULLC::dealloc(globalObjects[i]); |
| 1458 | globalObjects[i] = globalObjects.back(); |
| 1459 | globalObjects.pop_back(); |
| 1460 | unusedBlocks++; |
| 1461 | } |
| 1462 | } |
| 1463 | // printf("%d unused globally allocated blocks destroyed (%d remains)\r\n", unusedBlocks, globalObjects.size()); |
| 1464 | |
| 1465 | // printf("%d used memory\r\n", usedMemory); |
| 1466 | |
| 1467 | // Objects allocated from pools are freed |
| 1468 | unusedBlocks = pool8.FreeMarked(0); |
| 1469 | usedMemory -= unusedBlocks * 8; |
| 1470 | // printf("%d unused pool blocks freed (8 bytes)\r\n", unusedBlocks); |
| 1471 | unusedBlocks = pool16.FreeMarked(0); |
| 1472 | usedMemory -= unusedBlocks * 16; |
| 1473 | // printf("%d unused pool blocks freed (16 bytes)\r\n", unusedBlocks); |
| 1474 | unusedBlocks = pool32.FreeMarked(0); |
| 1475 | usedMemory -= unusedBlocks * 32; |
| 1476 | // printf("%d unused pool blocks freed (32 bytes)\r\n", unusedBlocks); |
| 1477 | unusedBlocks = pool64.FreeMarked(0); |
| 1478 | usedMemory -= unusedBlocks * 64; |
| 1479 | // printf("%d unused pool blocks freed (64 bytes)\r\n", unusedBlocks); |
| 1480 | unusedBlocks = pool128.FreeMarked(0); |
| 1481 | usedMemory -= unusedBlocks * 128; |
| 1482 | // printf("%d unused pool blocks freed (128 bytes)\r\n", unusedBlocks); |
| 1483 | unusedBlocks = pool256.FreeMarked(0); |
| 1484 | usedMemory -= unusedBlocks * 256; |
| 1485 | // printf("%d unused pool blocks freed (256 bytes)\r\n", unusedBlocks); |
| 1486 | unusedBlocks = pool512.FreeMarked(0); |
| 1487 | usedMemory -= unusedBlocks * 512; |
| 1488 | // printf("%d unused pool blocks freed (512 bytes)\r\n", unusedBlocks); |
| 1489 | |
| 1490 | GC_DEBUG_PRINT("%d used memory\r\n", usedMemory); |
| 1491 |
nothing calls this directly
no test coverage detected