virtualStack.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. namespace dsr {
  25. // 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.
  26. struct StackAllocationHeader {
  27. uint32_t totalSize; // Size of both header and payload.
  28. #ifdef SAFE_POINTER_CHECKS
  29. 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.
  30. #endif
  31. StackAllocationHeader(uint32_t totalSize);
  32. };
  33. // How many bytes that are allocated directly in thread local memory.
  34. static const size_t VIRTUAL_STACK_SIZE = 131072;
  35. // How many bytes are reserved for the head.
  36. static const size_t ALLOCATION_HEAD_SIZE = memory_getPaddedSize<StackAllocationHeader>();
  37. static const uintptr_t stackHeaderPaddedSize = memory_getPaddedSize<StackAllocationHeader>();
  38. static const uintptr_t stackHeaderAlignmentAndMask = memory_createAlignmentAndMask((uintptr_t)alignof(StackAllocationHeader));
  39. #ifdef SAFE_POINTER_CHECKS
  40. // In debug mode, each new thread creates a hash from its own identity to catch most of the memory errors in debug mode.
  41. std::hash<std::thread::id> hasher;
  42. thread_local uint32_t threadIdentity = hasher(std::this_thread::get_id());
  43. // 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.
  44. thread_local uint32_t nextIdentity = threadIdentity;
  45. #endif
  46. StackAllocationHeader::StackAllocationHeader(uint32_t totalSize) : totalSize(totalSize) {
  47. #ifdef SAFE_POINTER_CHECKS
  48. // No identity may be zero, because identity zero is no identity.
  49. if (nextIdentity == 0) nextIdentity++;
  50. this->identity = nextIdentity;
  51. nextIdentity++;
  52. #endif
  53. }
  54. struct StackMemory {
  55. uint8_t data[VIRTUAL_STACK_SIZE];
  56. uint8_t *top = nullptr;
  57. StackMemory() {
  58. this->top = this->data + VIRTUAL_STACK_SIZE;
  59. }
  60. };
  61. thread_local StackMemory virtualStack;
  62. // Returns the size of the allocation including alignment.
  63. inline uint64_t increaseTop(uint64_t paddedSize, uintptr_t alignmentAndMask) {
  64. // Add the padded payload and align.
  65. uintptr_t oldAddress = (uintptr_t)virtualStack.top;
  66. uintptr_t newAddress = (oldAddress - paddedSize) & alignmentAndMask;
  67. virtualStack.top = (uint8_t*)newAddress;
  68. return oldAddress - newAddress;
  69. }
  70. inline void decreaseTop(uint64_t totalSize) {
  71. // Remove the data and alignment.
  72. virtualStack.top += totalSize;
  73. }
  74. uint8_t *virtualStack_push(uint64_t paddedSize, uintptr_t alignmentAndMask) {
  75. uint8_t *oldTop = virtualStack.top;
  76. // Allocate memory for payload.
  77. uint64_t payloadTotalSize = increaseTop(paddedSize, alignmentAndMask);
  78. // Get a pointer to the payload.
  79. uint8_t *result = virtualStack.top;
  80. // Allocate memory for header.
  81. uint64_t headerTotalSize = increaseTop(stackHeaderPaddedSize, stackHeaderAlignmentAndMask);
  82. // Check that we did not run out of memory.
  83. if (virtualStack.top < virtualStack.data) {
  84. // TODO: Expand automatically using heap memory instead of crashing.
  85. throwError(U"Ran out of stack memory to allocate!\n");
  86. virtualStack.top = oldTop;
  87. return nullptr;
  88. } else {
  89. // Write the header to memory.
  90. *((StackAllocationHeader*)virtualStack.top) = StackAllocationHeader(payloadTotalSize + headerTotalSize);
  91. // Clear the new allocation for determinism.
  92. std::memset((char*)result, 0, payloadTotalSize);
  93. // Return a pointer to the payload.
  94. return result;
  95. }
  96. }
  97. void virtualStack_pop() {
  98. if (virtualStack.top + stackHeaderPaddedSize > virtualStack.data + VIRTUAL_STACK_SIZE) {
  99. throwError(U"No more stack memory to pop!\n");
  100. } else {
  101. // Read the header.
  102. StackAllocationHeader header = *((StackAllocationHeader*)virtualStack.top);
  103. // Deallocate both header and payload using the stored total size.
  104. decreaseTop(header.totalSize);
  105. }
  106. }
  107. }