mesa/swrast: Move memory allocation outside the blit loop
authorIan Romanick <ian.d.romanick@intel.com>
Mon, 15 Apr 2013 19:25:18 +0000 (12:25 -0700)
committerIan Romanick <ian.d.romanick@intel.com>
Tue, 16 Apr 2013 17:18:14 +0000 (10:18 -0700)
Assume the maximum pixel size (16 bytes per pixel).  In addition to
moving redundant malloc and free calls outside the loop, this fixes a
potential resource leak when a surface is mapped and the malloc fails.
This also makes blit_nearest look a bit more like blit_linear.

v2: Use MAX_PIXEL_BYTES instead of 16.  Suggested by Ken.

Signed-off-by: Ian Romanick <ian.d.romanick@intel.com>
Reviewed-by: Brian Paul <brianp@vmware.com>
Reviewed-by: Kenneth Graunke <kenneth@whitecape.org>
src/mesa/swrast/s_blit.c

index ecec734c233debec6a2dc2ff528c0b96165e7bfb..0e24c0e4257dc57ea888d38313f754efd38360a5 100644 (file)
@@ -188,6 +188,12 @@ blit_nearest(struct gl_context *ctx,
       return;
    }
 
+   /* allocate the src/dst row buffers */
+   srcBuffer = malloc(MAX_PIXEL_BYTES * srcWidth);
+   dstBuffer = malloc(MAX_PIXEL_BYTES * dstWidth);
+   if (!srcBuffer || !dstBuffer)
+      goto fail_no_memory;
+
    /* Blit to all the draw buffers */
    for (i = 0; i < numDrawBuffers; i++) {
       if (buffer == GL_COLOR_BUFFER_BIT) {
@@ -229,7 +235,7 @@ blit_nearest(struct gl_context *ctx,
       default:
          _mesa_problem(ctx, "unexpected pixel size (%d) in blit_nearest",
                        pixelSize);
-         return;
+         goto fail;
       }
 
       if ((readRb == drawRb) ||
@@ -248,8 +254,7 @@ blit_nearest(struct gl_context *ctx,
                                      GL_MAP_READ_BIT | GL_MAP_WRITE_BIT,
                                      &map, &rowStride);
          if (!map) {
-            _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBlitFramebuffer");
-            return;
+            goto fail_no_memory;
          }
 
          srcMap = map + srcYpos * rowStride + srcXpos * formatSize;
@@ -276,8 +281,7 @@ blit_nearest(struct gl_context *ctx,
                                      srcWidth, srcHeight,
                                      GL_MAP_READ_BIT, &srcMap, &srcRowStride);
          if (!srcMap) {
-            _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBlitFramebuffer");
-            return;
+            goto fail_no_memory;
          }
          ctx->Driver.MapRenderbuffer(ctx, drawRb,
                                      dstXpos, dstYpos,
@@ -285,24 +289,10 @@ blit_nearest(struct gl_context *ctx,
                                      GL_MAP_WRITE_BIT, &dstMap, &dstRowStride);
          if (!dstMap) {
             ctx->Driver.UnmapRenderbuffer(ctx, readRb);
-            _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBlitFramebuffer");
-            return;
+            goto fail_no_memory;
          }
       }
 
-      /* allocate the src/dst row buffers */
-      srcBuffer = malloc(pixelSize * srcWidth);
-      if (!srcBuffer) {
-         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBlitFrameBufferEXT");
-         return;
-      }
-      dstBuffer = malloc(pixelSize * dstWidth);
-      if (!dstBuffer) {
-         free(srcBuffer);
-         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBlitFrameBufferEXT");
-         return;
-      }
-
       for (dstRow = 0; dstRow < dstHeight; dstRow++) {
          GLfloat srcRowF = (dstRow + 0.5F) / dstHeight * srcHeight - 0.5F;
          GLint srcRow = IROUND(srcRowF);
@@ -369,14 +359,21 @@ blit_nearest(struct gl_context *ctx,
          }
       }
 
-      free(srcBuffer);
-      free(dstBuffer);
-
       ctx->Driver.UnmapRenderbuffer(ctx, readRb);
       if (drawRb != readRb) {
          ctx->Driver.UnmapRenderbuffer(ctx, drawRb);
       }
    }
+
+fail:
+   free(srcBuffer);
+   free(dstBuffer);
+   return;
+
+fail_no_memory:
+   free(srcBuffer);
+   free(dstBuffer);
+   _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBlitFrameBuffer");
 }