Advertisement
SigmaBoy456

Grok 3 Custom MemAlloc #2141

Mar 1st, 2025
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.81 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstddef> // For size_t
  3.  
  4. // Structure for each memory block
  5. struct Block {
  6. size_t size; // Size of this block (excluding header)
  7. bool free; // Is this block free?
  8. Block* next; // Pointer to the next block
  9. };
  10.  
  11. class SimpleAllocator {
  12. private:
  13. void* pool; // The big memory pool we manage
  14. Block* head; // Start of our block list
  15. size_t totalSize; // Total size of the pool
  16.  
  17. public:
  18. // Initialize the allocator with a fixed pool size
  19. SimpleAllocator(size_t size) : totalSize(size) {
  20. pool = malloc(size); // Get the raw memory from the system
  21. if (!pool) {
  22. throw std::bad_alloc();
  23. }
  24. // Set up the first block (whole pool is free initially)
  25. head = static_cast<Block*>(pool);
  26. head->size = size - sizeof(Block); // Account for header size
  27. head->free = true;
  28. head->next = nullptr;
  29. }
  30.  
  31. // Clean up
  32. ~SimpleAllocator() {
  33. free(pool); // Release the pool back to the system
  34. }
  35.  
  36. // Allocate memory
  37. void* allocate(size_t size) {
  38. // Find a free block big enough
  39. Block* current = head;
  40. Block* best = nullptr;
  41. while (current) {
  42. if (current->free && current->size >= size) {
  43. best = current; // First-fit strategy (could use best-fit)
  44. break;
  45. }
  46. current = current->next;
  47. }
  48.  
  49. if (!best) {
  50. return nullptr; // No suitable block found
  51. }
  52.  
  53. // Split the block if it’s too big
  54. if (best->size >= size + sizeof(Block) + 1) {
  55. Block* newBlock = reinterpret_cast<Block*>(
  56. reinterpret_cast<char*>(best) + sizeof(Block) + size
  57. );
  58. newBlock->size = best->size - size - sizeof(Block);
  59. newBlock->free = true;
  60. newBlock->next = best->next;
  61. best->size = size;
  62. best->next = newBlock;
  63. }
  64.  
  65. best->free = false; // Mark as used
  66. return reinterpret_cast<char*>(best) + sizeof(Block); // Return pointer after header
  67. }
  68.  
  69. // Deallocate memory
  70. void deallocate(void* ptr) {
  71. if (!ptr) return;
  72.  
  73. // Find the block (ptr is after the header)
  74. Block* block = reinterpret_cast<Block*>(
  75. static_cast<char*>(ptr) - sizeof(Block)
  76. );
  77. block->free = true; // Mark it free
  78.  
  79. // Merge with next block if it’s free
  80. if (block->next && block->next->free) {
  81. block->size += sizeof(Block) + block->next->size;
  82. block->next = block->next->next;
  83. }
  84.  
  85. // Merge with previous block if it’s free
  86. Block* prev = head;
  87. while (prev && prev->next != block) {
  88. prev = prev->next;
  89. }
  90. if (prev && prev->free) {
  91. prev->size += sizeof(Block) + block->size;
  92. prev->next = block->next;
  93. }
  94. }
  95. };
  96.  
  97. int main() {
  98. // Create an allocator with 1024 bytes
  99. SimpleAllocator alloc(1024);
  100.  
  101. // Allocate some memory
  102. int* num1 = static_cast<int*>(alloc.allocate(5 * sizeof(int)));
  103. if (num1) {
  104. for (int i = 0; i < 5; i++) {
  105. num1[i] = i + 1;
  106. }
  107. for (int i = 0; i < 5; i++) {
  108. std::cout << num1[i] << " "; // Prints 1 2 3 4 5
  109. }
  110. std::cout << "\n";
  111. }
  112.  
  113. // Free it
  114. alloc.deallocate(num1);
  115.  
  116. // Allocate again to test reuse
  117. int* num2 = static_cast<int*>(alloc.allocate(3 * sizeof(int)));
  118. if (num2) {
  119. num2[0] = 10;
  120. num2[1] = 20;
  121. num2[2] = 30;
  122. std::cout << num2[0] << " " << num2[1] << " " << num2[2] << "\n"; // Prints 10 20 30
  123. alloc.deallocate(num2);
  124. }
  125.  
  126. return 0;
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement