summaryrefslogtreecommitdiff
path: root/hash.h
diff options
context:
space:
mode:
authorbrian m. carlson <sandals@crustytoothpaste.net>2018-02-01 02:18:38 (GMT)
committerJunio C Hamano <gitster@pobox.com>2018-02-02 19:28:41 (GMT)
commitac73cedff0db06671e2cab871b84b909b71adbd2 (patch)
tree832a941d3fafe41260ac779b765e2ee611b42b5a /hash.h
parent164e716330a65dc6770cace76110375a20c45dea (diff)
downloadgit-ac73cedff0db06671e2cab871b84b909b71adbd2.zip
git-ac73cedff0db06671e2cab871b84b909b71adbd2.tar.gz
git-ac73cedff0db06671e2cab871b84b909b71adbd2.tar.bz2
hash: create union for hash context allocation
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. Use this union for defining the init, update, and final functions to avoid casts. 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> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'hash.h')
-rw-r--r--hash.h15
1 files changed, 9 insertions, 6 deletions
diff --git a/hash.h b/hash.h
index 7122dea..eb30f59 100644
--- a/hash.h
+++ b/hash.h
@@ -55,9 +55,15 @@
/* Number of algorithms supported (including unknown). */
#define GIT_HASH_NALGOS (GIT_HASH_SHA1 + 1)
-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);
+/* 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)(git_hash_ctx *ctx);
+typedef void (*git_hash_update_fn)(git_hash_ctx *ctx, const void *in, size_t len);
+typedef void (*git_hash_final_fn)(unsigned char *hash, git_hash_ctx *ctx);
struct git_hash_algo {
/*
@@ -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;