Changeset View
Changeset View
Standalone View
Standalone View
src/test/allocator_tests.cpp
Show First 20 Lines • Show All 71 Lines • ▼ Show 20 Lines | #endif | ||||
std::vector<void *> addr; | std::vector<void *> addr; | ||||
// allocating 0 always returns nullptr | // allocating 0 always returns nullptr | ||||
BOOST_CHECK(b.alloc(0) == nullptr); | BOOST_CHECK(b.alloc(0) == nullptr); | ||||
#ifdef ARENA_DEBUG | #ifdef ARENA_DEBUG | ||||
b.walk(); | b.walk(); | ||||
#endif | #endif | ||||
// Sweeping allocate all memory | // Sweeping allocate all memory | ||||
for (int x = 0; x < 1024; ++x) | for (int x = 0; x < 1024; ++x) { | ||||
addr.push_back(b.alloc(1024)); | addr.push_back(b.alloc(1024)); | ||||
} | |||||
BOOST_CHECK(b.stats().free == 0); | BOOST_CHECK(b.stats().free == 0); | ||||
// memory is full, this must return nullptr | // memory is full, this must return nullptr | ||||
BOOST_CHECK(b.alloc(1024) == nullptr); | BOOST_CHECK(b.alloc(1024) == nullptr); | ||||
BOOST_CHECK(b.alloc(0) == nullptr); | BOOST_CHECK(b.alloc(0) == nullptr); | ||||
for (int x = 0; x < 1024; ++x) | for (int x = 0; x < 1024; ++x) { | ||||
b.free(addr[x]); | b.free(addr[x]); | ||||
} | |||||
addr.clear(); | addr.clear(); | ||||
BOOST_CHECK(b.stats().total == synth_size); | BOOST_CHECK(b.stats().total == synth_size); | ||||
BOOST_CHECK(b.stats().free == synth_size); | BOOST_CHECK(b.stats().free == synth_size); | ||||
// Now in the other direction... | // Now in the other direction... | ||||
for (int x = 0; x < 1024; ++x) | for (int x = 0; x < 1024; ++x) { | ||||
addr.push_back(b.alloc(1024)); | addr.push_back(b.alloc(1024)); | ||||
for (int x = 0; x < 1024; ++x) | } | ||||
for (int x = 0; x < 1024; ++x) { | |||||
b.free(addr[1023 - x]); | b.free(addr[1023 - x]); | ||||
} | |||||
addr.clear(); | addr.clear(); | ||||
// Now allocate in smaller unequal chunks, then deallocate haphazardly | // Now allocate in smaller unequal chunks, then deallocate haphazardly | ||||
// Not all the chunks will succeed allocating, but freeing nullptr is | // Not all the chunks will succeed allocating, but freeing nullptr is | ||||
// allowed so that is no problem. | // allowed so that is no problem. | ||||
for (int x = 0; x < 2048; ++x) | for (int x = 0; x < 2048; ++x) { | ||||
addr.push_back(b.alloc(x + 1)); | addr.push_back(b.alloc(x + 1)); | ||||
for (int x = 0; x < 2048; ++x) | } | ||||
for (int x = 0; x < 2048; ++x) { | |||||
b.free(addr[((x * 23) % 2048) ^ 242]); | b.free(addr[((x * 23) % 2048) ^ 242]); | ||||
} | |||||
addr.clear(); | addr.clear(); | ||||
// Go entirely wild: free and alloc interleaved, generate targets and sizes | // Go entirely wild: free and alloc interleaved, generate targets and sizes | ||||
// using pseudo-randomness. | // using pseudo-randomness. | ||||
for (int x = 0; x < 2048; ++x) | for (int x = 0; x < 2048; ++x) { | ||||
addr.push_back(0); | addr.push_back(0); | ||||
} | |||||
uint32_t s = 0x12345678; | uint32_t s = 0x12345678; | ||||
for (int x = 0; x < 5000; ++x) { | for (int x = 0; x < 5000; ++x) { | ||||
int idx = s & (addr.size() - 1); | int idx = s & (addr.size() - 1); | ||||
if (s & 0x80000000) { | if (s & 0x80000000) { | ||||
b.free(addr[idx]); | b.free(addr[idx]); | ||||
addr[idx] = 0; | addr[idx] = 0; | ||||
} else if (!addr[idx]) { | } else if (!addr[idx]) { | ||||
addr[idx] = b.alloc((s >> 16) & 2047); | addr[idx] = b.alloc((s >> 16) & 2047); | ||||
} | } | ||||
bool lsb = s & 1; | bool lsb = s & 1; | ||||
s >>= 1; | s >>= 1; | ||||
// LFSR period 0xf7ffffe0 | // LFSR period 0xf7ffffe0 | ||||
if (lsb) s ^= 0xf00f00f0; | if (lsb) { | ||||
s ^= 0xf00f00f0; | |||||
} | } | ||||
for (void *ptr : addr) | } | ||||
for (void *ptr : addr) { | |||||
b.free(ptr); | b.free(ptr); | ||||
} | |||||
addr.clear(); | addr.clear(); | ||||
BOOST_CHECK(b.stats().total == synth_size); | BOOST_CHECK(b.stats().total == synth_size); | ||||
BOOST_CHECK(b.stats().free == synth_size); | BOOST_CHECK(b.stats().free == synth_size); | ||||
} | } | ||||
/** Mock LockedPageAllocator for testing */ | /** Mock LockedPageAllocator for testing */ | ||||
class TestLockedPageAllocator : public LockedPageAllocator { | class TestLockedPageAllocator : public LockedPageAllocator { | ||||
▲ Show 20 Lines • Show All 99 Lines • Show Last 20 Lines |