[Mesa-dev] [PATCH 2/5] util/sha1: rework _mesa_sha1_{init,final}

Emil Velikov emil.l.velikov at gmail.com
Tue Jan 24 21:21:09 UTC 2017


From: Emil Velikov <emil.velikov at collabora.com>

Rather than having an extra memory allocation [that we currently do not
and act accordingly] just make the API take an pointer to a stack
allocated instance.

This and follow-up steps will effectively make the _mesa_sha1_foo simple
define/inlines around their SHA1 counterparts.

Signed-off-by: Emil Velikov <emil.velikov at collabora.com>
---
 src/amd/vulkan/radv_descriptor_set.c  | 10 +++++-----
 src/amd/vulkan/radv_pipeline_cache.c  | 18 +++++++++---------
 src/intel/vulkan/anv_descriptor_set.c | 13 +++++++------
 src/intel/vulkan/anv_pipeline_cache.c | 18 +++++++++---------
 src/util/mesa-sha1.c                  | 34 +++++-----------------------------
 src/util/mesa-sha1.h                  | 13 ++++++++-----
 6 files changed, 43 insertions(+), 63 deletions(-)

diff --git a/src/amd/vulkan/radv_descriptor_set.c b/src/amd/vulkan/radv_descriptor_set.c
index eb8b5d6e3a..435b7394a3 100644
--- a/src/amd/vulkan/radv_descriptor_set.c
+++ b/src/amd/vulkan/radv_descriptor_set.c
@@ -180,7 +180,7 @@ VkResult radv_CreatePipelineLayout(
 {
 	RADV_FROM_HANDLE(radv_device, device, _device);
 	struct radv_pipeline_layout *layout;
-	struct mesa_sha1 *ctx;
+	struct mesa_sha1 ctx;
 
 	assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO);
 
@@ -194,7 +194,7 @@ VkResult radv_CreatePipelineLayout(
 	unsigned dynamic_offset_count = 0;
 
 
-	ctx = _mesa_sha1_init();
+	_mesa_sha1_init(&ctx);
 	for (uint32_t set = 0; set < pCreateInfo->setLayoutCount; set++) {
 		RADV_FROM_HANDLE(radv_descriptor_set_layout, set_layout,
 				 pCreateInfo->pSetLayouts[set]);
@@ -204,7 +204,7 @@ VkResult radv_CreatePipelineLayout(
 		for (uint32_t b = 0; b < set_layout->binding_count; b++) {
 			dynamic_offset_count += set_layout->binding[b].array_size * set_layout->binding[b].dynamic_offset_count;
 		}
-		_mesa_sha1_update(ctx, set_layout->binding,
+		_mesa_sha1_update(&ctx, set_layout->binding,
 				  sizeof(set_layout->binding[0]) * set_layout->binding_count);
 	}
 
@@ -217,9 +217,9 @@ VkResult radv_CreatePipelineLayout(
 	}
 
 	layout->push_constant_size = align(layout->push_constant_size, 16);
-	_mesa_sha1_update(ctx, &layout->push_constant_size,
+	_mesa_sha1_update(&ctx, &layout->push_constant_size,
 			  sizeof(layout->push_constant_size));
-	_mesa_sha1_final(ctx, layout->sha1);
+	_mesa_sha1_final(&ctx, layout->sha1);
 	*pPipelineLayout = radv_pipeline_layout_to_handle(layout);
 
 	return VK_SUCCESS;
diff --git a/src/amd/vulkan/radv_pipeline_cache.c b/src/amd/vulkan/radv_pipeline_cache.c
index 2cb1dfb6eb..1bfdbe804c 100644
--- a/src/amd/vulkan/radv_pipeline_cache.c
+++ b/src/amd/vulkan/radv_pipeline_cache.c
@@ -90,21 +90,21 @@ radv_hash_shader(unsigned char *hash, struct radv_shader_module *module,
 		 const struct radv_pipeline_layout *layout,
 		 const union ac_shader_variant_key *key)
 {
-	struct mesa_sha1 *ctx;
+	struct mesa_sha1 ctx;
 
-	ctx = _mesa_sha1_init();
+	_mesa_sha1_init(&ctx);
 	if (key)
-		_mesa_sha1_update(ctx, key, sizeof(*key));
-	_mesa_sha1_update(ctx, module->sha1, sizeof(module->sha1));
-	_mesa_sha1_update(ctx, entrypoint, strlen(entrypoint));
+		_mesa_sha1_update(&ctx, key, sizeof(*key));
+	_mesa_sha1_update(&ctx, module->sha1, sizeof(module->sha1));
+	_mesa_sha1_update(&ctx, entrypoint, strlen(entrypoint));
 	if (layout)
-		_mesa_sha1_update(ctx, layout->sha1, sizeof(layout->sha1));
+		_mesa_sha1_update(&ctx, layout->sha1, sizeof(layout->sha1));
 	if (spec_info) {
-		_mesa_sha1_update(ctx, spec_info->pMapEntries,
+		_mesa_sha1_update(&ctx, spec_info->pMapEntries,
 				  spec_info->mapEntryCount * sizeof spec_info->pMapEntries[0]);
-		_mesa_sha1_update(ctx, spec_info->pData, spec_info->dataSize);
+		_mesa_sha1_update(&ctx, spec_info->pData, spec_info->dataSize);
 	}
-	_mesa_sha1_final(ctx, hash);
+	_mesa_sha1_final(&ctx, hash);
 }
 
 
diff --git a/src/intel/vulkan/anv_descriptor_set.c b/src/intel/vulkan/anv_descriptor_set.c
index a5e65afc48..29bb67c5c3 100644
--- a/src/intel/vulkan/anv_descriptor_set.c
+++ b/src/intel/vulkan/anv_descriptor_set.c
@@ -259,18 +259,19 @@ VkResult anv_CreatePipelineLayout(
       }
    }
 
-   struct mesa_sha1 *ctx = _mesa_sha1_init();
+   struct mesa_sha1 ctx;
+   _mesa_sha1_init(&ctx);
    for (unsigned s = 0; s < layout->num_sets; s++) {
-      sha1_update_descriptor_set_layout(ctx, layout->set[s].layout);
-      _mesa_sha1_update(ctx, &layout->set[s].dynamic_offset_start,
+      sha1_update_descriptor_set_layout(&ctx, layout->set[s].layout);
+      _mesa_sha1_update(&ctx, &layout->set[s].dynamic_offset_start,
                         sizeof(layout->set[s].dynamic_offset_start));
    }
-   _mesa_sha1_update(ctx, &layout->num_sets, sizeof(layout->num_sets));
+   _mesa_sha1_update(&ctx, &layout->num_sets, sizeof(layout->num_sets));
    for (unsigned s = 0; s < MESA_SHADER_STAGES; s++) {
-      _mesa_sha1_update(ctx, &layout->stage[s].has_dynamic_offsets,
+      _mesa_sha1_update(&ctx, &layout->stage[s].has_dynamic_offsets,
                         sizeof(layout->stage[s].has_dynamic_offsets));
    }
-   _mesa_sha1_final(ctx, layout->sha1);
+   _mesa_sha1_final(&ctx, layout->sha1);
 
    *pPipelineLayout = anv_pipeline_layout_to_handle(layout);
 
diff --git a/src/intel/vulkan/anv_pipeline_cache.c b/src/intel/vulkan/anv_pipeline_cache.c
index a8ea80f51f..0b677a49f3 100644
--- a/src/intel/vulkan/anv_pipeline_cache.c
+++ b/src/intel/vulkan/anv_pipeline_cache.c
@@ -205,23 +205,23 @@ anv_hash_shader(unsigned char *hash, const void *key, size_t key_size,
                 const struct anv_pipeline_layout *pipeline_layout,
                 const VkSpecializationInfo *spec_info)
 {
-   struct mesa_sha1 *ctx;
+   struct mesa_sha1 ctx;
 
-   ctx = _mesa_sha1_init();
-   _mesa_sha1_update(ctx, key, key_size);
-   _mesa_sha1_update(ctx, module->sha1, sizeof(module->sha1));
-   _mesa_sha1_update(ctx, entrypoint, strlen(entrypoint));
+   _mesa_sha1_init(&ctx);
+   _mesa_sha1_update(&ctx, key, key_size);
+   _mesa_sha1_update(&ctx, module->sha1, sizeof(module->sha1));
+   _mesa_sha1_update(&ctx, entrypoint, strlen(entrypoint));
    if (pipeline_layout) {
-      _mesa_sha1_update(ctx, pipeline_layout->sha1,
+      _mesa_sha1_update(&ctx, pipeline_layout->sha1,
                         sizeof(pipeline_layout->sha1));
    }
    /* hash in shader stage, pipeline layout? */
    if (spec_info) {
-      _mesa_sha1_update(ctx, spec_info->pMapEntries,
+      _mesa_sha1_update(&ctx, spec_info->pMapEntries,
                         spec_info->mapEntryCount * sizeof spec_info->pMapEntries[0]);
-      _mesa_sha1_update(ctx, spec_info->pData, spec_info->dataSize);
+      _mesa_sha1_update(&ctx, spec_info->pData, spec_info->dataSize);
    }
-   _mesa_sha1_final(ctx, hash);
+   _mesa_sha1_final(&ctx, hash);
 }
 
 static struct anv_shader_bin *
diff --git a/src/util/mesa-sha1.c b/src/util/mesa-sha1.c
index e8f1bad22f..eb882e8bd0 100644
--- a/src/util/mesa-sha1.c
+++ b/src/util/mesa-sha1.c
@@ -27,45 +27,21 @@
 #include "sha1/sha1.h"
 #include "mesa-sha1.h"
 
-struct mesa_sha1 *
-_mesa_sha1_init(void)
-{
-   SHA1_CTX *ctx = malloc(sizeof(*ctx));
-
-   if (!ctx)
-      return NULL;
-
-   SHA1Init(ctx);
-   return (struct mesa_sha1 *) ctx;
-}
-
 int
 _mesa_sha1_update(struct mesa_sha1 *ctx, const void *data, int size)
 {
-   SHA1_CTX *sha1_ctx = (SHA1_CTX *) ctx;
-
-   SHA1Update(sha1_ctx, data, size);
-   return 1;
-}
-
-int
-_mesa_sha1_final(struct mesa_sha1 *ctx, unsigned char result[20])
-{
-   SHA1_CTX *sha1_ctx = (SHA1_CTX *) ctx;
-
-   SHA1Final(result, sha1_ctx);
-   free(sha1_ctx);
+   SHA1Update(ctx, data, size);
    return 1;
 }
 
 void
 _mesa_sha1_compute(const void *data, size_t size, unsigned char result[20])
 {
-   struct mesa_sha1 *ctx;
+   struct mesa_sha1 ctx;
 
-   ctx = _mesa_sha1_init();
-   _mesa_sha1_update(ctx, data, size);
-   _mesa_sha1_final(ctx, result);
+   _mesa_sha1_init(&ctx);
+   _mesa_sha1_update(&ctx, data, size);
+   _mesa_sha1_final(&ctx, result);
 }
 
 char *
diff --git a/src/util/mesa-sha1.h b/src/util/mesa-sha1.h
index 0be5485f31..f927d5772d 100644
--- a/src/util/mesa-sha1.h
+++ b/src/util/mesa-sha1.h
@@ -24,21 +24,24 @@
 #define SHA1_H
 
 #include <stdlib.h>
+#include "sha1/sha1.h"
 
 #ifdef __cplusplus
 extern "C" {
 #endif
 
-struct mesa_sha1;
+#define mesa_sha1 _SHA1_CTX
 
-struct mesa_sha1 *
-_mesa_sha1_init(void);
+#define _mesa_sha1_init SHA1Init
 
 int
 _mesa_sha1_update(struct mesa_sha1 *ctx, const void *data, int size);
 
-int
-_mesa_sha1_final(struct mesa_sha1 *ctx, unsigned char result[20]);
+static inline void
+_mesa_sha1_final(struct mesa_sha1 *ctx, unsigned char result[20])
+{
+   SHA1Final(result, ctx);
+}
 
 char *
 _mesa_sha1_format(char *buf, const unsigned char *sha1);
-- 
2.11.0



More information about the mesa-dev mailing list