git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 02/12] hash: create union for hash context allocation

From
brian m. carlson <sandals@crustytoothpaste.net>
Date
Jan 28, 2018, 15:57 UTC
Message-ID
<20180128155722.880805-3-sandals@crustytoothpaste.net>
In-Reply-To
<20180128155722.880805-1-sandals@crustytoothpaste.net>

In various parts of our code, we want to allocate a structure representing the internal state of a hash algorithm. The original implementation of the hash algorithm abstraction assumed we would do that using heap allocations, and added a context size element to struct git_hash_algo. However, most of the existing code uses stack allocations and conversion would needlessly complicate various parts of the code. Add a union for the purpose of allocating hash contexts on the stack and a typedef for ease of use. Remove the ctxsz element for struct git_hash_algo, which is no longer very useful.

This does mean that stack allocations will grow slightly as additional hash functions are added, but this should not be a significant problem, since we don't allocate many hash contexts. The improved usability and benefits from avoiding dynamic allocation outweigh this small downside.

Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net>
---
 hash.h      | 9 ++++++---
 sha1_file.c | 2 --
 2 files changed, 6 insertions(+), 5 deletions(-)
diff --git a/hash.h b/hash.h
index 7122dea7b3..365846a6b5 100644
--- a/hash.h
+++ b/hash.h
@@ -55,6 +55,12 @@
 /* Number of algorithms supported (including unknown). */
 #define GIT_HASH_NALGOS (GIT_HASH_SHA1 + 1)
 
+/* A suitably aligned type for stack allocations of hash contexts. */
+union git_hash_ctx {
+	git_SHA_CTX sha1;
+};
+typedef union git_hash_ctx git_hash_ctx;
+
 typedef void (*git_hash_init_fn)(void *ctx);
 typedef void (*git_hash_update_fn)(void *ctx, const void *in, size_t len);
 typedef void (*git_hash_final_fn)(unsigned char *hash, void *ctx);
@@ -69,9 +75,6 @@ struct git_hash_algo {
 	/* A four-byte version identifier, used in pack indices. */
 	uint32_t format_id;
 
-	/* The size of a hash context (e.g. git_SHA_CTX). */
-	size_t ctxsz;
-
 	/* The length of the hash in binary. */
 	size_t rawsz;
 
diff --git a/sha1_file.c b/sha1_file.c
index 3da70ac650..e61d93a6e8 100644
--- a/sha1_file.c
+++ b/sha1_file.c
@@ -75,7 +75,6 @@ const struct git_hash_algo hash_algos[GIT_HASH_NALGOS] = {
 		0x00000000,
 		0,
 		0,
-		0,
 		git_hash_unknown_init,
 		git_hash_unknown_update,
 		git_hash_unknown_final,
@@ -86,7 +85,6 @@ const struct git_hash_algo hash_algos[GIT_HASH_NALGOS] = {
 		"sha-1",
 		/* "sha1", big-endian */
 		0x73686131,
-		sizeof(git_SHA_CTX),
 		GIT_SHA1_RAWSZ,
 		GIT_SHA1_HEXSZ,
 		git_hash_sha1_init,
Previous: brian m. carlsonNext: Patryk Obara
Message 14 of 22 in “object_id part 11 (the_hash_algo)”
  1. 00/12 object_id part 11 (the_hash_algo)brian m. carlson, Jan 28, 2018
  2. 01/12 hash: move SHA-1 macros to hash.hbrian m. carlson, Jan 28, 2018
  3. 04/12 builtin/unpack-objects: switch uses of SHA-1 to the_hash_algobrian m. carlson, Jan 28, 2018
  4. 05/12 sha1_file: switch uses of SHA-1 to the_hash_algobrian m. carlson, Jan 28, 2018
  5. 06/12 fast-import: switch various uses of SHA-1 to the_hash_algobrian m. carlson, Jan 28, 2018
  6. Patryk ObaraJan 28, 2018
  7. 09/12 read-cache: abstract away uses of SHA-1brian m. carlson, Jan 28, 2018
  8. Eric SunshineJan 28, 2018
  9. brian m. carlsonJan 28, 2018
  10. 12/12 bulk-checkin: abstract SHA-1 usagebrian m. carlson, Jan 28, 2018
  11. 03/12 builtin/index-pack: improve hash function abstractionbrian m. carlson, Jan 28, 2018
  12. 11/12 csum-file: abstract uses of SHA-1brian m. carlson, Jan 28, 2018
  13. 10/12 csum-file: rename sha1file to hashfilebrian m. carlson, Jan 28, 2018
  14. 02/12 hash: create union for hash context allocationbrian m. carlson, Jan 28, 2018
  15. Patryk ObaraJan 28, 2018
  16. brian m. carlsonJan 28, 2018
  17. 07/12 pack-check: convert various uses of SHA-1 to abstract formsbrian m. carlson, Jan 28, 2018
  18. 08/12 pack-write: switch various SHA-1 values to abstract formsbrian m. carlson, Jan 28, 2018
  19. Patryk ObaraJan 28, 2018
  20. brian m. carlsonJan 28, 2018
  21. Patryk ObaraJan 28, 2018
  22. brian m. carlsonJan 28, 2018

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.