summaryrefslogtreecommitdiff
path: root/alloc.c
diff options
context:
space:
mode:
authorJunio C Hamano <gitster@pobox.com>2018-06-25 20:22:38 (GMT)
committerJunio C Hamano <gitster@pobox.com>2018-06-25 20:22:38 (GMT)
commit110240588d5c0ca88d3b55da52068f59d8d6367d (patch)
tree5a9120c2c70aeb9d2e7ac4347870094870019e49 /alloc.c
parentfa82bb70d9ef3acb6a47cddfb38a7b0ca40ac362 (diff)
parent14ba97f81c7b94e10d591b363688a073023f332d (diff)
downloadgit-110240588d5c0ca88d3b55da52068f59d8d6367d.zip
git-110240588d5c0ca88d3b55da52068f59d8d6367d.tar.gz
git-110240588d5c0ca88d3b55da52068f59d8d6367d.tar.bz2
Merge branch 'sb/object-store-alloc'
The conversion to pass "the_repository" and then "a_repository" throughout the object access API continues. * sb/object-store-alloc: alloc: allow arbitrary repositories for alloc functions object: allow create_object to handle arbitrary repositories object: allow grow_object_hash to handle arbitrary repositories alloc: add repository argument to alloc_commit_index alloc: add repository argument to alloc_report alloc: add repository argument to alloc_object_node alloc: add repository argument to alloc_tag_node alloc: add repository argument to alloc_commit_node alloc: add repository argument to alloc_tree_node alloc: add repository argument to alloc_blob_node object: add repository argument to grow_object_hash object: add repository argument to create_object repository: introduce parsed objects field
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 e8ab14f..c2fc5d6 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(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(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(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(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(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(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();
+ c->index = alloc_commit_index(r);
c->graph_pos = COMMIT_NOT_FROM_GRAPH;
c->generation = GENERATION_NUMBER_INFINITY;
return c;
@@ -105,9 +121,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(void)
+void alloc_report(struct repository *r)
{
REPORT(blob, struct blob);
REPORT(tree, struct tree);