[PATCH] Introduce region-object pool to avoid malloc/free overhead.

Jonathan Morton jmorton at sd070.hel.movial.fi
Mon Jun 8 02:17:24 PDT 2009


---
 mi/miregion.c |   22 ++++++++++++++++++----
 1 files changed, 18 insertions(+), 4 deletions(-)

diff --git a/mi/miregion.c b/mi/miregion.c
index c48befc..a352cc6 100644
--- a/mi/miregion.c
+++ b/mi/miregion.c
@@ -224,6 +224,11 @@ RegDataRec miEmptyData = {0, 0};
 RegDataRec  miBrokenData = {0, 0};
 static RegionRec   miBrokenRegion = { { 0, 0, 0, 0 }, &miBrokenData };
 
+/* Avoid calling malloc every time one of these is needed */
+#define REGION_POOL_SIZE (4)
+static RegionPtr miRegionPool[REGION_POOL_SIZE] = {NULL};
+static uint32_t miRegionPoolTop = 0;
+
 void
 InitRegions (void)
 {
@@ -239,9 +244,13 @@ InitRegions (void)
 RegionPtr
 miRegionCreate(BoxPtr rect, int size)
 {
-    RegionPtr pReg;
+    RegionPtr pReg = NULL;
    
-    pReg = (RegionPtr)xalloc(sizeof(RegionRec));
+    if(miRegionPoolTop)
+        pReg = miRegionPool[--miRegionPoolTop];
+    else
+        pReg = (RegionPtr)xalloc(sizeof(RegionRec));
+
     if (!pReg)
 	return &miBrokenRegion;
 
@@ -254,8 +263,13 @@ void
 miRegionDestroy(RegionPtr pReg)
 {
     pixman_region_fini (pReg);
-    if (pReg != &miBrokenRegion)
-	xfree(pReg);
+
+    if (pReg != &miBrokenRegion) {
+        if(miRegionPoolTop < REGION_POOL_SIZE)
+            miRegionPool[miRegionPoolTop++] = pReg;
+        else
+	    xfree(pReg);
+    }
 }
 
 void
-- 
1.5.6.3


--=-sSulcgRJlPy19NpCTn/b
Content-Disposition: attachment; filename*0=0002-Introduce-picture-object-pool-to-avoid-malloc-free-o.pat; filename*1=ch
Content-Type: text/x-patch; name="0002-Introduce-picture-object-pool-to-avoid-malloc-free-o.patch"; charset="UTF-8"
Content-Transfer-Encoding: 7bit



More information about the xorg-devel mailing list