// ImageMemoryPool and BlockRanges (runtime/src/image_memory.h): textures sharing blocks of device memory. CPU only, with // counted fake allocations: fixed cases for the block policy, then random requests checked against a list of the live // ranges (no overlap, aligned, inside the block, and all of it usable again when everything is released). #include "../src/image_memory.h" #include #include #include #include #include #include #include #include #include namespace { using leonida::BlockRanges; using Pool = leonida::ImageMemoryPool; constexpr std::uint64_t kMiB = std::uint64_t(1) << 20; int failures = 0; void check(bool ok, const char* what) { if (ok) return; std::printf("FAIL: %s\n", what); ++failures; } // A driver: hands out handles 1, 2, 3..., refuses when told, and notes what it freed. The pool frees a block outside // its own lock, so the driver takes one, as vkAllocateMemory and vkFreeMemory may run on several threads. struct Driver { std::mutex mutex; std::uintptr_t next = 1; std::set live; int allocations = 0; int frees = 0; bool full = false; Pool pool() { return Pool([this](std::uint32_t, std::uint64_t) -> std::uintptr_t { std::lock_guard lock(mutex); ++allocations; if (full) return 0; live.insert(next); return next++; }, [this](std::uintptr_t memory) { std::lock_guard lock(mutex); ++frees; check(live.erase(memory) == 1, "a block freed that was not live"); }); } }; void block_ranges() { BlockRanges ranges(1000); std::uint64_t a = 0, b = 0, c = 0, d = 0; bool reused = true; check(ranges.allocate(100, 1, a, reused) && a == 0 && !reused, "the first range starts at 0 and is fresh"); check(ranges.allocate(1, 256, b, reused) && b == 256, "a range starts at its alignment"); check(ranges.allocate(700, 1, c, reused) && c == 257, "the best fitting free piece is used"); check(!ranges.allocate(500, 1, d, reused), "a range larger than any free piece is refused"); check(!ranges.allocate(0, 1, d, reused) && !ranges.allocate(1001, 1, d, reused), "a zero or an oversized range is refused"); ranges.release(a, 100); ranges.release(c, 700); ranges.release(b, 1); check(ranges.empty() && ranges.largest() == 1000, "released ranges merge back into one piece"); check(ranges.allocate(1000, 1, d, reused) && d == 0 && reused, "a range over earlier bytes is marked as reused"); } void shared_blocks() { Driver driver; { Pool pool = driver.pool(); std::vector> ranges; for (int i = 0; i < 64; ++i) ranges.push_back(pool.allocate(2, kMiB, 65536, 64 * kMiB)); check(driver.allocations == 1 && pool.stats().blocks == 1 && pool.stats().ranges == 64 && pool.stats().block_bytes == 64 * kMiB, "64 one-MiB textures fill one block"); auto extra = pool.allocate(2, kMiB, 65536, 64 * kMiB); check(driver.allocations == 2, "the 65th makes a second block"); check(extra && !extra->reused(), "a range of a new block is fresh"); const auto other = pool.allocate(3, kMiB, 65536, 64 * kMiB); check(driver.allocations == 3 && other->memory() != extra->memory(), "memory types do not share blocks"); extra.reset(); check(driver.frees == 0 && pool.stats().blocks == 3, "an emptied block is kept for the next textures"); ranges.clear(); check(driver.frees == 1 && pool.stats().blocks == 2, "only one empty block is kept per memory type"); const auto again = pool.allocate(2, kMiB, 65536, 64 * kMiB); check(driver.allocations == 3 && again && again->reused(), "the kept block is used again, and marked as reused"); } check(driver.live.empty(), "the pool frees its blocks"); } void a_full_heap() { Driver driver; Pool pool = driver.pool(); driver.full = true; check(!pool.allocate(0, kMiB, 1024, 64 * kMiB), "a driver without room gives no range"); const int asked = driver.allocations; for (int i = 0; i < 64; ++i) check(!pool.allocate(0, kMiB, 1024, 64 * kMiB), "and goes on giving none"); check(driver.allocations == asked, "a full heap is not asked again at every texture"); check(!pool.allocate(0, kMiB, 1024, 64 * kMiB) && driver.allocations == asked + 1, "but is asked again later"); check(!pool.allocate(0, 2 * kMiB, 1024, kMiB) && driver.allocations == asked + 1, "a texture larger than a block is the caller's own allocation"); Driver other; Pool second = other.pool(); auto held = second.allocate(0, kMiB, 1024, 2 * kMiB); other.full = true; auto filling = second.allocate(0, kMiB, 1024, 2 * kMiB); check(filling && other.allocations == 1, "a full heap still serves the block's own free space"); check(!second.allocate(0, kMiB, 1024, 2 * kMiB) && other.allocations == 2, "and refuses once it is used up"); filling.reset(); check(second.allocate(0, kMiB, 1024, 2 * kMiB) != nullptr, "a released range is usable again at once"); held.reset(); } void random_requests() { std::mt19937 random(1274); for (int round = 0; round < 20; ++round) { Driver driver; Pool pool = driver.pool(); struct Live { std::unique_ptr range; std::uint64_t size; std::uint64_t alignment; }; std::vector live; for (int step = 0; step < 4000; ++step) { if (live.empty() || random() % 100 < 55) { const std::uint64_t alignment = std::uint64_t(1) << (8 + random() % 9); const std::uint64_t size = 1 + random() % (2 * kMiB); auto range = pool.allocate(1, size, alignment, 16 * kMiB); check(range != nullptr, "a driver with room serves a small range"); if (!range) return; check(range->offset() % alignment == 0 && range->offset() + size <= 16 * kMiB, "aligned, inside the block"); for (const auto& l : live) check(l.range->memory() != range->memory() || l.range->offset() >= range->offset() + size || range->offset() >= l.range->offset() + l.size, "live ranges do not overlap"); live.push_back({std::move(range), size, alignment}); } else { const std::size_t i = random() % live.size(); live[i] = std::move(live.back()); live.pop_back(); } } check(pool.stats().ranges == live.size(), "the range count follows"); live.clear(); check(pool.stats().ranges == 0 && pool.stats().blocks == 1, "everything released leaves one spare block"); const auto whole = pool.allocate(1, 16 * kMiB, 1, 16 * kMiB); check(whole && whole->offset() == 0, "the whole block is free again"); } } void threads() { Driver driver; { Pool pool = driver.pool(); std::vector workers; for (unsigned t = 0; t < 4; ++t) workers.emplace_back([&pool, t] { std::mt19937 random(t); std::vector> mine; for (int i = 0; i < 2000; ++i) { if (mine.size() < 16) mine.push_back(pool.allocate(0, 1 + random() % kMiB, 4096, 16 * kMiB)); else mine.erase(mine.begin() + random() % mine.size()); } }); for (auto& worker : workers) worker.join(); check(pool.stats().ranges == 0, "concurrent use leaves no range behind"); } check(driver.live.empty(), "nor a block"); } } // namespace int main() { block_ranges(); shared_blocks(); a_full_heap(); random_requests(); threads(); if (!failures) std::printf("image memory: textures share blocks, and released ranges are used again\n"); return failures ? 1 : 0; }