Skip to content

Commit 2c1cbec

Browse files
torvaldsJunio C Hamano
authored andcommitted
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 <torvalds@linux-foundation.org> Signed-off-by: Junio C Hamano <junkio@cox.net>
1 parent f948792 commit 2c1cbec

File tree

3 files changed

+24
-20
lines changed

3 files changed

+24
-20
lines changed

alloc.c

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,38 @@
1818

1919
#define BLOCKING 1024
2020

21-
#define DEFINE_ALLOCATOR(name) \
21+
#define DEFINE_ALLOCATOR(name, type) \
2222
static unsigned int name##_allocs; \
2323
struct name *alloc_##name##_node(void) \
2424
{ \
2525
static int nr; \
26-
static struct name *block; \
26+
static type *block; \
27+
void *ret; \
2728
\
2829
if (!nr) { \
2930
nr = BLOCKING; \
30-
block = xcalloc(BLOCKING, sizeof(struct name)); \
31+
block = xmalloc(BLOCKING * sizeof(type)); \
3132
} \
3233
nr--; \
3334
name##_allocs++; \
34-
return block++; \
35+
ret = block++; \
36+
memset(ret, 0, sizeof(type)); \
37+
return ret; \
3538
}
3639

37-
DEFINE_ALLOCATOR(blob)
38-
DEFINE_ALLOCATOR(tree)
39-
DEFINE_ALLOCATOR(commit)
40-
DEFINE_ALLOCATOR(tag)
40+
union any_object {
41+
struct object object;
42+
struct blob blob;
43+
struct tree tree;
44+
struct commit commit;
45+
struct tag tag;
46+
};
47+
48+
DEFINE_ALLOCATOR(blob, struct blob)
49+
DEFINE_ALLOCATOR(tree, struct tree)
50+
DEFINE_ALLOCATOR(commit, struct commit)
51+
DEFINE_ALLOCATOR(tag, struct tag)
52+
DEFINE_ALLOCATOR(object, union any_object)
4153

4254
#ifdef NO_C99_FORMAT
4355
#define SZ_FMT "%u"

cache.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,7 @@ extern struct blob *alloc_blob_node(void);
484484
extern struct tree *alloc_tree_node(void);
485485
extern struct commit *alloc_commit_node(void);
486486
extern struct tag *alloc_tag_node(void);
487+
extern struct object *alloc_object_node(void);
487488
extern void alloc_report(void);
488489

489490
/* trace.c */

object.c

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -120,22 +120,13 @@ void created_object(const unsigned char *sha1, struct object *obj)
120120
nr_objs++;
121121
}
122122

123-
union any_object {
124-
struct object object;
125-
struct commit commit;
126-
struct tree tree;
127-
struct blob blob;
128-
struct tag tag;
129-
};
130-
131123
struct object *lookup_unknown_object(const unsigned char *sha1)
132124
{
133125
struct object *obj = lookup_object(sha1);
134126
if (!obj) {
135-
union any_object *ret = xcalloc(1, sizeof(*ret));
136-
created_object(sha1, &ret->object);
137-
ret->object.type = OBJ_NONE;
138-
return &ret->object;
127+
obj = alloc_object_node();
128+
created_object(sha1, obj);
129+
obj->type = OBJ_NONE;
139130
}
140131
return obj;
141132
}

0 commit comments

Comments
 (0)