hash: convert `global_init` macros to real function The `git_hash_global_init` function is simply defined as a macro to zero for most of the different hash implementations. This makes it impossible to treat it like a function pointer, which is required for a later commit where we want to improve the way global initialization works. Fix the issue by converting all no-op macros to an inline function returning zero. There's a small gotcha here, though: as most hash implementations only have a header file, but not a corresponding implementation file, we cannot declare the function as non-static. But declaring it as `static inline` fails, too, as there is a previous declaration as non-static. So we have to move the function declaration after the include that brings in the function definition, as it is allowed to have a non-static declaration after a static definition, but not the other way round.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
diff --git a/src/hash.h b/src/hash.h
index 93765ad..0502e35 100644
--- a/src/hash.h
+++ b/src/hash.h
@@ -14,7 +14,6 @@
typedef struct git_hash_prov git_hash_prov;
typedef struct git_hash_ctx git_hash_ctx;
-int git_hash_global_init(void);
int git_hash_ctx_init(git_hash_ctx *ctx);
void git_hash_ctx_cleanup(git_hash_ctx *ctx);
@@ -32,6 +31,8 @@ void git_hash_ctx_cleanup(git_hash_ctx *ctx);
# include "hash/hash_generic.h"
#endif
+int git_hash_global_init(void);
+
typedef struct {
void *data;
size_t len;
diff --git a/src/hash/hash_collisiondetect.h b/src/hash/hash_collisiondetect.h
index 4c5e3c3..4e4c7da 100644
--- a/src/hash/hash_collisiondetect.h
+++ b/src/hash/hash_collisiondetect.h
@@ -15,10 +15,14 @@ struct git_hash_ctx {
SHA1_CTX c;
};
-#define git_hash_global_init() 0
#define git_hash_ctx_init(ctx) git_hash_init(ctx)
#define git_hash_ctx_cleanup(ctx)
+GIT_INLINE(int) git_hash_global_init(void)
+{
+ return 0;
+}
+
GIT_INLINE(int) git_hash_init(git_hash_ctx *ctx)
{
assert(ctx);
diff --git a/src/hash/hash_common_crypto.h b/src/hash/hash_common_crypto.h
index 5c3887d..ce352a6 100644
--- a/src/hash/hash_common_crypto.h
+++ b/src/hash/hash_common_crypto.h
@@ -18,10 +18,14 @@ struct git_hash_ctx {
#define CC_LONG_MAX ((CC_LONG)-1)
-#define git_hash_global_init() 0
#define git_hash_ctx_init(ctx) git_hash_init(ctx)
#define git_hash_ctx_cleanup(ctx)
+GIT_INLINE(int) git_hash_global_init(void)
+{
+ return 0;
+}
+
GIT_INLINE(int) git_hash_init(git_hash_ctx *ctx)
{
assert(ctx);
diff --git a/src/hash/hash_generic.h b/src/hash/hash_generic.h
index 21a0428..fb0009c 100644
--- a/src/hash/hash_generic.h
+++ b/src/hash/hash_generic.h
@@ -18,8 +18,12 @@ struct git_hash_ctx {
unsigned int W[16];
};
-#define git_hash_global_init() 0
#define git_hash_ctx_init(ctx) git_hash_init(ctx)
#define git_hash_ctx_cleanup(ctx)
+GIT_INLINE(int) git_hash_global_init(void)
+{
+ return 0;
+}
+
#endif
diff --git a/src/hash/hash_mbedtls.h b/src/hash/hash_mbedtls.h
index 24196c5..7f3decd 100644
--- a/src/hash/hash_mbedtls.h
+++ b/src/hash/hash_mbedtls.h
@@ -14,7 +14,11 @@ struct git_hash_ctx {
mbedtls_sha1_context c;
};
-#define git_hash_global_init() 0
#define git_hash_ctx_init(ctx) git_hash_init(ctx)
+GIT_INLINE(int) git_hash_global_init(void)
+{
+ return 0;
+}
+
#endif /* INCLUDE_hash_mbedtld_h__ */
diff --git a/src/hash/hash_openssl.h b/src/hash/hash_openssl.h
index eb2dcb0..9a32cba 100644
--- a/src/hash/hash_openssl.h
+++ b/src/hash/hash_openssl.h
@@ -16,10 +16,14 @@ struct git_hash_ctx {
SHA_CTX c;
};
-#define git_hash_global_init() 0
#define git_hash_ctx_init(ctx) git_hash_init(ctx)
#define git_hash_ctx_cleanup(ctx)
+GIT_INLINE(int) git_hash_global_init(void)
+{
+ return 0;
+}
+
GIT_INLINE(int) git_hash_init(git_hash_ctx *ctx)
{
assert(ctx);