static void fill(Counter &counter, const typename Counter::Container &container, bool fragmented)
{
    qint64 allocated = 0;
    QHash<void *, int> allocations;
    for (int i = 0; i < 100; ++i) {
        for (int j = 0; j < 100; ++j) {
            int amount = fragmented ? j : i;
            allocated += amount;
            counter.request(amount);
            void *alloc = malloc(amount);
            allocations.insertMulti(alloc, amount);
            counter.obtain(reinterpret_cast<quintptr>(alloc));
            QCOMPARE(counter.currentTotal(), allocated);
        }
    }

    QCOMPARE(allocated, 99 * 50 * 100);
    QCOMPARE(counter.currentTotal(), allocated);
    QCOMPARE(sum(container), allocated);

    for (auto it = allocations.begin(), end = allocations.end(); it != end; ++it) {
        free(it.key());
        counter.release(reinterpret_cast<quintptr>(it.key()));
        allocated -= it.value();
        QCOMPARE(counter.currentTotal(), allocated);
    }

    allocations.clear();

    QCOMPARE(allocated, 0);
    QCOMPARE(counter.currentTotal(), 0);
    QCOMPARE(sum(container), 0);
}