Memory.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #include "Memory.h"
  2. #include <memory>
  3. #include <stdint.h>
  4. #include <stdlib.h>
  5. namespace Rml {
  6. namespace Detail {
  7. inline void* rmlui_align(size_t alignment, size_t size, void*& ptr, size_t& space)
  8. {
  9. #if defined(_MSC_VER)
  10. return std::align(alignment, size, ptr, space);
  11. #else
  12. // std::align replacement to support compilers missing this feature.
  13. // From https://gcc.gnu.org/bugzilla/show_bug.cgi?id=57350
  14. uintptr_t pn = reinterpret_cast<uintptr_t>(ptr);
  15. uintptr_t aligned = (pn + alignment - 1) & -alignment;
  16. size_t padding = aligned - pn;
  17. if (space < size + padding)
  18. return nullptr;
  19. space -= padding;
  20. return ptr = reinterpret_cast<void*>(aligned);
  21. #endif
  22. }
  23. BasicStackAllocator::BasicStackAllocator(size_t N) : N(N), data((byte*)malloc(N)), p(data) {}
  24. BasicStackAllocator::~BasicStackAllocator() noexcept
  25. {
  26. RMLUI_ASSERT(p == data);
  27. free(data);
  28. }
  29. void* BasicStackAllocator::allocate(size_t alignment, size_t byte_size)
  30. {
  31. size_t available_space = N - ((byte*)p - data);
  32. if (rmlui_align(alignment, byte_size, p, available_space))
  33. {
  34. void* result = p;
  35. p = (byte*)p + byte_size;
  36. return result;
  37. }
  38. // Fall back to malloc
  39. return malloc(byte_size);
  40. }
  41. void BasicStackAllocator::deallocate(void* obj) noexcept
  42. {
  43. if (obj < data || obj >= data + N)
  44. {
  45. free(obj);
  46. return;
  47. }
  48. p = obj;
  49. }
  50. BasicStackAllocator& GetGlobalBasicStackAllocator()
  51. {
  52. static BasicStackAllocator stack_allocator(10 * 1024);
  53. return stack_allocator;
  54. }
  55. } // namespace Detail
  56. } // namespace Rml