virtualStack.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. // zlib open source license
  2. //
  3. // Copyright (c) 2024 David Forsgren Piuva
  4. //
  5. // This software is provided 'as-is', without any express or implied
  6. // warranty. In no event will the authors be held liable for any damages
  7. // arising from the use of this software.
  8. //
  9. // Permission is granted to anyone to use this software for any purpose,
  10. // including commercial applications, and to alter it and redistribute it
  11. // freely, subject to the following restrictions:
  12. //
  13. // 1. The origin of this software must not be misrepresented; you must not
  14. // claim that you wrote the original software. If you use this software
  15. // in a product, an acknowledgment in the product documentation would be
  16. // appreciated but is not required.
  17. //
  18. // 2. Altered source versions must be plainly marked as such, and must not be
  19. // misrepresented as being the original software.
  20. //
  21. // 3. This notice may not be removed or altered from any source
  22. // distribution.
  23. #include "virtualStack.h"
  24. #include <thread>
  25. #include "../api/stringAPI.h"
  26. namespace dsr {
  27. // A structure that is placed in front of each stack allocation while allocating backwards along decreasing addresses to allow aligning memory quickly using bit masking.
  28. struct StackAllocationHeader {
  29. uint32_t totalSize; // Size of both header and payload.
  30. #ifdef SAFE_POINTER_CHECKS
  31. uint32_t identity; // A unique identifier that can be used to reduce the risk of using a block of memory after it has been freed.
  32. #endif
  33. StackAllocationHeader(uint32_t totalSize);
  34. };
  35. // How many bytes that are allocated directly in thread local memory.
  36. static const uint64_t VIRTUAL_STACK_SIZE = 262144;
  37. static const int MAX_EXTRA_STACKS = 63;
  38. static const uintptr_t stackHeaderPaddedSize = memory_getPaddedSize<StackAllocationHeader>();
  39. static const uintptr_t stackHeaderAlignmentAndMask = memory_createAlignmentAndMask((uintptr_t)alignof(StackAllocationHeader));
  40. #ifdef SAFE_POINTER_CHECKS
  41. // In debug mode, each new thread creates a hash from its own identity to catch most of the memory errors in debug mode.
  42. std::hash<std::thread::id> hasher;
  43. thread_local uint32_t threadIdentity = hasher(std::this_thread::get_id());
  44. // To check the allocation identity, subtract the padded size of the header from the base pointer, cast to the head type and compare to the pointer's identity.
  45. thread_local uint32_t nextIdentity = threadIdentity;
  46. #endif
  47. StackAllocationHeader::StackAllocationHeader(uint32_t totalSize) : totalSize(totalSize) {
  48. #ifdef SAFE_POINTER_CHECKS
  49. // No identity may be zero, because identity zero is no identity.
  50. if (nextIdentity == 0) nextIdentity++;
  51. this->identity = nextIdentity;
  52. nextIdentity++;
  53. #endif
  54. }
  55. struct StackMemory {
  56. uint8_t *top = nullptr; // The stack pointer is here when completely full.
  57. uint8_t *stackPointer = nullptr; // The virtual stack pointer.
  58. uint8_t *bottom = nullptr; // The stack pointer is here when empty.
  59. };
  60. struct FixedStackMemory : public StackMemory {
  61. uint8_t data[VIRTUAL_STACK_SIZE];
  62. FixedStackMemory() {
  63. this->top = this->data;
  64. this->stackPointer = this->data + VIRTUAL_STACK_SIZE;
  65. this->bottom = this->data + VIRTUAL_STACK_SIZE;
  66. }
  67. };
  68. struct DynamicStackMemory : public StackMemory {
  69. // Allocate the data dynamically in top when needed.
  70. // Clean up when the thread terminates.
  71. ~DynamicStackMemory() {
  72. if (this->top != nullptr) {
  73. free(this->top);
  74. }
  75. }
  76. };
  77. // Returns the size of the allocation including alignment.
  78. inline uint64_t increaseStackPointer(uint8_t *&pointer, uint64_t paddedSize, uintptr_t alignmentAndMask) {
  79. // Add the padded payload and align.
  80. uintptr_t oldAddress = (uintptr_t)pointer;
  81. uintptr_t newAddress = (oldAddress - paddedSize) & alignmentAndMask;
  82. pointer = (uint8_t*)newAddress;
  83. return oldAddress - newAddress;
  84. }
  85. inline void decreaseStackPointer(uint8_t *&pointer, uint64_t totalSize) {
  86. // Remove the data and alignment.
  87. pointer += totalSize;
  88. }
  89. static uint8_t *stackAllocate(StackMemory& stack, uint64_t paddedSize, uintptr_t alignmentAndMask) {
  90. uint8_t *newStackPointer = stack.stackPointer;
  91. // Allocate memory for payload.
  92. uint64_t payloadTotalSize = increaseStackPointer(newStackPointer, paddedSize, alignmentAndMask);
  93. // Get a pointer to the payload.
  94. uint8_t *result = newStackPointer;
  95. // Allocate memory for header.
  96. uint64_t headerTotalSize = increaseStackPointer(newStackPointer, stackHeaderPaddedSize, stackHeaderAlignmentAndMask);
  97. // Check that we did not run out of memory.
  98. if (newStackPointer < stack.top) {
  99. // Not enough space.
  100. return nullptr;
  101. } else {
  102. stack.stackPointer = newStackPointer;
  103. // Write the header to memory.
  104. *((StackAllocationHeader*)stack.stackPointer) = StackAllocationHeader(payloadTotalSize + headerTotalSize);
  105. // Clear the new allocation for determinism.
  106. std::memset((void*)result, 0, payloadTotalSize);
  107. // Return a pointer to the payload.
  108. return result;
  109. }
  110. }
  111. thread_local FixedStackMemory fixedMemory; // Index -1
  112. thread_local DynamicStackMemory dynamicMemory[MAX_EXTRA_STACKS]; // Index 0..MAX_EXTRA_STACKS-1
  113. thread_local int32_t stackIndex = -1;
  114. uint8_t *virtualStack_push(uint64_t paddedSize, uintptr_t alignmentAndMask) {
  115. if (stackIndex < 0) {
  116. uint8_t *result = stackAllocate(fixedMemory, paddedSize, alignmentAndMask);
  117. // Check that we did not run out of memory.
  118. if (result == nullptr) {
  119. // Not enough space in thread local memory. Moving to the first dynamic stack.
  120. stackIndex = 0;
  121. goto allocateDynamic;
  122. } else {
  123. // Return a pointer to the payload.
  124. return result;
  125. }
  126. }
  127. allocateDynamic:
  128. // We should only reach this place if allocating in dynamic stack memory.
  129. assert(stackIndex >= 0);
  130. // Never go above the maximum index.
  131. assert(stackIndex < MAX_EXTRA_STACKS);
  132. // Allocate memory in the dynamic stack if not yet allocated.
  133. if (dynamicMemory[stackIndex].top == nullptr) {
  134. uint64_t regionSize = 16777216 * (1 << stackIndex);
  135. if (paddedSize * 4 > regionSize) {
  136. regionSize = paddedSize * 4;
  137. }
  138. uint8_t *newMemory = (uint8_t*)malloc(regionSize);
  139. if (newMemory == nullptr) {
  140. throwError(U"Failed to allocate ", regionSize, U" bytes of heap memory for expanding the virtual stack when trying to allocate ", paddedSize, " bytes!\n");
  141. return nullptr;
  142. } else {
  143. // Keep the new allocation.
  144. dynamicMemory[stackIndex].top = newMemory;
  145. // Start from the back of the new allocation.
  146. dynamicMemory[stackIndex].stackPointer = newMemory + regionSize;
  147. dynamicMemory[stackIndex].bottom = newMemory + regionSize;
  148. }
  149. }
  150. assert(dynamicMemory[stackIndex].stackPointer != nullptr);
  151. // Allocate memory.
  152. uint8_t *result = stackAllocate(dynamicMemory[stackIndex], paddedSize, alignmentAndMask);
  153. if (result == nullptr) {
  154. if (stackIndex >= MAX_EXTRA_STACKS - 1) {
  155. throwError(U"Exceeded MAX_EXTRA_STACKS to allocate more heap memory for a thread local virtual stack!\n");
  156. return nullptr;
  157. } else {
  158. stackIndex++;
  159. goto allocateDynamic;
  160. }
  161. } else {
  162. // Return a pointer to the payload.
  163. return result;
  164. }
  165. }
  166. // Deallocates the topmost allocation in the stack or returns false if it does not contain any more allocations.
  167. static bool stackDeallocate(StackMemory& stack) {
  168. if (stack.stackPointer + stackHeaderPaddedSize > stack.bottom) {
  169. // If the allocated memory does not fit a header, then it is empty.
  170. return false;
  171. } else {
  172. // TODO: Prevent deallocating past the top.
  173. // Read the header.
  174. StackAllocationHeader header = *((StackAllocationHeader*)stack.stackPointer);
  175. // Deallocate both header and payload using the stored total size.
  176. decreaseStackPointer(stack.stackPointer, header.totalSize);
  177. return true;
  178. }
  179. }
  180. void virtualStack_pop() {
  181. if (stackIndex < 0) {
  182. if (!stackDeallocate(fixedMemory)) {
  183. throwError(U"No more stack memory to pop!\n");
  184. }
  185. } else {
  186. if (!stackDeallocate(dynamicMemory[stackIndex])) {
  187. throwError(U"The virtual stack has been corrupted!\n");
  188. } else {
  189. // If the bottom has been reached then go to the lower stack.
  190. if (dynamicMemory[stackIndex].stackPointer >= dynamicMemory[stackIndex].bottom) {
  191. stackIndex--;
  192. }
  193. }
  194. }
  195. }
  196. }