r300: fix valgrind warnings
[mesa.git] / src / mesa / drivers / dri / common / dri_util.c
index a9e37ca51eb77204a687269cbf7c77bbe3c74aee..ae0e61e515b77d90b37ffb59eff1f3d4beb96fd9 100644 (file)
@@ -37,6 +37,9 @@
 typedef GLboolean ( * PFNGLXGETMSCRATEOMLPROC) (__DRIdrawable *drawable, int32_t *numerator, int32_t *denominator);
 #endif
 
+static void dri_get_drawable(__DRIdrawable *pdp);
+static void dri_put_drawable(__DRIdrawable *pdp);
+
 /**
  * This is just a token extension used to signal that the driver
  * supports setting a read drawable.
@@ -59,7 +62,7 @@ __driUtilMessage(const char *f, ...)
     va_list args;
 
     if (getenv("LIBGL_DEBUG")) {
-        fprintf(stderr, "libGL error: \n");
+        fprintf(stderr, "libGL");
         va_start(args, f);
         vfprintf(stderr, f, args);
         va_end(args);
@@ -127,7 +130,7 @@ static int driUnbindContext(__DRIcontext *pcp)
        return GL_FALSE;
     }
 
-    pdp->refcount--;
+    dri_put_drawable(pdp);
 
     if (prp != pdp) {
         if (prp->refcount == 0) {
@@ -135,7 +138,7 @@ static int driUnbindContext(__DRIcontext *pcp)
            return GL_FALSE;
        }
 
-       prp->refcount--;
+       dri_put_drawable(prp);
     }
 
 
@@ -163,21 +166,18 @@ static int driBindContext(__DRIcontext *pcp,
 {
     __DRIscreenPrivate *psp = pcp->driScreenPriv;
 
-    /*
-    ** Assume error checking is done properly in glXMakeCurrent before
-    ** calling driBindContext.
-    */
-
-    if (pcp == NULL || pdp == None || prp == None)
-       return GL_FALSE;
-
     /* Bind the drawable to the context */
-    pcp->driDrawablePriv = pdp;
-    pcp->driReadablePriv = prp;
-    pdp->driContextPriv = pcp;
-    pdp->refcount++;
-    if ( pdp != prp ) {
-       prp->refcount++;
+
+    if (pcp) {
+       pcp->driDrawablePriv = pdp;
+       pcp->driReadablePriv = prp;
+       if (pdp) {
+           pdp->driContextPriv = pcp;
+           dri_get_drawable(pdp);
+       }
+       if ( prp && pdp != prp ) {
+           dri_get_drawable(prp);
+       }
     }
 
     /*
@@ -186,17 +186,16 @@ static int driBindContext(__DRIcontext *pcp,
     */
 
     if (!psp->dri2.enabled) {
-       if (!pdp->pStamp || *pdp->pStamp != pdp->lastStamp) {
+       if (pdp && !pdp->pStamp) {
            DRM_SPINLOCK(&psp->pSAREA->drawable_lock, psp->drawLockID);
            __driUtilUpdateDrawableInfo(pdp);
            DRM_SPINUNLOCK(&psp->pSAREA->drawable_lock, psp->drawLockID);
        }
-       
-       if ((pdp != prp) && (!prp->pStamp || *prp->pStamp != prp->lastStamp)) {
+       if (prp && pdp != prp && !prp->pStamp) {
            DRM_SPINLOCK(&psp->pSAREA->drawable_lock, psp->drawLockID);
            __driUtilUpdateDrawableInfo(prp);
            DRM_SPINUNLOCK(&psp->pSAREA->drawable_lock, psp->drawLockID);
-       }
+        }
     }
 
     /* Call device-specific MakeCurrent */
@@ -315,12 +314,12 @@ static void driSwapBuffers(__DRIdrawable *dPriv)
     __DRIscreen *psp = dPriv->driScreenPriv;
     drm_clip_rect_t *rects;
     int i;
-    
-    if (!dPriv->numClipRects)
-        return;
 
     psp->DriverAPI.SwapBuffers(dPriv);
 
+    if (!dPriv->numClipRects)
+        return;
+
     rects = _mesa_malloc(sizeof(*rects) * dPriv->numClipRects);
 
     if (!rects)
@@ -434,7 +433,7 @@ driCreateNewDrawable(__DRIscreen *psp, const __DRIconfig *config,
 
     pdp->loaderPrivate = data;
     pdp->hHWDrawable = hwDrawable;
-    pdp->refcount = 0;
+    pdp->refcount = 1;
     pdp->pStamp = NULL;
     pdp->lastStamp = 0;
     pdp->index = 0;
@@ -487,12 +486,19 @@ dri2CreateNewDrawable(__DRIscreen *screen,
     return pdraw;
 }
 
-
-static void
-driDestroyDrawable(__DRIdrawable *pdp)
+static void dri_get_drawable(__DRIdrawable *pdp)
+{
+    pdp->refcount++;
+}
+       
+static void dri_put_drawable(__DRIdrawable *pdp)
 {
     __DRIscreenPrivate *psp;
 
+    pdp->refcount--;
+    if (pdp->refcount)
+       return;
+
     if (pdp) {
        psp = pdp->driScreenPriv;
         (*psp->DriverAPI.DestroyBuffer)(pdp);
@@ -508,6 +514,12 @@ driDestroyDrawable(__DRIdrawable *pdp)
     }
 }
 
+static void
+driDestroyDrawable(__DRIdrawable *pdp)
+{
+    dri_put_drawable(pdp);
+}
+
 /*@}*/