Skip to content

Commit ac73ced

Browse files
bk2204gitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 164e716 commit ac73ced

File tree

2 files changed

+18
-17
lines changed

2 files changed

+18
-17
lines changed

hash.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,15 @@
5555
/* Number of algorithms supported (including unknown). */
5656
#define GIT_HASH_NALGOS (GIT_HASH_SHA1 + 1)
5757

58-
typedef void (*git_hash_init_fn)(void *ctx);
59-
typedef void (*git_hash_update_fn)(void *ctx, const void *in, size_t len);
60-
typedef void (*git_hash_final_fn)(unsigned char *hash, void *ctx);
58+
/* A suitably aligned type for stack allocations of hash contexts. */
59+
union git_hash_ctx {
60+
git_SHA_CTX sha1;
61+
};
62+
typedef union git_hash_ctx git_hash_ctx;
63+
64+
typedef void (*git_hash_init_fn)(git_hash_ctx *ctx);
65+
typedef void (*git_hash_update_fn)(git_hash_ctx *ctx, const void *in, size_t len);
66+
typedef void (*git_hash_final_fn)(unsigned char *hash, git_hash_ctx *ctx);
6167

6268
struct git_hash_algo {
6369
/*
@@ -69,9 +75,6 @@ struct git_hash_algo {
6975
/* A four-byte version identifier, used in pack indices. */
7076
uint32_t format_id;
7177

72-
/* The size of a hash context (e.g. git_SHA_CTX). */
73-
size_t ctxsz;
74-
7578
/* The length of the hash in binary. */
7679
size_t rawsz;
7780

sha1_file.c

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,32 +39,32 @@ const struct object_id empty_blob_oid = {
3939
EMPTY_BLOB_SHA1_BIN_LITERAL
4040
};
4141

42-
static void git_hash_sha1_init(void *ctx)
42+
static void git_hash_sha1_init(git_hash_ctx *ctx)
4343
{
44-
git_SHA1_Init((git_SHA_CTX *)ctx);
44+
git_SHA1_Init(&ctx->sha1);
4545
}
4646

47-
static void git_hash_sha1_update(void *ctx, const void *data, size_t len)
47+
static void git_hash_sha1_update(git_hash_ctx *ctx, const void *data, size_t len)
4848
{
49-
git_SHA1_Update((git_SHA_CTX *)ctx, data, len);
49+
git_SHA1_Update(&ctx->sha1, data, len);
5050
}
5151

52-
static void git_hash_sha1_final(unsigned char *hash, void *ctx)
52+
static void git_hash_sha1_final(unsigned char *hash, git_hash_ctx *ctx)
5353
{
54-
git_SHA1_Final(hash, (git_SHA_CTX *)ctx);
54+
git_SHA1_Final(hash, &ctx->sha1);
5555
}
5656

57-
static void git_hash_unknown_init(void *ctx)
57+
static void git_hash_unknown_init(git_hash_ctx *ctx)
5858
{
5959
die("trying to init unknown hash");
6060
}
6161

62-
static void git_hash_unknown_update(void *ctx, const void *data, size_t len)
62+
static void git_hash_unknown_update(git_hash_ctx *ctx, const void *data, size_t len)
6363
{
6464
die("trying to update unknown hash");
6565
}
6666

67-
static void git_hash_unknown_final(unsigned char *hash, void *ctx)
67+
static void git_hash_unknown_final(unsigned char *hash, git_hash_ctx *ctx)
6868
{
6969
die("trying to finalize unknown hash");
7070
}
@@ -75,7 +75,6 @@ const struct git_hash_algo hash_algos[GIT_HASH_NALGOS] = {
7575
0x00000000,
7676
0,
7777
0,
78-
0,
7978
git_hash_unknown_init,
8079
git_hash_unknown_update,
8180
git_hash_unknown_final,
@@ -86,7 +85,6 @@ const struct git_hash_algo hash_algos[GIT_HASH_NALGOS] = {
8685
"sha-1",
8786
/* "sha1", big-endian */
8887
0x73686131,
89-
sizeof(git_SHA_CTX),
9088
GIT_SHA1_RAWSZ,
9189
GIT_SHA1_HEXSZ,
9290
git_hash_sha1_init,

0 commit comments

Comments
 (0)