Implement reference counting for git_objects All `git_object` instances looked up from the repository are reference counted. User is expected to use the new `git_object_close` when an object is no longer needed to force freeing it. Signed-off-by: Vicent Marti <tanoku@gmail.com>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
diff --git a/src/blob.c b/src/blob.c
index c5a7143..f271cc7 100644
--- a/src/blob.c
+++ b/src/blob.c
@@ -129,6 +129,10 @@ int git_blob_writefile(git_oid *written_id, git_repository *repo, const char *pa
return error;
git_oid_cpy(written_id, git_object_id((git_object *)blob));
+
+ /* FIXME: maybe we don't want to free this already?
+ * the user may want to access it again */
+ git_object_close((git_object *)blob);
return GIT_SUCCESS;
}
diff --git a/src/commit.c b/src/commit.c
index 3eebb92..3edc573 100644
--- a/src/commit.c
+++ b/src/commit.c
@@ -55,6 +55,8 @@ void git_commit__free(git_commit *commit)
git_signature_free(commit->author);
git_signature_free(commit->committer);
+ git_object_close((git_object *)commit->tree);
+
free(commit->message);
free(commit->message_short);
free(commit);
@@ -121,6 +123,7 @@ int commit_parse_buffer(git_commit *commit, void *data, size_t len, unsigned int
if ((error = git__parse_oid(&oid, &buffer, buffer_end, "tree ")) < GIT_SUCCESS)
return error;
+ git_object_close((git_object *)commit->tree);
if ((error = git_object_lookup((git_object **)&commit->tree, commit->object.repo, &oid, GIT_OBJ_TREE)) < GIT_SUCCESS)
return error;
@@ -232,7 +235,21 @@ int git_commit__parse_full(git_commit *commit)
if (!commit->object.in_memory && !commit->full_parse)\
git_commit__parse_full(commit);
-GIT_COMMIT_GETTER(git_tree *, tree)
+const git_tree *git_commit_tree(git_commit *commit)
+{
+ assert(commit);
+
+ if (!commit->object.in_memory && commit->tree == NULL)
+ git_commit__parse_full(commit);
+
+ if (commit->tree) {
+ GIT_OBJECT_INCREF(commit->tree);
+ return commit->tree;
+ }
+
+ return NULL;
+}
+
GIT_COMMIT_GETTER(git_signature *, author)
GIT_COMMIT_GETTER(git_signature *, committer)
GIT_COMMIT_GETTER(char *, message)
@@ -267,6 +284,9 @@ void git_commit_set_tree(git_commit *commit, git_tree *tree)
assert(commit && tree);
commit->object.modified = 1;
CHECK_FULL_PARSE();
+
+ git_object_close((git_object *)commit->tree);
+ GIT_OBJECT_INCREF(tree);
commit->tree = tree;
}
diff --git a/src/git2/object.h b/src/git2/object.h
index 80477d4..af0f014 100644
--- a/src/git2/object.h
+++ b/src/git2/object.h
@@ -128,18 +128,27 @@ GIT_EXTERN(git_otype) git_object_type(const git_object *obj);
GIT_EXTERN(git_repository *) git_object_owner(const git_object *obj);
/**
- * Free a reference to one of the objects in the repository.
+ * Close an open object
*
- * Repository objects are managed automatically by the library,
- * but this method can be used to force freeing one of the
- * objects.
+ * This method instructs the library to close an existing
+ * object; note that git_objects are owned by the repository
+ * and are reference counted, so the object may or may not be
+ * freed after this library call, depending on whether any other
+ * objects still depend on it.
*
- * Careful: freeing objects in the middle of a repository
- * traversal will most likely cause errors.
+ * IMPORTANT:
+ * It is *not* necessary to call this method when you stop using
+ * an object, since all object memory is automatically reclaimed
+ * by the repository when it is freed.
*
- * @param object the object to free
+ * Forgetting to call `git_object_close` does not cause memory
+ * leaks, but it's is recommended to close as soon as possible
+ * the biggest objects (e.g. blobs) to prevent wasting memory
+ * space.
+ *
+ * @param object the object to close
*/
-GIT_EXTERN(void) git_object_free(git_object *object);
+GIT_EXTERN(void) git_object_close(git_object *object);
/**
* Convert an object type to it's string representation.
diff --git a/src/object.c b/src/object.c
index c9809c5..e232714 100644
--- a/src/object.c
+++ b/src/object.c
@@ -166,8 +166,12 @@ static int write_back(git_object *object)
if ((error = git_odb_write(&new_id, object->repo->db, &object->source.raw)) < GIT_SUCCESS)
return error;
- if (!object->in_memory)
+ if (object->in_memory) {
+ int idx = git_vector_search(&object->repo->memory_objects, object);
+ git_vector_remove(&object->repo->memory_objects, idx);
+ } else {
git_hashtable_remove(object->repo->objects, &object->id);
+ }
git_oid_cpy(&object->id, &new_id);
git_hashtable_insert(object->repo->objects, &object->id, object);
@@ -257,6 +261,7 @@ int git_object_new(git_object **object_out, git_repository *repo, git_otype type
object->source.raw.type = type;
+ object->refcount++;
*object_out = object;
return GIT_SUCCESS;
}
@@ -317,13 +322,14 @@ int git_object_lookup(git_object **object_out, git_repository *repo, const git_o
}
if (error < GIT_SUCCESS) {
- git_object_free(object);
+ git_object__free(object);
return error;
}
git_object__source_close(object);
git_hashtable_insert(repo->objects, &object->id, object);
+ object->refcount++;
*object_out = object;
return GIT_SUCCESS;
}
@@ -371,13 +377,18 @@ int git_object_write(git_object *object)
return write_back(object);
}
-void git_object_free(git_object *object)
+void git_object__free(git_object *object)
{
- if (object == NULL)
- return;
+ assert(object);
git_object__source_close(object);
- git_hashtable_remove(object->repo->objects, &object->id);
+
+ if (object->in_memory) {
+ int idx = git_vector_search(&object->repo->memory_objects, object);
+ git_vector_remove(&object->repo->memory_objects, idx);
+ } else {
+ git_hashtable_remove(object->repo->objects, &object->id);
+ }
switch (object->source.raw.type) {
case GIT_OBJ_COMMIT:
@@ -402,6 +413,15 @@ void git_object_free(git_object *object)
}
}
+void git_object_close(git_object *object)
+{
+ if (object == NULL)
+ return;
+
+ if (--object->refcount <= 0)
+ git_object__free(object);
+}
+
const git_oid *git_object_id(const git_object *obj)
{
assert(obj);
diff --git a/src/repository.c b/src/repository.c
index 77ce0c0..9f27a38 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -209,6 +209,13 @@ static git_repository *repository_alloc()
return NULL;
}
+ if (git_vector_init(&repo->memory_objects, 16, NULL) < GIT_SUCCESS) {
+ git_hashtable_free(repo->objects);
+ git_repository__refcache_free(&repo->references);
+ free(repo);
+ return NULL;
+ }
+
return repo;
}
@@ -328,7 +335,8 @@ int git_repository_open(git_repository **repo_out, const char *path)
void git_repository_free(git_repository *repo)
{
git_object *object;
- const git_oid *oid;
+ const void *_unused;
+ unsigned int i;
if (repo == NULL)
return;
@@ -338,11 +346,24 @@ void git_repository_free(git_repository *repo)
free(repo->path_repository);
free(repo->path_odb);
- GIT_HASHTABLE_FOREACH(repo->objects, oid, object, {
- git_object_free(object);
- });
+ /* Increment the refcount of all the objects in the repository
+ * to prevent freeing dependencies */
+ GIT_HASHTABLE_FOREACH(repo->objects, _unused, object,
+ GIT_OBJECT_INCREF(object);
+ );
+
+ /* force free all the objects */
+ GIT_HASHTABLE_FOREACH(repo->objects, _unused, object,
+ git_object__free(object);
+ );
+
+ for (i = 0; i < repo->memory_objects.length; ++i) {
+ object = git_vector_get(&repo->memory_objects, i);
+ git_object__free(object);
+ }
git_hashtable_free(repo->objects);
+ git_vector_free(&repo->memory_objects);
git_repository__refcache_free(&repo->references);
diff --git a/src/repository.h b/src/repository.h
index c8e4186..2e8d187 100644
--- a/src/repository.h
+++ b/src/repository.h
@@ -15,6 +15,8 @@
#define GIT_OBJECTS_DIR "objects/"
#define GIT_INDEX_FILE "index"
+#define GIT_OBJECT_INCREF(ob) ++(((git_object *)(ob))->refcount)
+
typedef struct {
git_rawobj raw;
void *write_ptr;
@@ -26,13 +28,16 @@ struct git_object {
git_oid id;
git_repository *repo;
git_odb_source source;
- int in_memory:1, modified:1;
+ unsigned short refcount;
+ short in_memory:1, modified:1;
};
struct git_repository {
git_odb *db;
git_index *index;
+
git_hashtable *objects;
+ git_vector memory_objects;
git_refcache references;
@@ -47,6 +52,10 @@ struct git_repository {
int git_object__source_open(git_object *object);
void git_object__source_close(git_object *object);
+/* fully free the object; internal method, do not
+ * export */
+void git_object__free(git_object *object);
+
int git__source_printf(git_odb_source *source, const char *format, ...);
int git__source_write(git_odb_source *source, const void *bytes, size_t len);
diff --git a/src/revwalk.c b/src/revwalk.c
index c073be1..872cdbc 100644
--- a/src/revwalk.c
+++ b/src/revwalk.c
@@ -114,6 +114,7 @@ static git_revwalk_commit *commit_to_walkcommit(git_revwalk *walk, git_commit *c
memset(commit, 0x0, sizeof(git_revwalk_commit));
commit->commit_object = commit_object;
+ GIT_OBJECT_INCREF(commit_object);
git_hashtable_insert(walk->commits, commit_object, commit);
@@ -229,6 +230,7 @@ int git_revwalk_next(git_commit **commit, git_revwalk *walk)
while ((next = walk->next(&walk->iterator)) != NULL) {
if (!next->uninteresting) {
*commit = next->commit_object;
+ GIT_OBJECT_INCREF(*commit);
return GIT_SUCCESS;
}
}
@@ -246,6 +248,7 @@ void git_revwalk_reset(git_revwalk *walk)
assert(walk);
GIT_HASHTABLE_FOREACH(walk->commits, _unused, commit, {
+ git_object_close((git_object *)commit->commit_object);
git_revwalk_list_clear(&commit->parents);
free(commit);
});
diff --git a/src/tag.c b/src/tag.c
index 7d57ee0..01cc0dc 100644
--- a/src/tag.c
+++ b/src/tag.c
@@ -35,6 +35,7 @@
void git_tag__free(git_tag *tag)
{
git_signature_free(tag->tagger);
+ git_object_close(tag->target);
free(tag->message);
free(tag->tag_name);
free(tag);
@@ -48,6 +49,7 @@ const git_oid *git_tag_id(git_tag *c)
const git_object *git_tag_target(git_tag *t)
{
assert(t);
+ GIT_OBJECT_INCREF(t->target);
return t->target;
}
@@ -55,6 +57,9 @@ void git_tag_set_target(git_tag *tag, git_object *target)
{
assert(tag && target);
+ git_object_close(tag->target);
+ GIT_OBJECT_INCREF(target);
+
tag->object.modified = 1;
tag->target = target;
tag->type = git_object_type(target);
@@ -66,14 +71,6 @@ git_otype git_tag_type(git_tag *t)
return t->type;
}
-void git_tag_set_type(git_tag *tag, git_otype type)
-{
- assert(tag);
-
- tag->object.modified = 1;
- tag->type = type;
-}
-
const char *git_tag_name(git_tag *t)
{
assert(t);
@@ -164,6 +161,7 @@ static int parse_tag_buffer(git_tag *tag, char *buffer, const char *buffer_end)
if (tag->type == GIT_OBJ_BAD)
return GIT_EOBJCORRUPTED;
+ git_object_close(tag->target);
error = git_object_lookup(&tag->target, tag->object.repo, &target_oid, tag->type);
if (error < 0)
return error;
diff --git a/src/tree.c b/src/tree.c
index 5ea0622..30938f2 100644
--- a/src/tree.c
+++ b/src/tree.c
@@ -67,9 +67,7 @@ void git_tree_clear_entries(git_tree *tree)
}
git_vector_clear(&tree->entries);
-
tree->object.modified = 1;
- tree->sorted = 1;
}
@@ -88,7 +86,6 @@ git_tree *git_tree__new(void)
return NULL;
}
- tree->sorted = 1;
return tree;
}
@@ -155,10 +152,7 @@ int git_tree_entry_2object(git_object **object_out, git_tree_entry *entry)
static void sort_entries(git_tree *tree)
{
- if (tree->sorted == 0) {
- git_vector_sort(&tree->entries);
- tree->sorted = 1;
- }
+ git_vector_sort(&tree->entries);
}
git_tree_entry *git_tree_entry_byname(git_tree *tree, const char *filename)
@@ -167,8 +161,7 @@ git_tree_entry *git_tree_entry_byname(git_tree *tree, const char *filename)
assert(tree && filename);
- if (!tree->sorted)
- sort_entries(tree);
+ sort_entries(tree);
idx = git_vector_bsearch2(&tree->entries, entry_search_cmp, filename);
if (idx == GIT_ENOTFOUND)
@@ -181,8 +174,7 @@ git_tree_entry *git_tree_entry_byindex(git_tree *tree, int idx)
{
assert(tree);
- if (!tree->sorted)
- sort_entries(tree);
+ sort_entries(tree);
return git_vector_get(&tree->entries, (unsigned int)idx);
}
@@ -216,7 +208,6 @@ int git_tree_add_entry(git_tree_entry **entry_out, git_tree *tree, const git_oid
*entry_out = entry;
tree->object.modified = 1;
- tree->sorted = 0;
return GIT_SUCCESS;
}
@@ -226,8 +217,7 @@ int git_tree_remove_entry_byindex(git_tree *tree, int idx)
assert(tree);
- if (!tree->sorted)
- sort_entries(tree);
+ sort_entries(tree);
remove_ptr = git_vector_get(&tree->entries, (unsigned int)idx);
if (remove_ptr == NULL)
@@ -247,8 +237,7 @@ int git_tree_remove_entry_byname(git_tree *tree, const char *filename)
assert(tree && filename);
- if (!tree->sorted)
- sort_entries(tree);
+ sort_entries(tree);
idx = git_vector_bsearch2(&tree->entries, entry_search_cmp, filename);
if (idx == GIT_ENOTFOUND)
@@ -267,8 +256,7 @@ int git_tree__writeback(git_tree *tree, git_odb_source *src)
if (tree->entries.length == 0)
return GIT_EMISSINGOBJDATA;
- if (!tree->sorted)
- sort_entries(tree);
+ sort_entries(tree);
for (i = 0; i < tree->entries.length; ++i) {
git_tree_entry *entry;
diff --git a/src/tree.h b/src/tree.h
index 796c5b9..78500c4 100644
--- a/src/tree.h
+++ b/src/tree.h
@@ -16,7 +16,6 @@ struct git_tree_entry {
struct git_tree {
git_object object;
git_vector entries;
- unsigned sorted:1;
};
void git_tree__free(git_tree *tree);
diff --git a/src/vector.c b/src/vector.c
index 126aec1..6313640 100644
--- a/src/vector.c
+++ b/src/vector.c
@@ -104,6 +104,10 @@ int git_vector_bsearch2(git_vector *v, git_vector_cmp key_lookup, const void *ke
assert(v && key && key_lookup);
+ /* need comparison function to sort the vector */
+ if (v->_cmp == NULL)
+ return GIT_ENOTFOUND;
+
git_vector_sort(v);
find = bsearch(key, v->contents, v->length, sizeof(void *), key_lookup);
@@ -127,12 +131,14 @@ int git_vector_search2(git_vector *v, git_vector_cmp key_lookup, const void *key
return GIT_ENOTFOUND;
}
-int git_vector_search(git_vector *v, const void *key)
+static int strict_comparison(const void *a, const void *b)
{
- if (v->_cmp == NULL)
- return GIT_ENOTFOUND;
+ return a - b;
+}
- return git_vector_search2(v, v->_cmp, key);
+int git_vector_search(git_vector *v, const void *entry)
+{
+ return git_vector_search2(v, v->_cmp ? v->_cmp : strict_comparison, entry);
}
int git_vector_bsearch(git_vector *v, const void *key)
diff --git a/tests/t09-tree.c b/tests/t09-tree.c
index cb12755..6bc2a84 100644
--- a/tests/t09-tree.c
+++ b/tests/t09-tree.c
@@ -101,8 +101,6 @@ BEGIN_TEST(write0, "add a new entry to a tree and write it back to disk")
must_pass(git_object_write((git_object *)tree));
must_pass(remove_loose_object(REPOSITORY_FOLDER, (git_object *)tree));
- git_object_free((git_object *)tree);
-
git_repository_free(repo);
END_TEST
@@ -152,7 +150,6 @@ BEGIN_TEST(write1, "add several entries in-memory and validate that they exist;
*/
must_pass(remove_loose_object(REPOSITORY_FOLDER, (git_object *)tree));
- git_object_free((git_object *)tree);
git_repository_free(repo);
END_TEST