LayoutPools.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 "LayoutPools.h"
  29. #include "../../../Include/RmlUi/Core/Element.h"
  30. #include "../Pool.h"
  31. #include "BlockContainer.h"
  32. #include "FloatedBoxSpace.h"
  33. #include "FormattingContext.h"
  34. #include "InlineBox.h"
  35. #include "InlineContainer.h"
  36. #include "LineBox.h"
  37. #include "ReplacedFormattingContext.h"
  38. #include <algorithm>
  39. #include <cstddef>
  40. namespace Rml {
  41. template <size_t Size>
  42. struct LayoutChunk {
  43. alignas(std::max_align_t) byte buffer[Size];
  44. };
  45. static constexpr std::size_t ChunkSizeBig = std::max({sizeof(BlockContainer)});
  46. static constexpr std::size_t ChunkSizeMedium =
  47. std::max({sizeof(InlineContainer), sizeof(InlineBox), sizeof(RootBox), sizeof(FlexContainer), sizeof(TableWrapper)});
  48. static constexpr std::size_t ChunkSizeSmall =
  49. std::max({sizeof(ReplacedBox), sizeof(InlineLevelBox_Text), sizeof(InlineLevelBox_Atomic), sizeof(LineBox), sizeof(FloatedBoxSpace)});
  50. static Pool<LayoutChunk<ChunkSizeBig>> layout_chunk_pool_big(50, true);
  51. static Pool<LayoutChunk<ChunkSizeMedium>> layout_chunk_pool_medium(50, true);
  52. static Pool<LayoutChunk<ChunkSizeSmall>> layout_chunk_pool_small(50, true);
  53. void* LayoutPools::AllocateLayoutChunk(size_t size)
  54. {
  55. static_assert(ChunkSizeBig > ChunkSizeMedium && ChunkSizeMedium > ChunkSizeSmall, "The following assumes a strict ordering of the chunk sizes.");
  56. // Note: If any change is made here, make sure a corresponding change is applied to the deallocation procedure below.
  57. if (size <= ChunkSizeSmall)
  58. return layout_chunk_pool_small.AllocateAndConstruct();
  59. else if (size <= ChunkSizeMedium)
  60. return layout_chunk_pool_medium.AllocateAndConstruct();
  61. else if (size <= ChunkSizeBig)
  62. return layout_chunk_pool_big.AllocateAndConstruct();
  63. RMLUI_ERROR;
  64. return nullptr;
  65. }
  66. void LayoutPools::DeallocateLayoutChunk(void* chunk, size_t size)
  67. {
  68. // Note: If any change is made here, make sure a corresponding change is applied to the allocation procedure above.
  69. if (size <= ChunkSizeSmall)
  70. layout_chunk_pool_small.DestroyAndDeallocate((LayoutChunk<ChunkSizeSmall>*)chunk);
  71. else if (size <= ChunkSizeMedium)
  72. layout_chunk_pool_medium.DestroyAndDeallocate((LayoutChunk<ChunkSizeMedium>*)chunk);
  73. else if (size <= ChunkSizeBig)
  74. layout_chunk_pool_big.DestroyAndDeallocate((LayoutChunk<ChunkSizeBig>*)chunk);
  75. else
  76. {
  77. RMLUI_ERROR;
  78. }
  79. }
  80. } // namespace Rml