Skip to content

Commit f29070c

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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f00c4ac commit f29070c

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
@@ -248,6 +248,41 @@ const struct git_hash_algo *hash_algo_ptr_by_number(uint32_t algo)
248248
return &hash_algos[algo];
249249
}
250250

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