Commit b7c891c6 by Shawn O. Pearce

Add git_oid_cpy, git_oid_cmp as inline functions

These are easily built off the standard C library functions memcpy
and memcmp.  By marking these inline we stand a good chance of
the C compiler replacing the entire thing with tight machine code,
because many compilers will actually inline a memcmp or memcpy when
the 3rd argument (the size) is a constant value.

Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
parent 367ab010
......@@ -17,6 +17,7 @@ CASE_SENSE_NAMES = NO
PREDEFINED = \
"GIT_EXTERN(x)=x" \
"GIT_INLINE(x)=x" \
"GIT_BEGIN_DECL=" \
"GIT_END_DECL=" \
DOXYGEN=
......@@ -45,6 +45,9 @@
# define GIT_EXTERN(type) extern type
#endif
/** Declare a function as always inlined. */
# define GIT_INLINE(type) static inline type
/** Declare a function's takes printf style arguments. */
#ifdef __GNUC__
# define GIT_FORMAT_PRINTF(a,b) __attribute__((format (printf, a, b)))
......
......@@ -59,7 +59,31 @@ GIT_EXTERN(int) git_oid_mkstr(git_oid *out, const char *str);
* @param out oid structure the result is written into.
* @param raw the raw input bytes to be copied.
*/
GIT_EXTERN(void) git_oid_mkraw(git_oid *out, const unsigned char *raw);
GIT_INLINE(void) git_oid_mkraw(git_oid *out, const unsigned char *raw)
{
memcpy(out->id, raw, sizeof(out->id));
}
/**
* Copy an oid from one structure to another.
* @param out oid structure the result is written into.
* @param src oid structure to copy from.
*/
GIT_INLINE(void) git_oid_cpy(git_oid *out, const git_oid *src)
{
memcpy(out->id, src->id, sizeof(out->id));
}
/**
* Compare two oid structures.
* @param a first oid structure.
* @param b second oid structure.
* @return <0, 0, >0 if a < b, a == b, a > b.
*/
GIT_INLINE(int) git_oid_cmp(const git_oid *a, const git_oid *b)
{
return memcmp(a->id, b->id, sizeof(a->id));
}
/** @} */
GIT_END_DECL
......
......@@ -31,6 +31,7 @@
#include <fcntl.h>
#include <sys/types.h>
#include <time.h>
#include <string.h>
/**
* @file git/os/unix.h
......
......@@ -57,8 +57,3 @@ int git_oid_mkstr(git_oid *out, const char *str)
}
return GIT_SUCCESS;
}
void git_oid_mkraw(git_oid *out, const unsigned char *raw)
{
memcpy(out->id, raw, sizeof(out->id));
}
......@@ -81,3 +81,72 @@ BEGIN_TEST(valid_raw)
git_oid_mkraw(&out, exp);
must_pass(memcmp(out.id, exp, sizeof(out.id)));
END_TEST
BEGIN_TEST(copy_oid)
git_oid a, b;
unsigned char exp[] = {
0x16, 0xa6, 0x77, 0x70, 0xb7,
0xd8, 0xd7, 0x23, 0x17, 0xc4,
0xb7, 0x75, 0x21, 0x3c, 0x23,
0xa8, 0xbd, 0x74, 0xf5, 0xe0,
};
memset(&b, 0, sizeof(b));
git_oid_mkraw(&a, exp);
git_oid_cpy(&b, &a);
must_pass(memcmp(a.id, exp, sizeof(a.id)));
END_TEST
BEGIN_TEST(cmp_oid_lt)
git_oid a, b;
unsigned char a_in[] = {
0x16, 0xa6, 0x77, 0x70, 0xb7,
0xd8, 0xd7, 0x23, 0x17, 0xc4,
0xb7, 0x75, 0x21, 0x3c, 0x23,
0xa8, 0xbd, 0x74, 0xf5, 0xe0,
};
unsigned char b_in[] = {
0x16, 0xa6, 0x77, 0x70, 0xb7,
0xd8, 0xd7, 0x23, 0x17, 0xc4,
0xb7, 0x75, 0x21, 0x3c, 0x23,
0xa8, 0xbd, 0x74, 0xf5, 0xf0,
};
git_oid_mkraw(&a, a_in);
git_oid_mkraw(&b, b_in);
must_be_true(git_oid_cmp(&a, &b) < 0);
END_TEST
BEGIN_TEST(cmp_oid_eq)
git_oid a, b;
unsigned char a_in[] = {
0x16, 0xa6, 0x77, 0x70, 0xb7,
0xd8, 0xd7, 0x23, 0x17, 0xc4,
0xb7, 0x75, 0x21, 0x3c, 0x23,
0xa8, 0xbd, 0x74, 0xf5, 0xe0,
};
git_oid_mkraw(&a, a_in);
git_oid_mkraw(&b, a_in);
must_be_true(git_oid_cmp(&a, &b) == 0);
END_TEST
BEGIN_TEST(cmp_oid_gt)
git_oid a, b;
unsigned char a_in[] = {
0x16, 0xa6, 0x77, 0x70, 0xb7,
0xd8, 0xd7, 0x23, 0x17, 0xc4,
0xb7, 0x75, 0x21, 0x3c, 0x23,
0xa8, 0xbd, 0x74, 0xf5, 0xe0,
};
unsigned char b_in[] = {
0x16, 0xa6, 0x77, 0x70, 0xb7,
0xd8, 0xd7, 0x23, 0x17, 0xc4,
0xb7, 0x75, 0x21, 0x3c, 0x23,
0xa8, 0xbd, 0x74, 0xf5, 0xd0,
};
git_oid_mkraw(&a, a_in);
git_oid_mkraw(&b, b_in);
must_be_true(git_oid_cmp(&a, &b) > 0);
END_TEST
......@@ -90,3 +90,11 @@ extern void test_die(const char *fmt, ...)
*/
#define must_fail(expr) \
if (!(expr)) test_die("line %d: %s", __LINE__, #expr)
/**
* Evaluate an expression which must produce a true result.
*
* @param expr the expression to evaluate, and test the result of.
*/
#define must_be_true(expr) \
if (!(expr)) test_die("line %d: %s", __LINE__, #expr)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment