Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class HeapArea
- {
- public:
- HeapArea(size_t size)
- {
- mStart = (char*)malloc(size * sizeof(char));
- mEnd = mStart + size;
- mMemory = size;
- }
- ~HeapArea()
- {
- free(mStart);
- }
- char* GetStart() { return mStart; }
- char* GetEnd() { return mEnd; }
- size_t GetMemory() { return mMemory; }
- private:
- char* mStart;
- char* mEnd;
- size_t mMemory;
- };
- class LinearAllocator
- {
- public:
- LinearAllocator(char* start, char* end)
- : mCursor(start)
- , mEnd(end)
- {
- }
- void* Allocate(size_t size, size_t alignment, size_t boundsChecking)
- {
- size = ALIGNED_SIZE(size, alignment);
- char* result = mCursor;
- assert(mCursor + size <= mEnd);
- mCursor = mCursor + size;
- return result;
- }
- void Free(void* ptr)
- {
- }
- void Rewind(void* ptr)
- {
- mCursor = (char*)ptr;
- }
- void* GetCursor() const
- {
- return mCursor;
- }
- size_t GetAllocationSize(void* ptr)
- {
- return sizeof(ptr);
- }
- private:
- char* mCursor;
- char* mEnd;
- };
- class TestClass
- {
- public:
- TestClass(int num) : mNum(num) {}
- ~TestClass() {}
- void func()
- {
- std::cout << "Member func called with int: " << mNum << std::endl;
- }
- private:
- int mNum;
- };
- HeapArea heapArea(2048);
- LinearArena linearArena(heapArea);
- TestClass* test = ME_NEW(TestClass, linearArena)(4);
- TestClass* test2 = ME_NEW(TestClass, linearArena)(5);
- std::map<int, TestClass*> myMap;
- myMap.insert(std::make_pair(0, test));
- myMap.insert(std::make_pair(1, test2));
- std::map<int, TestClass*>::iterator i = myMap.find(0);
- if (i != myMap.end())
- {
- i->second->func();
- }
- ME_DELETE(test, linearArena);
- ME_DELETE(test2, linearArena);
- //
- // Output = Member func called with int: 5
- //
- // Should be 4!
- //
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement