Advertisement
RibbonWind

Untitled

Dec 10th, 2011
424
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class HeapArea
  2. {
  3. public:
  4.     HeapArea(size_t size)
  5.     {
  6.         mStart = (char*)malloc(size * sizeof(char));
  7.         mEnd = mStart + size;
  8.         mMemory = size;
  9.     }
  10.     ~HeapArea()
  11.     {
  12.         free(mStart);
  13.     }
  14.  
  15.     char* GetStart() { return mStart; }
  16.     char* GetEnd() { return mEnd; }
  17.     size_t GetMemory() { return mMemory; }
  18.  
  19. private:
  20.     char* mStart;
  21.     char* mEnd;
  22.     size_t mMemory;
  23. };
  24.  
  25. class LinearAllocator
  26. {
  27. public:
  28.     LinearAllocator(char* start, char* end)
  29.         : mCursor(start)
  30.         , mEnd(end)
  31.     {
  32.     }
  33.  
  34.     void* Allocate(size_t size, size_t alignment, size_t boundsChecking)
  35.     {
  36.         size = ALIGNED_SIZE(size, alignment);
  37.         char* result = mCursor;
  38.         assert(mCursor + size <= mEnd);
  39.         mCursor = mCursor + size;
  40.         return result;
  41.     }
  42.  
  43.     void Free(void* ptr)
  44.     {
  45.     }
  46.  
  47.     void Rewind(void* ptr)
  48.     {
  49.         mCursor = (char*)ptr;
  50.     }
  51.  
  52.     void* GetCursor() const
  53.     {
  54.         return mCursor;
  55.     }
  56.  
  57.     size_t GetAllocationSize(void* ptr)
  58.     {
  59.         return sizeof(ptr);
  60.     }
  61.  
  62. private:
  63.     char* mCursor;
  64.     char* mEnd;
  65. };
  66.  
  67. class TestClass
  68. {
  69. public:
  70.     TestClass(int num) : mNum(num) {}
  71.     ~TestClass() {}
  72.  
  73.     void func()
  74.     {
  75.         std::cout << "Member func called with int: " << mNum << std::endl;
  76.     }
  77.  
  78. private:
  79.     int mNum;
  80. };
  81.  
  82. HeapArea heapArea(2048);
  83. LinearArena linearArena(heapArea);
  84.  
  85. TestClass* test = ME_NEW(TestClass, linearArena)(4);
  86. TestClass* test2 = ME_NEW(TestClass, linearArena)(5);
  87.  
  88. std::map<int, TestClass*> myMap;
  89.  
  90. myMap.insert(std::make_pair(0, test));
  91. myMap.insert(std::make_pair(1, test2));
  92.  
  93. std::map<int, TestClass*>::iterator i = myMap.find(0);
  94. if (i != myMap.end())
  95. {
  96.     i->second->func();
  97. }
  98.  
  99. ME_DELETE(test, linearArena);
  100. ME_DELETE(test2, linearArena);
  101.  
  102. //
  103. // Output = Member func called with int: 5
  104. //
  105. // Should be 4!
  106. //
  107.  
Advertisement
RAW Paste Data Copied
Advertisement