fork download
  1. #include <vector>
  2. #include <string>
  3. #include <algorithm>
  4. #include <iostream>
  5. #include <iomanip>
  6.  
  7. #include <malloc.h>
  8.  
  9. using namespace std;
  10.  
  11. typedef void* voidptr;
  12. typedef bool* byteptr;
  13.  
  14. #ifdef _WIN64
  15. #define BSMARTHEAP_SIZEINFOBLOCK 16
  16. #define SIZET_BYTES 8
  17. #else
  18. #define BSMARTHEAP_SIZEINFOBLOCK 8
  19. #define SIZET_BYTES 4
  20. #endif
  21.  
  22. template<class T>
  23. T* move_pointer_right(voidptr ptr, size_t bytes)
  24. {
  25. byteptr pointer = (byteptr)ptr;
  26. pointer += bytes;
  27. return (T*)pointer;
  28. }
  29.  
  30. template<class T>
  31. T* move_pointer_left(voidptr ptr, size_t bytes)
  32. {
  33. byteptr pointer = (byteptr)ptr;
  34. pointer -= bytes;
  35. return (T*)pointer;
  36. }
  37.  
  38. class Bsmartheap
  39. {
  40. private:
  41. vector<voidptr>infoheap;
  42. public:
  43.  
  44. // size(8bytes) links(8bytes) data
  45.  
  46. voidptr allocate_memory(size_t SizeBytes);
  47. void free_memory(voidptr _Block);
  48. voidptr reallocate_block(voidptr _Block, size_t NewSize);
  49. size_t size_block(voidptr _Block);
  50. size_t pointers_to_block(voidptr _Block);
  51. };
  52.  
  53. size_t Bsmartheap::size_block(voidptr _Block)
  54. {
  55. const size_t sizevec = infoheap.size();
  56. for (size_t x = 0; x < sizevec; x++)
  57. {
  58. if (&infoheap[x] == _Block)
  59. {
  60. cout << infoheap[x] << endl;
  61. cout << move_pointer_left<size_t>(infoheap[x], BSMARTHEAP_SIZEINFOBLOCK) << endl;
  62. return move_pointer_left<size_t>(infoheap[x], BSMARTHEAP_SIZEINFOBLOCK)[0];
  63. }
  64. }
  65. return 0;
  66. }
  67.  
  68. size_t Bsmartheap::pointers_to_block(voidptr _Block)
  69. {
  70. const size_t sizevec = infoheap.size();
  71. for (size_t x = 0; x < sizevec; x++)
  72. {
  73. if (&infoheap[x] == _Block)
  74. {
  75. return move_pointer_left<size_t>(infoheap[x], BSMARTHEAP_SIZEINFOBLOCK)[1];
  76. }
  77. }
  78. return 0;
  79. }
  80.  
  81. voidptr Bsmartheap::reallocate_block(voidptr _Block, size_t NewSize)
  82. {
  83. if (NewSize == 0)
  84. {
  85. free_memory(_Block);
  86. return nullptr;
  87. }
  88.  
  89. const size_t vecsize = infoheap.size();
  90. for (size_t x = 0; x < vecsize; x++)
  91. {
  92. if (&infoheap[x] == _Block)
  93. {
  94. size_t* infoblock = move_pointer_left<size_t>(infoheap[x], BSMARTHEAP_SIZEINFOBLOCK);
  95. size_t Countpointers = infoblock[1];
  96. voidptr tmpptr = realloc(move_pointer_left<void>(infoheap[x], BSMARTHEAP_SIZEINFOBLOCK), BSMARTHEAP_SIZEINFOBLOCK + NewSize);
  97. if (tmpptr == nullptr)
  98. {
  99. throw "realloc return a nullptr";
  100. return nullptr;
  101. }
  102. size_t* setinfoblock = (size_t*)tmpptr;
  103. setinfoblock[0] = NewSize;
  104. setinfoblock[1] = Countpointers;
  105.  
  106. tmpptr = move_pointer_right<void>(tmpptr, BSMARTHEAP_SIZEINFOBLOCK);
  107.  
  108. infoheap[x] = tmpptr;
  109. return &infoheap[x];
  110. }
  111. }
  112. return nullptr;
  113. }
  114.  
  115. void Bsmartheap::free_memory(voidptr _Block)
  116. {
  117.  
  118. cout << hex << "Free memory receive " << _Block << endl;
  119.  
  120. const size_t sizevec = infoheap.size();
  121. for (size_t x = 0; x < sizevec; x++)
  122. {
  123. if (&infoheap[x] == _Block)
  124. {
  125. free(move_pointer_left<void>(infoheap[x], BSMARTHEAP_SIZEINFOBLOCK));
  126. infoheap.erase(infoheap.begin() + x);
  127. return;
  128. }
  129. }
  130. }
  131.  
  132. voidptr Bsmartheap::allocate_memory(size_t Sizebytes)
  133. {
  134. if (Sizebytes == 0)
  135. {
  136. throw "Size is null";
  137. return nullptr;
  138. }
  139.  
  140. voidptr _Ptr = malloc(BSMARTHEAP_SIZEINFOBLOCK + Sizebytes);
  141.  
  142. cout << hex << "Malloc return " << _Ptr << endl;
  143.  
  144. if (_Ptr == nullptr)
  145. {
  146. throw "malloc return a nullptr";
  147. return nullptr;
  148. }
  149.  
  150. size_t* infoblock = (size_t*)_Ptr;
  151. infoblock[0] = Sizebytes;
  152. infoblock[1] = 0;
  153. _Ptr = move_pointer_right<void>(_Ptr, BSMARTHEAP_SIZEINFOBLOCK);
  154.  
  155. cout << hex << "_Ptr is " << _Ptr << endl;
  156.  
  157. infoheap.push_back(_Ptr);
  158.  
  159. return &infoheap[infoheap.size() - 1];
  160. }
  161.  
  162. static Bsmartheap heap;
  163.  
  164.  
  165. int main()
  166. {
  167. char* ptr = (char*)heap.allocate_memory(6);
  168.  
  169. cout << hex << "Allocate_memory return " << voidptr(ptr) << endl;
  170.  
  171. const char* str = "Hello";
  172. for (size_t x = 0; x < 6; x++)
  173. {
  174. ptr[x] = str[x];
  175. }
  176.  
  177. cout << ptr << endl;
  178. cout << heap.size_block(ptr) << endl;
  179. cout << heap.pointers_to_block(ptr) << endl;
  180.  
  181. heap.free_memory(ptr);
  182.  
  183. return 1;
  184. }
  185.  
Runtime error #stdin #stdout 0.01s 5496KB
stdin
Standard input is empty
stdout
Malloc return 0x555cdebbbe70
_Ptr is  0x555cdebbbe78
Allocate_memory return 0x555cdebbcea0
Hello
0x6f6c6c6548
0x6f6c6c6540