Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. #include <stack>
  2. #include <iostream>
  3.  
  4. template<typename Tp>
  5. class FixedAllocator {
  6.  
  7. public:
  8. explicit FixedAllocator(std::uint64_t page_size):
  9. page_allocator_(page_size), page_size_(page_size) {
  10. }
  11.  
  12. Tp* Allocate() {
  13. Tp* ptr;
  14. if (container.empty()) {
  15. ptr = static_cast<Tp*>(page_allocator_.Allocate());
  16. for (size_t i = 0; i < page_size_; i++) {
  17. ptr += i;
  18. container.push(ptr);
  19. }
  20. }
  21. ptr = container.top();
  22. container.pop();
  23. return ptr;
  24. }
  25.  
  26. void Deallocate(Tp* p) {
  27. container.push(p);
  28. }
  29.  
  30. const PageAllocator& InnerAllocator() const noexcept {
  31. return page_allocator_;
  32. }
  33.  
  34. private:
  35. std::stack <Tp*> container;
  36. PageAllocator page_allocator_;
  37. size_t page_size_;
  38. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement