Memory.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // Copyright (C) 2009-2015, Panagiotis Christopoulos Charitos.
  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 <type_traits>
  9. ANKI_TEST(Util, HeapMemoryPool)
  10. {
  11. // Simple
  12. {
  13. HeapMemoryPool pool;
  14. pool.create(allocAligned, nullptr);
  15. void* ptr = pool.allocate(123, 1);
  16. ANKI_TEST_EXPECT_NEQ(ptr, nullptr);
  17. pool.free(ptr);
  18. }
  19. // Simple array
  20. {
  21. HeapMemoryPool pool;
  22. pool.create(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;
  33. }
  34. // Create/destroy test #2
  35. {
  36. StackMemoryPool pool;
  37. pool.create(allocAligned, nullptr, 10, 4);
  38. }
  39. // Allocate
  40. {
  41. StackMemoryPool pool;
  42. pool.create(allocAligned, nullptr, 100, false, 4);
  43. void* a = pool.allocate(25, 1);
  44. ANKI_TEST_EXPECT_NEQ(a, nullptr);
  45. ANKI_TEST_EXPECT_EQ(pool.getAllocationsCount(), 1);
  46. ANKI_TEST_EXPECT_GEQ(pool.getAllocatedSize(), 25);
  47. pool.free(a);
  48. ANKI_TEST_EXPECT_EQ(pool.getAllocationsCount(), 0);
  49. // Allocate a few
  50. const U SIZE = 25 - 4;
  51. a = pool.allocate(SIZE, 1);
  52. ANKI_TEST_EXPECT_NEQ(a, nullptr);
  53. a = pool.allocate(SIZE, 1);
  54. ANKI_TEST_EXPECT_NEQ(a, nullptr);
  55. a = pool.allocate(SIZE, 1);
  56. ANKI_TEST_EXPECT_NEQ(a, nullptr);
  57. a = pool.allocate(SIZE, 1);
  58. ANKI_TEST_EXPECT_EQ(a, nullptr);
  59. ANKI_TEST_EXPECT_EQ(pool.getAllocationsCount(), 3);
  60. // Reset
  61. pool.reset();
  62. ANKI_TEST_EXPECT_EQ(pool.getAllocationsCount(), 0);
  63. // Allocate again
  64. a = pool.allocate(SIZE, 1);
  65. ANKI_TEST_EXPECT_NEQ(a, nullptr);
  66. a = pool.allocate(SIZE, 1);
  67. ANKI_TEST_EXPECT_NEQ(a, nullptr);
  68. a = pool.allocate(SIZE, 1);
  69. ANKI_TEST_EXPECT_NEQ(a, nullptr);
  70. a = pool.allocate(SIZE, 1);
  71. ANKI_TEST_EXPECT_EQ(a, nullptr);
  72. ANKI_TEST_EXPECT_EQ(pool.getAllocationsCount(), 3);
  73. }
  74. }
  75. ANKI_TEST(Util, ChainMemoryPool)
  76. {
  77. // Basic test
  78. {
  79. const U size = 8;
  80. ChainMemoryPool pool;
  81. pool.create(
  82. allocAligned, nullptr,
  83. size, 2.0, 0, 1);
  84. void* mem = pool.allocate(5, 1);
  85. ANKI_TEST_EXPECT_NEQ(mem, nullptr);
  86. void* mem1 = pool.allocate(5, 1);
  87. ANKI_TEST_EXPECT_NEQ(mem1, nullptr);
  88. pool.free(mem1);
  89. pool.free(mem);
  90. ANKI_TEST_EXPECT_EQ(pool.getChunksCount(), 0);
  91. }
  92. // Basic test 2
  93. {
  94. const U size = sizeof(PtrSize) + 10;
  95. ChainMemoryPool pool;
  96. pool.create(
  97. allocAligned, nullptr,
  98. size, 2.0, 0, 1);
  99. void* mem = pool.allocate(size, 1);
  100. ANKI_TEST_EXPECT_NEQ(mem, nullptr);
  101. void* mem1 = pool.allocate(size, 1);
  102. ANKI_TEST_EXPECT_NEQ(mem1, nullptr);
  103. void* mem3 = pool.allocate(size, 1);
  104. ANKI_TEST_EXPECT_NEQ(mem1, nullptr);
  105. pool.free(mem1);
  106. mem1 = pool.allocate(size, 1);
  107. ANKI_TEST_EXPECT_NEQ(mem1, nullptr);
  108. pool.free(mem3);
  109. pool.free(mem1);
  110. pool.free(mem);
  111. ANKI_TEST_EXPECT_EQ(pool.getChunksCount(), 0);
  112. }
  113. }