alloc: allow arbitrary repositories for alloc functions

We have to convert all of the alloc functions at once, because alloc_report uses a funky macro for reporting. It is better for the sake of mechanical conversion to convert multiple functions at once rather than changing the structure of the reporting function. We record all memory allocation in alloc.c, and free them in clear_alloc_state, which is called for all repositories except the_repository. Signed-off-by: Stefan Beller <sbeller@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Stefan Beller committed May 15, 2018 at 14:48 UTC 14ba97f81c7b94e10d591b363688a073023f332d
13 files changed +140 -42
alloc.c
+41 -24
@@ -4,8 +4,7 @@
4 * Copyright (C) 2006 Linus Torvalds
5 *
6 * The standard malloc/free wastes too much space for objects, partly because
7 - * it maintains all the allocation infrastructure (which isn't needed, since
8 - * we never free an object descriptor anyway), but even more because it ends
7 + * it maintains all the allocation infrastructure, but even more because it ends
8 * up with maximal alignment because it doesn't know what the object alignment
9 * for the new allocation is.
10 */
@@ -15,6 +14,7 @@
14 #include "tree.h"
15 #include "commit.h"
16 #include "tag.h"
17 +#include "alloc.h"
18
19 #define BLOCKING 1024
20
@@ -30,8 +30,27 @@ struct alloc_state {
30 int count; /* total number of nodes allocated */
31 int nr; /* number of nodes left in current allocation */
32 void *p; /* first free node in current allocation */
33 +
34 + /* bookkeeping of allocations */
35 + void **slabs;
36 + int slab_nr, slab_alloc;
37 };
38
39 +void *allocate_alloc_state(void)
40 +{
41 + return xcalloc(1, sizeof(struct alloc_state));
42 +}
43 +
44 +void clear_alloc_state(struct alloc_state *s)
45 +{
46 + while (s->slab_nr > 0) {
47 + s->slab_nr--;
48 + free(s->slabs[s->slab_nr]);
49 + }
50 +
51 + FREE_AND_NULL(s->slabs);
52 +}
53 +
54 static inline void *alloc_node(struct alloc_state *s, size_t node_size)
55 {
56 void *ret;
@@ -39,60 +58,57 @@ static inline void *alloc_node(struct alloc_state *s, size_t node_size)
58 if (!s->nr) {
59 s->nr = BLOCKING;
60 s->p = xmalloc(BLOCKING * node_size);
61 +
62 + ALLOC_GROW(s->slabs, s->slab_nr + 1, s->slab_alloc);
63 + s->slabs[s->slab_nr++] = s->p;
64 }
65 s->nr--;
66 s->count++;
67 ret = s->p;
68 s->p = (char *)s->p + node_size;
69 memset(ret, 0, node_size);
70 +
71 return ret;
72 }
73
51 -static struct alloc_state blob_state;
52 -void *alloc_blob_node_the_repository(void)
74 +void *alloc_blob_node(struct repository *r)
75 {
54 - struct blob *b = alloc_node(&blob_state, sizeof(struct blob));
76 + struct blob *b = alloc_node(r->parsed_objects->blob_state, sizeof(struct blob));
77 b->object.type = OBJ_BLOB;
78 return b;
79 }
80
59 -static struct alloc_state tree_state;
60 -void *alloc_tree_node_the_repository(void)
81 +void *alloc_tree_node(struct repository *r)
82 {
62 - struct tree *t = alloc_node(&tree_state, sizeof(struct tree));
83 + struct tree *t = alloc_node(r->parsed_objects->tree_state, sizeof(struct tree));
84 t->object.type = OBJ_TREE;
85 return t;
86 }
87
67 -static struct alloc_state tag_state;
68 -void *alloc_tag_node_the_repository(void)
88 +void *alloc_tag_node(struct repository *r)
89 {
70 - struct tag *t = alloc_node(&tag_state, sizeof(struct tag));
90 + struct tag *t = alloc_node(r->parsed_objects->tag_state, sizeof(struct tag));
91 t->object.type = OBJ_TAG;
92 return t;
93 }
94
75 -static struct alloc_state object_state;
76 -void *alloc_object_node_the_repository(void)
95 +void *alloc_object_node(struct repository *r)
96 {
78 - struct object *obj = alloc_node(&object_state, sizeof(union any_object));
97 + struct object *obj = alloc_node(r->parsed_objects->object_state, sizeof(union any_object));
98 obj->type = OBJ_NONE;
99 return obj;
100 }
101
83 -static struct alloc_state commit_state;
84 -
85 -unsigned int alloc_commit_index_the_repository(void)
102 +unsigned int alloc_commit_index(struct repository *r)
103 {
87 - static unsigned int count;
88 - return count++;
104 + return r->parsed_objects->commit_count++;
105 }
106
91 -void *alloc_commit_node_the_repository(void)
107 +void *alloc_commit_node(struct repository *r)
108 {
93 - struct commit *c = alloc_node(&commit_state, sizeof(struct commit));
109 + struct commit *c = alloc_node(r->parsed_objects->commit_state, sizeof(struct commit));
110 c->object.type = OBJ_COMMIT;
95 - c->index = alloc_commit_index(the_repository);
111 + c->index = alloc_commit_index(r);
112 return c;
113 }
114
@@ -103,9 +119,10 @@ static void report(const char *name, unsigned int count, size_t size)
119 }
120
121 #define REPORT(name, type) \
106 - report(#name, name##_state.count, name##_state.count * sizeof(type) >> 10)
122 + report(#name, r->parsed_objects->name##_state->count, \
123 + r->parsed_objects->name##_state->count * sizeof(type) >> 10)
124
108 -void alloc_report_the_repository(void)
125 +void alloc_report(struct repository *r)
126 {
127 REPORT(blob, struct blob);
128 REPORT(tree, struct tree);
alloc.h new
+19
@@ -0,0 +1,19 @@
1 +#ifndef ALLOC_H
2 +#define ALLOC_H
3 +
4 +struct tree;
5 +struct commit;
6 +struct tag;
7 +
8 +void *alloc_blob_node(struct repository *r);
9 +void *alloc_tree_node(struct repository *r);
10 +void *alloc_commit_node(struct repository *r);
11 +void *alloc_tag_node(struct repository *r);
12 +void *alloc_object_node(struct repository *r);
13 +void alloc_report(struct repository *r);
14 +unsigned int alloc_commit_index(struct repository *r);
15 +
16 +void *allocate_alloc_state(void);
17 +void clear_alloc_state(struct alloc_state *s);
18 +
19 +#endif
blame.c
+1
@@ -6,6 +6,7 @@
6 #include "diffcore.h"
7 #include "tag.h"
8 #include "blame.h"
9 +#include "alloc.h"
10
11 void blame_origin_decref(struct blame_origin *o)
12 {
blob.c
+1
@@ -1,6 +1,7 @@
1 #include "cache.h"
2 #include "blob.h"
3 #include "repository.h"
4 +#include "alloc.h"
5
6 const char *blob_type = "blob";
7
cache.h
-16
@@ -1763,22 +1763,6 @@ extern const char *excludes_file;
1763 int decode_85(char *dst, const char *line, int linelen);
1764 void encode_85(char *buf, const unsigned char *data, int bytes);
1765
1766 -/* alloc.c */
1767 -#define alloc_blob_node(r) alloc_blob_node_##r()
1768 -extern void *alloc_blob_node_the_repository(void);
1769 -#define alloc_tree_node(r) alloc_tree_node_##r()
1770 -extern void *alloc_tree_node_the_repository(void);
1771 -#define alloc_commit_node(r) alloc_commit_node_##r()
1772 -extern void *alloc_commit_node_the_repository(void);
1773 -#define alloc_tag_node(r) alloc_tag_node_##r()
1774 -extern void *alloc_tag_node_the_repository(void);
1775 -#define alloc_object_node(r) alloc_object_node_##r()
1776 -extern void *alloc_object_node_the_repository(void);
1777 -#define alloc_report(r) alloc_report_##r()
1778 -extern void alloc_report_the_repository(void);
1779 -#define alloc_commit_index(r) alloc_commit_index_##r()
1780 -extern unsigned int alloc_commit_index_the_repository(void);
1781 -
1766 /* pkt-line.c */
1767 void packet_trace_identity(const char *prog);
1768
commit.c
+12
@@ -6,6 +6,7 @@
6 #include "diff.h"
7 #include "revision.h"
8 #include "notes.h"
9 +#include "alloc.h"
10 #include "gpg-interface.h"
11 #include "mergesort.h"
12 #include "commit-slab.h"
@@ -296,6 +297,17 @@ void free_commit_buffer(struct commit *commit)
297 }
298 }
299
300 +void release_commit_memory(struct commit *c)
301 +{
302 + c->tree = NULL;
303 + c->index = 0;
304 + free_commit_buffer(c);
305 + free_commit_list(c->parents);
306 + /* TODO: what about commit->util? */
307 +
308 + c->object.parsed = 0;
309 +}
310 +
311 const void *detach_commit_buffer(struct commit *commit, unsigned long *sizep)
312 {
313 struct commit_buffer *v = buffer_slab_peek(&buffer_slab, commit);
commit.h
+6
@@ -99,6 +99,12 @@ void unuse_commit_buffer(const struct commit *, const void *buffer);
99 */
100 void free_commit_buffer(struct commit *);
101
102 +/*
103 + * Release memory related to a commit, including the parent list and
104 + * any cached object buffer.
105 + */
106 +void release_commit_memory(struct commit *c);
107 +
108 /*
109 * Disassociate any cached object buffer from the commit, but do not free it.
110 * The buffer (or NULL, if none) is returned.
merge-recursive.c
+1
@@ -15,6 +15,7 @@
15 #include "diff.h"
16 #include "diffcore.h"
17 #include "tag.h"
18 +#include "alloc.h"
19 #include "unpack-trees.h"
20 #include "string-list.h"
21 #include "xdiff-interface.h"
object.c
+40 -2
@@ -5,6 +5,7 @@
5 #include "tree.h"
6 #include "commit.h"
7 #include "tag.h"
8 +#include "alloc.h"
9 #include "object-store.h"
10 #include "packfile.h"
11
@@ -455,6 +456,13 @@ struct parsed_object_pool *parsed_object_pool_new(void)
456 {
457 struct parsed_object_pool *o = xmalloc(sizeof(*o));
458 memset(o, 0, sizeof(*o));
459 +
460 + o->blob_state = allocate_alloc_state();
461 + o->tree_state = allocate_alloc_state();
462 + o->commit_state = allocate_alloc_state();
463 + o->tag_state = allocate_alloc_state();
464 + o->object_state = allocate_alloc_state();
465 +
466 return o;
467 }
468
@@ -501,9 +509,39 @@ void raw_object_store_clear(struct raw_object_store *o)
509 void parsed_object_pool_clear(struct parsed_object_pool *o)
510 {
511 /*
504 - * TOOD free objects in o->obj_hash.
505 - *
512 * As objects are allocated in slabs (see alloc.c), we do
513 * not need to free each object, but each slab instead.
514 + *
515 + * Before doing so, we need to free any additional memory
516 + * the objects may hold.
517 */
518 + unsigned i;
519 +
520 + for (i = 0; i < o->obj_hash_size; i++) {
521 + struct object *obj = o->obj_hash[i];
522 +
523 + if (!obj)
524 + continue;
525 +
526 + if (obj->type == OBJ_TREE)
527 + free_tree_buffer((struct tree*)obj);
528 + else if (obj->type == OBJ_COMMIT)
529 + release_commit_memory((struct commit*)obj);
530 + else if (obj->type == OBJ_TAG)
531 + release_tag_memory((struct tag*)obj);
532 + }
533 +
534 + FREE_AND_NULL(o->obj_hash);
535 + o->obj_hash_size = 0;
536 +
537 + clear_alloc_state(o->blob_state);
538 + clear_alloc_state(o->tree_state);
539 + clear_alloc_state(o->commit_state);
540 + clear_alloc_state(o->tag_state);
541 + clear_alloc_state(o->object_state);
542 + FREE_AND_NULL(o->blob_state);
543 + FREE_AND_NULL(o->tree_state);
544 + FREE_AND_NULL(o->commit_state);
545 + FREE_AND_NULL(o->tag_state);
546 + FREE_AND_NULL(o->object_state);
547 }
object.h
+8
@@ -4,6 +4,14 @@
4 struct parsed_object_pool {
5 struct object **obj_hash;
6 int nr_objs, obj_hash_size;
7 +
8 + /* TODO: migrate alloc_states to mem-pool? */
9 + struct alloc_state *blob_state;
10 + struct alloc_state *tree_state;
11 + struct alloc_state *commit_state;
12 + struct alloc_state *tag_state;
13 + struct alloc_state *object_state;
14 + unsigned commit_count;
15 };
16
17 struct parsed_object_pool *parsed_object_pool_new(void);
tag.c
+9
@@ -3,6 +3,7 @@
3 #include "commit.h"
4 #include "tree.h"
5 #include "blob.h"
6 +#include "alloc.h"
7 #include "gpg-interface.h"
8
9 const char *tag_type = "tag";
@@ -115,6 +116,14 @@ static timestamp_t parse_tag_date(const char *buf, const char *tail)
116 return parse_timestamp(dateptr, NULL, 10);
117 }
118
119 +void release_tag_memory(struct tag *t)
120 +{
121 + free(t->tag);
122 + t->tagged = NULL;
123 + t->object.parsed = 0;
124 + t->date = 0;
125 +}
126 +
127 int parse_tag_buffer(struct tag *item, const void *data, unsigned long size)
128 {
129 struct object_id oid;
tag.h
+1
@@ -15,6 +15,7 @@ struct tag {
15 extern struct tag *lookup_tag(const struct object_id *oid);
16 extern int parse_tag_buffer(struct tag *item, const void *data, unsigned long size);
17 extern int parse_tag(struct tag *item);
18 +extern void release_tag_memory(struct tag *t);
19 extern struct object *deref_tag(struct object *, const char *, int);
20 extern struct object *deref_tag_noverify(struct object *);
21 extern int gpg_verify_tag(const struct object_id *oid,
tree.c
+1
@@ -5,6 +5,7 @@
5 #include "blob.h"
6 #include "commit.h"
7 #include "tag.h"
8 +#include "alloc.h"
9 #include "tree-walk.h"
10
11 const char *tree_type = "tree";