Advertisement
Guest User

Untitled

a guest
May 22nd, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.16 KB | None | 0 0
  1. #include <iostream>
  2.  
  3.  
  4. template <typename T> class mvector
  5. {
  6.  
  7. public:
  8. T *array;
  9. int size = 0, capasity = 0;
  10. mvector(int __cap = 2)
  11. {
  12. array = new T[__cap];
  13. size = 0;
  14. capasity = __cap;
  15. }
  16. ~mvector()
  17. {
  18. delete[] array;
  19. }
  20. void push_back(const T &arg)
  21. {
  22. if(size == capasity)
  23. {
  24. T *array2 = new T[2*capasity];
  25. T *array3 = array;
  26. for(int i = 0; i < capasity; ++i)
  27. {
  28. array2[i] = array[i];
  29. }
  30. array = array2;
  31. delete[] array3;
  32. capasity*=2;
  33. array[size] = arg;
  34. size+=1;
  35.  
  36. }
  37. else
  38. {
  39. array[size] = arg;
  40. size+=1;
  41. }
  42. }
  43.  
  44.  
  45.  
  46. };
  47.  
  48. template <typename T> T max(const T &a, const T &b)
  49. {
  50. if(a > b)
  51. return a;
  52. else
  53. return b;
  54. }
  55.  
  56. template <typename T>
  57. class Node
  58. {
  59. public:
  60. T value;
  61. Node *mask;
  62. explicit Node(const T &val, Node *prev = nullptr, Node *next = nullptr)
  63. {
  64. value = val;
  65. mask = (Node *)(reinterpret_cast<uintptr_t>(prev) ^ reinterpret_cast<uintptr_t>(next));
  66. }
  67. explicit Node(T&& _val, Node* prev = nullptr, Node *next = nullptr)
  68. {
  69. value = _val;
  70. mask = (Node *)(reinterpret_cast<uintptr_t>(prev) ^ reinterpret_cast<uintptr_t>(next));
  71. }
  72. void update_mask(Node* prev = nullptr, Node *next = nullptr)
  73. {
  74. mask = (Node *)(reinterpret_cast<uintptr_t>(prev) ^ reinterpret_cast<uintptr_t>(next));
  75. }
  76. void update_mask_front(Node *next = nullptr)
  77. {
  78. mask = (Node *)(reinterpret_cast<uintptr_t>(mask) ^ reinterpret_cast<uintptr_t>(next));
  79. }
  80. void update_mask_prev(Node *prev = nullptr)
  81. {
  82.  
  83. mask = (Node *)(reinterpret_cast<uintptr_t>(prev) ^ reinterpret_cast<uintptr_t>(mask));
  84. }
  85. ~Node()
  86. {
  87.  
  88. }
  89. };
  90.  
  91.  
  92. template <typename T> class TrapStackAllocator
  93. {
  94. public:
  95. typedef T value_type;
  96. typedef size_t size_type;
  97. typedef ptrdiff_t difference_type;
  98. typedef value_type* pointer;
  99. typedef const value_type* const_pointer;
  100. typedef value_type* reference;
  101. typedef const value_type* const_reference;
  102. mvector <value_type *> page ;
  103. value_type *cur_point;
  104. size_t availible_space = 0;
  105. mvector <reference > refs;
  106. TrapStackAllocator() {
  107. }
  108. ~TrapStackAllocator() {
  109. for(int i = 0; i < page.size; ++i)
  110. {
  111. free(page.array[i]);
  112. }
  113.  
  114. }
  115. pointer allocate(size_type _Count)
  116. {
  117. if(availible_space >= _Count)
  118. {
  119. //std::cout << "JJJJJ";
  120. cur_point+=_Count;
  121. availible_space-=_Count;
  122. return (cur_point - _Count);
  123.  
  124. }
  125. void *q = ::operator new((max(_Count, (size_t)10000) * sizeof (value_type)));
  126. availible_space = max(_Count, (size_t)10000) - _Count;
  127. cur_point = (pointer)q+_Count;
  128. page.push_back((pointer)q);
  129. //std::cout << page.capasity << std::endl;
  130. return (pointer)q;
  131.  
  132. }
  133. void *Alloc(size_t _Count)
  134. {
  135. if(availible_space >= _Count)
  136. {
  137. //std::cout << "JJJJJ";
  138. cur_point+=_Count;
  139. availible_space-=_Count;
  140. return (cur_point - _Count);
  141.  
  142. }
  143. void *q = new(::malloc((max(_Count, (size_t)10000)))) void *();
  144. availible_space = max(_Count, (size_t)10000) - _Count;
  145. cur_point = (T*)q+_Count;
  146. page.push_back((T*)q);
  147. //std::cout << page.capasity << std::endl;
  148. return q;
  149. }
  150. void deallocate(pointer _Ptr, size_type)
  151. {
  152.  
  153. }
  154.  
  155. };
  156.  
  157. template <typename T> class StackAllocator
  158. {
  159. public:
  160. typedef T value_type;
  161. typedef size_t size_type;
  162. typedef ptrdiff_t difference_type;
  163. typedef value_type* pointer;
  164. typedef const value_type* const_pointer;
  165. typedef value_type* reference;
  166. typedef const value_type* const_reference;
  167. StackAllocator()
  168. {
  169. true_alloc = std::make_shared<TrapStackAllocator<T>>();
  170. }
  171. StackAllocator(const StackAllocator &b)
  172. {
  173. true_alloc = b.true_alloc;
  174. }
  175. StackAllocator& operator=(const StackAllocator& other)
  176. {
  177. true_alloc = other.true_alloc;
  178. }
  179. template< class U > struct rebind {
  180. typedef StackAllocator<U> other;
  181. };
  182. pointer allocate(size_type _Count)
  183. {
  184. return true_alloc->allocate(_Count);
  185. }
  186. void deallocate(T* place)
  187. {
  188.  
  189. }
  190. void *Alloc(size_t size)
  191. {
  192. return true_alloc->Alloc(size);
  193. }
  194. void Free(void *ptr)
  195. {
  196.  
  197. }
  198. ~StackAllocator() = default;
  199. private:
  200. std::shared_ptr<TrapStackAllocator<T>> true_alloc;
  201. };
  202.  
  203.  
  204.  
  205. #pragma pack(push, 1)
  206. class IMemoryManager
  207. {
  208. public:
  209. int num;
  210. virtual ~IMemoryManager() = default;
  211. virtual void *Alloc(size_t size){};
  212. virtual void Free(void *ptr){};
  213. };
  214.  
  215.  
  216.  
  217. class StackMemoryManager:public IMemoryManager
  218. {
  219. public:
  220. StackMemoryManager()
  221. {
  222. num = 3;
  223. }
  224. StackAllocator<int> a;
  225. void *Alloc(size_t size) override
  226. {
  227. return a.Alloc(size);
  228. }
  229. void Free(void *ptr) override
  230. {
  231. a.Free(ptr);
  232. }
  233. };
  234.  
  235. class AnotherAlloc: public IMemoryManager
  236. {
  237. public:
  238. AnotherAlloc() {
  239. num = 2;
  240. }
  241. void *Alloc(size_t size) override
  242. {
  243. std::cout << "KKK";
  244. return malloc(size);
  245. }
  246. void Free(void *ptr) override
  247. {
  248. std::cout << "JJJ";
  249. free(ptr);
  250. }
  251. };
  252.  
  253.  
  254. template <class T>
  255. class CAllocatedOn
  256. {
  257. void* operator new(std::size_t size)
  258. {
  259. return T::Alloc(size);
  260. }
  261. void operator delete(void *ptr)
  262. {
  263. T::Free(ptr);
  264. }
  265. };
  266.  
  267. constexpr size_t diff = max(sizeof(IMemoryManager *), alignof(max_align_t));
  268.  
  269. class RuntimeHeap: public IMemoryManager
  270. {
  271. public:
  272. RuntimeHeap(){num = 1;}
  273. void *Alloc(size_t size) override
  274. {
  275. return malloc(size);
  276. }
  277. void Free(void *ptr) override
  278. {
  279. free(ptr);
  280. }
  281. };
  282.  
  283. class CMemoryManagerSwitcher;
  284.  
  285. class CurrentMemoryManager
  286. {
  287.  
  288. public:
  289. IMemoryManager *cur_Alloc;
  290. int now = 0;
  291. CurrentMemoryManager()
  292. {
  293. cur_Alloc = new(::malloc(sizeof(RuntimeHeap))) RuntimeHeap();
  294. now = 1;
  295.  
  296. }
  297. void *Alloc(size_t size)
  298. {
  299.  
  300. void *r = cur_Alloc->Alloc(size + sizeof(max_align_t));
  301. size_t e = 0;
  302. std::align(alignof(max_align_t), size + diff, r, e);
  303. IMemoryManager **cp = (IMemoryManager **)r;
  304. cp[0] = cur_Alloc;
  305. return (void *)(static_cast<char *>(r) + diff);
  306. }
  307. void Free(void *ptr)
  308. {
  309. ptr = (void *)(static_cast<char *>(ptr) - diff);
  310. IMemoryManager **w = (IMemoryManager**)(ptr);
  311. w[0]->Free(ptr);
  312. }
  313. };
  314.  
  315. CurrentMemoryManager f;
  316.  
  317. class CMemoryManagerSwitcher
  318. {
  319. public:
  320. CMemoryManagerSwitcher(IMemoryManager *oh)
  321. {
  322. f.cur_Alloc = oh;
  323. f.now = oh->num;
  324. }
  325. ~CMemoryManagerSwitcher()
  326. {
  327.  
  328. }
  329. };
  330.  
  331.  
  332. void *allocc(size_t size) {
  333.  
  334. static RuntimeHeap cur_Alloc;
  335. void *r = cur_Alloc.Alloc(size + sizeof(max_align_t));
  336. size_t e = 0;
  337. std::align(alignof(max_align_t), size + diff, r, e);
  338. IMemoryManager **cp = (IMemoryManager **)r;
  339. cp[0] = &cur_Alloc;
  340. return (void *)((char *)(r) + diff);
  341. }
  342.  
  343. #pragma pack(pop)
  344. void *operator new(size_t size)
  345. {
  346. if(f.now == 0)
  347. {
  348. return allocc(size);
  349. }
  350. return f.Alloc(size);
  351. }
  352.  
  353. void *operator new(size_t size, const std::nothrow_t&) noexcept
  354. {
  355.  
  356. try
  357. {
  358. if(f.now == 0)
  359. {
  360. return allocc(size);
  361. }
  362. return f.Alloc(size);
  363. }
  364. catch (...)
  365. {
  366. void *p = 0;
  367. return p;
  368. }
  369.  
  370.  
  371. }
  372.  
  373. void operator delete(void *p) noexcept
  374. {
  375. f.Free(p);
  376. }
  377.  
  378. void operator delete(void *p, const std::nothrow_t&) noexcept
  379. {
  380. ::delete(p);
  381. }
  382.  
  383. void *operator new[](size_t size)
  384. {
  385. if(f.now == 0)
  386. {
  387. return allocc(size);
  388. }
  389. return f.Alloc(size);
  390. }
  391.  
  392. void *operator new[](size_t size, const std::nothrow_t&) noexcept
  393. {
  394.  
  395. try
  396. {
  397. if(f.now == 0)
  398. {
  399. return allocc(size);
  400. }
  401. return f.Alloc(size);
  402. }
  403. catch (...)
  404. {
  405. void *p = 0;
  406. return p;
  407. }
  408. }
  409.  
  410. void operator delete[](void *p) noexcept
  411. {
  412. // освобождение памяти, на которую указывает р
  413. f.Free(p);
  414. }
  415.  
  416. void operator delete[](void *p, const std::nothrow_t&) noexcept
  417. {
  418.  
  419. ::delete(p);
  420. }
  421.  
  422. int main() {
  423.  
  424. RuntimeHeap *d = new(::malloc(sizeof(RuntimeHeap))) RuntimeHeap();
  425. StackMemoryManager *c = new(::malloc(sizeof(StackMemoryManager))) StackMemoryManager();
  426. AnotherAlloc *b = new(::malloc(sizeof(AnotherAlloc))) AnotherAlloc();
  427. CMemoryManagerSwitcher aw(b);
  428. int *a = new int[100];
  429. CMemoryManagerSwitcher q(d);
  430. //aw.set_new(d);
  431. std::cout << a[0];
  432. delete[] a;
  433. return 0;
  434. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement