From 2c1cbec1e2f0bd7b15fe5e921d287babfd91c7d3 Mon Sep 17 00:00:00 2001 From: Linus Torvalds Date: Mon, 16 Apr 2007 22:10:19 -0700 Subject: Use proper object allocators for unknown object nodes too We used to use a different allocator scheme for when we didn't know the object type. That meant that objects that were created without any up-front knowledge of the type would not go through the same allocation paths as normal object allocations, and would miss out on the statistics. But perhaps more importantly than the statistics (that are useful when looking at memory usage but not much else), if we want to make the object hash tables use a denser object pointer representation, we need to make sure that they all go through the same blocking allocator. Signed-off-by: Linus Torvalds Signed-off-by: Junio C Hamano --- alloc.c | 28 ++++++++++++++++++++-------- cache.h | 1 + object.c | 15 +++------------ 3 files changed, 24 insertions(+), 20 deletions(-) diff --git a/alloc.c b/alloc.c index 460db192d5..53eba373db 100644 --- a/alloc.c +++ b/alloc.c @@ -18,26 +18,38 @@ #define BLOCKING 1024 -#define DEFINE_ALLOCATOR(name) \ +#define DEFINE_ALLOCATOR(name, type) \ static unsigned int name##_allocs; \ struct name *alloc_##name##_node(void) \ { \ static int nr; \ - static struct name *block; \ + static type *block; \ + void *ret; \ \ if (!nr) { \ nr = BLOCKING; \ - block = xcalloc(BLOCKING, sizeof(struct name)); \ + block = xmalloc(BLOCKING * sizeof(type)); \ } \ nr--; \ name##_allocs++; \ - return block++; \ + ret = block++; \ + memset(ret, 0, sizeof(type)); \ + return ret; \ } -DEFINE_ALLOCATOR(blob) -DEFINE_ALLOCATOR(tree) -DEFINE_ALLOCATOR(commit) -DEFINE_ALLOCATOR(tag) +union any_object { + struct object object; + struct blob blob; + struct tree tree; + struct commit commit; + struct tag tag; +}; + +DEFINE_ALLOCATOR(blob, struct blob) +DEFINE_ALLOCATOR(tree, struct tree) +DEFINE_ALLOCATOR(commit, struct commit) +DEFINE_ALLOCATOR(tag, struct tag) +DEFINE_ALLOCATOR(object, union any_object) #ifdef NO_C99_FORMAT #define SZ_FMT "%u" diff --git a/cache.h b/cache.h index b1bd9e46c2..aa72791947 100644 --- a/cache.h +++ b/cache.h @@ -484,6 +484,7 @@ extern struct blob *alloc_blob_node(void); extern struct tree *alloc_tree_node(void); extern struct commit *alloc_commit_node(void); extern struct tag *alloc_tag_node(void); +extern struct object *alloc_object_node(void); extern void alloc_report(void); /* trace.c */ diff --git a/object.c b/object.c index 78a44a6ef4..153ebac66d 100644 --- a/object.c +++ b/object.c @@ -120,22 +120,13 @@ void created_object(const unsigned char *sha1, struct object *obj) nr_objs++; } -union any_object { - struct object object; - struct commit commit; - struct tree tree; - struct blob blob; - struct tag tag; -}; - struct object *lookup_unknown_object(const unsigned char *sha1) { struct object *obj = lookup_object(sha1); if (!obj) { - union any_object *ret = xcalloc(1, sizeof(*ret)); - created_object(sha1, &ret->object); - ret->object.type = OBJ_NONE; - return &ret->object; + obj = alloc_object_node(); + created_object(sha1, obj); + obj->type = OBJ_NONE; } return obj; } -- cgit v1.2.3 From 100c5f3b0b27ec6617de1a785c4ff481e92636c1 Mon Sep 17 00:00:00 2001 From: Linus Torvalds Date: Mon, 16 Apr 2007 22:11:43 -0700 Subject: Clean up object creation to use more common code This replaces the fairly odd "created_object()" function that did _most_ of the object setup with a more complete "create_object()" function that also has a more natural calling convention. Signed-off-by: Linus Torvalds Signed-off-by: Junio C Hamano --- alloc.c | 2 +- blob.c | 8 ++------ cache.h | 14 +++++--------- commit.c | 8 ++------ object.c | 14 +++++++------- object.h | 2 +- tag.c | 10 +++------- tree.c | 8 ++------ 8 files changed, 23 insertions(+), 43 deletions(-) diff --git a/alloc.c b/alloc.c index 53eba373db..216c23a6f8 100644 --- a/alloc.c +++ b/alloc.c @@ -20,7 +20,7 @@ #define DEFINE_ALLOCATOR(name, type) \ static unsigned int name##_allocs; \ -struct name *alloc_##name##_node(void) \ +void *alloc_##name##_node(void) \ { \ static int nr; \ static type *block; \ diff --git a/blob.c b/blob.c index 0a9ea417b8..bd7d078e1a 100644 --- a/blob.c +++ b/blob.c @@ -6,12 +6,8 @@ const char *blob_type = "blob"; struct blob *lookup_blob(const unsigned char *sha1) { struct object *obj = lookup_object(sha1); - if (!obj) { - struct blob *ret = alloc_blob_node(); - created_object(sha1, &ret->object); - ret->object.type = OBJ_BLOB; - return ret; - } + if (!obj) + return create_object(sha1, OBJ_BLOB, alloc_blob_node()); if (!obj->type) obj->type = OBJ_BLOB; if (obj->type != OBJ_BLOB) { diff --git a/cache.h b/cache.h index aa72791947..4de25cc4b2 100644 --- a/cache.h +++ b/cache.h @@ -476,15 +476,11 @@ int decode_85(char *dst, const char *line, int linelen); void encode_85(char *buf, const unsigned char *data, int bytes); /* alloc.c */ -struct blob; -struct tree; -struct commit; -struct tag; -extern struct blob *alloc_blob_node(void); -extern struct tree *alloc_tree_node(void); -extern struct commit *alloc_commit_node(void); -extern struct tag *alloc_tag_node(void); -extern struct object *alloc_object_node(void); +extern void *alloc_blob_node(void); +extern void *alloc_tree_node(void); +extern void *alloc_commit_node(void); +extern void *alloc_tag_node(void); +extern void *alloc_object_node(void); extern void alloc_report(void); /* trace.c */ diff --git a/commit.c b/commit.c index 952095faa7..10466c4ae0 100644 --- a/commit.c +++ b/commit.c @@ -98,12 +98,8 @@ struct commit *lookup_commit_reference(const unsigned char *sha1) struct commit *lookup_commit(const unsigned char *sha1) { struct object *obj = lookup_object(sha1); - if (!obj) { - struct commit *ret = alloc_commit_node(); - created_object(sha1, &ret->object); - ret->object.type = OBJ_COMMIT; - return ret; - } + if (!obj) + return create_object(sha1, OBJ_COMMIT, alloc_commit_node()); if (!obj->type) obj->type = OBJ_COMMIT; return check_commit(obj, sha1, 0); diff --git a/object.c b/object.c index 153ebac66d..7bd3fec556 100644 --- a/object.c +++ b/object.c @@ -105,11 +105,13 @@ static void grow_object_hash(void) obj_hash_size = new_hash_size; } -void created_object(const unsigned char *sha1, struct object *obj) +void *create_object(const unsigned char *sha1, int type, void *o) { + struct object *obj = o; + obj->parsed = 0; obj->used = 0; - obj->type = OBJ_NONE; + obj->type = type; obj->flags = 0; hashcpy(obj->sha1, sha1); @@ -118,16 +120,14 @@ void created_object(const unsigned char *sha1, struct object *obj) insert_obj_hash(obj, obj_hash, obj_hash_size); nr_objs++; + return obj; } struct object *lookup_unknown_object(const unsigned char *sha1) { struct object *obj = lookup_object(sha1); - if (!obj) { - obj = alloc_object_node(); - created_object(sha1, obj); - obj->type = OBJ_NONE; - } + if (!obj) + obj = create_object(sha1, OBJ_NONE, alloc_object_node()); return obj; } diff --git a/object.h b/object.h index bdbbc1889c..3e26a0e8b9 100644 --- a/object.h +++ b/object.h @@ -46,7 +46,7 @@ extern struct object_refs *lookup_object_refs(struct object *); /** Internal only **/ struct object *lookup_object(const unsigned char *sha1); -void created_object(const unsigned char *sha1, struct object *obj); +extern void *create_object(const unsigned char *sha1, int type, void *obj); /** Returns the object, having parsed it to find out what it is. **/ struct object *parse_object(const unsigned char *sha1); diff --git a/tag.c b/tag.c index 56a49f4fe1..330d287924 100644 --- a/tag.c +++ b/tag.c @@ -20,13 +20,9 @@ struct object *deref_tag(struct object *o, const char *warn, int warnlen) struct tag *lookup_tag(const unsigned char *sha1) { - struct object *obj = lookup_object(sha1); - if (!obj) { - struct tag *ret = alloc_tag_node(); - created_object(sha1, &ret->object); - ret->object.type = OBJ_TAG; - return ret; - } + struct object *obj = lookup_object(sha1); + if (!obj) + return create_object(sha1, OBJ_TAG, alloc_tag_node()); if (!obj->type) obj->type = OBJ_TAG; if (obj->type != OBJ_TAG) { diff --git a/tree.c b/tree.c index d188c0fbae..e5bfbceb22 100644 --- a/tree.c +++ b/tree.c @@ -127,12 +127,8 @@ int read_tree(struct tree *tree, int stage, const char **match) struct tree *lookup_tree(const unsigned char *sha1) { struct object *obj = lookup_object(sha1); - if (!obj) { - struct tree *ret = alloc_tree_node(); - created_object(sha1, &ret->object); - ret->object.type = OBJ_TREE; - return ret; - } + if (!obj) + return create_object(sha1, OBJ_TREE, alloc_tree_node()); if (!obj->type) obj->type = OBJ_TREE; if (obj->type != OBJ_TREE) { -- cgit v1.2.3