diff options
author | Vicent Martà <vicent@github.com> | 2012-12-05 11:47:19 -0800 |
---|---|---|
committer | Vicent Martà <vicent@github.com> | 2012-12-05 11:47:19 -0800 |
commit | e05ca13f1f3550f59790c0f992841abceee1b4c5 (patch) | |
tree | a01c119011d0c6020c9288096d09ed055c475bd8 /src/common.h | |
parent | a541eafa606b58e7ce3df8e496da8e032fdb74ec (diff) | |
parent | ee1c33b146a366260a4648b1f29f470fedaca0fa (diff) | |
download | libgit2-e05ca13f1f3550f59790c0f992841abceee1b4c5.tar.gz |
Merge pull request #1115 from ben/struct-versions
Version info for public structs
Diffstat (limited to 'src/common.h')
-rw-r--r-- | src/common.h | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/src/common.h b/src/common.h index b3b551508..3152669df 100644 --- a/src/common.h +++ b/src/common.h @@ -65,6 +65,33 @@ void giterr_set(int error_class, const char *string, ...); */ int giterr_set_regex(const regex_t *regex, int error_code); +/** + * Check a versioned structure for validity + */ +GIT_INLINE(bool) giterr__check_version(const void *structure, unsigned int expected_max, const char *name) +{ + if (!structure) + return true; + + unsigned int actual = *(const unsigned int*)structure; + if (actual > 0 && actual <= expected_max) + return true; + + giterr_set(GITERR_INVALID, "Invalid version %d on %s", actual, name); + return false; +} +#define GITERR_CHECK_VERSION(S,V,N) if (!giterr__check_version(S,V,N)) return -1 + +/** + * Initialize a structure with a version. + */ +GIT_INLINE(void) git__init_structure(void *structure, size_t len, unsigned int version) +{ + memset(structure, 0, len); + *((int*)structure) = version; +} +#define GIT_INIT_STRUCTURE(S,V) git__init_structure(S, sizeof(*S), V) + /* NOTE: other giterr functions are in the public errors.h header file */ #include "util.h" |