[Mesa-dev] [PATCH 1/8] glsl: don't use ralloc for blob creation

Timothy Arceri tarceri at itsqueeze.com
Fri Mar 10 02:28:48 UTC 2017


There is no need to use ralloc here.
---
 src/compiler/glsl/blob.c                 |  9 +++------
 src/compiler/glsl/blob.h                 |  4 ++--
 src/compiler/glsl/shader_cache.cpp       |  4 ++--
 src/compiler/glsl/tests/blob_test.c      | 18 ++++++++----------
 src/mesa/state_tracker/st_shader_cache.c |  4 ++--
 5 files changed, 17 insertions(+), 22 deletions(-)

diff --git a/src/compiler/glsl/blob.c b/src/compiler/glsl/blob.c
index 14dc690..769ebf1 100644
--- a/src/compiler/glsl/blob.c
+++ b/src/compiler/glsl/blob.c
@@ -17,21 +17,20 @@
  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  * IN THE SOFTWARE.
  */
 
 #include <string.h>
 
 #include "main/macros.h"
-#include "util/ralloc.h"
 #include "blob.h"
 
 #define BLOB_INITIAL_SIZE 4096
 
 /* Ensure that \blob will be able to fit an additional object of size
  * \additional.  The growing (if any) will occur by doubling the existing
  * allocation.
  */
 static bool
 grow_to_fit(struct blob *blob, size_t additional)
@@ -42,21 +41,21 @@ grow_to_fit(struct blob *blob, size_t additional)
    if (blob->size + additional <= blob->allocated)
       return true;
 
    if (blob->allocated == 0)
       to_allocate = BLOB_INITIAL_SIZE;
    else
       to_allocate = blob->allocated * 2;
 
    to_allocate = MAX2(to_allocate, blob->allocated + additional);
 
-   new_data = reralloc_size(blob, blob->data, to_allocate);
+   new_data = realloc(blob->data, to_allocate);
    if (new_data == NULL)
       return false;
 
    blob->data = new_data;
    blob->allocated = to_allocate;
 
    return true;
 }
 
 /* Align the blob->size so that reading or writing a value at (blob->data +
@@ -81,25 +80,23 @@ align_blob(struct blob *blob, size_t alignment)
    return true;
 }
 
 static void
 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 *mem_ctx)
+blob_create()
 {
-   struct blob *blob;
-
-   blob = ralloc(mem_ctx, struct 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;
 }
 
diff --git a/src/compiler/glsl/blob.h b/src/compiler/glsl/blob.h
index 21fa43d..6d21ffd 100644
--- a/src/compiler/glsl/blob.h
+++ b/src/compiler/glsl/blob.h
@@ -66,26 +66,26 @@ struct blob {
  *   2. blob->overrun should be false, (otherwise, too much was read).
  */
 struct blob_reader {
    uint8_t *data;
    uint8_t *end;
    uint8_t *current;
    bool overrun;
 };
 
 /**
- * Create a new, empty blob, belonging to \mem_ctx.
+ * Create a new, empty blob.
  *
  * \return The new blob, (or NULL in case of allocation failure).
  */
 struct blob *
-blob_create(void *mem_ctx);
+blob_create(void);
 
 /**
  * Add some unstructured, fixed-size data to a blob.
  *
  * \return True unless allocation failed.
  */
 bool
 blob_write_bytes(struct blob *blob, const void *bytes, size_t to_write);
 
 /**
diff --git a/src/compiler/glsl/shader_cache.cpp b/src/compiler/glsl/shader_cache.cpp
index 4450657..6e2c527 100644
--- a/src/compiler/glsl/shader_cache.cpp
+++ b/src/compiler/glsl/shader_cache.cpp
@@ -1223,21 +1223,21 @@ shader_cache_write_program_metadata(struct gl_context *ctx,
 
    /* Exit early when we are dealing with a ff shader with no source file to
     * generate a source from.
     *
     * TODO: In future we should use another method to generate a key for ff
     * programs.
     */
    if (*prog->data->sha1 == 0)
       return;
 
-   struct blob *metadata = blob_create(NULL);
+   struct blob *metadata = blob_create();
 
    write_uniforms(ctx, metadata, prog);
 
    write_hash_tables(metadata, prog);
 
    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];
@@ -1271,21 +1271,21 @@ shader_cache_write_program_metadata(struct gl_context *ctx,
    for (unsigned i = 0; i < prog->NumShaders; i++) {
       disk_cache_put_key(cache, prog->Shaders[i]->sha1);
       if (ctx->_Shader->Flags & GLSL_CACHE_INFO) {
          fprintf(stderr, "marking shader: %s\n",
                  _mesa_sha1_format(sha1_buf, prog->Shaders[i]->sha1));
       }
    }
 
    disk_cache_put(cache, prog->data->sha1, metadata->data, metadata->size);
 
-   ralloc_free(metadata);
+   free(metadata);
 
    if (ctx->_Shader->Flags & GLSL_CACHE_INFO) {
       fprintf(stderr, "putting program metadata in cache: %s\n",
               _mesa_sha1_format(sha1_buf, prog->data->sha1));
    }
 }
 
 bool
 shader_cache_read_program_metadata(struct gl_context *ctx,
                                    struct gl_shader_program *prog)
diff --git a/src/compiler/glsl/tests/blob_test.c b/src/compiler/glsl/tests/blob_test.c
index 09114b1..01b5ef0 100644
--- a/src/compiler/glsl/tests/blob_test.c
+++ b/src/compiler/glsl/tests/blob_test.c
@@ -111,28 +111,27 @@ expect_equal_bytes(uint8_t *expected, uint8_t *actual,
    }
 }
 
 /* Test at least one call of each blob_write_foo and blob_read_foo function,
  * verifying that we read out everything we wrote, that every bytes is
  * consumed, and that the overrun bit is not set.
  */
 static void
 test_write_and_read_functions (void)
 {
-   void *ctx = ralloc_context(NULL);
    struct blob *blob;
    struct blob_reader reader;
    uint8_t *reserved;
    size_t str_offset, uint_offset;
    uint8_t reserve_buf[sizeof(reserve_test_str)];
 
-   blob = blob_create(ctx);
+   blob = blob_create();
 
    /*** Test blob by writing one of every possible kind of value. */
 
    blob_write_bytes(blob, bytes_test_str, sizeof(bytes_test_str));
 
    reserved = blob_reserve_bytes(blob, sizeof(reserve_test_str));
    memcpy(reserved, reserve_test_str, sizeof(reserve_test_str));
 
    /* Write a placeholder, (to be replaced later via overwrite_bytes) */
    str_offset = blob->size;
@@ -178,34 +177,33 @@ test_write_and_read_functions (void)
                 "blob_write/read_uint64");
    expect_equal((intptr_t) blob, blob_read_intptr(&reader),
                 "blob_write/read_intptr");
    expect_equal_str(string_test_str, blob_read_string(&reader),
                     "blob_write/read_string");
 
    expect_equal(reader.end - reader.data, reader.current - reader.data,
                 "read_consumes_all_bytes");
    expect_equal(false, reader.overrun, "read_does_not_overrun");
 
-   ralloc_free(ctx);
+   free(blob);
 }
 
 /* Test that data values are written and read with proper alignment. */
 static void
 test_alignment(void)
 {
-   void *ctx = ralloc_context(NULL);
    struct blob *blob;
    struct blob_reader reader;
    uint8_t bytes[] = "ABCDEFGHIJKLMNOP";
    size_t delta, last, num_bytes;
 
-   blob = blob_create(ctx);
+   blob = blob_create();
 
    /* First, write an intptr value to the blob and capture that size. This is
     * the expected offset between any pair of intptr values (if written with
     * alignment).
     */
    blob_write_intptr(blob, (intptr_t) blob);
 
    delta = blob->size;
    last = blob->size;
 
@@ -237,61 +235,60 @@ test_alignment(void)
    expect_equal((intptr_t) blob, blob_read_intptr(&reader),
                 "read of initial, aligned intptr_t");
 
    for (num_bytes = 1; num_bytes < sizeof(intptr_t); num_bytes++) {
       expect_equal_bytes(bytes, blob_read_bytes(&reader, num_bytes),
                          num_bytes, "unaligned read of bytes");
       expect_equal((intptr_t) blob, blob_read_intptr(&reader),
                    "aligned read of intptr_t");
    }
 
-   ralloc_free(ctx);
+   free(blob);
 }
 
 /* Test that we detect overrun. */
 static void
 test_overrun(void)
 {
-   void *ctx =ralloc_context(NULL);
    struct blob *blob;
    struct blob_reader reader;
    uint32_t value = 0xdeadbeef;
 
-   blob = blob_create(ctx);
+   blob = blob_create();
 
    blob_write_uint32(blob, value);
 
    blob_reader_init(&reader, blob->data, blob->size);
 
    expect_equal(value, blob_read_uint32(&reader), "read before overrun");
    expect_equal(false, reader.overrun, "overrun flag not set");
    expect_equal(0, blob_read_uint32(&reader), "read at overrun");
    expect_equal(true, reader.overrun, "overrun flag set");
 
-   ralloc_free(ctx);
+   free(blob);
 }
 
 /* Test that we can read and write some large objects, (exercising the code in
  * the blob_write functions to realloc blob->data.
  */
 static void
 test_big_objects(void)
 {
    void *ctx = ralloc_context(NULL);
    struct blob *blob;
    struct blob_reader reader;
    int size = 1000;
    int count = 1000;
    size_t i;
    char *buf;
 
-   blob = blob_create(ctx);
+   blob = blob_create();
 
    /* Initialize our buffer. */
    buf = ralloc_size(ctx, size);
    for (i = 0; i < size; i++) {
       buf[i] = i % 256;
    }
 
    /* Write it many times. */
    for (i = 0; i < count; i++) {
       blob_write_bytes(blob, buf, size);
@@ -304,20 +301,21 @@ test_big_objects(void)
       expect_equal_bytes((uint8_t *) buf, blob_read_bytes(&reader, size), size,
                          "read of large objects");
    }
 
    expect_equal(reader.end - reader.data, reader.current - reader.data,
                 "number of bytes read reading large objects");
 
    expect_equal(false, reader.overrun,
                 "overrun flag not set reading large objects");
 
+   free(blob);
    ralloc_free(ctx);
 }
 
 int
 main (void)
 {
    test_write_and_read_functions ();
    test_alignment ();
    test_overrun ();
    test_big_objects ();
diff --git a/src/mesa/state_tracker/st_shader_cache.c b/src/mesa/state_tracker/st_shader_cache.c
index f22380c..4383194 100644
--- a/src/mesa/state_tracker/st_shader_cache.c
+++ b/src/mesa/state_tracker/st_shader_cache.c
@@ -61,21 +61,21 @@ st_store_tgsi_in_disk_cache(struct st_context *st, struct gl_program *prog,
    if (!st->ctx->Cache)
       return;
 
    /* Exit early when we are dealing with a ff shader with no source file to
     * generate a source from.
     */
    if (*prog->sh.data->sha1 == 0)
       return;
 
    unsigned char *sha1;
-   struct blob *blob = blob_create(NULL);
+   struct blob *blob = blob_create();
 
    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,
                        sizeof(stvp->index_to_input));
       blob_write_bytes(blob, stvp->result_to_output,
@@ -127,21 +127,21 @@ st_store_tgsi_in_disk_cache(struct st_context *st, struct gl_program *prog,
       unreachable("Unsupported stage");
    }
 
    if (st->ctx->_Shader->Flags & GLSL_CACHE_INFO) {
       char sha1_buf[41];
       _mesa_sha1_format(sha1_buf, sha1);
       fprintf(stderr, "putting %s tgsi_tokens in cache: %s\n",
               _mesa_shader_stage_to_string(prog->info.stage), sha1_buf);
    }
 
-   ralloc_free(blob);
+   free(blob);
 }
 
 static void
 read_stream_out_from_cache(struct blob_reader *blob_reader,
                            struct pipe_shader_state *tgsi)
 {
    blob_copy_bytes(blob_reader, (uint8_t *) &tgsi->stream_output,
                     sizeof(tgsi->stream_output));
 }
 
-- 
2.9.3



More information about the mesa-dev mailing list