瀏覽代碼

Memory pool for ElementMeta

Michael Ragazzon 6 年之前
父節點
當前提交
ece191a7d8
共有 5 個文件被更改,包括 38 次插入5 次删除
  1. 3 2
      Samples/basic/animation/src/main.cpp
  2. 24 0
      Source/Core/Element.cpp
  3. 1 1
      Source/Core/LayoutEngine.cpp
  4. 2 2
      Source/Core/Pool.h
  5. 8 0
      Source/Core/Pool.inl

+ 3 - 2
Samples/basic/animation/src/main.cpp

@@ -144,8 +144,9 @@ public:
 		  Dirty flag for structure changes: 43.0  [fdf6f53]
 		  Replacing containers: 46.0  [c307140]
 		  Replace 'resize' event with virtual function call: 53.0  [7ad658f]
-		  Use all_properties_dirty flag when constructing elements: 55.0
-		  Don't double create input elements: 58.0
+		  Use all_properties_dirty flag when constructing elements: 55.0 [fa6bd0a]
+		  Don't double create input elements: 58.0  [e162637]
+		  Memory pool for ElementMeta: 59.0
 		
 		*/
 

+ 24 - 0
Source/Core/Element.cpp

@@ -43,6 +43,7 @@
 #include "FontFaceHandle.h"
 #include "LayoutEngine.h"
 #include "PluginRegistry.h"
+#include "Pool.h"
 #include "StyleSheetParser.h"
 #include "XMLParseTools.h"
 #include "../../Include/Rocket/Core/Core.h"
@@ -77,6 +78,11 @@ public:
 	}
 };
 
+
+struct ElementMetaChunk;
+static Pool< ElementMetaChunk > element_meta_chunk_pool(200, true);
+
+
 // Meta objects for element collected in a single struct to reduce memory allocations
 struct ElementMeta 
 {
@@ -87,8 +93,26 @@ struct ElementMeta
 	ElementBorder border;
 	ElementDecoration decoration;
 	ElementScroll scroll;
+
+	void* ElementMeta::operator new(size_t size)
+	{
+		void* memory = element_meta_chunk_pool.AllocateObject();
+		return memory;
+	}
+	void ElementMeta::operator delete(void* chunk)
+	{
+		element_meta_chunk_pool.DeallocateObject((ElementMetaChunk*)chunk);
+	}
+}; 
+
+struct alignas(ElementMeta) ElementMetaChunk
+{
+	ElementMetaChunk() { memset(buffer, 0, size); }
+	static constexpr size_t size = sizeof(ElementMeta);
+	char buffer[size];
 };
 
+
 /// Constructs a new libRocket element.
 Element::Element(const String& _tag) : relative_offset_base(0, 0), relative_offset_position(0, 0), absolute_offset(0, 0), scroll_offset(0, 0), boxes(1), content_offset(0, 0), content_box(0, 0), 
 transform_state(), transform_state_perspective_dirty(true), transform_state_transform_dirty(true), transform_state_parent_transform_dirty(true), dirty_animation(false)

+ 1 - 1
Source/Core/LayoutEngine.cpp

@@ -44,7 +44,7 @@ namespace Core {
 
 #define MAX(a, b) (a > b ? a : b)
 
-struct LayoutChunk
+struct alignas(LayoutBlockBox) LayoutChunk
 {
 	LayoutChunk()
 	{

+ 2 - 2
Source/Core/Pool.h

@@ -144,9 +144,9 @@ private:
 	int num_allocated_objects;
 };
 
-#include "Pool.inl"
-
 }
 }
 
+#include "Pool.inl"
+
 #endif

+ 8 - 0
Source/Core/Pool.inl

@@ -25,6 +25,11 @@
  *
  */
 
+
+
+namespace Rocket {
+namespace Core {
+
 template < typename PoolType >
 Pool< PoolType >::Pool(int _chunk_size, bool _grow)
 {
@@ -245,3 +250,6 @@ void Pool< PoolType >::CreateChunk()
 
 	first_free_node = new_chunk->chunk;
 }
+
+}
+}