| 518 | } |
| 519 | |
| 520 | bool AwaitAllocator::ensureCommitted(size_t sizeInBytes) |
| 521 | { |
| 522 | if (currentMode != AwaitAllocatorMode::Virtual or virtualMemory == nullptr) |
| 523 | { |
| 524 | return false; |
| 525 | } |
| 526 | if (sizeInBytes <= virtualCommittedBytes) |
| 527 | { |
| 528 | return true; |
| 529 | } |
| 530 | if (sizeInBytes > virtualReservedBytes) |
| 531 | { |
| 532 | return false; |
| 533 | } |
| 534 | |
| 535 | const size_t previousCommittedBytes = virtualCommittedBytes; |
| 536 | const size_t targetCommittedBytes = roundUpToAwaitAllocatorPageSize(sizeInBytes); |
| 537 | void* commitAddress = static_cast<char*>(virtualMemory) + previousCommittedBytes; |
| 538 | const size_t bytesToCommit = targetCommittedBytes - previousCommittedBytes; |
| 539 | #if SC_PLATFORM_WINDOWS |
| 540 | if (::VirtualAlloc(commitAddress, bytesToCommit, MEM_COMMIT, PAGE_READWRITE) == nullptr) |
| 541 | { |
| 542 | return false; |
| 543 | } |
| 544 | #else |
| 545 | if (::mprotect(commitAddress, bytesToCommit, PROT_READ | PROT_WRITE) != 0) |
| 546 | { |
| 547 | return false; |
| 548 | } |
| 549 | #endif |
| 550 | virtualCommittedBytes = targetCommittedBytes; |
| 551 | |
| 552 | if (firstBlock == nullptr) |
| 553 | { |
| 554 | firstBlock = static_cast<BlockHeader*>(virtualMemory); |
| 555 | firstBlock->allocator = this; |
| 556 | firstBlock->previous = nullptr; |
| 557 | firstBlock->next = nullptr; |
| 558 | firstBlock->blockBytes = virtualCommittedBytes; |
| 559 | firstBlock->free = true; |
| 560 | return true; |
| 561 | } |
| 562 | |
| 563 | BlockHeader* lastBlock = firstBlock; |
| 564 | while (lastBlock->next != nullptr) |
| 565 | { |
| 566 | lastBlock = lastBlock->next; |
| 567 | } |
| 568 | |
| 569 | const size_t addedBytes = virtualCommittedBytes - previousCommittedBytes; |
| 570 | if (lastBlock->free) |
| 571 | { |
| 572 | lastBlock->blockBytes += addedBytes; |
| 573 | } |
| 574 | else |
| 575 | { |
| 576 | BlockHeader* nextBlock = |
| 577 | reinterpret_cast<BlockHeader*>(static_cast<char*>(virtualMemory) + previousCommittedBytes); |
nothing calls this directly
no test coverage detected