memory.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. #include "pocketpy/memory.h"
  2. namespace pkpy{
  3. struct LinkedListNode{
  4. LinkedListNode* prev;
  5. LinkedListNode* next;
  6. };
  7. template<typename T>
  8. struct DoubleLinkedList{
  9. static_assert(std::is_base_of_v<LinkedListNode, T>);
  10. int _size;
  11. LinkedListNode head;
  12. LinkedListNode tail;
  13. DoubleLinkedList(): _size(0){
  14. head.prev = nullptr;
  15. head.next = &tail;
  16. tail.prev = &head;
  17. tail.next = nullptr;
  18. }
  19. void push_back(T* node){
  20. node->prev = tail.prev;
  21. node->next = &tail;
  22. tail.prev->next = node;
  23. tail.prev = node;
  24. _size++;
  25. }
  26. void push_front(T* node){
  27. node->prev = &head;
  28. node->next = head.next;
  29. head.next->prev = node;
  30. head.next = node;
  31. _size++;
  32. }
  33. void pop_back(){
  34. #if PK_DEBUG_MEMORY_POOL
  35. if(empty()) throw std::runtime_error("DoubleLinkedList::pop_back() called on empty list");
  36. #endif
  37. tail.prev->prev->next = &tail;
  38. tail.prev = tail.prev->prev;
  39. _size--;
  40. }
  41. void pop_front(){
  42. #if PK_DEBUG_MEMORY_POOL
  43. if(empty()) throw std::runtime_error("DoubleLinkedList::pop_front() called on empty list");
  44. #endif
  45. head.next->next->prev = &head;
  46. head.next = head.next->next;
  47. _size--;
  48. }
  49. T* back() const {
  50. #if PK_DEBUG_MEMORY_POOL
  51. if(empty()) throw std::runtime_error("DoubleLinkedList::back() called on empty list");
  52. #endif
  53. return static_cast<T*>(tail.prev);
  54. }
  55. T* front() const {
  56. #if PK_DEBUG_MEMORY_POOL
  57. if(empty()) throw std::runtime_error("DoubleLinkedList::front() called on empty list");
  58. #endif
  59. return static_cast<T*>(head.next);
  60. }
  61. void erase(T* node){
  62. #if PK_DEBUG_MEMORY_POOL
  63. if(empty()) throw std::runtime_error("DoubleLinkedList::erase() called on empty list");
  64. LinkedListNode* n = head.next;
  65. while(n != &tail){
  66. if(n == node) break;
  67. n = n->next;
  68. }
  69. if(n != node) throw std::runtime_error("DoubleLinkedList::erase() called on node not in the list");
  70. #endif
  71. node->prev->next = node->next;
  72. node->next->prev = node->prev;
  73. _size--;
  74. }
  75. // void move_all_back(DoubleLinkedList<T>& other){
  76. // if(other.empty()) return;
  77. // other.tail.prev->next = &tail;
  78. // tail.prev->next = other.head.next;
  79. // other.head.next->prev = tail.prev;
  80. // tail.prev = other.tail.prev;
  81. // _size += other._size;
  82. // other.head.next = &other.tail;
  83. // other.tail.prev = &other.head;
  84. // other._size = 0;
  85. // }
  86. bool empty() const {
  87. #if PK_DEBUG_MEMORY_POOL
  88. if(size() == 0){
  89. if(head.next != &tail || tail.prev != &head){
  90. throw std::runtime_error("DoubleLinkedList::size() returned 0 but the list is not empty");
  91. }
  92. return true;
  93. }
  94. #endif
  95. return _size == 0;
  96. }
  97. int size() const { return _size; }
  98. template<typename Func>
  99. void apply(Func func){
  100. LinkedListNode* p = head.next;
  101. while(p != &tail){
  102. LinkedListNode* next = p->next;
  103. func(static_cast<T*>(p));
  104. p = next;
  105. }
  106. }
  107. };
  108. template<int __BlockSize=128>
  109. struct MemoryPool{
  110. static const size_t __MaxBlocks = 256*1024 / __BlockSize;
  111. struct Block{
  112. void* arena;
  113. char data[__BlockSize];
  114. };
  115. struct Arena: LinkedListNode{
  116. Block _blocks[__MaxBlocks];
  117. Block* _free_list[__MaxBlocks];
  118. int _free_list_size;
  119. Arena(): _free_list_size(__MaxBlocks) {
  120. for(int i=0; i<__MaxBlocks; i++){
  121. _blocks[i].arena = this;
  122. _free_list[i] = &_blocks[i];
  123. }
  124. }
  125. bool empty() const { return _free_list_size == 0; }
  126. bool full() const { return _free_list_size == __MaxBlocks; }
  127. size_t allocated_size() const{
  128. return __BlockSize * (__MaxBlocks - _free_list_size);
  129. }
  130. Block* alloc(){
  131. #if PK_DEBUG_MEMORY_POOL
  132. if(empty()) throw std::runtime_error("Arena::alloc() called on empty arena");
  133. #endif
  134. _free_list_size--;
  135. return _free_list[_free_list_size];
  136. }
  137. void dealloc(Block* block){
  138. #if PK_DEBUG_MEMORY_POOL
  139. if(full()) throw std::runtime_error("Arena::dealloc() called on full arena");
  140. #endif
  141. _free_list[_free_list_size] = block;
  142. _free_list_size++;
  143. }
  144. };
  145. MemoryPool() = default;
  146. MemoryPool(const MemoryPool&) = delete;
  147. MemoryPool& operator=(const MemoryPool&) = delete;
  148. MemoryPool(MemoryPool&&) = delete;
  149. MemoryPool& operator=(MemoryPool&&) = delete;
  150. DoubleLinkedList<Arena> _arenas;
  151. DoubleLinkedList<Arena> _empty_arenas;
  152. void* alloc(size_t size){
  153. PK_GLOBAL_SCOPE_LOCK();
  154. #if PK_DEBUG_NO_MEMORY_POOL
  155. return malloc(size);
  156. #endif
  157. if(size > __BlockSize){
  158. void* p = malloc(sizeof(void*) + size);
  159. memset(p, 0, sizeof(void*));
  160. return (char*)p + sizeof(void*);
  161. }
  162. if(_arenas.empty()){
  163. // std::cout << _arenas.size() << ',' << _empty_arenas.size() << ',' << _full_arenas.size() << std::endl;
  164. _arenas.push_back(new Arena());
  165. }
  166. Arena* arena = _arenas.back();
  167. void* p = arena->alloc()->data;
  168. if(arena->empty()){
  169. _arenas.pop_back();
  170. _empty_arenas.push_back(arena);
  171. }
  172. return p;
  173. }
  174. void dealloc(void* p){
  175. PK_GLOBAL_SCOPE_LOCK();
  176. #if PK_DEBUG_NO_MEMORY_POOL
  177. free(p);
  178. return;
  179. #endif
  180. #if PK_DEBUG_MEMORY_POOL
  181. if(p == nullptr) throw std::runtime_error("MemoryPool::dealloc() called on nullptr");
  182. #endif
  183. Block* block = (Block*)((char*)p - sizeof(void*));
  184. if(block->arena == nullptr){
  185. free(block);
  186. }else{
  187. Arena* arena = (Arena*)block->arena;
  188. if(arena->empty()){
  189. _empty_arenas.erase(arena);
  190. _arenas.push_front(arena);
  191. arena->dealloc(block);
  192. }else{
  193. arena->dealloc(block);
  194. }
  195. }
  196. }
  197. void shrink_to_fit(){
  198. PK_GLOBAL_SCOPE_LOCK();
  199. _arenas.apply([this](Arena* arena){
  200. if(arena->full()){
  201. _arenas.erase(arena);
  202. delete arena;
  203. }
  204. });
  205. }
  206. ~MemoryPool(){
  207. _arenas.apply([](Arena* arena){ delete arena; });
  208. _empty_arenas.apply([](Arena* arena){ delete arena; });
  209. }
  210. };
  211. static MemoryPool<64> pool64;
  212. static MemoryPool<128> pool128;
  213. void* pool64_alloc(size_t size){ return pool64.alloc(size); }
  214. void pool64_dealloc(void* p){ pool64.dealloc(p); }
  215. void* pool128_alloc(size_t size){ return pool128.alloc(size); }
  216. void pool128_dealloc(void* p){ pool128.dealloc(p); }
  217. void pools_shrink_to_fit(){
  218. pool64.shrink_to_fit();
  219. pool128.shrink_to_fit();
  220. }
  221. }