vbo/dlist: realloc prims array instead of free/malloc
authorPierre-Eric Pelloux-Prayer <pierre-eric.pelloux-prayer@amd.com>
Thu, 12 Aug 2021 12:06:08 +0000 (14:06 +0200)
committerPierre-Eric Pelloux-Prayer <pierre-eric.pelloux-prayer@amd.com>
Thu, 9 Sep 2021 14:42:16 +0000 (16:42 +0200)
Now that we don't have links to the prim_store->prims pointers, we
can replace the free/malloc by a realloc version to grow the prim
storage as needed by a list.

This will be used in the next commit to avoid splitting lists in
multiple vbo_save_vertex_list instances because the prim_store was full.

Reviewed-by: Marek Olšák <marek.olsak@amd.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/12646>

src/mesa/vbo/vbo_save_api.c

index 50279ca..e2fd009 100644 (file)
@@ -170,12 +170,14 @@ free_vertex_store(struct gl_context *ctx,
 
 
 static struct vbo_save_primitive_store *
-alloc_prim_store(int prim_count)
+realloc_prim_store(struct vbo_save_primitive_store *store, int prim_count)
 {
-   struct vbo_save_primitive_store *store =
-      CALLOC_STRUCT(vbo_save_primitive_store);
-   store->size = MAX2(prim_count, VBO_SAVE_PRIM_SIZE);
-   store->prims = calloc(store->size, sizeof(struct _mesa_prim));
+   if (store == NULL)
+      store = CALLOC_STRUCT(vbo_save_primitive_store);
+   uint32_t old_size = store->size;
+   store->size = MAX3(store->size, prim_count, VBO_SAVE_PRIM_SIZE);
+   store->prims = realloc(store->prims, store->size * sizeof(struct _mesa_prim));
+   memset(&store->prims[old_size], 0, (store->size - old_size) * sizeof(struct _mesa_prim));
    store->used = 0;
    return store;
 }
@@ -419,11 +421,8 @@ realloc_storage(struct gl_context *ctx, int prim_count, int vertex_count)
       save->out_of_memory = save->buffer_ptr == NULL;
    }
 
-   if (prim_count >= 0) {
-      free(save->prim_store->prims);
-      free(save->prim_store);
-      save->prim_store = alloc_prim_store(prim_count);
-   }
+   if (prim_count >= 0)
+      save->prim_store = realloc_prim_store(save->prim_store, prim_count);
 }
 
 struct vertex_key {
@@ -565,7 +564,6 @@ compile_vertex_list(struct gl_context *ctx)
       ctx->ListState.Current.UseLoopback = true;
 
    save->vertex_store->used += save->vertex_size * node->cold->vertex_count;
-   save->prim_store->used += node->cold->prim_count;
 
    /* Copy duplicated vertices
     */
@@ -1952,7 +1950,7 @@ vbo_save_NewList(struct gl_context *ctx, GLuint list, GLenum mode)
    (void) mode;
 
    if (!save->prim_store)
-      save->prim_store = alloc_prim_store(0);
+      save->prim_store = realloc_prim_store(NULL, 8);
 
    if (!save->vertex_store)
       save->vertex_store = alloc_vertex_store(ctx, 0);