MCPcopy Create free account
hub / github.com/cameron314/concurrentqueue / block_alloc

Method block_alloc

tests/unittests/unittests.cpp:1007–1205  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1005 }
1006
1007 bool block_alloc()
1008 {
1009 typedef TestTraits<2> Traits;
1010 typedef TestTraits<2, 32, true> RecycleTraits;
1011 Traits::reset();
1012
1013 // Explicit
1014 {
1015 ConcurrentQueue<int, Traits> q(7);
1016 ASSERT_OR_FAIL(q.initialBlockPoolSize == 4);
1017
1018 ASSERT_OR_FAIL(Traits::malloc_count() == 1);
1019 ASSERT_OR_FAIL(Traits::free_count() == 0);
1020
1021 {
1022 ProducerToken tok(q);
1023 ASSERT_OR_FAIL(Traits::malloc_count() == 3); // one for producer, one for its block index
1024 ASSERT_OR_FAIL(Traits::free_count() == 0);
1025
1026 // Enqueue one item too many (force extra block allocation)
1027 for (int i = 0; i != 9; ++i) {
1028 ASSERT_OR_FAIL(q.enqueue(tok, i));
1029 }
1030
1031 ASSERT_OR_FAIL(Traits::malloc_count() == 4);
1032 ASSERT_OR_FAIL(Traits::free_count() == 0);
1033
1034 // Still room for one more...
1035 ASSERT_OR_FAIL(q.enqueue(tok, 9));
1036 ASSERT_OR_FAIL(Traits::malloc_count() == 4);
1037 ASSERT_OR_FAIL(Traits::free_count() == 0);
1038
1039 // No more room without further allocations
1040 ASSERT_OR_FAIL(!q.try_enqueue(tok, 10));
1041 ASSERT_OR_FAIL(Traits::malloc_count() == 4);
1042 ASSERT_OR_FAIL(Traits::free_count() == 0);
1043
1044 // Check items were enqueued properly
1045 int item;
1046 for (int i = 0; i != 10; ++i) {
1047 ASSERT_OR_FAIL(q.try_dequeue_from_producer(tok, item));
1048 ASSERT_OR_FAIL(item == i);
1049 }
1050
1051 // Queue should be empty, but not freed
1052 ASSERT_OR_FAIL(!q.try_dequeue_from_producer(tok, item));
1053 ASSERT_OR_FAIL(Traits::free_count() == 0);
1054 }
1055 // Explicit producers are recycled, so block should still be allocated
1056 ASSERT_OR_FAIL(Traits::free_count() == 0);
1057 }
1058
1059 ASSERT_OR_FAIL(Traits::malloc_count() == 4);
1060 ASSERT_OR_FAIL(Traits::free_count() == 4);
1061
1062 // Implicit
1063 Traits::reset();
1064 {

Callers

nothing calls this directly

Calls 4

enqueueMethod · 0.45
try_enqueueMethod · 0.45
try_dequeueMethod · 0.45

Tested by

no test coverage detected