mirror of
https://git.proxmox.com/git/libgit2
synced 2025-05-07 23:19:28 +00:00
Merge pull request #1497 from carlosmn/atomic-refcount
Make refcounting atomic
This commit is contained in:
commit
fe7199329b
@ -188,20 +188,20 @@ extern int git__strncmp(const char *a, const char *b, size_t sz);
|
||||
extern int git__strncasecmp(const char *a, const char *b, size_t sz);
|
||||
|
||||
typedef struct {
|
||||
short refcount;
|
||||
git_atomic refcount;
|
||||
void *owner;
|
||||
} git_refcount;
|
||||
|
||||
typedef void (*git_refcount_freeptr)(void *r);
|
||||
|
||||
#define GIT_REFCOUNT_INC(r) { \
|
||||
((git_refcount *)(r))->refcount++; \
|
||||
git_atomic_inc(&((git_refcount *)(r))->refcount); \
|
||||
}
|
||||
|
||||
#define GIT_REFCOUNT_DEC(_r, do_free) { \
|
||||
git_refcount *r = (git_refcount *)(_r); \
|
||||
r->refcount--; \
|
||||
if (r->refcount <= 0 && r->owner == NULL) { do_free(_r); } \
|
||||
int val = git_atomic_dec(&r->refcount); \
|
||||
if (val <= 0 && r->owner == NULL) { do_free(_r); } \
|
||||
}
|
||||
|
||||
#define GIT_REFCOUNT_OWN(r, o) { \
|
||||
|
@ -31,10 +31,10 @@ void test_repo_getters__retrieving_the_odb_honors_the_refcount(void)
|
||||
cl_git_pass(git_repository_open(&repo, cl_fixture("testrepo.git")));
|
||||
|
||||
cl_git_pass(git_repository_odb(&odb, repo));
|
||||
cl_assert(((git_refcount *)odb)->refcount == 2);
|
||||
cl_assert(((git_refcount *)odb)->refcount.val == 2);
|
||||
|
||||
git_repository_free(repo);
|
||||
cl_assert(((git_refcount *)odb)->refcount == 1);
|
||||
cl_assert(((git_refcount *)odb)->refcount.val == 1);
|
||||
|
||||
git_odb_free(odb);
|
||||
}
|
||||
|
@ -69,13 +69,13 @@ void test_repo_setters__setting_a_new_index_on_a_repo_which_has_already_loaded_o
|
||||
git_index *new_index;
|
||||
|
||||
cl_git_pass(git_index_open(&new_index, "./my-index"));
|
||||
cl_assert(((git_refcount *)new_index)->refcount == 1);
|
||||
cl_assert(((git_refcount *)new_index)->refcount.val == 1);
|
||||
|
||||
git_repository_set_index(repo, new_index);
|
||||
cl_assert(((git_refcount *)new_index)->refcount == 2);
|
||||
cl_assert(((git_refcount *)new_index)->refcount.val == 2);
|
||||
|
||||
git_repository_free(repo);
|
||||
cl_assert(((git_refcount *)new_index)->refcount == 1);
|
||||
cl_assert(((git_refcount *)new_index)->refcount.val == 1);
|
||||
|
||||
git_index_free(new_index);
|
||||
|
||||
@ -90,13 +90,13 @@ void test_repo_setters__setting_a_new_odb_on_a_repo_which_already_loaded_one_pro
|
||||
git_odb *new_odb;
|
||||
|
||||
cl_git_pass(git_odb_open(&new_odb, "./testrepo.git/objects"));
|
||||
cl_assert(((git_refcount *)new_odb)->refcount == 1);
|
||||
cl_assert(((git_refcount *)new_odb)->refcount.val == 1);
|
||||
|
||||
git_repository_set_odb(repo, new_odb);
|
||||
cl_assert(((git_refcount *)new_odb)->refcount == 2);
|
||||
cl_assert(((git_refcount *)new_odb)->refcount.val == 2);
|
||||
|
||||
git_repository_free(repo);
|
||||
cl_assert(((git_refcount *)new_odb)->refcount == 1);
|
||||
cl_assert(((git_refcount *)new_odb)->refcount.val == 1);
|
||||
|
||||
git_odb_free(new_odb);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user