radeon: Fix memory leak in radeonCreateScreen2.
[mesa.git] / src / mesa / swrast / s_copypix.c
index 91541d2c93c85030b94b7adc85dd9af4afdde8f9..bbd1f228c3925f82c3e982b7c51f570287a37765 100644 (file)
@@ -139,7 +139,7 @@ copy_rgba_pixels(struct gl_context *ctx, GLint srcx, GLint srcy,
    span.arrayAttribs = FRAG_BIT_COL0; /* we'll fill in COL0 attrib values */
 
    if (overlapping) {
-      tmpImage = (GLfloat *) malloc(width * height * sizeof(GLfloat) * 4);
+      tmpImage = malloc(width * height * sizeof(GLfloat) * 4);
       if (!tmpImage) {
          _mesa_error( ctx, GL_OUT_OF_MEMORY, "glCopyPixels" );
          return;
@@ -158,7 +158,7 @@ copy_rgba_pixels(struct gl_context *ctx, GLint srcx, GLint srcy,
       p = NULL;
    }
 
-   ASSERT(width < MAX_WIDTH);
+   ASSERT(width < SWRAST_MAX_WIDTH);
 
    for (row = 0; row < height; row++, sy += stepy, dy += stepy) {
       GLvoid *rgba = span.array->attribs[FRAG_ATTRIB_COL0];
@@ -246,7 +246,7 @@ copy_depth_pixels( struct gl_context *ctx, GLint srcx, GLint srcy,
 {
    struct gl_framebuffer *fb = ctx->ReadBuffer;
    struct gl_renderbuffer *readRb = fb->Attachment[BUFFER_DEPTH].Renderbuffer;
-   GLfloat *p, *tmpImage;
+   GLfloat *p, *tmpImage, *depth;
    GLint sy, dy, stepy;
    GLint j;
    const GLboolean zoom = ctx->Pixel.ZoomX != 1.0F || ctx->Pixel.ZoomY != 1.0F;
@@ -286,7 +286,7 @@ copy_depth_pixels( struct gl_context *ctx, GLint srcx, GLint srcy,
 
    if (overlapping) {
       GLint ssy = sy;
-      tmpImage = (GLfloat *) malloc(width * height * sizeof(GLfloat));
+      tmpImage = malloc(width * height * sizeof(GLfloat));
       if (!tmpImage) {
          _mesa_error( ctx, GL_OUT_OF_MEMORY, "glCopyPixels" );
          return;
@@ -303,8 +303,13 @@ copy_depth_pixels( struct gl_context *ctx, GLint srcx, GLint srcy,
       p = NULL;
    }
 
+   depth = malloc(width * sizeof(GLfloat));
+   if (!depth) {
+      _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyPixels()");
+      goto end;
+   }
+
    for (j = 0; j < height; j++, sy += stepy, dy += stepy) {
-      GLfloat depth[MAX_WIDTH];
       /* get depth values */
       if (overlapping) {
          memcpy(depth, p, width * sizeof(GLfloat));
@@ -327,6 +332,9 @@ copy_depth_pixels( struct gl_context *ctx, GLint srcx, GLint srcy,
          _swrast_write_rgba_span(ctx, &span);
    }
 
+   free(depth);
+
+end:
    if (overlapping)
       free(tmpImage);
 }
@@ -342,7 +350,7 @@ copy_stencil_pixels( struct gl_context *ctx, GLint srcx, GLint srcy,
    struct gl_renderbuffer *rb = fb->Attachment[BUFFER_STENCIL].Renderbuffer;
    GLint sy, dy, stepy;
    GLint j;
-   GLubyte *p, *tmpImage;
+   GLubyte *p, *tmpImage, *stencil;
    const GLboolean zoom = ctx->Pixel.ZoomX != 1.0F || ctx->Pixel.ZoomY != 1.0F;
    GLint overlapping;
 
@@ -375,7 +383,7 @@ copy_stencil_pixels( struct gl_context *ctx, GLint srcx, GLint srcy,
 
    if (overlapping) {
       GLint ssy = sy;
-      tmpImage = (GLubyte *) malloc(width * height * sizeof(GLubyte));
+      tmpImage = malloc(width * height * sizeof(GLubyte));
       if (!tmpImage) {
          _mesa_error( ctx, GL_OUT_OF_MEMORY, "glCopyPixels" );
          return;
@@ -392,9 +400,13 @@ copy_stencil_pixels( struct gl_context *ctx, GLint srcx, GLint srcy,
       p = NULL;
    }
 
-   for (j = 0; j < height; j++, sy += stepy, dy += stepy) {
-      GLubyte stencil[MAX_WIDTH];
+   stencil = malloc(width * sizeof(GLubyte));
+   if (!stencil) {
+      _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyPixels()");
+      goto end;
+   }
 
+   for (j = 0; j < height; j++, sy += stepy, dy += stepy) {
       /* Get stencil values */
       if (overlapping) {
          memcpy(stencil, p, width * sizeof(GLubyte));
@@ -416,6 +428,9 @@ copy_stencil_pixels( struct gl_context *ctx, GLint srcx, GLint srcy,
       }
    }
 
+   free(stencil);
+
+end:
    if (overlapping)
       free(tmpImage);
 }
@@ -567,6 +582,7 @@ map_readbuffer(struct gl_context *ctx, GLenum type)
 {
    struct gl_framebuffer *fb = ctx->ReadBuffer;
    struct gl_renderbuffer *rb;
+   struct swrast_renderbuffer *srb;
 
    switch (type) {
    case GL_COLOR:
@@ -583,7 +599,9 @@ map_readbuffer(struct gl_context *ctx, GLenum type)
       return NULL;
    }
 
-   if (!rb || rb->Map) {
+   srb = swrast_renderbuffer(rb);
+
+   if (!srb || srb->Map) {
       /* no buffer, or buffer is mapped already, we're done */
       return NULL;
    }
@@ -591,7 +609,7 @@ map_readbuffer(struct gl_context *ctx, GLenum type)
    ctx->Driver.MapRenderbuffer(ctx, rb,
                                0, 0, rb->Width, rb->Height,
                                GL_MAP_READ_BIT,
-                               &rb->Map, &rb->RowStrideBytes);
+                               &srb->Map, &srb->RowStride);
 
    return rb;
 }
@@ -650,7 +668,8 @@ _swrast_CopyPixels( struct gl_context *ctx,
    swrast_render_finish(ctx);
 
    if (rb) {
+      struct swrast_renderbuffer *srb = swrast_renderbuffer(rb);
       ctx->Driver.UnmapRenderbuffer(ctx, rb);
-      rb->Map = NULL;
+      srb->Map = NULL;
    }
 }