Memory.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "Memory.h"
  29. #include <memory>
  30. #include <stdlib.h>
  31. #include <stdint.h>
  32. namespace Rml {
  33. namespace Core {
  34. namespace Detail {
  35. inline void* rmlui_align(size_t alignment, size_t size, void*& ptr, size_t& space)
  36. {
  37. #if defined(_MSC_VER)
  38. return std::align(alignment, size, ptr, space);
  39. #else
  40. // std::align replacement to support compilers missing this feature.
  41. // From https://gcc.gnu.org/bugzilla/show_bug.cgi?id=57350
  42. uintptr_t pn = reinterpret_cast<uintptr_t>(ptr);
  43. uintptr_t aligned = (pn + alignment - 1) & -alignment;
  44. size_t padding = aligned - pn;
  45. if (space < size + padding)
  46. return nullptr;
  47. space -= padding;
  48. return ptr = reinterpret_cast<void*>(aligned);
  49. #endif
  50. }
  51. BasicStackAllocator::BasicStackAllocator(size_t N) : N(N), data((byte*)malloc(N)), p(data)
  52. {}
  53. BasicStackAllocator::~BasicStackAllocator() noexcept {
  54. RMLUI_ASSERT(p == data);
  55. free(data);
  56. }
  57. void* BasicStackAllocator::allocate(size_t alignment, size_t byte_size)
  58. {
  59. size_t available_space = N - ((byte*)p - data);
  60. if (rmlui_align(alignment, byte_size, p, available_space))
  61. {
  62. void* result = p;
  63. p = (byte*)p + byte_size;
  64. return result;
  65. }
  66. // Fall back to malloc
  67. return malloc(byte_size);
  68. }
  69. void BasicStackAllocator::deallocate(void* obj) noexcept
  70. {
  71. if (obj < data || obj >= data + N)
  72. {
  73. free(obj);
  74. return;
  75. }
  76. p = obj;
  77. }
  78. BasicStackAllocator& GetGlobalBasicStackAllocator()
  79. {
  80. static BasicStackAllocator stack_allocator(10 * 1024);
  81. return stack_allocator;
  82. }
  83. }
  84. }
  85. }