git-pickaxe: WIP to refcount origin structure.
The origin structure is allocated for each commit and path while the code traverse down it is copied into different blame entries. To avoid leaks, try refcounting them. This still seems to leak, which I haven't tracked down fully yet. Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Oct 29, 2006 at 03:07 UTC
54a4c6173e30774125016f7ff4976eb4a7485d0c
1 file changed
+121
-21
builtin-pickaxe.c
+121
-21
@@ -36,6 +36,10 @@ static int max_orig_digits;
36
static int max_digits;
37
static int max_score_digits;
38
39
+#ifndef DEBUG
40
+#define DEBUG 0
41
+#endif
42
+
43
#define PICKAXE_BLAME_MOVE 01
44
#define PICKAXE_BLAME_COPY 02
45
#define PICKAXE_BLAME_COPY_HARDER 04
@@ -54,14 +58,30 @@ static unsigned blame_copy_score;
58
#define MORE_THAN_ONE_PATH (1u<<13)
59
60
/*
57
- * One blob in a commit
61
+ * One blob in a commit that is being suspected
62
*/
63
struct origin {
64
+ int refcnt;
65
struct commit *commit;
66
unsigned char blob_sha1[20];
67
char path[FLEX_ARRAY];
68
};
69
70
+static inline struct origin *origin_incref(struct origin *o)
71
+{
72
+ if (o)
73
+ o->refcnt++;
74
+ return o;
75
+}
76
+
77
+static void origin_decref(struct origin *o)
78
+{
79
+ if (o && --o->refcnt <= 0) {
80
+ memset(o, 0, sizeof(*o));
81
+ free(o);
82
+ }
83
+}
84
+
85
struct blame_entry {
86
struct blame_entry *prev;
87
struct blame_entry *next;
@@ -121,6 +141,8 @@ static int cmp_suspect(struct origin *a, struct origin *b)
141
return strcmp(a->path, b->path);
142
}
143
144
+static void sanity_check_refcnt(struct scoreboard *);
145
+
146
static void coalesce(struct scoreboard *sb)
147
{
148
struct blame_entry *ent, *next;
@@ -133,11 +155,15 @@ static void coalesce(struct scoreboard *sb)
155
ent->next = next->next;
156
if (ent->next)
157
ent->next->prev = ent;
158
+ origin_decref(next->suspect);
159
free(next);
160
ent->score = 0;
161
next = ent; /* again */
162
}
163
}
164
+
165
+ if (DEBUG) /* sanity */
166
+ sanity_check_refcnt(sb);
167
}
168
169
static struct origin *get_origin(struct scoreboard *sb,
@@ -150,10 +176,11 @@ static struct origin *get_origin(struct scoreboard *sb,
176
for (e = sb->ent; e; e = e->next) {
177
if (e->suspect->commit == commit &&
178
!strcmp(e->suspect->path, path))
153
- return e->suspect;
179
+ return origin_incref(e->suspect);
180
}
181
o = xcalloc(1, sizeof(*o) + strlen(path) + 1);
182
o->commit = commit;
183
+ o->refcnt = 1;
184
strcpy(o->path, path);
185
return o;
186
}
@@ -400,6 +427,8 @@ static void add_blame_entry(struct scoreboard *sb, struct blame_entry *e)
427
{
428
struct blame_entry *ent, *prev = NULL;
429
430
+ origin_incref(e->suspect);
431
+
432
for (ent = sb->ent; ent && ent->lno < e->lno; ent = ent->next)
433
prev = ent;
434
@@ -420,8 +449,11 @@ static void add_blame_entry(struct scoreboard *sb, struct blame_entry *e)
449
static void dup_entry(struct blame_entry *dst, struct blame_entry *src)
450
{
451
struct blame_entry *p, *n;
452
+
453
p = dst->prev;
454
n = dst->next;
455
+ origin_incref(src->suspect);
456
+ origin_decref(dst->suspect);
457
memcpy(dst, src, sizeof(*src));
458
dst->prev = p;
459
dst->next = n;
@@ -433,7 +465,7 @@ static const char *nth_line(struct scoreboard *sb, int lno)
465
return sb->final_buf + sb->lineno[lno];
466
}
467
436
-static void split_overlap(struct blame_entry split[3],
468
+static void split_overlap(struct blame_entry *split,
469
struct blame_entry *e,
470
int tlno, int plno, int same,
471
struct origin *parent)
@@ -457,7 +489,7 @@ static void split_overlap(struct blame_entry split[3],
489
490
if (e->s_lno < tlno) {
491
/* there is a pre-chunk part not blamed on parent */
460
- split[0].suspect = e->suspect;
492
+ split[0].suspect = origin_incref(e->suspect);
493
split[0].lno = e->lno;
494
split[0].s_lno = e->s_lno;
495
split[0].num_lines = tlno - e->s_lno;
@@ -471,7 +503,7 @@ static void split_overlap(struct blame_entry split[3],
503
504
if (same < e->s_lno + e->num_lines) {
505
/* there is a post-chunk part not blamed on parent */
474
- split[2].suspect = e->suspect;
506
+ split[2].suspect = origin_incref(e->suspect);
507
split[2].lno = e->lno + (same - e->s_lno);
508
split[2].s_lno = e->s_lno + (same - e->s_lno);
509
split[2].num_lines = e->s_lno + e->num_lines - same;
@@ -483,11 +515,11 @@ static void split_overlap(struct blame_entry split[3],
515
516
if (split[1].num_lines < 1)
517
return;
486
- split[1].suspect = parent;
518
+ split[1].suspect = origin_incref(parent);
519
}
520
521
static void split_blame(struct scoreboard *sb,
490
- struct blame_entry split[3],
522
+ struct blame_entry *split,
523
struct blame_entry *e)
524
{
525
struct blame_entry *new_entry;
@@ -522,7 +554,7 @@ static void split_blame(struct scoreboard *sb,
554
add_blame_entry(sb, new_entry);
555
}
556
525
- if (1) { /* sanity */
557
+ if (DEBUG) { /* sanity */
558
struct blame_entry *ent;
559
int lno = sb->ent->lno, corrupt = 0;
560
@@ -545,6 +577,14 @@ static void split_blame(struct scoreboard *sb,
577
}
578
}
579
580
+static void decref_split(struct blame_entry *split)
581
+{
582
+ int i;
583
+
584
+ for (i = 0; i < 3; i++)
585
+ origin_decref(split[i].suspect);
586
+}
587
+
588
static void blame_overlap(struct scoreboard *sb, struct blame_entry *e,
589
int tlno, int plno, int same,
590
struct origin *parent)
@@ -552,9 +592,9 @@ static void blame_overlap(struct scoreboard *sb, struct blame_entry *e,
592
struct blame_entry split[3];
593
594
split_overlap(split, e, tlno, plno, same, parent);
555
- if (!split[1].suspect)
556
- return;
557
- split_blame(sb, split, e);
595
+ if (split[1].suspect)
596
+ split_blame(sb, split, e);
597
+ decref_split(split);
598
}
599
600
static int find_last_in_target(struct scoreboard *sb, struct origin *target)
@@ -636,22 +676,28 @@ static unsigned ent_score(struct scoreboard *sb, struct blame_entry *e)
676
}
677
678
static void copy_split_if_better(struct scoreboard *sb,
639
- struct blame_entry best_so_far[3],
640
- struct blame_entry this[3])
679
+ struct blame_entry *best_so_far,
680
+ struct blame_entry *this)
681
{
682
+ int i;
683
+
684
if (!this[1].suspect)
685
return;
686
if (best_so_far[1].suspect) {
687
if (ent_score(sb, &this[1]) < ent_score(sb, &best_so_far[1]))
688
return;
689
}
690
+
691
+ for (i = 0; i < 3; i++)
692
+ origin_incref(this[i].suspect);
693
+ decref_split(best_so_far);
694
memcpy(best_so_far, this, sizeof(struct blame_entry [3]));
695
}
696
697
static void find_copy_in_blob(struct scoreboard *sb,
698
struct blame_entry *ent,
699
struct origin *parent,
654
- struct blame_entry split[3],
700
+ struct blame_entry *split,
701
mmfile_t *file_p)
702
{
703
const char *cp;
@@ -687,6 +733,7 @@ static void find_copy_in_blob(struct scoreboard *sb,
733
chunk->same + ent->s_lno,
734
parent);
735
copy_split_if_better(sb, split, this);
736
+ decref_split(this);
737
}
738
plno = chunk->p_next;
739
tlno = chunk->t_next;
@@ -723,6 +770,7 @@ static int find_move_in_parent(struct scoreboard *sb,
770
if (split[1].suspect &&
771
blame_move_score < ent_score(sb, &split[1]))
772
split_blame(sb, split, e);
773
+ decref_split(split);
774
}
775
free(blob_p);
776
return 0;
@@ -806,6 +854,7 @@ static int find_copy_in_parent(struct scoreboard *sb,
854
this);
855
}
856
free(blob);
857
+ origin_decref(norigin);
858
}
859
diff_flush(&diff_opts);
860
@@ -814,6 +863,7 @@ static int find_copy_in_parent(struct scoreboard *sb,
863
if (split[1].suspect &&
864
blame_copy_score < ent_score(sb, &split[1]))
865
split_blame(sb, split, blame_list[j].ent);
866
+ decref_split(split);
867
}
868
free(blame_list);
869
@@ -843,9 +893,13 @@ static void pass_blame(struct scoreboard *sb, struct origin *origin, int opt)
893
if (!hashcmp(porigin->blob_sha1, origin->blob_sha1)) {
894
struct blame_entry *e;
895
for (e = sb->ent; e; e = e->next)
846
- if (e->suspect == origin)
896
+ if (e->suspect == origin) {
897
+ origin_incref(porigin);
898
+ origin_decref(e->suspect);
899
e->suspect = porigin;
848
- return;
900
+ }
901
+ origin_decref(porigin);
902
+ goto finish;
903
}
904
parent_origin[i] = porigin;
905
}
@@ -857,7 +911,7 @@ static void pass_blame(struct scoreboard *sb, struct origin *origin, int opt)
911
if (!porigin)
912
continue;
913
if (pass_blame_to_parent(sb, origin, porigin))
860
- return;
914
+ goto finish;
915
}
916
917
/*
@@ -871,7 +925,7 @@ static void pass_blame(struct scoreboard *sb, struct origin *origin, int opt)
925
if (!porigin)
926
continue;
927
if (find_move_in_parent(sb, origin, porigin))
874
- return;
928
+ goto finish;
929
}
930
931
/*
@@ -884,8 +938,12 @@ static void pass_blame(struct scoreboard *sb, struct origin *origin, int opt)
938
struct origin *porigin = parent_origin[i];
939
if (find_copy_in_parent(sb, origin, parent->item,
940
porigin, opt))
887
- return;
941
+ goto finish;
942
}
943
+
944
+ finish:
945
+ for (i = 0; i < MAXPARENT; i++)
946
+ origin_decref(parent_origin[i]);
947
}
948
949
static void assign_blame(struct scoreboard *sb, struct rev_info *revs, int opt)
@@ -902,6 +960,7 @@ static void assign_blame(struct scoreboard *sb, struct rev_info *revs, int opt)
960
if (!suspect)
961
return; /* all done */
962
963
+ origin_incref(suspect);
964
commit = suspect->commit;
965
parse_commit(commit);
966
if (!(commit->object.flags & UNINTERESTING) &&
@@ -912,7 +971,7 @@ static void assign_blame(struct scoreboard *sb, struct rev_info *revs, int opt)
971
for (ent = sb->ent; ent; ent = ent->next)
972
if (!cmp_suspect(ent->suspect, suspect))
973
ent->guilty = 1;
915
-
974
+ origin_decref(suspect);
975
coalesce(sb);
976
}
977
}
@@ -1132,7 +1191,9 @@ static void emit_other(struct scoreboard *sb, struct blame_entry *ent, int opt)
1191
ent->lno + 1 + cnt);
1192
else {
1193
if (opt & OUTPUT_SHOW_SCORE)
1135
- printf(" %*d", max_score_digits, ent->score);
1194
+ printf(" %*d %02d",
1195
+ max_score_digits, ent->score,
1196
+ ent->suspect->refcnt);
1197
if (opt & OUTPUT_SHOW_NAME)
1198
printf(" %-*.*s", longest_file, longest_file,
1199
suspect->path);
@@ -1273,6 +1334,45 @@ static void find_alignment(struct scoreboard *sb, int *option)
1334
max_score_digits = lineno_width(largest_score);
1335
}
1336
1337
+static void sanity_check_refcnt(struct scoreboard *sb)
1338
+{
1339
+ int baa = 0;
1340
+ struct blame_entry *ent;
1341
+
1342
+ for (ent = sb->ent; ent; ent = ent->next) {
1343
+ /* first mark the ones that haven't been checked */
1344
+ if (0 < ent->suspect->refcnt)
1345
+ ent->suspect->refcnt = -ent->suspect->refcnt;
1346
+ else if (!ent->suspect->refcnt)
1347
+ baa = 1;
1348
+ }
1349
+ for (ent = sb->ent; ent; ent = ent->next) {
1350
+ /* then pick each and see if they have the the
1351
+ * correct refcnt
1352
+ */
1353
+ int found;
1354
+ struct blame_entry *e;
1355
+ struct origin *suspect = ent->suspect;
1356
+
1357
+ if (0 < suspect->refcnt)
1358
+ continue;
1359
+ suspect->refcnt = -suspect->refcnt;
1360
+ for (found = 0, e = sb->ent; e; e = e->next) {
1361
+ if (e->suspect != suspect)
1362
+ continue;
1363
+ found++;
1364
+ }
1365
+ if (suspect->refcnt != found)
1366
+ baa = 1;
1367
+ }
1368
+ if (baa) {
1369
+ int opt = 0160;
1370
+ find_alignment(sb, &opt);
1371
+ output(sb, opt);
1372
+ die("Baa!");
1373
+ }
1374
+}
1375
+
1376
static int has_path_in_work_tree(const char *path)
1377
{
1378
struct stat st;