| 82 | } |
| 83 | |
| 84 | int main() { |
| 85 | FakeOs os; |
| 86 | // pool off: every segment is a plain allocation (nothing pooled), the sizes are what they were |
| 87 | { |
| 88 | check(segment_pool() == nullptr, "no pool is installed"); |
| 89 | Image img(61225, 1); |
| 90 | check(img.intact(1) && img.bytes > 700 * MiB && img.bytes < 900 * MiB, "a 64K image is about 0.83 GB (13 layers)"); |
| 91 | } |
| 92 | SegmentPool pool(os, 1024 * MiB); |
| 93 | check(pool.active() && segment_pool_install(&pool), "a 1 GiB pool is installed"); |
| 94 | // a 2K and a 64K conversation alternating: the second 64K image is served entirely from the pool |
| 95 | { |
| 96 | auto a = std::make_unique<Image>(1931, 10); // 2K |
| 97 | const uint64_t after_2k = pool.hits(); |
| 98 | check(after_2k > 0 && pool.live_requested() > 0, "the 2K image's large buffers come from the pool"); |
| 99 | check(pool.live_requested() < a->bytes, "its small buffers (scales, pooled rows) stay on the heap"); |
| 100 | auto z = std::make_unique<Image>(61225, 20); // new 64K |
| 101 | check(z->intact(20) && a->intact(10), "both images keep their bytes"); |
| 102 | const size_t live_both = pool.live_requested(); |
| 103 | const uint64_t misses_first = pool.misses(); |
| 104 | z.reset(); // the 64K image goes (evicted): its ranges are free and warm |
| 105 | check(pool.live_requested() < live_both && pool.idle_bytes() > 0, "an evicted image's bytes go back to the pool"); |
| 106 | auto w = std::make_unique<Image>(1931, 30); // another 2K |
| 107 | check(w->intact(30), "a 2K image takes some of the freed ranges"); |
| 108 | auto z2 = std::make_unique<Image>(61225, 40); // the 64K conversation again |
| 109 | check(z2->intact(40) && a->intact(10) && w->intact(30), "the second 64K image and the 2K images are intact"); |
| 110 | check(pool.misses() == misses_first, "the second 64K image took every large buffer from the pool: no new miss"); |
| 111 | z2.reset(); w.reset(); a.reset(); |
| 112 | check(pool.live_requested() == 0, "everything is given back"); |
| 113 | } |
| 114 | // growth by appending (a continued conversation) and rewind |
| 115 | { |
| 116 | ConversationBuffer b; |
| 117 | b.resize(3 * MiB, 7); |
| 118 | const size_t live = pool.live_requested(); |
| 119 | b.resize(3 * MiB + 5, 9); // within the segment's spare capacity or a new small segment |
| 120 | std::vector<uint8_t> tail(5); |
| 121 | check(b.read(tail.data(), 3 * MiB, 5) && tail[0] == 9 && tail[4] == 9, "append keeps working on a pooled buffer"); |
| 122 | b.resize(1 * MiB); |
| 123 | check(b.size() == 1 * MiB && pool.live_requested() >= live, "a shortened buffer keeps its segment"); |
| 124 | ConversationBuffer c = b; // a copy is its own storage |
| 125 | check(c == b, "a copy equals its source"); |
| 126 | ConversationBuffer d = std::move(b); |
| 127 | check(d.size() == 1 * MiB && b.size() == 0, "a move leaves the source empty"); |
| 128 | } |
| 129 | check(pool.live_requested() == 0, "no pooled byte is left after the buffers are gone"); |
| 130 | segment_pool_uninstall_for_test(); |
| 131 | // a pool smaller than the request: the pool takes what it can, the rest is the heap, and every byte is right |
| 132 | { |
| 133 | SegmentPool small(os, 256 * MiB); |
| 134 | segment_pool_install(&small); |
| 135 | { |
| 136 | Image big(61225, 50); |
| 137 | check(big.intact(50), "a 64K image over a 256 MiB pool keeps its bytes"); |
| 138 | check(small.hits() > 0 && small.misses() > 0, "the pool served part of it and missed the rest"); |
| 139 | check(small.live_requested() <= 256 * MiB, "the pool never holds more than its size"); |
| 140 | Image other(8000, 60); |
| 141 | check(other.intact(60) && big.intact(50), "an 8K image beside it, both intact"); |
nothing calls this directly
no test coverage detected