[Mesa-dev] [PATCH 04/11] compiler/blob: Switch to init/finsih instead of create/destroy

Jason Ekstrand jason at jlekstrand.net
Wed Oct 11 20:38:44 UTC 2017


There's no reason why that tiny bit of memory needs to be on the heap.
We always put blob_reader on the stack, so why not do the same with the
writable blob.
---
 src/compiler/blob.c                      | 10 ++------
 src/compiler/blob.h                      | 11 ++++-----
 src/compiler/glsl/shader_cache.cpp       | 39 ++++++++++++++++----------------
 src/mesa/state_tracker/st_shader_cache.c | 23 ++++++++++---------
 4 files changed, 38 insertions(+), 45 deletions(-)

diff --git a/src/compiler/blob.c b/src/compiler/blob.c
index 041a352..0b42871 100644
--- a/src/compiler/blob.c
+++ b/src/compiler/blob.c
@@ -99,18 +99,12 @@ align_blob_reader(struct blob_reader *blob, size_t alignment)
    blob->current = blob->data + ALIGN(blob->current - blob->data, alignment);
 }
 
-struct blob *
-blob_create()
+void
+blob_init(struct blob *blob)
 {
-   struct blob *blob = (struct blob *) malloc(sizeof(struct blob));
-   if (blob == NULL)
-      return NULL;
-
    blob->data = NULL;
    blob->allocated = 0;
    blob->size = 0;
-
-   return blob;
 }
 
 bool
diff --git a/src/compiler/blob.h b/src/compiler/blob.h
index 4cbbb01..fd13a16 100644
--- a/src/compiler/blob.h
+++ b/src/compiler/blob.h
@@ -79,21 +79,18 @@ struct blob_reader {
 };
 
 /**
- * Create a new, empty blob.
- *
- * \return The new blob, (or NULL in case of allocation failure).
+ * Init a new, empty blob.
  */
-struct blob *
-blob_create(void);
+void
+blob_init(struct blob *blob);
 
 /**
  * Destroy a blob and free its memory.
  */
 static inline void
-blob_destroy(struct blob *blob)
+blob_finish(struct blob *blob)
 {
    free(blob->data);
-   free(blob);
 }
 
 /**
diff --git a/src/compiler/glsl/shader_cache.cpp b/src/compiler/glsl/shader_cache.cpp
index c9109aa..f3c7a57 100644
--- a/src/compiler/glsl/shader_cache.cpp
+++ b/src/compiler/glsl/shader_cache.cpp
@@ -1356,52 +1356,53 @@ shader_cache_write_program_metadata(struct gl_context *ctx,
    if (memcmp(prog->data->sha1, zero, sizeof(prog->data->sha1)) == 0)
       return;
 
-   struct blob *metadata = blob_create();
+   struct blob metadata;
+   blob_init(&metadata);
 
-   write_uniforms(metadata, prog);
+   write_uniforms(&metadata, prog);
 
-   write_hash_tables(metadata, prog);
+   write_hash_tables(&metadata, prog);
 
-   blob_write_uint32(metadata, prog->data->Version);
-   blob_write_uint32(metadata, prog->data->linked_stages);
+   blob_write_uint32(&metadata, prog->data->Version);
+   blob_write_uint32(&metadata, prog->data->linked_stages);
 
    for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
       struct gl_linked_shader *sh = prog->_LinkedShaders[i];
       if (sh) {
-         write_shader_metadata(metadata, sh);
+         write_shader_metadata(&metadata, sh);
 
          if (sh->Program->info.name)
-            blob_write_string(metadata, sh->Program->info.name);
+            blob_write_string(&metadata, sh->Program->info.name);
          else
-            blob_write_string(metadata, "");
+            blob_write_string(&metadata, "");
 
          if (sh->Program->info.label)
-            blob_write_string(metadata, sh->Program->info.label);
+            blob_write_string(&metadata, sh->Program->info.label);
          else
-            blob_write_string(metadata, "");
+            blob_write_string(&metadata, "");
 
          size_t s_info_size, s_info_ptrs;
          get_shader_info_and_pointer_sizes(&s_info_size, &s_info_ptrs,
                                            &sh->Program->info);
 
          /* Store shader info */
-         blob_write_bytes(metadata,
+         blob_write_bytes(&metadata,
                           ((char *) &sh->Program->info) + s_info_ptrs,
                           s_info_size - s_info_ptrs);
       }
    }
 
-   write_xfb(metadata, prog);
+   write_xfb(&metadata, prog);
 
-   write_uniform_remap_tables(metadata, prog);
+   write_uniform_remap_tables(&metadata, prog);
 
-   write_atomic_buffers(metadata, prog);
+   write_atomic_buffers(&metadata, prog);
 
-   write_buffer_blocks(metadata, prog);
+   write_buffer_blocks(&metadata, prog);
 
-   write_subroutines(metadata, prog);
+   write_subroutines(&metadata, prog);
 
-   write_program_resource_list(metadata, prog);
+   write_program_resource_list(&metadata, prog);
 
    struct cache_item_metadata cache_item_metadata;
    cache_item_metadata.type = CACHE_ITEM_TYPE_GLSL;
@@ -1423,7 +1424,7 @@ shader_cache_write_program_metadata(struct gl_context *ctx,
       }
    }
 
-   disk_cache_put(cache, prog->data->sha1, metadata->data, metadata->size,
+   disk_cache_put(cache, prog->data->sha1, metadata.data, metadata.size,
                   &cache_item_metadata);
 
    if (ctx->_Shader->Flags & GLSL_CACHE_INFO) {
@@ -1433,7 +1434,7 @@ shader_cache_write_program_metadata(struct gl_context *ctx,
 
 fail:
    free(cache_item_metadata.keys);
-   blob_destroy(metadata);
+   blob_finish(&metadata);
 }
 
 bool
diff --git a/src/mesa/state_tracker/st_shader_cache.c b/src/mesa/state_tracker/st_shader_cache.c
index 2c38f27..2e9b7cc 100644
--- a/src/mesa/state_tracker/st_shader_cache.c
+++ b/src/mesa/state_tracker/st_shader_cache.c
@@ -69,21 +69,22 @@ st_store_tgsi_in_disk_cache(struct st_context *st, struct gl_program *prog,
       return;
 
    unsigned char *sha1;
-   struct blob *blob = blob_create();
+   struct blob blob;
+   blob_init(&blob);
 
    switch (prog->info.stage) {
    case MESA_SHADER_VERTEX: {
       struct st_vertex_program *stvp = (struct st_vertex_program *) prog;
       sha1 = stvp->sha1;
 
-      blob_write_uint32(blob, stvp->num_inputs);
-      blob_write_bytes(blob, stvp->index_to_input,
+      blob_write_uint32(&blob, stvp->num_inputs);
+      blob_write_bytes(&blob, stvp->index_to_input,
                        sizeof(stvp->index_to_input));
-      blob_write_bytes(blob, stvp->result_to_output,
+      blob_write_bytes(&blob, stvp->result_to_output,
                        sizeof(stvp->result_to_output));
 
-      write_stream_out_to_cache(blob, &stvp->tgsi);
-      write_tgsi_to_cache(blob, &stvp->tgsi, st, sha1, num_tokens);
+      write_stream_out_to_cache(&blob, &stvp->tgsi);
+      write_tgsi_to_cache(&blob, &stvp->tgsi, st, sha1, num_tokens);
       break;
    }
    case MESA_SHADER_TESS_CTRL:
@@ -92,22 +93,22 @@ st_store_tgsi_in_disk_cache(struct st_context *st, struct gl_program *prog,
       struct st_common_program *p = st_common_program(prog);
       sha1 = p->sha1;
 
-      write_stream_out_to_cache(blob, out_state);
-      write_tgsi_to_cache(blob, out_state, st, sha1, num_tokens);
+      write_stream_out_to_cache(&blob, out_state);
+      write_tgsi_to_cache(&blob, out_state, st, sha1, num_tokens);
       break;
    }
    case MESA_SHADER_FRAGMENT: {
       struct st_fragment_program *stfp = (struct st_fragment_program *) prog;
       sha1 = stfp->sha1;
 
-      write_tgsi_to_cache(blob, &stfp->tgsi, st, sha1, num_tokens);
+      write_tgsi_to_cache(&blob, &stfp->tgsi, st, sha1, num_tokens);
       break;
    }
    case MESA_SHADER_COMPUTE: {
       struct st_compute_program *stcp = (struct st_compute_program *) prog;
       sha1 = stcp->sha1;
 
-      write_tgsi_to_cache(blob, out_state, st, sha1, num_tokens);
+      write_tgsi_to_cache(&blob, out_state, st, sha1, num_tokens);
       break;
    }
    default:
@@ -121,7 +122,7 @@ st_store_tgsi_in_disk_cache(struct st_context *st, struct gl_program *prog,
               _mesa_shader_stage_to_string(prog->info.stage), sha1_buf);
    }
 
-   blob_destroy(blob);
+   blob_finish(&blob);
 }
 
 static void
-- 
2.5.0.400.gff86faf



More information about the mesa-dev mailing list