44 * Copyright (C) 2006 Linus Torvalds
55 *
66 * The standard malloc/free wastes too much space for objects, partly because
7- * it maintains all the allocation infrastructure (which isn't needed, since
8- * we never free an object descriptor anyway), but even more because it ends
7+ * it maintains all the allocation infrastructure, but even more because it ends
98 * up with maximal alignment because it doesn't know what the object alignment
109 * for the new allocation is.
1110 */
1514#include "tree.h"
1615#include "commit.h"
1716#include "tag.h"
17+ #include "alloc.h"
1818
1919#define BLOCKING 1024
2020
@@ -30,69 +30,85 @@ struct alloc_state {
3030 int count ; /* total number of nodes allocated */
3131 int nr ; /* number of nodes left in current allocation */
3232 void * p ; /* first free node in current allocation */
33+
34+ /* bookkeeping of allocations */
35+ void * * slabs ;
36+ int slab_nr , slab_alloc ;
3337};
3438
39+ void * allocate_alloc_state (void )
40+ {
41+ return xcalloc (1 , sizeof (struct alloc_state ));
42+ }
43+
44+ void clear_alloc_state (struct alloc_state * s )
45+ {
46+ while (s -> slab_nr > 0 ) {
47+ s -> slab_nr -- ;
48+ free (s -> slabs [s -> slab_nr ]);
49+ }
50+
51+ FREE_AND_NULL (s -> slabs );
52+ }
53+
3554static inline void * alloc_node (struct alloc_state * s , size_t node_size )
3655{
3756 void * ret ;
3857
3958 if (!s -> nr ) {
4059 s -> nr = BLOCKING ;
4160 s -> p = xmalloc (BLOCKING * node_size );
61+
62+ ALLOC_GROW (s -> slabs , s -> slab_nr + 1 , s -> slab_alloc );
63+ s -> slabs [s -> slab_nr ++ ] = s -> p ;
4264 }
4365 s -> nr -- ;
4466 s -> count ++ ;
4567 ret = s -> p ;
4668 s -> p = (char * )s -> p + node_size ;
4769 memset (ret , 0 , node_size );
70+
4871 return ret ;
4972}
5073
51- static struct alloc_state blob_state ;
52- void * alloc_blob_node_the_repository (void )
74+ void * alloc_blob_node (struct repository * r )
5375{
54- struct blob * b = alloc_node (& blob_state , sizeof (struct blob ));
76+ struct blob * b = alloc_node (r -> parsed_objects -> blob_state , sizeof (struct blob ));
5577 b -> object .type = OBJ_BLOB ;
5678 return b ;
5779}
5880
59- static struct alloc_state tree_state ;
60- void * alloc_tree_node_the_repository (void )
81+ void * alloc_tree_node (struct repository * r )
6182{
62- struct tree * t = alloc_node (& tree_state , sizeof (struct tree ));
83+ struct tree * t = alloc_node (r -> parsed_objects -> tree_state , sizeof (struct tree ));
6384 t -> object .type = OBJ_TREE ;
6485 return t ;
6586}
6687
67- static struct alloc_state tag_state ;
68- void * alloc_tag_node_the_repository (void )
88+ void * alloc_tag_node (struct repository * r )
6989{
70- struct tag * t = alloc_node (& tag_state , sizeof (struct tag ));
90+ struct tag * t = alloc_node (r -> parsed_objects -> tag_state , sizeof (struct tag ));
7191 t -> object .type = OBJ_TAG ;
7292 return t ;
7393}
7494
75- static struct alloc_state object_state ;
76- void * alloc_object_node_the_repository (void )
95+ void * alloc_object_node (struct repository * r )
7796{
78- struct object * obj = alloc_node (& object_state , sizeof (union any_object ));
97+ struct object * obj = alloc_node (r -> parsed_objects -> object_state , sizeof (union any_object ));
7998 obj -> type = OBJ_NONE ;
8099 return obj ;
81100}
82101
83- static struct alloc_state commit_state ;
84-
85- unsigned int alloc_commit_index_the_repository (void )
102+ unsigned int alloc_commit_index (struct repository * r )
86103{
87- static unsigned int count ;
88- return count ++ ;
104+ return r -> parsed_objects -> commit_count ++ ;
89105}
90106
91- void * alloc_commit_node_the_repository ( void )
107+ void * alloc_commit_node ( struct repository * r )
92108{
93- struct commit * c = alloc_node (& commit_state , sizeof (struct commit ));
109+ struct commit * c = alloc_node (r -> parsed_objects -> commit_state , sizeof (struct commit ));
94110 c -> object .type = OBJ_COMMIT ;
95- c -> index = alloc_commit_index (the_repository );
111+ c -> index = alloc_commit_index (r );
96112 return c ;
97113}
98114
@@ -103,9 +119,10 @@ static void report(const char *name, unsigned int count, size_t size)
103119}
104120
105121#define REPORT (name , type ) \
106- report(#name, name##_state.count, name##_state.count * sizeof(type) >> 10)
122+ report(#name, r->parsed_objects->name##_state->count, \
123+ r->parsed_objects->name##_state->count * sizeof(type) >> 10)
107124
108- void alloc_report_the_repository ( void )
125+ void alloc_report ( struct repository * r )
109126{
110127 REPORT (blob , struct blob );
111128 REPORT (tree , struct tree );
0 commit comments