Use proper object allocators for unknown object nodes too
authorLinus Torvalds <torvalds@linux-foundation.org>
Tue, 17 Apr 2007 05:10:19 +0000 (22:10 -0700)
committerJunio C Hamano <junkio@cox.net>
Tue, 17 Apr 2007 06:36:11 +0000 (23:36 -0700)
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 <torvalds@linux-foundation.org>
Signed-off-by: Junio C Hamano <junkio@cox.net>
alloc.c
cache.h
object.c

diff --git a/alloc.c b/alloc.c
index 460db192d5a7de4073eb89972871720dd1c8fe1e..53eba373dbb923e27998ccac5e655047772d41fe 100644 (file)
--- a/alloc.c
+++ b/alloc.c
 
 #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 b1bd9e46c2bd64e00b671ff5ed512d9c12b53309..aa72791947cd17c9b78313771c4ba6443787ed7f 100644 (file)
--- 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 */
index 78a44a6ef4e4823487861c9173f3db4a3fb76e3a..153ebac66d86fb49df449edc4d92c9db4428f44a 100644 (file)
--- 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;
 }