Advertisement
ReF01L

Untitled

Dec 7th, 2019
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.26 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. explicit PageAllocator(std::uint64_t page_size) : page_size_(page_size) {}
  10.  
  11. ~PageAllocator() {
  12. for (void* page : pages_)
  13. ::operator delete(page);
  14. }
  15.  
  16. void* Allocate() {
  17. void* page = ::operator new(page_size_);
  18. pages_.push_back(page);
  19. return page;
  20. }
  21.  
  22. std::uint64_t Allocated() const noexcept {
  23. return pages_.size();
  24. }
  25. };
  26.  
  27. template<typename Tp>
  28. class FixedAllocator {
  29.  
  30. public:
  31. explicit FixedAllocator(std::uint64_t page_size) : page_allocator_(page_size), _page_size(page_size) {
  32. memory = page_allocator_.Allocate();
  33. for (size_t i = 0; i < page_size; i++)
  34. mem_block.push_back(static_cast<Tp*>(memory));
  35. }
  36.  
  37. Tp* Allocate() {
  38. if (mem_block.empty()) {
  39. memory = page_allocator_.Allocate();
  40.  
  41. for (size_t i = 0; i < _page_size; i++)
  42. mem_block.push_back(static_cast<Tp*>(memory));
  43. }
  44.  
  45. Tp* tmp = mem_block.back();
  46. mem_block.pop_back();
  47. return tmp;
  48. }
  49.  
  50. void Deallocate(Tp* p) {
  51. mem_block.push_back(p);
  52. }
  53.  
  54. const PageAllocator& InnerAllocator() const noexcept {
  55. return page_allocator_;
  56. }
  57.  
  58. private:
  59. PageAllocator page_allocator_;
  60. std::vector<Tp *> mem_block;
  61. std::uint64_t _page_size;
  62. void* memory;
  63. };
  64.  
  65. int main()
  66. {
  67. srand(666);
  68.  
  69. const auto require_allocs = [](std::uint64_t expected, std::uint64_t actual) -> void {
  70. if (expected == actual)
  71. return;
  72. std::cout << "Invalid count of page allocations" << std::endl;
  73. std::cout << "Expected: " << expected << std::endl;
  74. std::cout << "You have: " << actual << std::endl;
  75. std::exit(0);
  76. };
  77.  
  78. {
  79. FixedAllocator<int> a(1);
  80. auto p1 = a.Allocate();
  81. auto p2 = a.Allocate();
  82. a.Deallocate(p1);
  83. a.Deallocate(p2);
  84. p1 = a.Allocate();
  85. p2 = a.Allocate();
  86. require_allocs(2, a.InnerAllocator().Allocated());
  87. }
  88.  
  89. {
  90. FixedAllocator<std::string> a(10);
  91. for (int i = 0; i < 10; ++i)
  92. a.Allocate();
  93. require_allocs(1, a.InnerAllocator().Allocated());
  94. }
  95.  
  96. {
  97. FixedAllocator<std::string> a(100);
  98. for (int i = 0; i < 50; ++i)
  99. a.Deallocate(a.Allocate());
  100. require_allocs(1, a.InnerAllocator().Allocated());
  101. }
  102.  
  103. {
  104. FixedAllocator<std::string> a(1);
  105. for (int i = 0; i < 5; ++i)
  106. a.Deallocate(a.Allocate());
  107. require_allocs(1, a.InnerAllocator().Allocated());
  108. }
  109.  
  110. {
  111. FixedAllocator<std::string> a(1);
  112. for (int i = 0; i < 50; ++i)
  113. {
  114. a.Allocate();
  115. a.Deallocate(a.Allocate());
  116. }
  117. require_allocs(51, a.InnerAllocator().Allocated());
  118. }
  119.  
  120. {
  121. FixedAllocator<std::string> a(100);
  122. for (int i = 0; i < 500; ++i)
  123. {
  124. a.Allocate();
  125. a.Deallocate(a.Allocate());
  126. }
  127. require_allocs(6, a.InnerAllocator().Allocated());
  128. }
  129.  
  130. {
  131. FixedAllocator<std::string> a(100);
  132. for (int i = 0; i < 1000; ++i)
  133. {
  134. a.Allocate();
  135. a.Deallocate(a.Allocate());
  136. }
  137. require_allocs(11, a.InnerAllocator().Allocated());
  138. }
  139.  
  140. {
  141. FixedAllocator<std::string> a(1000000);
  142. for (int i = 0; i < 1000000; ++i)
  143. {
  144. a.Allocate();
  145. a.Deallocate(a.Allocate());
  146. }
  147. require_allocs(2, a.InnerAllocator().Allocated());
  148. }
  149.  
  150. {
  151. FixedAllocator<char> a(100);
  152. std::vector<char*> store;
  153. for (int i = 0; i < 100; ++i)
  154. store.push_back(a.Allocate());
  155. for (int i = 0; i < 10000; ++i)
  156. {
  157. const int rnd = rand() % 100;
  158. a.Deallocate(store[rnd]);
  159. if (a.Allocate() != store[rnd])
  160. {
  161. std::cout << "Invalid allocation result" << std::endl;
  162. return 0;
  163. }
  164. }
  165. }
  166.  
  167. std::cout << "GOOD" << std::endl;
  168. return 0;
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement