pack-bitmap: add free function

Add a function to free struct bitmap_index instances, and use it where needed (except when rebuild_existing_bitmaps() is used, since it creates references to the bitmaps within the struct bitmap_index passed to it). Note that the hashes field in struct bitmap_index is not freed because it points to another field within the same struct. The documentation for that field has been updated to clarify that. Signed-off-by: Jonathan Tan <jonathantanmy@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jonathan Tan committed Jun 7, 2018 at 12:04 UTC f3c23db2d7e764b247f7d76a8d0ba180811e9525
5 files changed +37 -6
builtin/pack-objects.c
+1
@@ -2945,6 +2945,7 @@ static int get_object_list_from_bitmap(struct rev_info *revs)
2945 }
2946
2947 traverse_bitmap_commit_list(bitmap_git, &add_object_entry_from_bitmap);
2948 + free_bitmap_index(bitmap_git);
2949 return 0;
2950 }
2951
builtin/rev-list.c
+2
@@ -521,6 +521,7 @@ int cmd_rev_list(int argc, const char **argv, const char *prefix)
521 if (max_count >= 0 && max_count < commit_count)
522 commit_count = max_count;
523 printf("%d\n", commit_count);
524 + free_bitmap_index(bitmap_git);
525 return 0;
526 }
527 } else if (revs.max_count < 0 &&
@@ -528,6 +529,7 @@ int cmd_rev_list(int argc, const char **argv, const char *prefix)
529 struct bitmap_index *bitmap_git;
530 if ((bitmap_git = prepare_bitmap_walk(&revs))) {
531 traverse_bitmap_commit_list(bitmap_git, &show_object_fast);
532 + free_bitmap_index(bitmap_git);
533 return 0;
534 }
535 }
pack-bitmap-write.c
+4
@@ -367,6 +367,10 @@ void bitmap_writer_reuse_bitmaps(struct packing_data *to_pack)
367 writer.reused = kh_init_sha1();
368 rebuild_existing_bitmaps(bitmap_git, to_pack, writer.reused,
369 writer.show_progress);
370 + /*
371 + * NEEDSWORK: rebuild_existing_bitmaps() makes writer.reused reference
372 + * some bitmaps in bitmap_git, so we can't free the latter.
373 + */
374 }
375
376 static struct ewah_bitmap *find_reused_bitmap(const unsigned char *sha1)
pack-bitmap.c
+29 -6
@@ -66,7 +66,7 @@ struct bitmap_index {
66 /* Number of bitmapped commits */
67 uint32_t entry_count;
68
69 - /* Name-hash cache (or NULL if not present). */
69 + /* If not NULL, this is a name-hash cache pointing into map. */
70 uint32_t *hashes;
71
72 /*
@@ -350,6 +350,7 @@ struct bitmap_index *prepare_bitmap_git(void)
350 if (!open_pack_bitmap(bitmap_git) && !load_pack_bitmap(bitmap_git))
351 return bitmap_git;
352
353 + free_bitmap_index(bitmap_git);
354 return NULL;
355 }
356
@@ -690,7 +691,7 @@ struct bitmap_index *prepare_bitmap_walk(struct rev_info *revs)
691 /* try to open a bitmapped pack, but don't parse it yet
692 * because we may not need to use it */
693 if (open_pack_bitmap(bitmap_git) < 0)
693 - return NULL;
694 + goto cleanup;
695
696 for (i = 0; i < revs->pending.nr; ++i) {
697 struct object *object = revs->pending.objects[i].item;
@@ -723,11 +724,11 @@ struct bitmap_index *prepare_bitmap_walk(struct rev_info *revs)
724 * optimize here
725 */
726 if (haves && !in_bitmapped_pack(bitmap_git, haves))
726 - return NULL;
727 + goto cleanup;
728
729 /* if we don't want anything, we're done here */
730 if (!wants)
730 - return NULL;
731 + goto cleanup;
732
733 /*
734 * now we're going to use bitmaps, so load the actual bitmap entries
@@ -735,7 +736,7 @@ struct bitmap_index *prepare_bitmap_walk(struct rev_info *revs)
736 * becomes invalidated and we must perform the revwalk through bitmaps
737 */
738 if (!bitmap_git->loaded && load_pack_bitmap(bitmap_git) < 0)
738 - return NULL;
739 + goto cleanup;
740
741 object_array_clear(&revs->pending);
742
@@ -761,6 +762,10 @@ struct bitmap_index *prepare_bitmap_walk(struct rev_info *revs)
762
763 bitmap_free(haves_bitmap);
764 return bitmap_git;
765 +
766 +cleanup:
767 + free_bitmap_index(bitmap_git);
768 + return NULL;
769 }
770
771 int reuse_partial_packfile_from_bitmap(struct bitmap_index *bitmap_git,
@@ -1001,7 +1006,7 @@ void test_bitmap_walk(struct rev_info *revs)
1006 else
1007 fprintf(stderr, "Mismatch!\n");
1008
1004 - bitmap_free(result);
1009 + free_bitmap_index(bitmap_git);
1010 }
1011
1012 static int rebuild_bitmap(uint32_t *reposition,
@@ -1093,3 +1098,21 @@ int rebuild_existing_bitmaps(struct bitmap_index *bitmap_git,
1098 bitmap_free(rebuild);
1099 return 0;
1100 }
1101 +
1102 +void free_bitmap_index(struct bitmap_index *b)
1103 +{
1104 + if (!b)
1105 + return;
1106 +
1107 + if (b->map)
1108 + munmap(b->map, b->map_size);
1109 + ewah_pool_free(b->commits);
1110 + ewah_pool_free(b->trees);
1111 + ewah_pool_free(b->blobs);
1112 + ewah_pool_free(b->tags);
1113 + kh_destroy_sha1(b->bitmaps);
1114 + free(b->ext_index.objects);
1115 + free(b->ext_index.hashes);
1116 + bitmap_free(b->result);
1117 + free(b);
1118 +}
pack-bitmap.h
+1
@@ -48,6 +48,7 @@ int reuse_partial_packfile_from_bitmap(struct bitmap_index *,
48 uint32_t *entries, off_t *up_to);
49 int rebuild_existing_bitmaps(struct bitmap_index *, struct packing_data *mapping,
50 khash_sha1 *reused_bitmaps, int show_progress);
51 +void free_bitmap_index(struct bitmap_index *);
52
53 void bitmap_writer_show_progress(int show);
54 void bitmap_writer_set_checksum(unsigned char *sha1);