Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. class PageAllocator {
  5. const std::uint64_t page_size_;
  6. std::vector<void*> pages_;
  7.  
  8. public:
  9. PageAllocator(std::uint64_t page_size) : page_size_(page_size) {
  10. }
  11.  
  12. ~PageAllocator() {
  13. for (void* page : pages_)
  14. ::operator delete(page);
  15. }
  16.  
  17. void* Allocate() {
  18. void* page = ::operator new(page_size_);
  19. pages_.push_back(page);
  20. return page;
  21. }
  22.  
  23. std::uint64_t Allocated() const noexcept {
  24. return pages_.size();
  25. }
  26. };
  27.  
  28. #include "fixed_allocator.h"
  29.  
  30. int main()
  31. {
  32. srand(666);
  33.  
  34. const auto require_allocs = [](std::uint64_t expected, std::uint64_t actual) -> void {
  35. if (expected == actual)
  36. return;
  37. std::cout << "Invalid count of page allocations" << std::endl;
  38. std::cout << "Expected: " << expected << std::endl;
  39. std::cout << "You have: " << actual << std::endl;
  40. std::exit(0);
  41. };
  42.  
  43. {
  44. FixedAllocator<int> a(1);
  45. auto p1 = a.Allocate();
  46. auto p2 = a.Allocate();
  47. a.Deallocate(p1);
  48. a.Deallocate(p2);
  49. p1 = a.Allocate();
  50. p2 = a.Allocate();
  51. require_allocs(2, a.InnerAllocator().Allocated());
  52. }
  53.  
  54. {
  55. FixedAllocator<std::string> a(10);
  56. for (int i = 0; i < 10; ++i)
  57. a.Allocate();
  58. require_allocs(1, a.InnerAllocator().Allocated());
  59. }
  60.  
  61. {
  62. FixedAllocator<std::string> a(100);
  63. for (int i = 0; i < 50; ++i)
  64. a.Deallocate(a.Allocate());
  65. require_allocs(1, a.InnerAllocator().Allocated());
  66. }
  67.  
  68. {
  69. FixedAllocator<std::string> a(1);
  70. for (int i = 0; i < 5; ++i)
  71. a.Deallocate(a.Allocate());
  72. require_allocs(1, a.InnerAllocator().Allocated());
  73. }
  74.  
  75. {
  76. FixedAllocator<std::string> a(1);
  77. for (int i = 0; i < 50; ++i)
  78. {
  79. a.Allocate();
  80. a.Deallocate(a.Allocate());
  81. }
  82. require_allocs(51, a.InnerAllocator().Allocated());
  83. }
  84.  
  85. {
  86. FixedAllocator<std::string> a(100);
  87. for (int i = 0; i < 500; ++i)
  88. {
  89. a.Allocate();
  90. a.Deallocate(a.Allocate());
  91. }
  92. require_allocs(6, a.InnerAllocator().Allocated());
  93. }
  94.  
  95. {
  96. FixedAllocator<std::string> a(100);
  97. for (int i = 0; i < 1000; ++i)
  98. {
  99. a.Allocate();
  100. a.Deallocate(a.Allocate());
  101. }
  102. require_allocs(11, a.InnerAllocator().Allocated());
  103. }
  104.  
  105. {
  106. FixedAllocator<std::string> a(1000000);
  107. for (int i = 0; i < 1000000; ++i)
  108. {
  109. a.Allocate();
  110. a.Deallocate(a.Allocate());
  111. }
  112. require_allocs(2, a.InnerAllocator().Allocated());
  113. }
  114.  
  115. {
  116. FixedAllocator<char> a(100);
  117. std::vector<char*> store;
  118. for (int i = 0; i < 100; ++i)
  119. store.push_back(a.Allocate());
  120. for (int i = 0; i < 10000; ++i)
  121. {
  122. const int rnd = rand() % 100;
  123. a.Deallocate(store[rnd]);
  124. if (a.Allocate() != store[rnd])
  125. {
  126. std::cout << "Invalid allocation result" << std::endl;
  127. return 0;
  128. }
  129. }
  130. }
  131.  
  132. std::cout << "GOOD" << std::endl;
  133. return 0;
  134.  
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement