git-pickaxe: fix origin refcounting
When we introduced the cached origin per commit, we gave up proper garbage collecting because it meant that commits hold onto their cached copy. There is no need to do so. Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Nov 4, 2006 at 19:18 UTC
854b97f6eb3bcbeb76b2705bfbffead1558bde77
1 file changed
+42
-19
builtin-pickaxe.c
+42
-19
@@ -168,23 +168,28 @@ static void coalesce(struct scoreboard *sb)
168
sanity_check_refcnt(sb);
169
}
170
171
+static struct origin *make_origin(struct commit *commit, const char *path)
172
+{
173
+ struct origin *o;
174
+ o = xcalloc(1, sizeof(*o) + strlen(path) + 1);
175
+ o->commit = commit;
176
+ o->refcnt = 1;
177
+ strcpy(o->path, path);
178
+ return o;
179
+}
180
+
181
static struct origin *get_origin(struct scoreboard *sb,
182
struct commit *commit,
183
const char *path)
184
{
185
struct blame_entry *e;
176
- struct origin *o;
186
187
for (e = sb->ent; e; e = e->next) {
188
if (e->suspect->commit == commit &&
189
!strcmp(e->suspect->path, path))
190
return origin_incref(e->suspect);
191
}
183
- o = xcalloc(1, sizeof(*o) + strlen(path) + 1);
184
- o->commit = commit;
185
- o->refcnt = 1;
186
- strcpy(o->path, path);
187
- return o;
192
+ return make_origin(commit, path);
193
}
194
195
static int fill_blob_sha1(struct origin *origin)
@@ -216,9 +221,19 @@ static struct origin *find_origin(struct scoreboard *sb,
221
const char *paths[2];
222
223
if (parent->util) {
224
+ /* This is a freestanding copy of origin and not
225
+ * refcounted.
226
+ */
227
struct origin *cached = parent->util;
220
- if (!strcmp(cached->path, origin->path))
221
- return origin_incref(cached);
228
+ if (!strcmp(cached->path, origin->path)) {
229
+ porigin = get_origin(sb, parent, cached->path);
230
+ if (porigin->refcnt == 1)
231
+ hashcpy(porigin->blob_sha1, cached->blob_sha1);
232
+ return porigin;
233
+ }
234
+ /* otherwise it was not very useful; free it */
235
+ free(parent->util);
236
+ parent->util = NULL;
237
}
238
239
/* See if the origin->path is different between parent
@@ -268,10 +283,10 @@ static struct origin *find_origin(struct scoreboard *sb,
283
}
284
diff_flush(&diff_opts);
285
if (porigin) {
271
- origin_incref(porigin);
272
- if (parent->util)
273
- origin_decref(parent->util);
274
- parent->util = porigin;
286
+ struct origin *cached;
287
+ cached = make_origin(porigin->commit, porigin->path);
288
+ hashcpy(cached->blob_sha1, porigin->blob_sha1);
289
+ parent->util = cached;
290
}
291
return porigin;
292
}
@@ -1434,8 +1449,13 @@ static void sanity_check_refcnt(struct scoreboard *sb)
1449
1450
for (ent = sb->ent; ent; ent = ent->next) {
1451
/* Nobody should have zero or negative refcnt */
1437
- if (ent->suspect->refcnt <= 0)
1452
+ if (ent->suspect->refcnt <= 0) {
1453
+ fprintf(stderr, "%s in %s has negative refcnt %d\n",
1454
+ ent->suspect->path,
1455
+ sha1_to_hex(ent->suspect->commit->object.sha1),
1456
+ ent->suspect->refcnt);
1457
baa = 1;
1458
+ }
1459
}
1460
for (ent = sb->ent; ent; ent = ent->next) {
1461
/* Mark the ones that haven't been checked */
@@ -1444,9 +1464,7 @@ static void sanity_check_refcnt(struct scoreboard *sb)
1464
}
1465
for (ent = sb->ent; ent; ent = ent->next) {
1466
/* then pick each and see if they have the the correct
1447
- * refcnt; note that ->util caching means origin's refcnt
1448
- * may well be greater than the number of blame entries
1449
- * that use it.
1467
+ * refcnt.
1468
*/
1469
int found;
1470
struct blame_entry *e;
@@ -1460,14 +1478,19 @@ static void sanity_check_refcnt(struct scoreboard *sb)
1478
continue;
1479
found++;
1480
}
1463
- if (suspect->refcnt < found)
1464
- baa = 1;
1481
+ if (suspect->refcnt != found) {
1482
+ fprintf(stderr, "%s in %s has refcnt %d, not %d\n",
1483
+ ent->suspect->path,
1484
+ sha1_to_hex(ent->suspect->commit->object.sha1),
1485
+ ent->suspect->refcnt, found);
1486
+ baa = 2;
1487
+ }
1488
}
1489
if (baa) {
1490
int opt = 0160;
1491
find_alignment(sb, &opt);
1492
output(sb, opt);
1470
- die("Baa!");
1493
+ die("Baa %d!", baa);
1494
}
1495
}
1496