Memory.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // Copyright (C) 2009-present, 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/MemoryPool.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(allocAligned, nullptr);
  16. void* ptr = pool.allocate(123, 1);
  17. ANKI_TEST_EXPECT_NEQ(ptr, nullptr);
  18. pool.free(ptr);
  19. }
  20. // Simple array
  21. {
  22. HeapMemoryPool pool(allocAligned, nullptr);
  23. void* ptr = pool.allocate(2, 1);
  24. ANKI_TEST_EXPECT_NEQ(ptr, nullptr);
  25. pool.free(ptr);
  26. }
  27. }
  28. ANKI_TEST(Util, StackMemoryPool)
  29. {
  30. // Create/destroy test
  31. {
  32. StackMemoryPool pool(allocAligned, nullptr, 10);
  33. }
  34. // Allocate
  35. {
  36. StackMemoryPool pool(allocAligned, nullptr, 100, 2.0, 0, true);
  37. void* a = pool.allocate(25, 1);
  38. ANKI_TEST_EXPECT_NEQ(a, nullptr);
  39. ANKI_TEST_EXPECT_EQ(pool.getAllocationCount(), 1);
  40. pool.free(a);
  41. ANKI_TEST_EXPECT_EQ(pool.getAllocationCount(), 0);
  42. // Allocate a few
  43. constexpr U kSize = 75;
  44. a = pool.allocate(kSize, 1);
  45. ANKI_TEST_EXPECT_NEQ(a, nullptr);
  46. a = pool.allocate(kSize, 1);
  47. ANKI_TEST_EXPECT_NEQ(a, nullptr);
  48. a = pool.allocate(kSize, 1);
  49. ANKI_TEST_EXPECT_NEQ(a, nullptr);
  50. a = pool.allocate(kSize, 1);
  51. ANKI_TEST_EXPECT_NEQ(a, nullptr);
  52. ANKI_TEST_EXPECT_EQ(pool.getAllocationCount(), 4);
  53. // Reset
  54. pool.reset();
  55. ANKI_TEST_EXPECT_EQ(pool.getAllocationCount(), 0);
  56. // Allocate again
  57. a = pool.allocate(kSize, 1);
  58. ANKI_TEST_EXPECT_NEQ(a, nullptr);
  59. a = pool.allocate(kSize, 1);
  60. ANKI_TEST_EXPECT_NEQ(a, nullptr);
  61. a = pool.allocate(kSize, 1);
  62. ANKI_TEST_EXPECT_NEQ(a, nullptr);
  63. a = pool.allocate(kSize, 1);
  64. ANKI_TEST_EXPECT_NEQ(a, nullptr);
  65. ANKI_TEST_EXPECT_EQ(pool.getAllocationCount(), 4);
  66. }
  67. // Parallel
  68. {
  69. StackMemoryPool pool(allocAligned, nullptr, 100, 1.0, 0, true);
  70. const U THREAD_COUNT = 32;
  71. const U ALLOC_SIZE = 25;
  72. ThreadPool threadPool(THREAD_COUNT);
  73. class AllocateTask : public ThreadPoolTask
  74. {
  75. public:
  76. StackMemoryPool* m_pool = nullptr;
  77. Array<void*, 0xF> m_allocations;
  78. Error operator()(U32 taskId, [[maybe_unused]] PtrSize threadsCount)
  79. {
  80. for(U i = 0; i < m_allocations.getSize(); ++i)
  81. {
  82. void* ptr = m_pool->allocate(ALLOC_SIZE, 1);
  83. memset(ptr, U8((taskId << 4) | i), ALLOC_SIZE);
  84. m_allocations[i] = ptr;
  85. }
  86. return Error::kNone;
  87. }
  88. };
  89. Array<AllocateTask, THREAD_COUNT> tasks;
  90. for(U32 i = 0; i < THREAD_COUNT; ++i)
  91. {
  92. tasks[i].m_pool = &pool;
  93. threadPool.assignNewTask(i, &tasks[i]);
  94. }
  95. ANKI_TEST_EXPECT_NO_ERR(threadPool.waitForAllThreadsToFinish());
  96. // Check
  97. for(U i = 0; i < THREAD_COUNT; ++i)
  98. {
  99. const auto& task = tasks[i];
  100. for(U j = 0; j < task.m_allocations.getSize(); ++j)
  101. {
  102. U8 magic = U8((i << 4) | j);
  103. U8* ptr = static_cast<U8*>(task.m_allocations[j]);
  104. for(U k = 0; k < ALLOC_SIZE; ++k)
  105. {
  106. ANKI_TEST_EXPECT_EQ(ptr[k], magic);
  107. }
  108. }
  109. }
  110. // Reset and allocate again
  111. pool.reset();
  112. for(U32 i = 0; i < THREAD_COUNT; ++i)
  113. {
  114. threadPool.assignNewTask(i, &tasks[i]);
  115. }
  116. ANKI_TEST_EXPECT_NO_ERR(threadPool.waitForAllThreadsToFinish());
  117. for(U i = 0; i < THREAD_COUNT; ++i)
  118. {
  119. const auto& task = tasks[i];
  120. for(U j = 0; j < task.m_allocations.getSize(); ++j)
  121. {
  122. U8 magic = U8((i << 4) | j);
  123. U8* ptr = static_cast<U8*>(task.m_allocations[j]);
  124. for(U k = 0; k < ALLOC_SIZE; ++k)
  125. {
  126. ANKI_TEST_EXPECT_EQ(ptr[k], magic);
  127. }
  128. }
  129. }
  130. }
  131. }