Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- template <typename T> class mvector
- {
- public:
- T *array;
- int size = 0, capasity = 0;
- mvector(int __cap = 2)
- {
- array = new T[__cap];
- size = 0;
- capasity = __cap;
- }
- ~mvector()
- {
- delete[] array;
- }
- void push_back(const T &arg)
- {
- if(size == capasity)
- {
- T *array2 = new T[2*capasity];
- T *array3 = array;
- for(int i = 0; i < capasity; ++i)
- {
- array2[i] = array[i];
- }
- array = array2;
- delete[] array3;
- capasity*=2;
- array[size] = arg;
- size+=1;
- }
- else
- {
- array[size] = arg;
- size+=1;
- }
- }
- };
- template <typename T> T max(const T &a, const T &b)
- {
- if(a > b)
- return a;
- else
- return b;
- }
- template <typename T>
- class Node
- {
- public:
- T value;
- Node *mask;
- explicit Node(const T &val, Node *prev = nullptr, Node *next = nullptr)
- {
- value = val;
- mask = (Node *)(reinterpret_cast<uintptr_t>(prev) ^ reinterpret_cast<uintptr_t>(next));
- }
- explicit Node(T&& _val, Node* prev = nullptr, Node *next = nullptr)
- {
- value = _val;
- mask = (Node *)(reinterpret_cast<uintptr_t>(prev) ^ reinterpret_cast<uintptr_t>(next));
- }
- void update_mask(Node* prev = nullptr, Node *next = nullptr)
- {
- mask = (Node *)(reinterpret_cast<uintptr_t>(prev) ^ reinterpret_cast<uintptr_t>(next));
- }
- void update_mask_front(Node *next = nullptr)
- {
- mask = (Node *)(reinterpret_cast<uintptr_t>(mask) ^ reinterpret_cast<uintptr_t>(next));
- }
- void update_mask_prev(Node *prev = nullptr)
- {
- mask = (Node *)(reinterpret_cast<uintptr_t>(prev) ^ reinterpret_cast<uintptr_t>(mask));
- }
- ~Node()
- {
- }
- };
- template <typename T> class TrapStackAllocator
- {
- public:
- typedef T value_type;
- typedef size_t size_type;
- typedef ptrdiff_t difference_type;
- typedef value_type* pointer;
- typedef const value_type* const_pointer;
- typedef value_type* reference;
- typedef const value_type* const_reference;
- mvector <value_type *> page ;
- value_type *cur_point;
- size_t availible_space = 0;
- mvector <reference > refs;
- TrapStackAllocator() {
- }
- ~TrapStackAllocator() {
- for(int i = 0; i < page.size; ++i)
- {
- free(page.array[i]);
- }
- }
- pointer allocate(size_type _Count)
- {
- if(availible_space >= _Count)
- {
- //std::cout << "JJJJJ";
- cur_point+=_Count;
- availible_space-=_Count;
- return (cur_point - _Count);
- }
- void *q = ::operator new((max(_Count, (size_t)10000) * sizeof (value_type)));
- availible_space = max(_Count, (size_t)10000) - _Count;
- cur_point = (pointer)q+_Count;
- page.push_back((pointer)q);
- //std::cout << page.capasity << std::endl;
- return (pointer)q;
- }
- void *Alloc(size_t _Count)
- {
- if(availible_space >= _Count)
- {
- //std::cout << "JJJJJ";
- cur_point+=_Count;
- availible_space-=_Count;
- return (cur_point - _Count);
- }
- void *q = new(::malloc((max(_Count, (size_t)10000)))) void *();
- availible_space = max(_Count, (size_t)10000) - _Count;
- cur_point = (T*)q+_Count;
- page.push_back((T*)q);
- //std::cout << page.capasity << std::endl;
- return q;
- }
- void deallocate(pointer _Ptr, size_type)
- {
- }
- };
- template <typename T> class StackAllocator
- {
- public:
- typedef T value_type;
- typedef size_t size_type;
- typedef ptrdiff_t difference_type;
- typedef value_type* pointer;
- typedef const value_type* const_pointer;
- typedef value_type* reference;
- typedef const value_type* const_reference;
- StackAllocator()
- {
- true_alloc = std::make_shared<TrapStackAllocator<T>>();
- }
- StackAllocator(const StackAllocator &b)
- {
- true_alloc = b.true_alloc;
- }
- StackAllocator& operator=(const StackAllocator& other)
- {
- true_alloc = other.true_alloc;
- }
- template< class U > struct rebind {
- typedef StackAllocator<U> other;
- };
- pointer allocate(size_type _Count)
- {
- return true_alloc->allocate(_Count);
- }
- void deallocate(T* place)
- {
- }
- void *Alloc(size_t size)
- {
- return true_alloc->Alloc(size);
- }
- void Free(void *ptr)
- {
- }
- ~StackAllocator() = default;
- private:
- std::shared_ptr<TrapStackAllocator<T>> true_alloc;
- };
- #pragma pack(push, 1)
- class IMemoryManager
- {
- public:
- int num;
- virtual ~IMemoryManager() = default;
- virtual void *Alloc(size_t size){};
- virtual void Free(void *ptr){};
- };
- class StackMemoryManager:public IMemoryManager
- {
- public:
- StackMemoryManager()
- {
- num = 3;
- }
- StackAllocator<int> a;
- void *Alloc(size_t size) override
- {
- return a.Alloc(size);
- }
- void Free(void *ptr) override
- {
- a.Free(ptr);
- }
- };
- class AnotherAlloc: public IMemoryManager
- {
- public:
- AnotherAlloc() {
- num = 2;
- }
- void *Alloc(size_t size) override
- {
- std::cout << "KKK";
- return malloc(size);
- }
- void Free(void *ptr) override
- {
- std::cout << "JJJ";
- free(ptr);
- }
- };
- template <class T>
- class CAllocatedOn
- {
- void* operator new(std::size_t size)
- {
- return T::Alloc(size);
- }
- void operator delete(void *ptr)
- {
- T::Free(ptr);
- }
- };
- constexpr size_t diff = max(sizeof(IMemoryManager *), alignof(max_align_t));
- class RuntimeHeap: public IMemoryManager
- {
- public:
- RuntimeHeap(){num = 1;}
- void *Alloc(size_t size) override
- {
- return malloc(size);
- }
- void Free(void *ptr) override
- {
- free(ptr);
- }
- };
- class CMemoryManagerSwitcher;
- class CurrentMemoryManager
- {
- public:
- IMemoryManager *cur_Alloc;
- int now = 0;
- CurrentMemoryManager()
- {
- cur_Alloc = new(::malloc(sizeof(RuntimeHeap))) RuntimeHeap();
- now = 1;
- }
- void *Alloc(size_t size)
- {
- void *r = cur_Alloc->Alloc(size + sizeof(max_align_t));
- size_t e = 0;
- std::align(alignof(max_align_t), size + diff, r, e);
- IMemoryManager **cp = (IMemoryManager **)r;
- cp[0] = cur_Alloc;
- return (void *)(static_cast<char *>(r) + diff);
- }
- void Free(void *ptr)
- {
- ptr = (void *)(static_cast<char *>(ptr) - diff);
- IMemoryManager **w = (IMemoryManager**)(ptr);
- w[0]->Free(ptr);
- }
- };
- CurrentMemoryManager f;
- class CMemoryManagerSwitcher
- {
- public:
- CMemoryManagerSwitcher(IMemoryManager *oh)
- {
- f.cur_Alloc = oh;
- f.now = oh->num;
- }
- ~CMemoryManagerSwitcher()
- {
- }
- };
- void *allocc(size_t size) {
- static RuntimeHeap cur_Alloc;
- void *r = cur_Alloc.Alloc(size + sizeof(max_align_t));
- size_t e = 0;
- std::align(alignof(max_align_t), size + diff, r, e);
- IMemoryManager **cp = (IMemoryManager **)r;
- cp[0] = &cur_Alloc;
- return (void *)((char *)(r) + diff);
- }
- #pragma pack(pop)
- void *operator new(size_t size)
- {
- if(f.now == 0)
- {
- return allocc(size);
- }
- return f.Alloc(size);
- }
- void *operator new(size_t size, const std::nothrow_t&) noexcept
- {
- try
- {
- if(f.now == 0)
- {
- return allocc(size);
- }
- return f.Alloc(size);
- }
- catch (...)
- {
- void *p = 0;
- return p;
- }
- }
- void operator delete(void *p) noexcept
- {
- f.Free(p);
- }
- void operator delete(void *p, const std::nothrow_t&) noexcept
- {
- ::delete(p);
- }
- void *operator new[](size_t size)
- {
- if(f.now == 0)
- {
- return allocc(size);
- }
- return f.Alloc(size);
- }
- void *operator new[](size_t size, const std::nothrow_t&) noexcept
- {
- try
- {
- if(f.now == 0)
- {
- return allocc(size);
- }
- return f.Alloc(size);
- }
- catch (...)
- {
- void *p = 0;
- return p;
- }
- }
- void operator delete[](void *p) noexcept
- {
- // освобождение памяти, на которую указывает р
- f.Free(p);
- }
- void operator delete[](void *p, const std::nothrow_t&) noexcept
- {
- ::delete(p);
- }
- int main() {
- RuntimeHeap *d = new(::malloc(sizeof(RuntimeHeap))) RuntimeHeap();
- StackMemoryManager *c = new(::malloc(sizeof(StackMemoryManager))) StackMemoryManager();
- AnotherAlloc *b = new(::malloc(sizeof(AnotherAlloc))) AnotherAlloc();
- CMemoryManagerSwitcher aw(b);
- int *a = new int[100];
- CMemoryManagerSwitcher q(d);
- //aw.set_new(d);
- std::cout << a[0];
- delete[] a;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement