memory.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. #pragma once
  2. #include "common.h"
  3. namespace pkpy{
  4. struct LinkedListNode{
  5. LinkedListNode* prev;
  6. LinkedListNode* next;
  7. };
  8. template<typename T>
  9. struct DoubleLinkedList{
  10. static_assert(std::is_base_of_v<LinkedListNode, T>);
  11. int _size;
  12. LinkedListNode head;
  13. LinkedListNode tail;
  14. DoubleLinkedList(): _size(0){
  15. head.prev = nullptr;
  16. head.next = &tail;
  17. tail.prev = &head;
  18. tail.next = nullptr;
  19. }
  20. void push_back(T* node){
  21. node->prev = tail.prev;
  22. node->next = &tail;
  23. tail.prev->next = node;
  24. tail.prev = node;
  25. _size++;
  26. }
  27. void push_front(T* node){
  28. node->prev = &head;
  29. node->next = head.next;
  30. head.next->prev = node;
  31. head.next = node;
  32. _size++;
  33. }
  34. void pop_back(){
  35. #if DEBUG_MEMORY_POOL
  36. if(empty()) throw std::runtime_error("DoubleLinkedList::pop_back() called on empty list");
  37. #endif
  38. tail.prev->prev->next = &tail;
  39. tail.prev = tail.prev->prev;
  40. _size--;
  41. }
  42. void pop_front(){
  43. #if DEBUG_MEMORY_POOL
  44. if(empty()) throw std::runtime_error("DoubleLinkedList::pop_front() called on empty list");
  45. #endif
  46. head.next->next->prev = &head;
  47. head.next = head.next->next;
  48. _size--;
  49. }
  50. T* back() const {
  51. #if DEBUG_MEMORY_POOL
  52. if(empty()) throw std::runtime_error("DoubleLinkedList::back() called on empty list");
  53. #endif
  54. return static_cast<T*>(tail.prev);
  55. }
  56. T* front() const {
  57. #if DEBUG_MEMORY_POOL
  58. if(empty()) throw std::runtime_error("DoubleLinkedList::front() called on empty list");
  59. #endif
  60. return static_cast<T*>(head.next);
  61. }
  62. void erase(T* node){
  63. #if DEBUG_MEMORY_POOL
  64. if(empty()) throw std::runtime_error("DoubleLinkedList::erase() called on empty list");
  65. LinkedListNode* n = head.next;
  66. while(n != &tail){
  67. if(n == node) break;
  68. n = n->next;
  69. }
  70. if(n != node) throw std::runtime_error("DoubleLinkedList::erase() called on node not in the list");
  71. #endif
  72. node->prev->next = node->next;
  73. node->next->prev = node->prev;
  74. _size--;
  75. }
  76. void move_all_back(DoubleLinkedList<T>& other){
  77. if(other.empty()) return;
  78. other.tail.prev->next = &tail;
  79. tail.prev->next = other.head.next;
  80. other.head.next->prev = tail.prev;
  81. tail.prev = other.tail.prev;
  82. _size += other._size;
  83. other.head.next = &other.tail;
  84. other.tail.prev = &other.head;
  85. other._size = 0;
  86. }
  87. bool empty() const {
  88. #if DEBUG_MEMORY_POOL
  89. if(size() == 0){
  90. if(head.next != &tail || tail.prev != &head){
  91. throw std::runtime_error("DoubleLinkedList::size() returned 0 but the list is not empty");
  92. }
  93. return true;
  94. }
  95. #endif
  96. return _size == 0;
  97. }
  98. int size() const { return _size; }
  99. void apply(std::function<void(T*)> 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. bool dirty;
  120. Arena(): _free_list_size(__MaxBlocks), dirty(false){
  121. for(int i=0; i<__MaxBlocks; i++){
  122. _blocks[i].arena = this;
  123. _free_list[i] = &_blocks[i];
  124. }
  125. }
  126. bool empty() const { return _free_list_size == 0; }
  127. bool full() const { return _free_list_size == __MaxBlocks; }
  128. void tidy(){
  129. #if DEBUG_MEMORY_POOL
  130. if(!full()) throw std::runtime_error("Arena::tidy() called on non-full arena");
  131. #endif
  132. std::sort(_free_list, _free_list+__MaxBlocks);
  133. }
  134. Block* alloc(){
  135. #if DEBUG_MEMORY_POOL
  136. if(empty()) throw std::runtime_error("Arena::alloc() called on empty arena");
  137. #endif
  138. _free_list_size--;
  139. return _free_list[_free_list_size];
  140. }
  141. void dealloc(Block* block){
  142. #if DEBUG_MEMORY_POOL
  143. if(full()) throw std::runtime_error("Arena::dealloc() called on full arena");
  144. #endif
  145. _free_list[_free_list_size] = block;
  146. _free_list_size++;
  147. }
  148. };
  149. DoubleLinkedList<Arena> _arenas;
  150. DoubleLinkedList<Arena> _empty_arenas;
  151. template<typename __T>
  152. void* alloc() { return alloc(sizeof(__T)); }
  153. void* alloc(size_t size){
  154. #if 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. arena->dirty = true;
  171. _empty_arenas.push_back(arena);
  172. }
  173. return p;
  174. }
  175. void dealloc(void* p){
  176. #if DEBUG_NO_MEMORY_POOL
  177. free(p);
  178. return;
  179. #endif
  180. #if 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. if(arena->full() && arena->dirty){
  195. _arenas.erase(arena);
  196. delete arena;
  197. }
  198. }
  199. }
  200. }
  201. ~MemoryPool(){
  202. _arenas.apply([](Arena* arena){ delete arena; });
  203. _empty_arenas.apply([](Arena* arena){ delete arena; });
  204. }
  205. };
  206. inline MemoryPool<64> pool64;
  207. inline MemoryPool<128> pool128;
  208. // inline MemoryPool<256> pool256;
  209. #define SP_MALLOC(size) pool64.alloc(size)
  210. #define SP_FREE(p) pool64.dealloc(p)
  211. template <typename T>
  212. struct shared_ptr {
  213. int* counter;
  214. T* _t() const noexcept { return (T*)(counter + 1); }
  215. void _inc_counter() { if(counter) ++(*counter); }
  216. void _dec_counter() { if(counter && --(*counter) == 0) {((T*)(counter + 1))->~T(); SP_FREE(counter);} }
  217. public:
  218. shared_ptr() : counter(nullptr) {}
  219. shared_ptr(int* counter) : counter(counter) {}
  220. shared_ptr(const shared_ptr& other) : counter(other.counter) {
  221. _inc_counter();
  222. }
  223. shared_ptr(shared_ptr&& other) noexcept : counter(other.counter) {
  224. other.counter = nullptr;
  225. }
  226. ~shared_ptr() { _dec_counter(); }
  227. bool operator==(const shared_ptr& other) const { return counter == other.counter; }
  228. bool operator!=(const shared_ptr& other) const { return counter != other.counter; }
  229. bool operator<(const shared_ptr& other) const { return counter < other.counter; }
  230. bool operator>(const shared_ptr& other) const { return counter > other.counter; }
  231. bool operator<=(const shared_ptr& other) const { return counter <= other.counter; }
  232. bool operator>=(const shared_ptr& other) const { return counter >= other.counter; }
  233. bool operator==(std::nullptr_t) const { return counter == nullptr; }
  234. bool operator!=(std::nullptr_t) const { return counter != nullptr; }
  235. shared_ptr& operator=(const shared_ptr& other) {
  236. _dec_counter();
  237. counter = other.counter;
  238. _inc_counter();
  239. return *this;
  240. }
  241. shared_ptr& operator=(shared_ptr&& other) noexcept {
  242. _dec_counter();
  243. counter = other.counter;
  244. other.counter = nullptr;
  245. return *this;
  246. }
  247. T& operator*() const { return *_t(); }
  248. T* operator->() const { return _t(); }
  249. T* get() const { return _t(); }
  250. int use_count() const {
  251. return counter ? *counter : 0;
  252. }
  253. void reset(){
  254. _dec_counter();
  255. counter = nullptr;
  256. }
  257. };
  258. template <typename T, typename... Args>
  259. shared_ptr<T> make_sp(Args&&... args) {
  260. int* p = (int*)SP_MALLOC(sizeof(int) + sizeof(T));
  261. *p = 1;
  262. new(p+1) T(std::forward<Args>(args)...);
  263. return shared_ptr<T>(p);
  264. }
  265. }; // namespace pkpy