Advertisement
Guest User

Untitled

a guest
Apr 25th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. #include <iostream>
  2. #include <memory>
  3. #include <vector>
  4. #include <cstdlib>
  5. #include <list>
  6. #include <ctime>
  7.  
  8. const std::size_t block_size = 1 << 11;
  9. using std::vector;
  10.  
  11. class Block;
  12.  
  13. class SharedAllocator {
  14. private:
  15. Block* LastBlock;
  16. public:
  17. SharedAllocator() {
  18. LastBlock = nullptr;
  19. }
  20.  
  21. ~SharedAllocator() {
  22. delete LastBlock;
  23. }
  24.  
  25. template <typename T>
  26. T* allocate(std::size_t n) {
  27. if (LastBlock == nullptr) {
  28. Block* b = new Block(n * sizeof(T));
  29. LastBlock = b;
  30. return reinterpret_cast<T*>(LastBlock->allocate(n * sizeof(T)));
  31. } else if (LastBlock->unused_memory n * sizeof(T)) {
  32. Block* b = new Block(n * sizeof(T));
  33. blocks.push_back(b);
  34. return reinterpret_cast<T*>(blocks.back()->allocate(n * sizeof(T)));
  35. } else {
  36. return reinterpret_cast<T*>(blocks.back()->allocate(n * sizeof(T)));
  37. }
  38. }
  39.  
  40. void deallocate() {
  41. return;
  42. }
  43. };
  44.  
  45.  
  46. template <typename T>
  47. class StackAllocator : public std::allocator<T> {
  48. public:
  49. typedef T value_type;
  50. typedef T* pointer;
  51. typedef const T* const_pointer;
  52. typedef T& reference;
  53. typedef const T& const_reference;
  54. typedef std::size_t size_type;
  55. typedef std::ptrdiff_t difference_type;
  56. template< class U > struct rebind { typedef StackAllocator<U> other; };
  57.  
  58. StackAllocator(): m_allocator(std::make_shared<SharedAllocator>()) {
  59. }
  60.  
  61.  
  62. template<class U>
  63. StackAllocator(const StackAllocator<U>& other) {
  64. m_allocator = other.m_allocator;
  65. }
  66.  
  67. ~StackAllocator() {
  68.  
  69. }
  70.  
  71. pointer allocate(size_type n, const_pointer hint = 0) {
  72. return m_allocator->allocate<T>(n);
  73. }
  74.  
  75. void deallocate(pointer p, size_type n) {
  76. return;
  77. }
  78.  
  79. std::shared_ptr<SharedAllocator> m_allocator;
  80. };
  81.  
  82. class Block {
  83. public:
  84. typedef char* pointer;
  85. typedef std::size_t size_type;
  86. friend class SharedAllocator;
  87. Block(const size_type n) {
  88. unused_memory = n;
  89. used_memory = 0;
  90. current_memory_pointer = reinterpret_cast<char*>(std::malloc(n));
  91. }
  92.  
  93. ~Block() {
  94. std::free(current_memory_pointer);
  95. }
  96.  
  97. pointer get_current_place() {
  98. return current_memory_pointer + used_memory;
  99. }
  100.  
  101. char* allocate(size_type n) {
  102. pointer p = get_current_place();
  103. unused_memory -= n;
  104. used_memory += n;
  105. return p;
  106. }
  107.  
  108. void deallocate(size_type n) {
  109. return;
  110. }
  111.  
  112. private:
  113. pointer current_memory_pointer;
  114. size_type unused_memory;
  115. size_type used_memory;
  116. Block* previous;
  117. };
  118.  
  119. int main() {
  120. std::list<int, StackAllocator<int>> l;
  121.  
  122. clock_t t1, t2;
  123. t1 = clock();
  124. for (int i = 0; i < 1000000; ++i) {
  125. l.push_back(1);
  126. }
  127. t1 = clock() - t1;
  128. std::cout << (double) t1 / CLOCKS_PER_SEC << "\n";
  129.  
  130.  
  131. std::list<int, std::allocator<int>> l2;
  132. t2 = clock();
  133. for (int i = 0; i < 1000000; ++i) {
  134. l2.push_back(1);
  135. }
  136. t2 = clock() - t2;
  137.  
  138. std::cout << (double) t2 / CLOCKS_PER_SEC;
  139. system("pause");
  140. return 0;
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement