1818
1919#define BLOCKING 1024
2020
21- #define DEFINE_ALLOCATOR (name , type ) \
22- static unsigned int name##_allocs; \
23- void *alloc_##name##_node(void) \
24- { \
25- static int nr; \
26- static type *block; \
27- void *ret; \
28- \
29- if (!nr) { \
30- nr = BLOCKING; \
31- block = xmalloc(BLOCKING * sizeof(type)); \
32- } \
33- nr--; \
34- name##_allocs++; \
35- ret = block++; \
36- memset(ret, 0, sizeof(type)); \
37- return ret; \
38- }
39-
4021union any_object {
4122 struct object object ;
4223 struct blob blob ;
@@ -45,17 +26,73 @@ union any_object {
4526 struct tag tag ;
4627};
4728
48- DEFINE_ALLOCATOR (blob , struct blob )
49- DEFINE_ALLOCATOR (tree , struct tree )
50- DEFINE_ALLOCATOR (raw_commit , struct commit )
51- DEFINE_ALLOCATOR (tag , struct tag )
52- DEFINE_ALLOCATOR (object , union any_object )
29+ struct alloc_state {
30+ int count ; /* total number of nodes allocated */
31+ int nr ; /* number of nodes left in current allocation */
32+ void * p ; /* first free node in current allocation */
33+ };
34+
35+ static inline void * alloc_node (struct alloc_state * s , size_t node_size )
36+ {
37+ void * ret ;
38+
39+ if (!s -> nr ) {
40+ s -> nr = BLOCKING ;
41+ s -> p = xmalloc (BLOCKING * node_size );
42+ }
43+ s -> nr -- ;
44+ s -> count ++ ;
45+ ret = s -> p ;
46+ s -> p = (char * )s -> p + node_size ;
47+ memset (ret , 0 , node_size );
48+ return ret ;
49+ }
50+
51+ static struct alloc_state blob_state ;
52+ void * alloc_blob_node (void )
53+ {
54+ struct blob * b = alloc_node (& blob_state , sizeof (struct blob ));
55+ b -> object .type = OBJ_BLOB ;
56+ return b ;
57+ }
58+
59+ static struct alloc_state tree_state ;
60+ void * alloc_tree_node (void )
61+ {
62+ struct tree * t = alloc_node (& tree_state , sizeof (struct tree ));
63+ t -> object .type = OBJ_TREE ;
64+ return t ;
65+ }
66+
67+ static struct alloc_state tag_state ;
68+ void * alloc_tag_node (void )
69+ {
70+ struct tag * t = alloc_node (& tag_state , sizeof (struct tag ));
71+ t -> object .type = OBJ_TAG ;
72+ return t ;
73+ }
74+
75+ static struct alloc_state object_state ;
76+ void * alloc_object_node (void )
77+ {
78+ struct object * obj = alloc_node (& object_state , sizeof (union any_object ));
79+ obj -> type = OBJ_NONE ;
80+ return obj ;
81+ }
82+
83+ static struct alloc_state commit_state ;
84+
85+ unsigned int alloc_commit_index (void )
86+ {
87+ static unsigned int count ;
88+ return count ++ ;
89+ }
5390
5491void * alloc_commit_node (void )
5592{
56- static int commit_count ;
57- struct commit * c = alloc_raw_commit_node () ;
58- c -> index = commit_count ++ ;
93+ struct commit * c = alloc_node ( & commit_state , sizeof ( struct commit )) ;
94+ c -> object . type = OBJ_COMMIT ;
95+ c -> index = alloc_commit_index () ;
5996 return c ;
6097}
6198
@@ -66,13 +103,13 @@ static void report(const char *name, unsigned int count, size_t size)
66103}
67104
68105#define REPORT (name , type ) \
69- report(#name, name##_allocs , name##_allocs * sizeof(type) >> 10)
106+ report(#name, name##_state.count , name##_state.count * sizeof(type) >> 10)
70107
71108void alloc_report (void )
72109{
73110 REPORT (blob , struct blob );
74111 REPORT (tree , struct tree );
75- REPORT (raw_commit , struct commit );
112+ REPORT (commit , struct commit );
76113 REPORT (tag , struct tag );
77114 REPORT (object , union any_object );
78115}
0 commit comments