test_page_heap.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #include "catch.hpp"
  2. #include "page_heap.h"
  3. #include "shm.h"
  4. #include "size_map.h"
  5. TEST_CASE("RadixTree, Allocate Span")
  6. {
  7. bg::ShmOptions opts(false, "Test", nullptr);
  8. opts.fix_vptr_on_init = false;
  9. REQUIRE(bg::ShmInit(opts) == true);
  10. bg::detail::MetadataAllocator<bg::detail::Span> span_allocator;
  11. std::vector<bg::detail::Span*> vec;
  12. vec.reserve(2340);
  13. for(int i = 0; i < 2340; ++i)
  14. {
  15. bg::detail::Span* span = span_allocator.Allocate();
  16. REQUIRE(span);
  17. span->SetUsedCount(i);
  18. vec.push_back(span);
  19. }
  20. for(int i = 2340 - 1; i >= 0; --i)
  21. {
  22. span_allocator.Destroy(vec[i]);
  23. }
  24. for(size_t i = 0; i < 2340; ++i)
  25. {
  26. bg::detail::Span* span = span_allocator.Allocate();
  27. REQUIRE(span);
  28. REQUIRE(span->GetUsedCount() == i);
  29. }
  30. for(int i = 2340 - 1; i >= 0; --i)
  31. {
  32. span_allocator.Destroy(vec[i]);
  33. }
  34. bg::ShmFini();
  35. }
  36. TEST_CASE("RadixTree, 1- CLASS_MAX_BYTES ")
  37. {
  38. bg::ShmOptions opts(false, "Test", nullptr);
  39. opts.fix_vptr_on_init = false;
  40. REQUIRE(bg::ShmInit(opts) == true);
  41. bg::detail::SizeMap sizemap;
  42. sizemap.Init();
  43. bg::detail::PageHeap page_heap;
  44. for(size_t i = 1; i < CLASS_MAX_BYTES; i = i << 2)
  45. {
  46. size_t index = sizemap.GetClassByBytes(i);
  47. REQUIRE(index);
  48. REQUIRE(sizemap.ClassToSize(index) >= i);
  49. REQUIRE(PAGES_TO_BYTES(sizemap.ClassToPages(index)) >= i);
  50. auto span = page_heap.AllocateSpan(sizemap.ClassToPages(index));
  51. REQUIRE(span);
  52. void* memory = (void*)PAGES_TO_BYTES(span->GetStartPage());
  53. REQUIRE(memory);
  54. memset(memory, 0, PAGES_TO_BYTES(span->GetPageCount()));
  55. REQUIRE(page_heap.GetSpanMap(span->GetStartPage()) == span);
  56. REQUIRE(page_heap.GetSpanMap(span->GetLastPage()) == span);
  57. page_heap.RegisterSpan(span);
  58. for(size_t j = 1; i < span->GetPageCount(); ++j)
  59. {
  60. REQUIRE(page_heap.GetSpanMap(span->GetStartPage() + i) == span);
  61. }
  62. page_heap.DeallocateSpan(span);
  63. }
  64. bg::ShmFini();
  65. }
  66. TEST_CASE("RadixTree, 0x1000000LL - CLASS_MAX_BYTES ")
  67. {
  68. bg::ShmOptions opts(false, "Test", nullptr);
  69. opts.fix_vptr_on_init = false;
  70. REQUIRE(bg::ShmInit(opts) == true);
  71. bg::detail::PageHeap page_heap;
  72. for(size_t i = 0x1000000LL; i >= CLASS_MAX_BYTES; i = i >> 10)
  73. {
  74. auto span = page_heap.AllocateSpan(BYTES_TO_PAGES(i));
  75. REQUIRE(span);
  76. void* memory = (void*)PAGES_TO_BYTES(span->GetStartPage());
  77. REQUIRE(memory);
  78. memset(memory, 0, PAGES_TO_BYTES(span->GetPageCount()));
  79. REQUIRE(page_heap.GetSpanMap(span->GetStartPage()) == span);
  80. REQUIRE(page_heap.GetSpanMap(span->GetLastPage()) == span);
  81. page_heap.DeallocateSpan(span);
  82. }
  83. bg::ShmFini();
  84. }