Memory.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. // Copyright (C) 2009-2021, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <Tests/Framework/Framework.h>
  6. #include <Tests/Util/Foo.h>
  7. #include <AnKi/Util/Memory.h>
  8. #include <AnKi/Util/ThreadPool.h>
  9. #include <type_traits>
  10. #include <cstring>
  11. ANKI_TEST(Util, HeapMemoryPool)
  12. {
  13. // Simple
  14. {
  15. HeapMemoryPool pool;
  16. pool.init(allocAligned, nullptr);
  17. void* ptr = pool.allocate(123, 1);
  18. ANKI_TEST_EXPECT_NEQ(ptr, nullptr);
  19. pool.free(ptr);
  20. }
  21. // Simple array
  22. {
  23. HeapMemoryPool pool;
  24. pool.init(allocAligned, nullptr);
  25. void* ptr = pool.allocate(2, 1);
  26. ANKI_TEST_EXPECT_NEQ(ptr, nullptr);
  27. pool.free(ptr);
  28. }
  29. }
  30. ANKI_TEST(Util, StackMemoryPool)
  31. {
  32. // Create/destroy test
  33. {
  34. StackMemoryPool pool;
  35. }
  36. // Create/destroy test #2
  37. {
  38. StackMemoryPool pool;
  39. pool.init(allocAligned, nullptr, 10);
  40. }
  41. // Allocate
  42. {
  43. StackMemoryPool pool;
  44. pool.init(allocAligned, nullptr, 100, 1.0, 0, true);
  45. void* a = pool.allocate(25, 1);
  46. ANKI_TEST_EXPECT_NEQ(a, nullptr);
  47. ANKI_TEST_EXPECT_EQ(pool.getAllocationsCount(), 1);
  48. pool.free(a);
  49. ANKI_TEST_EXPECT_EQ(pool.getAllocationsCount(), 0);
  50. // Allocate a few
  51. const U SIZE = 75;
  52. a = pool.allocate(SIZE, 1);
  53. ANKI_TEST_EXPECT_NEQ(a, nullptr);
  54. a = pool.allocate(SIZE, 1);
  55. ANKI_TEST_EXPECT_NEQ(a, nullptr);
  56. a = pool.allocate(SIZE, 1);
  57. ANKI_TEST_EXPECT_NEQ(a, nullptr);
  58. a = pool.allocate(SIZE, 1);
  59. ANKI_TEST_EXPECT_NEQ(a, nullptr);
  60. ANKI_TEST_EXPECT_EQ(pool.getAllocationsCount(), 4);
  61. // Reset
  62. pool.reset();
  63. ANKI_TEST_EXPECT_EQ(pool.getAllocationsCount(), 0);
  64. // Allocate again
  65. a = pool.allocate(SIZE, 1);
  66. ANKI_TEST_EXPECT_NEQ(a, nullptr);
  67. a = pool.allocate(SIZE, 1);
  68. ANKI_TEST_EXPECT_NEQ(a, nullptr);
  69. a = pool.allocate(SIZE, 1);
  70. ANKI_TEST_EXPECT_NEQ(a, nullptr);
  71. a = pool.allocate(SIZE, 1);
  72. ANKI_TEST_EXPECT_NEQ(a, nullptr);
  73. ANKI_TEST_EXPECT_EQ(pool.getAllocationsCount(), 4);
  74. }
  75. // Parallel
  76. {
  77. StackMemoryPool pool;
  78. const U THREAD_COUNT = 32;
  79. const U ALLOC_SIZE = 25;
  80. ThreadPool threadPool(THREAD_COUNT);
  81. class AllocateTask : public ThreadPoolTask
  82. {
  83. public:
  84. StackMemoryPool* m_pool = nullptr;
  85. Array<void*, 0xF> m_allocations;
  86. Error operator()(U32 taskId, PtrSize threadsCount)
  87. {
  88. for(U i = 0; i < m_allocations.getSize(); ++i)
  89. {
  90. void* ptr = m_pool->allocate(ALLOC_SIZE, 1);
  91. memset(ptr, U8((taskId << 4) | i), ALLOC_SIZE);
  92. m_allocations[i] = ptr;
  93. }
  94. return Error::NONE;
  95. }
  96. };
  97. pool.init(allocAligned, nullptr, 100, 1.0, 0, true);
  98. Array<AllocateTask, THREAD_COUNT> tasks;
  99. for(U32 i = 0; i < THREAD_COUNT; ++i)
  100. {
  101. tasks[i].m_pool = &pool;
  102. threadPool.assignNewTask(i, &tasks[i]);
  103. }
  104. ANKI_TEST_EXPECT_NO_ERR(threadPool.waitForAllThreadsToFinish());
  105. // Check
  106. for(U i = 0; i < THREAD_COUNT; ++i)
  107. {
  108. const auto& task = tasks[i];
  109. for(U j = 0; j < task.m_allocations.getSize(); ++j)
  110. {
  111. U8 magic = U8((i << 4) | j);
  112. U8* ptr = static_cast<U8*>(task.m_allocations[j]);
  113. for(U k = 0; k < ALLOC_SIZE; ++k)
  114. {
  115. ANKI_TEST_EXPECT_EQ(ptr[k], magic);
  116. }
  117. }
  118. }
  119. // Reset and allocate again
  120. pool.reset();
  121. for(U32 i = 0; i < THREAD_COUNT; ++i)
  122. {
  123. threadPool.assignNewTask(i, &tasks[i]);
  124. }
  125. ANKI_TEST_EXPECT_NO_ERR(threadPool.waitForAllThreadsToFinish());
  126. for(U i = 0; i < THREAD_COUNT; ++i)
  127. {
  128. const auto& task = tasks[i];
  129. for(U j = 0; j < task.m_allocations.getSize(); ++j)
  130. {
  131. U8 magic = U8((i << 4) | j);
  132. U8* ptr = static_cast<U8*>(task.m_allocations[j]);
  133. for(U k = 0; k < ALLOC_SIZE; ++k)
  134. {
  135. ANKI_TEST_EXPECT_EQ(ptr[k], magic);
  136. }
  137. }
  138. }
  139. }
  140. }
  141. ANKI_TEST(Util, ChainMemoryPool)
  142. {
  143. // Basic test
  144. {
  145. const U size = 8;
  146. ChainMemoryPool pool;
  147. pool.init(allocAligned, nullptr, size, 2.0, 0, 1);
  148. void* mem = pool.allocate(5, 1);
  149. ANKI_TEST_EXPECT_NEQ(mem, nullptr);
  150. void* mem1 = pool.allocate(5, 1);
  151. ANKI_TEST_EXPECT_NEQ(mem1, nullptr);
  152. pool.free(mem1);
  153. pool.free(mem);
  154. ANKI_TEST_EXPECT_EQ(pool.getChunksCount(), 0);
  155. }
  156. // Basic test 2
  157. {
  158. const U size = sizeof(PtrSize) + 10;
  159. ChainMemoryPool pool;
  160. pool.init(allocAligned, nullptr, size, 2.0, 0, 1);
  161. void* mem = pool.allocate(size, 1);
  162. ANKI_TEST_EXPECT_NEQ(mem, nullptr);
  163. void* mem1 = pool.allocate(size, 1);
  164. ANKI_TEST_EXPECT_NEQ(mem1, nullptr);
  165. void* mem3 = pool.allocate(size, 1);
  166. ANKI_TEST_EXPECT_NEQ(mem1, nullptr);
  167. pool.free(mem1);
  168. mem1 = pool.allocate(size, 1);
  169. ANKI_TEST_EXPECT_NEQ(mem1, nullptr);
  170. pool.free(mem3);
  171. pool.free(mem1);
  172. pool.free(mem);
  173. ANKI_TEST_EXPECT_EQ(pool.getChunksCount(), 0);
  174. }
  175. }