Advertisement
Guest User

Untitled

a guest
Jul 18th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. // Source: C++ Game Development Primer - Chapter 1
  2.  
  3. #include <iostream>
  4. using namespace std;
  5.  
  6. class DummyClass{
  7. double a,b;
  8.  
  9. public:
  10.  
  11. DummyClass(){cout<<"Const\n";}
  12. virtual ~DummyClass(){cout<<"Dest\n";}
  13. void* operator new(size_t size)
  14. {
  15. cout << "Allocating "<<size<<" byte for a new object\n";
  16. //DummyClass* o = ::new DummyClass;
  17. void* o = malloc(size);
  18. return o;
  19. }
  20.  
  21. void operator delete(void *mem)
  22. {
  23. cout << "Freeing ...\n";
  24. //::delete DummyClass;
  25. free(mem);
  26. }
  27. };
  28.  
  29. struct MemoryAllocationHeader{
  30. void* start;
  31. void* nextFree;
  32. size_t size;
  33.  
  34. };
  35.  
  36.  
  37.  
  38. namespace MyMemoryManager
  39. {
  40. char mem[1024*1024];
  41. }
  42.  
  43.  
  44. void* operator new(size_t size)
  45. {
  46. MemoryAllocationHeader* newHeader = reinterpret_cast<MemoryAllocationHeader*>(MyMemoryManager::mem);
  47.  
  48. // First empty slot when nextFree=0x00
  49. while(newHeader->nextFree!= nullptr)
  50. {
  51. newHeader = reinterpret_cast<MemoryAllocationHeader*>(newHeader->nextFree);
  52. }
  53.  
  54. newHeader->size = size;
  55. newHeader->start = newHeader+sizeof(MemoryAllocationHeader);
  56. newHeader->nextFree = (char*)(newHeader->start) + size;
  57.  
  58. return newHeader->start;
  59. }
  60.  
  61.  
  62. void operator delete(void* memory)
  63. {
  64. MemoryAllocationHeader* current = reinterpret_cast<MemoryAllocationHeader*>(MyMemoryManager::mem);
  65.  
  66. MemoryAllocationHeader *last = nullptr;
  67.  
  68. while(current->start!=memory)
  69. {
  70. last = current;
  71. current = reinterpret_cast<MemoryAllocationHeader*>(current->nextFree);
  72. }
  73.  
  74. if(last!=nullptr)
  75. last->nextFree = current->nextFree;
  76. current->size = 0;
  77. current->start = nullptr;
  78. }
  79.  
  80. void PrintAllocations() {
  81.  
  82. MemoryAllocationHeader* pHeader = reinterpret_cast<MemoryAllocationHeader*>(MyMemoryManager::mem);
  83.  
  84. while (pHeader != nullptr) {
  85.  
  86. std::cout << pHeader << std::endl;
  87. std::cout << pHeader->start << std::endl;
  88. std::cout << "NextHeader: "<<pHeader->nextFree << std::endl; std::cout << pHeader->size << std::endl;
  89.  
  90. pHeader = reinterpret_cast<MemoryAllocationHeader*> (pHeader->nextFree);
  91.  
  92. std::cout << std::endl << std::endl;
  93.  
  94. }
  95.  
  96. }
  97.  
  98.  
  99. int main()
  100. {
  101. auto x1 = new DummyClass;
  102. auto x2 = new int;
  103. auto x3 = new DummyClass;
  104. PrintAllocations();
  105. cout<<"---------------------\n";
  106.  
  107. delete x2;
  108. PrintAllocations();
  109. cout<<"---------------------\n";
  110.  
  111. x2 = new int;
  112. PrintAllocations();
  113. cout<<"---------------------\n";
  114.  
  115. delete x1;
  116. delete x2;
  117. delete x3;
  118. PrintAllocations();
  119. cout<<"---------------------\n";
  120.  
  121. x2 = new int;
  122. PrintAllocations();
  123. cout<<"---------------------\n";
  124.  
  125. delete x2;
  126. PrintAllocations();
  127. cout<<"---------------------\n";
  128.  
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement