Skip to content

Commit 84af9d2

Browse files
bk2204gitster
authored andcommitted
hash: expose hash context functions to Rust
We'd like to be able to hash our data in Rust using the same contexts as in C. However, we need our helper functions to not be inline so they can be linked into the binary appropriately. In addition, to avoid managing memory manually and since we don't know the size of the hash context structure, we want to have simple alloc and free functions we can use to make sure a context can be easily dynamically created. Expose the helper functions and create alloc, free, and init functions we can call. Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent a95b74b commit 84af9d2

File tree

2 files changed

+42
-20
lines changed

2 files changed

+42
-20
lines changed

hash.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,41 @@ const struct git_hash_algo *hash_algo_ptr_by_offset(uint32_t algo)
246246
return &hash_algos[algo];
247247
}
248248

249+
struct git_hash_ctx *git_hash_alloc(void)
250+
{
251+
return malloc(sizeof(struct git_hash_ctx));
252+
}
253+
254+
void git_hash_free(struct git_hash_ctx *ctx)
255+
{
256+
free(ctx);
257+
}
258+
259+
void git_hash_init(struct git_hash_ctx *ctx, const struct git_hash_algo *algop)
260+
{
261+
algop->init_fn(ctx);
262+
}
263+
264+
void git_hash_clone(struct git_hash_ctx *dst, const struct git_hash_ctx *src)
265+
{
266+
src->algop->clone_fn(dst, src);
267+
}
268+
269+
void git_hash_update(struct git_hash_ctx *ctx, const void *in, size_t len)
270+
{
271+
ctx->algop->update_fn(ctx, in, len);
272+
}
273+
274+
void git_hash_final(unsigned char *hash, struct git_hash_ctx *ctx)
275+
{
276+
ctx->algop->final_fn(hash, ctx);
277+
}
278+
279+
void git_hash_final_oid(struct object_id *oid, struct git_hash_ctx *ctx)
280+
{
281+
ctx->algop->final_oid_fn(oid, ctx);
282+
}
283+
249284
uint32_t hash_algo_by_name(const char *name)
250285
{
251286
if (!name)

hash.h

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -320,27 +320,14 @@ struct git_hash_algo {
320320
};
321321
extern const struct git_hash_algo hash_algos[GIT_HASH_NALGOS];
322322

323-
static inline void git_hash_clone(struct git_hash_ctx *dst, const struct git_hash_ctx *src)
324-
{
325-
src->algop->clone_fn(dst, src);
326-
}
327-
328-
static inline void git_hash_update(struct git_hash_ctx *ctx, const void *in, size_t len)
329-
{
330-
ctx->algop->update_fn(ctx, in, len);
331-
}
332-
333-
static inline void git_hash_final(unsigned char *hash, struct git_hash_ctx *ctx)
334-
{
335-
ctx->algop->final_fn(hash, ctx);
336-
}
337-
338-
static inline void git_hash_final_oid(struct object_id *oid, struct git_hash_ctx *ctx)
339-
{
340-
ctx->algop->final_oid_fn(oid, ctx);
341-
}
342-
323+
void git_hash_init(struct git_hash_ctx *ctx, const struct git_hash_algo *algop);
324+
void git_hash_clone(struct git_hash_ctx *dst, const struct git_hash_ctx *src);
325+
void git_hash_update(struct git_hash_ctx *ctx, const void *in, size_t len);
326+
void git_hash_final(unsigned char *hash, struct git_hash_ctx *ctx);
327+
void git_hash_final_oid(struct object_id *oid, struct git_hash_ctx *ctx);
343328
const struct git_hash_algo *hash_algo_ptr_by_offset(uint32_t algo);
329+
struct git_hash_ctx *git_hash_alloc(void);
330+
void git_hash_free(struct git_hash_ctx *ctx);
344331
/*
345332
* Return a GIT_HASH_* constant based on the name. Returns GIT_HASH_UNKNOWN if
346333
* the name doesn't match a known algorithm.

0 commit comments

Comments
 (0)