Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #ifndef stack_new
- #include <new>
- #define stack_new(type, sa, ...) new (sa.AllocateObject<type>()) type(__VA_ARGS__);
- #endif
- #include "linearallocator.h"
- #include "./finalizer.h"
- #include "./destructorcall.h"
- #include "../Async/lock.h"
- class ScopeStack
- {
- LinearAllocator& m_Allocator;
- void* m_RewindCursorPosition;
- Finalizer* m_FinalizerChain;
- Lock m_FinalizerChainLock;
- Finalizer* AllocWithFinalizer(size_t size);
- void* GetObjectFromFinalizer(Finalizer* f);
- ScopeStack& operator = (const ScopeStack&) { return *this; /* yeah, no. */ };
- public:
- explicit ScopeStack(LinearAllocator& allocator);
- ~ScopeStack(void);
- // I use this to allocate POD types, like your sample.
- void* Allocate(unsigned int size);
- // I use this one in conjunction with stack_new(), so it's till not entirely template
- // free, but you won't need to create a specialization for each constructor.
- template<class T>
- T* AllocateObject(void)
- {
- auto finalizer = AllocWithFinalizer(sizeof(T));
- auto result = (T*)GetObjectFromFinalizer(finalizer);
- finalizer->function = &DestructorCall<T>;
- m_FinalizerChainLock.Acquire();
- {
- finalizer->chain = m_FinalizerChain;
- m_FinalizerChain = finalizer;
- }
- m_FinalizerChainLock.Release();
- return result;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement