[PATCH 03/14] miwideline: Factor out span buffer allocation.

Jamey Sharp jamey at minilop.net
Sat May 8 16:39:18 PDT 2010


Signed-off-by: Jamey Sharp <jamey at minilop.net>
---
 mi/miwideline.c |  106 ++++++++++++++++--------------------------------------
 1 files changed, 32 insertions(+), 74 deletions(-)

diff --git a/mi/miwideline.c b/mi/miwideline.c
index 181b12e..585d4d2 100644
--- a/mi/miwideline.c
+++ b/mi/miwideline.c
@@ -52,6 +52,21 @@ from The Open Group.
 #include "miwideline.h"
 #include "mi.h"
 
+static Bool
+InitSpans(Spans *spans, size_t nspans)
+{
+    spans->points = xalloc (nspans * sizeof (*spans->points));
+    if (!spans->points)
+	return FALSE;
+    spans->widths = xalloc (nspans * sizeof (*spans->widths));
+    if (!spans->widths)
+    {
+	xfree (spans->points);
+	return FALSE;
+    }
+    return TRUE;
+}
+
 /*
  * interface data to span-merging polygon filler
  */
@@ -110,9 +125,7 @@ miFillPolyHelper (DrawablePtr pDrawable, GCPtr pGC, unsigned long pixel,
     int	left_height = 0, right_height = 0;
 
     DDXPointPtr ppt;
-    DDXPointPtr pptInit = NULL;
     int 	*pwidth;
-    int 	*pwidthInit = NULL;
     XID		oldPixel;
     int		xorg;
     Spans	spanRec;
@@ -120,19 +133,12 @@ miFillPolyHelper (DrawablePtr pDrawable, GCPtr pGC, unsigned long pixel,
     left_height = 0;
     right_height = 0;
     
+    if (!InitSpans(&spanRec, overall_height))
+	return;
+    ppt = spanRec.points;
+    pwidth = spanRec.widths;
     if (!spanData)
     {
-    	pptInit = xalloc (overall_height * sizeof(*ppt));
-    	if (!pptInit)
-	    return;
-    	pwidthInit = xalloc (overall_height * sizeof(*pwidth));
-    	if (!pwidthInit)
-    	{
-	    xfree (pptInit);
-	    return;
-    	}
-	ppt = pptInit;
-	pwidth = pwidthInit;
     	oldPixel = pGC->fgPixel;
     	if (pixel != oldPixel)
     	{
@@ -141,20 +147,6 @@ miFillPolyHelper (DrawablePtr pDrawable, GCPtr pGC, unsigned long pixel,
     	    ValidateGC (pDrawable, pGC);
     	}
     }
-    else
-    {
-	spanRec.points = xalloc (overall_height * sizeof (*ppt));
-	if (!spanRec.points)
-	    return;
-	spanRec.widths = xalloc (overall_height * sizeof (int));
-	if (!spanRec.widths)
-	{
-	    xfree (spanRec.points);
-	    return;
-	}
-	ppt = spanRec.points;
-	pwidth = spanRec.widths;
-    }
 
     xorg = 0;
     if (pGC->miTranslate)
@@ -226,11 +218,12 @@ miFillPolyHelper (DrawablePtr pDrawable, GCPtr pGC, unsigned long pixel,
 	    }
 	}
     }
+    spanRec.count = ppt - spanRec.points;
     if (!spanData)
     {
-    	(*pGC->ops->FillSpans) (pDrawable, pGC, ppt - pptInit, pptInit, pwidthInit, TRUE);
-    	xfree (pwidthInit);
-    	xfree (pptInit);
+	(*pGC->ops->FillSpans) (pDrawable, pGC, spanRec.count, spanRec.points, spanRec.widths, TRUE);
+	xfree (spanRec.widths);
+	xfree (spanRec.points);
     	if (pixel != oldPixel)
     	{
 	    DoChangeGC (pGC, GCForeground, &oldPixel, FALSE);
@@ -238,10 +231,7 @@ miFillPolyHelper (DrawablePtr pDrawable, GCPtr pGC, unsigned long pixel,
     	}
     }
     else
-    {
-	spanRec.count = ppt - spanRec.points;
 	AppendSpanGroup (pGC, pixel, &spanRec, spanData);
-    }
 }
 
 static void
@@ -283,15 +273,8 @@ miFillRectPolyHelper (
     }
     else
     {
-	spanRec.points = xalloc (h * sizeof (*ppt));
-	if (!spanRec.points)
+	if (!InitSpans(&spanRec, h))
 	    return;
-	spanRec.widths = xalloc (h * sizeof (int));
-	if (!spanRec.widths)
-	{
-	    xfree (spanRec.points);
-	    return;
-	}
 	ppt = spanRec.points;
 	pwidth = spanRec.widths;
 
@@ -1058,8 +1041,6 @@ miLineArc (
     double          	yorg,
     Bool	    	isInt)
 {
-    DDXPointPtr points;
-    int *widths;
     int xorgi = 0, yorgi = 0;
     XID		oldPixel;
     Spans spanRec;
@@ -1105,17 +1086,10 @@ miLineArc (
 	}
 	isInt = FALSE;
     }
+    if (!InitSpans(&spanRec, pGC->lineWidth))
+	return;
     if (!spanData)
     {
-    	points = xalloc(sizeof(DDXPointRec) * pGC->lineWidth);
-    	if (!points)
-	    return;
-    	widths = xalloc(sizeof(int) * pGC->lineWidth);
-    	if (!widths)
-    	{
-	    xfree(points);
-	    return;
-    	}
     	oldPixel = pGC->fgPixel;
     	if (pixel != oldPixel)
     	{
@@ -1124,32 +1098,19 @@ miLineArc (
 	    ValidateGC (pDraw, pGC);
     	}
     }
-    else
-    {
-	points = xalloc (pGC->lineWidth * sizeof (DDXPointRec));
-	if (!points)
-	    return;
-	widths = xalloc (pGC->lineWidth * sizeof (int));
-	if (!widths)
-	{
-	    xfree (points);
-	    return;
-	}
-	spanRec.points = points;
-	spanRec.widths = widths;
-    }
     if (isInt)
-	n = miLineArcI(pDraw, pGC, xorgi, yorgi, points, widths);
+	n = miLineArcI(pDraw, pGC, xorgi, yorgi, spanRec.points, spanRec.widths);
     else
-	n = miLineArcD(pDraw, pGC, xorg, yorg, points, widths,
+	n = miLineArcD(pDraw, pGC, xorg, yorg, spanRec.points, spanRec.widths,
 		       &edge1, edgey1, edgeleft1,
 		       &edge2, edgey2, edgeleft2);
+    spanRec.count = n;
 
     if (!spanData)
     {
-    	(*pGC->ops->FillSpans)(pDraw, pGC, n, points, widths, TRUE);
-    	xfree(widths);
-    	xfree(points);
+	(*pGC->ops->FillSpans)(pDraw, pGC, spanRec.count, spanRec.points, spanRec.widths, TRUE);
+	xfree(spanRec.widths);
+	xfree(spanRec.points);
     	if (pixel != oldPixel)
     	{
 	    DoChangeGC(pGC, GCForeground, &oldPixel, FALSE);
@@ -1157,10 +1118,7 @@ miLineArc (
     	}
     }
     else
-    {
-	spanRec.count = n;
 	AppendSpanGroup (pGC, pixel, &spanRec, spanData);
-    }
 }
 
 static void
-- 
1.7.0



More information about the xorg-devel mailing list