diff options
-rw-r--r-- | alloc.c | 28 | ||||
-rw-r--r-- | cache.h | 1 | ||||
-rw-r--r-- | object.c | 15 |
3 files changed, 24 insertions, 20 deletions
@@ -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" @@ -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 */ @@ -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; } |