| 35 | StreamArena::StreamArena(memory::MemoryPool* pool) : pool_(pool) {} |
| 36 | |
| 37 | void StreamArena::newRange( |
| 38 | int32_t bytes, |
| 39 | ByteRange* /*lastRange*/, |
| 40 | ByteRange* range) { |
| 41 | BOLT_CHECK_GT(bytes, 0, "StreamArena::newRange can't be zero length"); |
| 42 | const memory::MachinePageCount numPages = |
| 43 | memory::AllocationTraits::numPages(bytes); |
| 44 | // If new range 'bytes' is larger than the largest class page size, then we |
| 45 | // allocate a large chunk of contiguous memory for this range. |
| 46 | if (numPages > pool_->largestSizeClass()) { |
| 47 | memory::ContiguousAllocation largeAllocation; |
| 48 | pool_->allocateContiguous(numPages, largeAllocation); |
| 49 | range->buffer = largeAllocation.data(); |
| 50 | range->size = largeAllocation.size(); |
| 51 | range->position = 0; |
| 52 | size_ += range->size; |
| 53 | largeAllocations_.push_back(std::move(largeAllocation)); |
| 54 | return; |
| 55 | } |
| 56 | |
| 57 | const int32_t numRuns = allocation_.numRuns(); |
| 58 | if (currentRun_ >= numRuns) { |
| 59 | if (numRuns > 0) { |
| 60 | allocations_.push_back( |
| 61 | std::make_unique<memory::Allocation>(std::move(allocation_))); |
| 62 | } |
| 63 | pool_->allocateNonContiguous( |
| 64 | std::max(allocationQuantum_, numPages), allocation_); |
| 65 | currentRun_ = 0; |
| 66 | currentOffset_ = 0; |
| 67 | size_ += allocation_.byteSize(); |
| 68 | } |
| 69 | auto run = allocation_.runAt(currentRun_); |
| 70 | range->buffer = run.data() + currentOffset_; |
| 71 | const int32_t availableBytes = run.numBytes() - currentOffset_; |
| 72 | range->size = std::min(bytes, availableBytes); |
| 73 | range->position = 0; |
| 74 | currentOffset_ += range->size; |
| 75 | BOLT_DCHECK_LE(currentOffset_, run.numBytes()); |
| 76 | if (currentOffset_ == run.numBytes()) { |
| 77 | ++currentRun_; |
| 78 | ++currentOffset_ = 0; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | void StreamArena::newTinyRange( |
| 83 | int32_t bytes, |