commit: convert commit->buffer to a slab

This will make it easier to manage the buffer cache independently of the "struct commit" objects. It also shrinks "struct commit" by one pointer, which may be helpful. Unfortunately it does not reduce the max memory size of something like "rev-list", because rev-list uses get_cached_commit_buffer() to decide not to show each commit's output (and due to the design of slab_at, accessing the slab requires us to extend it, allocating exactly the same number of buffer pointers we dropped from the commit structs). Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Jun 10, 2014 at 17:43 UTC c1b3c71f4b4571abb2b2a457122fd100dc9f7eb0
2 files changed +13 -8
commit.c
+13 -7
@@ -245,14 +245,17 @@ int unregister_shallow(const unsigned char *sha1)
245 return 0;
246 }
247
248 +define_commit_slab(buffer_slab, void *);
249 +static struct buffer_slab buffer_slab = COMMIT_SLAB_INIT(1, buffer_slab);
250 +
251 void set_commit_buffer(struct commit *commit, void *buffer)
252 {
250 - commit->buffer = buffer;
253 + *buffer_slab_at(&buffer_slab, commit) = buffer;
254 }
255
256 const void *get_cached_commit_buffer(const struct commit *commit)
257 {
255 - return commit->buffer;
258 + return *buffer_slab_at(&buffer_slab, commit);
259 }
260
261 const void *get_commit_buffer(const struct commit *commit)
@@ -274,20 +277,23 @@ const void *get_commit_buffer(const struct commit *commit)
277
278 void unuse_commit_buffer(const struct commit *commit, const void *buffer)
279 {
277 - if (commit->buffer != buffer)
280 + void *cached = *buffer_slab_at(&buffer_slab, commit);
281 + if (cached != buffer)
282 free((void *)buffer);
283 }
284
285 void free_commit_buffer(struct commit *commit)
286 {
283 - free(commit->buffer);
284 - commit->buffer = NULL;
287 + void **b = buffer_slab_at(&buffer_slab, commit);
288 + free(*b);
289 + *b = NULL;
290 }
291
292 const void *detach_commit_buffer(struct commit *commit)
293 {
289 - void *ret = commit->buffer;
290 - commit->buffer = NULL;
294 + void **b = buffer_slab_at(&buffer_slab, commit);
295 + void *ret = *b;
296 + *b = NULL;
297 return ret;
298 }
299
commit.h
-1
@@ -20,7 +20,6 @@ struct commit {
20 unsigned long date;
21 struct commit_list *parents;
22 struct tree *tree;
23 - char *buffer;
23 };
24
25 extern int save_commit_buffer;