allocator.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <mutex>
  5. static std::mutex allocationLock;
  6. // This allocator does not have any header and can be disabled by not linking it into the application
  7. // Allocation head stored in the beginning of each allocation
  8. // The caller's content begins alignedHeadSize bytes after the head
  9. struct AllocationHead {
  10. AllocationHead* nextUnused = nullptr;
  11. uintptr_t contentSize = 0;
  12. };
  13. // A fixed byte offset to make sure that structures are still aligned in memory
  14. // Increase if values larger than this are stored in data structures using new and delete operations
  15. static const uintptr_t alignedHeadSize = 16;
  16. static_assert(sizeof(AllocationHead) <= alignedHeadSize, "Increase alignedHeadSize to the next power of two.\n");
  17. static AllocationHead* createAllocation(size_t contentSize) {
  18. //printf("createAllocation head(%li) + %li bytes\n", alignedHeadSize, contentSize);
  19. // calloc is faster than malloc for small allocations by not having to fetch old data to the cache
  20. AllocationHead* allocation = (AllocationHead*)calloc(alignedHeadSize + contentSize, 1);
  21. allocation->nextUnused = nullptr;
  22. allocation->contentSize = contentSize;
  23. return allocation;
  24. }
  25. static void* getContent(AllocationHead* head) {
  26. return (void*)(((uintptr_t)head) + alignedHeadSize);
  27. }
  28. static AllocationHead* getHead(void* content) {
  29. return (AllocationHead*)(((uintptr_t)content) - alignedHeadSize);
  30. }
  31. // Garbage pile
  32. struct GarbagePile {
  33. AllocationHead* pileHead; // Linked list using nextUnused in AllocationHead
  34. const size_t fixedBufferSize;
  35. ~GarbagePile() {
  36. AllocationHead* current = this->pileHead;
  37. while (current != nullptr) {
  38. // Free and go to the next allocation
  39. AllocationHead* next = current->nextUnused;
  40. free(current);
  41. current = next;
  42. }
  43. }
  44. AllocationHead* getAllocation() {
  45. //printf("getAllocation head(%li) + %li bytes\n", alignedHeadSize, this->fixedBufferSize);
  46. if (pileHead != nullptr) {
  47. // Pop the first unused allocation
  48. AllocationHead* result = this->pileHead;
  49. this->pileHead = this->pileHead->nextUnused;
  50. result->nextUnused = nullptr;
  51. result->contentSize = this->fixedBufferSize;
  52. return result;
  53. } else {
  54. // Create a new allocation
  55. return createAllocation(this->fixedBufferSize);
  56. }
  57. }
  58. void recycleAllocation(AllocationHead* unused) {
  59. // Clear old data to make debugging easier
  60. memset(getContent(unused), 0, this->fixedBufferSize);
  61. // Push new allocation to the pile
  62. unused->nextUnused = this->pileHead;
  63. this->pileHead = unused;
  64. }
  65. };
  66. static GarbagePile garbagePiles[8] = {
  67. {nullptr, 16},
  68. {nullptr, 32},
  69. {nullptr, 64},
  70. {nullptr, 128},
  71. {nullptr, 256},
  72. {nullptr, 512},
  73. {nullptr, 1024},
  74. {nullptr, 2048}
  75. };
  76. static int getBufferIndex(size_t contentSize) {
  77. if (contentSize <= 16) {
  78. return 0;
  79. } else if (contentSize <= 32) {
  80. return 1;
  81. } else if (contentSize <= 64) {
  82. return 2;
  83. } else if (contentSize <= 128) {
  84. return 3;
  85. } else if (contentSize <= 256) {
  86. return 4;
  87. } else if (contentSize <= 512) {
  88. return 5;
  89. } else if (contentSize <= 1024) {
  90. return 6;
  91. } else if (contentSize <= 2048) {
  92. return 7;
  93. } else {
  94. return -1;
  95. }
  96. }
  97. void* operator new(size_t contentSize) {
  98. allocationLock.lock();
  99. int bufferIndex = getBufferIndex(contentSize);
  100. AllocationHead* head;
  101. if (bufferIndex == -1) {
  102. head = createAllocation(contentSize);
  103. //printf("Allocated %li bytes without a size group\n", contentSize);
  104. } else {
  105. //printf("Requested at least %li bytes from size group %i\n", contentSize, bufferIndex);
  106. head = garbagePiles[bufferIndex].getAllocation();
  107. }
  108. allocationLock.unlock();
  109. return getContent(head);
  110. }
  111. void operator delete(void* content) {
  112. allocationLock.lock();
  113. AllocationHead* head = getHead(content);
  114. int bufferIndex = getBufferIndex(head->contentSize);
  115. if (bufferIndex == -1) {
  116. free(head);
  117. //printf("Freed memory of size %li without a size group\n", head->contentSize);
  118. } else {
  119. garbagePiles[bufferIndex].recycleAllocation(head);
  120. //printf("Freed memory of size %li from size group %i\n", head->contentSize, bufferIndex);
  121. }
  122. allocationLock.unlock();
  123. }