summaryrefslogtreecommitdiff
path: root/alloc.c
diff options
context:
space:
mode:
authorStefan Beller <sbeller@google.com>2018-05-15 21:48:42 (GMT)
committerJunio C Hamano <gitster@pobox.com>2018-05-16 02:16:50 (GMT)
commit14ba97f81c7b94e10d591b363688a073023f332d (patch)
treeed3672cff9cc1e0df22baa6c689db4deb06805f3 /alloc.c
parent341e45e46bba094ef1274957ef5891f43e91b344 (diff)
downloadgit-14ba97f81c7b94e10d591b363688a073023f332d.zip
git-14ba97f81c7b94e10d591b363688a073023f332d.tar.gz
git-14ba97f81c7b94e10d591b363688a073023f332d.tar.bz2
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>
Diffstat (limited to 'alloc.c')
-rw-r--r--alloc.c65
1 files changed, 41 insertions, 24 deletions
diff --git a/alloc.c b/alloc.c
index 277dadd..714df63 100644
--- a/alloc.c
+++ b/alloc.c
@@ -4,8 +4,7 @@
* Copyright (C) 2006 Linus Torvalds
*
* The standard malloc/free wastes too much space for objects, partly because
- * it maintains all the allocation infrastructure (which isn't needed, since
- * we never free an object descriptor anyway), but even more because it ends
+ * it maintains all the allocation infrastructure, but even more because it ends
* up with maximal alignment because it doesn't know what the object alignment
* for the new allocation is.
*/
@@ -15,6 +14,7 @@
#include "tree.h"
#include "commit.h"
#include "tag.h"
+#include "alloc.h"
#define BLOCKING 1024
@@ -30,8 +30,27 @@ struct alloc_state {
int count; /* total number of nodes allocated */
int nr; /* number of nodes left in current allocation */
void *p; /* first free node in current allocation */
+
+ /* bookkeeping of allocations */
+ void **slabs;
+ int slab_nr, slab_alloc;
};
+void *allocate_alloc_state(void)
+{
+ return xcalloc(1, sizeof(struct alloc_state));
+}
+
+void clear_alloc_state(struct alloc_state *s)
+{
+ while (s->slab_nr > 0) {
+ s->slab_nr--;
+ free(s->slabs[s->slab_nr]);
+ }
+
+ FREE_AND_NULL(s->slabs);
+}
+
static inline void *alloc_node(struct alloc_state *s, size_t node_size)
{
void *ret;
@@ -39,60 +58,57 @@ static inline void *alloc_node(struct alloc_state *s, size_t node_size)
if (!s->nr) {
s->nr = BLOCKING;
s->p = xmalloc(BLOCKING * node_size);
+
+ ALLOC_GROW(s->slabs, s->slab_nr + 1, s->slab_alloc);
+ s->slabs[s->slab_nr++] = s->p;
}
s->nr--;
s->count++;
ret = s->p;
s->p = (char *)s->p + node_size;
memset(ret, 0, node_size);
+
return ret;
}
-static struct alloc_state blob_state;
-void *alloc_blob_node_the_repository(void)
+void *alloc_blob_node(struct repository *r)
{
- struct blob *b = alloc_node(&blob_state, sizeof(struct blob));
+ struct blob *b = alloc_node(r->parsed_objects->blob_state, sizeof(struct blob));
b->object.type = OBJ_BLOB;
return b;
}
-static struct alloc_state tree_state;
-void *alloc_tree_node_the_repository(void)
+void *alloc_tree_node(struct repository *r)
{
- struct tree *t = alloc_node(&tree_state, sizeof(struct tree));
+ struct tree *t = alloc_node(r->parsed_objects->tree_state, sizeof(struct tree));
t->object.type = OBJ_TREE;
return t;
}
-static struct alloc_state tag_state;
-void *alloc_tag_node_the_repository(void)
+void *alloc_tag_node(struct repository *r)
{
- struct tag *t = alloc_node(&tag_state, sizeof(struct tag));
+ struct tag *t = alloc_node(r->parsed_objects->tag_state, sizeof(struct tag));
t->object.type = OBJ_TAG;
return t;
}
-static struct alloc_state object_state;
-void *alloc_object_node_the_repository(void)
+void *alloc_object_node(struct repository *r)
{
- struct object *obj = alloc_node(&object_state, sizeof(union any_object));
+ struct object *obj = alloc_node(r->parsed_objects->object_state, sizeof(union any_object));
obj->type = OBJ_NONE;
return obj;
}
-static struct alloc_state commit_state;
-
-unsigned int alloc_commit_index_the_repository(void)
+unsigned int alloc_commit_index(struct repository *r)
{
- static unsigned int count;
- return count++;
+ return r->parsed_objects->commit_count++;
}
-void *alloc_commit_node_the_repository(void)
+void *alloc_commit_node(struct repository *r)
{
- struct commit *c = alloc_node(&commit_state, sizeof(struct commit));
+ struct commit *c = alloc_node(r->parsed_objects->commit_state, sizeof(struct commit));
c->object.type = OBJ_COMMIT;
- c->index = alloc_commit_index(the_repository);
+ c->index = alloc_commit_index(r);
return c;
}
@@ -103,9 +119,10 @@ static void report(const char *name, unsigned int count, size_t size)
}
#define REPORT(name, type) \
- report(#name, name##_state.count, name##_state.count * sizeof(type) >> 10)
+ report(#name, r->parsed_objects->name##_state->count, \
+ r->parsed_objects->name##_state->count * sizeof(type) >> 10)
-void alloc_report_the_repository(void)
+void alloc_report(struct repository *r)
{
REPORT(blob, struct blob);
REPORT(tree, struct tree);