llvmpipe: Make more resilient to out-of-memory situations.
authorJosé Fonseca <jfonseca@vmware.com>
Wed, 9 Nov 2011 19:29:37 +0000 (19:29 +0000)
committerJosé Fonseca <jfonseca@vmware.com>
Mon, 14 Nov 2011 10:06:01 +0000 (10:06 +0000)
Most of the code was alright, but we were missing a few paths.

Reviewed-by: Brian Paul <brianp@vmware.com>
src/gallium/drivers/llvmpipe/lp_rast.c
src/gallium/drivers/llvmpipe/lp_screen.c
src/gallium/drivers/llvmpipe/lp_setup.c
src/gallium/drivers/llvmpipe/lp_state_setup.c
src/gallium/drivers/llvmpipe/lp_texture.c

index c4560bfe6c02dab56743f228bfc18579e3b30b2d..873c2612e89aaaa0b45c468373cdba0987a7b91f 100644 (file)
@@ -879,10 +879,14 @@ lp_rast_create( unsigned num_threads )
    unsigned i;
 
    rast = CALLOC_STRUCT(lp_rasterizer);
-   if(!rast)
-      return NULL;
+   if (!rast) {
+      goto no_rast;
+   }
 
    rast->full_scenes = lp_scene_queue_create();
+   if (!rast->full_scenes) {
+      goto no_full_scenes;
+   }
 
    for (i = 0; i < Elements(rast->tasks); i++) {
       struct lp_rasterizer_task *task = &rast->tasks[i];
@@ -902,6 +906,11 @@ lp_rast_create( unsigned num_threads )
    memset(lp_dummy_tile, 0, sizeof lp_dummy_tile);
 
    return rast;
+
+no_full_scenes:
+   FREE(rast);
+no_rast:
+   return NULL;
 }
 
 
index 942ec623cb40cfb7670f2836d6afceb888d80780..fac13e7dda9e9572c1ee9a6c732871789df62d71 100644 (file)
@@ -399,14 +399,13 @@ llvmpipe_create_screen(struct sw_winsys *winsys)
        return NULL;
 #endif
 
-   screen = CALLOC_STRUCT(llvmpipe_screen);
-
 #ifdef DEBUG
    LP_DEBUG = debug_get_flags_option("LP_DEBUG", lp_debug_flags, 0 );
 #endif
 
    LP_PERF = debug_get_flags_option("LP_PERF", lp_perf_flags, 0 );
 
+   screen = CALLOC_STRUCT(llvmpipe_screen);
    if (!screen)
       return NULL;
 
index 1479ed44f0fca3d5afb59994f8003fd5c14fb296..4f7d116d4861886e58f0b7b49b604e96eb1be0b8 100644 (file)
@@ -39,6 +39,7 @@
 #include "util/u_inlines.h"
 #include "util/u_memory.h"
 #include "util/u_pack_color.h"
+#include "draw/draw_pipe.h"
 #include "lp_context.h"
 #include "lp_memory.h"
 #include "lp_scene.h"
@@ -1010,11 +1011,13 @@ lp_setup_create( struct pipe_context *pipe,
                  struct draw_context *draw )
 {
    struct llvmpipe_screen *screen = llvmpipe_screen(pipe->screen);
-   struct lp_setup_context *setup = CALLOC_STRUCT(lp_setup_context);
+   struct lp_setup_context *setup;
    unsigned i;
 
-   if (!setup)
-      return NULL;
+   setup = CALLOC_STRUCT(lp_setup_context);
+   if (!setup) {
+      goto no_setup;
+   }
 
    lp_setup_init_vbuf(setup);
    
@@ -1025,8 +1028,9 @@ lp_setup_create( struct pipe_context *pipe,
 
    setup->num_threads = screen->num_threads;
    setup->vbuf = draw_vbuf_stage(draw, &setup->base);
-   if (!setup->vbuf)
-      goto fail;
+   if (!setup->vbuf) {
+      goto no_vbuf;
+   }
 
    draw_set_rasterize_stage(draw, setup->vbuf);
    draw_set_render(draw, &setup->base);
@@ -1034,6 +1038,9 @@ lp_setup_create( struct pipe_context *pipe,
    /* create some empty scenes */
    for (i = 0; i < MAX_SCENES; i++) {
       setup->scenes[i] = lp_scene_create( pipe );
+      if (!setup->scenes[i]) {
+         goto no_scenes;
+      }
    }
 
    setup->triangle = first_triangle;
@@ -1044,11 +1051,17 @@ lp_setup_create( struct pipe_context *pipe,
 
    return setup;
 
-fail:
-   if (setup->vbuf)
-      ;
+no_scenes:
+   for (i = 0; i < MAX_SCENES; i++) {
+      if (setup->scenes[i]) {
+         lp_scene_destroy(setup->scenes[i]);
+      }
+   }
 
+   setup->vbuf->destroy(setup->vbuf);
+no_vbuf:
    FREE(setup);
+no_setup:
    return NULL;
 }
 
index a9d9f4f466591eef7b74dcb0bd8301a5757aab4b..97c9238f989e902830c3551d7f70b79abd2c7294 100644 (file)
@@ -874,10 +874,11 @@ llvmpipe_update_setup(struct llvmpipe_context *lp)
       }
 
       variant = generate_setup_variant(lp->gallivm, key, lp);
-      insert_at_head(&lp->setup_variants_list, &variant->list_item_global);
-      lp->nr_setup_variants++;
-
-      llvmpipe_variant_count++;
+      if (variant) {
+         insert_at_head(&lp->setup_variants_list, &variant->list_item_global);
+         lp->nr_setup_variants++;
+         llvmpipe_variant_count++;
+      }
    }
 
    lp_setup_set_setup_variant(lp->setup,
index fa4ce5bf2ac20816a187eb8b210219512e5194c6..ca38571b2bc273d4a0da37142a2ba2d8fb06c8dc 100644 (file)
@@ -163,6 +163,9 @@ llvmpipe_texture_layout(struct llvmpipe_screen *screen,
          lpr->num_slices_faces[level] = num_slices;
 
          lpr->layout[level] = alloc_layout_array(num_slices, width, height);
+         if (!lpr->layout[level]) {
+            goto fail;
+         }
       }
 
       /* Compute size of next mipmap level */
@@ -172,6 +175,15 @@ llvmpipe_texture_layout(struct llvmpipe_screen *screen,
    }
 
    return TRUE;
+
+fail:
+   for (level = 0; level <= pt->last_level; level++) {
+      if (lpr->layout[level]) {
+         FREE(lpr->layout[level]);
+      }
+   }
+
+   return FALSE;
 }
 
 
@@ -196,7 +208,9 @@ llvmpipe_displaytarget_layout(struct llvmpipe_screen *screen,
    lpr->img_stride[0] = 0;
 
    lpr->layout[0] = alloc_layout_array(1, width, height);
-   //lpr->layout[0][0] = LP_TEX_LAYOUT_LINEAR;
+   if (!lpr->layout[0]) {
+      return FALSE;
+   }
 
    lpr->dt = winsys->displaytarget_create(winsys,
                                           lpr->base.bind,
@@ -437,13 +451,15 @@ llvmpipe_resource_from_handle(struct pipe_screen *screen,
                              struct winsys_handle *whandle)
 {
    struct sw_winsys *winsys = llvmpipe_screen(screen)->winsys;
-   struct llvmpipe_resource *lpr = CALLOC_STRUCT(llvmpipe_resource);
+   struct llvmpipe_resource *lpr;
    unsigned width, height, width_t, height_t;
 
    /* XXX Seems like from_handled depth textures doesn't work that well */
 
-   if (!lpr)
-      return NULL;
+   lpr = CALLOC_STRUCT(llvmpipe_resource);
+   if (!lpr) {
+      goto no_lpr;
+   }
 
    lpr->base = *template;
    pipe_reference_init(&lpr->base.reference, 1);
@@ -472,12 +488,15 @@ llvmpipe_resource_from_handle(struct pipe_screen *screen,
                                                template,
                                                whandle,
                                                &lpr->row_stride[0]);
-   if (!lpr->dt)
-      goto fail;
+   if (!lpr->dt) {
+      goto no_dt;
+   }
 
    lpr->layout[0] = alloc_layout_array(1, lpr->base.width0, lpr->base.height0);
+   if (!lpr->layout[0]) {
+      goto no_layout_0;
+   }
 
-   assert(lpr->layout[0]);
    assert(lpr->layout[0][0] == LP_TEX_LAYOUT_NONE);
 
    lpr->id = id_counter++;
@@ -488,8 +507,11 @@ llvmpipe_resource_from_handle(struct pipe_screen *screen,
 
    return &lpr->base;
 
- fail:
+no_layout_0:
+   winsys->displaytarget_destroy(winsys, lpr->dt);
+no_dt:
    FREE(lpr);
+no_lpr:
    return NULL;
 }