@@ -36,65 +36,30 @@ struct alloc_state {
int slab_nr, slab_alloc;
};
-struct alloc_state *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;
-
- 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;
-}
-
void *alloc_blob_node(struct repository *r)
{
- struct blob *b = alloc_node(r->parsed_objects->blob_state, sizeof(struct blob));
+ struct blob *b = mem_pool_calloc(&r->parsed_objects->objects_pool, 1, sizeof(struct blob));
b->object.type = OBJ_BLOB;
return b;
}
void *alloc_tree_node(struct repository *r)
{
- struct tree *t = alloc_node(r->parsed_objects->tree_state, sizeof(struct tree));
+ struct tree *t = mem_pool_calloc(&r->parsed_objects->objects_pool, 1, sizeof(struct tree));
t->object.type = OBJ_TREE;
return t;
}
void *alloc_tag_node(struct repository *r)
{
- struct tag *t = alloc_node(r->parsed_objects->tag_state, sizeof(struct tag));
+ struct tag *t = mem_pool_calloc(&r->parsed_objects->objects_pool, 1, sizeof(struct tag));
t->object.type = OBJ_TAG;
return t;
}
void *alloc_object_node(struct repository *r)
{
- struct object *obj = alloc_node(r->parsed_objects->object_state, sizeof(union any_object));
+ struct object *obj = mem_pool_calloc(&r->parsed_objects->objects_pool, 1, sizeof(union any_object));
obj->type = OBJ_NONE;
return obj;
}
@@ -118,26 +83,7 @@ void init_commit_node(struct commit *c)
void *alloc_commit_node(struct repository *r)
{
- struct commit *c = alloc_node(r->parsed_objects->commit_state, sizeof(struct commit));
+ struct commit *c = mem_pool_calloc(&r->parsed_objects->objects_pool, 1, sizeof(struct commit));
init_commit_node(c);
return c;
}
-
-static void report(const char *name, unsigned int count, size_t size)
-{
- fprintf(stderr, "%10s: %8u (%"PRIuMAX" kB)\n",
- name, count, (uintmax_t) size);
-}
-
-#define REPORT(name, type) \
- report(#name, r->parsed_objects->name##_state->count, \
- r->parsed_objects->name##_state->count * sizeof(type) >> 10)
-
-void alloc_report(struct repository *r)
-{
- REPORT(blob, struct blob);
- REPORT(tree, struct tree);
- REPORT(commit, struct commit);
- REPORT(tag, struct tag);
- REPORT(object, union any_object);
-}
@@ -15,7 +15,4 @@ void *alloc_tag_node(struct repository *r);
void *alloc_object_node(struct repository *r);
void alloc_report(struct repository *r);
-struct alloc_state *allocate_alloc_state(void);
-void clear_alloc_state(struct alloc_state *s);
-
#endif
@@ -5,7 +5,6 @@
#include "cache.h"
#include "mem-pool.h"
-#define BLOCK_GROWTH_SIZE 1024*1024 - sizeof(struct mp_block);
/*
* Allocate a new mp_block and insert it after the block specified in
@@ -69,6 +68,7 @@ void *mem_pool_alloc(struct mem_pool *pool, size_t len)
struct mp_block *p = NULL;
void *r;
+ pool->alloc_count++;
/* round up to a 'uintmax_t' alignment */
if (len & (sizeof(uintmax_t) - 1))
len += sizeof(uintmax_t) - (len & (sizeof(uintmax_t) - 1));
@@ -19,8 +19,13 @@ struct mem_pool {
/* The total amount of memory allocated by the pool. */
size_t pool_alloc;
+
+ /* The count of calling mem_pool_alloc .*/
+ size_t alloc_count;
};
+#define BLOCK_GROWTH_SIZE 1024*1024 - sizeof(struct mp_block)
+
/*
* Initialize mem_pool with specified initial size.
*/
@@ -182,7 +182,7 @@ struct object *lookup_unknown_object(const struct object_id *oid)
struct object *obj = lookup_object(the_repository, oid);
if (!obj)
obj = create_object(the_repository, oid,
- alloc_object_node(the_repository));
+ alloc_object_node(the_repository));
return obj;
}
@@ -470,13 +470,7 @@ struct parsed_object_pool *parsed_object_pool_new(void)
{
struct parsed_object_pool *o = xmalloc(sizeof(*o));
memset(o, 0, sizeof(*o));
-
- o->blob_state = allocate_alloc_state();
- o->tree_state = allocate_alloc_state();
- o->commit_state = allocate_alloc_state();
- o->tag_state = allocate_alloc_state();
- o->object_state = allocate_alloc_state();
-
+ mem_pool_init(&o->objects_pool, BLOCK_GROWTH_SIZE);
o->is_shallow = -1;
o->shallow_stat = xcalloc(1, sizeof(*o->shallow_stat));
@@ -568,16 +562,6 @@ void parsed_object_pool_clear(struct parsed_object_pool *o)
free_commit_buffer_slab(o->buffer_slab);
o->buffer_slab = NULL;
- clear_alloc_state(o->blob_state);
- clear_alloc_state(o->tree_state);
- clear_alloc_state(o->commit_state);
- clear_alloc_state(o->tag_state);
- clear_alloc_state(o->object_state);
+ mem_pool_discard(&o->objects_pool,0);
stat_validity_clear(o->shallow_stat);
- FREE_AND_NULL(o->blob_state);
- FREE_AND_NULL(o->tree_state);
- FREE_AND_NULL(o->commit_state);
- FREE_AND_NULL(o->tag_state);
- FREE_AND_NULL(o->object_state);
- FREE_AND_NULL(o->shallow_stat);
}
@@ -9,12 +9,7 @@ struct parsed_object_pool {
struct object **obj_hash;
int nr_objs, obj_hash_size;
- /* TODO: migrate alloc_states to mem-pool? */
- struct alloc_state *blob_state;
- struct alloc_state *tree_state;
- struct alloc_state *commit_state;
- struct alloc_state *tag_state;
- struct alloc_state *object_state;
+ struct mem_pool objects_pool;
/* parent substitutions from .git/info/grafts and .git/shallow */
struct commit_graft **grafts;