60 std::iota(order.begin(), order.end(), 0);
61 llvm::sort(order, [&](
unsigned a,
unsigned b) {
62 return allocs[a].timeStart < allocs[
b].timeStart;
70 for (
unsigned idx : order) {
76 for (
const auto &p : placements) {
78 occupied.push_back({p.offset, p.offset + p.size});
84 int64_t bestGapSize = INT64_MAX;
87 for (
const auto &[occStart, occEnd] : occupied) {
89 if (alignedStart >= occStart) {
90 gapStart = std::max(gapStart, occEnd);
94 int64_t gapSize = gapEnd - alignedStart;
98 if (gapSize < bestGapSize) {
99 bestGapSize = gapSize;
100 bestOffset = alignedStart;
103 gapStart = std::max(gapStart, occEnd);
111 if (alignedTrailing + alloc.
sizeInBytes <= arenaEnd) {
112 int64_t trailingSize = arenaEnd - alignedTrailing;
113 if (trailingSize < bestGapSize) {
114 bestGapSize = trailingSize;
115 bestOffset = alignedTrailing;
121 bestOffset = alignedTrailing;
123 assert((arenaAlignment + bestOffset) % alloc.
alignment == 0 &&
124 "invalid alignment");
125 offsets[idx] = bestOffset;
127 arenaEnd = std::max(arenaEnd, bestOffset + alloc.
sizeInBytes);