+TEST(IndexedMemPool, no_data_races) {
+ const int count = 1000;
+ const uint32_t poolSize = 100;
+ const int nthreads = 10;
+
+ using Pool = IndexedMemPool<int, 8, 8>;
+ Pool pool(poolSize);
+
+ std::vector<std::thread> thr(nthreads);
+ for (auto i = 0; i < nthreads; ++i) {
+ thr[i] = std::thread([&]() {
+ for (auto j = 0; j < count; ++j) {
+ uint32_t idx = pool.allocIndex();
+ EXPECT_NE(idx, 0u);
+ EXPECT_LE(
+ idx, poolSize + (pool.NumLocalLists - 1) * pool.LocalListLimit);
+ pool[idx] = j;
+ pool.recycleIndex(idx);
+ }
+ });
+ }
+ for (auto& t : thr) {
+ t.join();
+ }
+}
+
+std::atomic<int> cnum{0};
+std::atomic<int> dnum{0};
+
+TEST(IndexedMemPool, construction_destruction) {
+ struct Foo {
+ Foo() {
+ cnum.fetch_add(1);
+ }
+ ~Foo() {
+ dnum.fetch_add(1);
+ }
+ };
+
+ std::atomic<bool> start{false};
+ std::atomic<int> started{0};
+
+ using Pool = IndexedMemPool<
+ Foo,
+ 1,
+ 1,
+ std::atomic,
+ IndexedMemPoolTraitsLazyRecycle<Foo>>;
+ int nthreads = 20;
+ int count = 1000;
+
+ {
+ Pool pool(2);
+ std::vector<std::thread> thr(nthreads);
+ for (auto i = 0; i < nthreads; ++i) {
+ thr[i] = std::thread([&]() {
+ started.fetch_add(1);
+ while (!start.load())
+ ;
+ for (auto j = 0; j < count; ++j) {
+ uint32_t idx = pool.allocIndex();
+ if (idx != 0) {
+ pool.recycleIndex(idx);
+ }
+ }
+ });
+ }
+
+ while (started.load() < nthreads)
+ ;
+ start.store(true);
+
+ for (auto& t : thr) {
+ t.join();
+ }
+ }
+
+ CHECK_EQ(cnum.load(), dnum.load());
+}
+
+/// Global Traits mock. It can't be a regular (non-global) mock because we
+/// don't have access to the instance.
+struct MockTraits {
+ static MockTraits* instance;
+
+ MockTraits() {
+ instance = this;
+ }
+
+ ~MockTraits() {
+ instance = nullptr;
+ }
+
+ MOCK_METHOD2(onAllocate, void(std::string*, std::string));
+ MOCK_METHOD1(onRecycle, void(std::string*));
+
+ struct Forwarder {
+ static void initialize(std::string* ptr) {
+ new (ptr) std::string();
+ }
+
+ static void cleanup(std::string* ptr) {
+ using std::string;
+ ptr->~string();
+ }
+
+ static void onAllocate(std::string* ptr, std::string s) {
+ instance->onAllocate(ptr, s);
+ }
+
+ static void onRecycle(std::string* ptr) {
+ instance->onRecycle(ptr);
+ }
+ };
+};
+
+MockTraits* MockTraits::instance;
+
+using TraitsTestPool =
+ IndexedMemPool<std::string, 1, 1, std::atomic, MockTraits::Forwarder>;
+
+void testTraits(TraitsTestPool& pool) {
+ MockTraits traits;
+ const std::string* elem = nullptr;
+ EXPECT_CALL(traits, onAllocate(_, _))
+ .WillOnce(Invoke([&](std::string* s, auto) {
+ EXPECT_FALSE(pool.isAllocated(pool.locateElem(s)));
+ elem = s;
+ }));
+ std::string* ptr = pool.allocElem("foo").release();
+ EXPECT_EQ(ptr, elem);
+
+ elem = nullptr;
+ EXPECT_CALL(traits, onRecycle(_)).WillOnce(Invoke([&](std::string* s) {
+ EXPECT_TRUE(pool.isAllocated(pool.locateElem(s)));
+ elem = s;
+ }));
+ pool.recycleIndex(pool.locateElem(ptr));
+ EXPECT_EQ(ptr, elem);
+}
+
+// Test that Traits is used when both local and global lists are empty.
+TEST(IndexedMemPool, use_traits_empty) {
+ TraitsTestPool pool(10);
+ testTraits(pool);
+}
+
+// Test that Traits is used when allocating from a local list.
+TEST(IndexedMemPool, use_traits_local_list) {
+ TraitsTestPool pool(10);
+ MockTraits traits;
+ EXPECT_CALL(traits, onAllocate(_, _));
+ // Allocate and immediately recycle an element to populate the local list.
+ pool.allocElem("");
+ testTraits(pool);