2 * alloc.c - specialized allocator for internal objects
4 * Copyright (C) 2006 Linus Torvalds
6 * 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
9 * up with maximal alignment because it doesn't know what the object alignment
10 * for the new allocation is.
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 */
35 static inline void *alloc_node(struct alloc_state
*s
, size_t node_size
)
41 s
->p
= xmalloc(BLOCKING
* node_size
);
46 s
->p
= (char *)s
->p
+ node_size
;
47 memset(ret
, 0, node_size
);
51 static struct alloc_state blob_state
;
52 void *alloc_blob_node(void)
54 struct blob
*b
= alloc_node(&blob_state
, sizeof(struct blob
));
55 b
->object
.type
= OBJ_BLOB
;
59 static struct alloc_state tree_state
;
60 void *alloc_tree_node(void)
62 struct tree
*t
= alloc_node(&tree_state
, sizeof(struct tree
));
63 t
->object
.type
= OBJ_TREE
;
67 static struct alloc_state tag_state
;
68 void *alloc_tag_node(void)
70 struct tag
*t
= alloc_node(&tag_state
, sizeof(struct tag
));
71 t
->object
.type
= OBJ_TAG
;
75 static struct alloc_state object_state
;
76 void *alloc_object_node(void)
78 struct object
*obj
= alloc_node(&object_state
, sizeof(union any_object
));
83 static struct alloc_state commit_state
;
85 unsigned int alloc_commit_index(void)
87 static unsigned int count
;
91 void *alloc_commit_node(void)
93 struct commit
*c
= alloc_node(&commit_state
, sizeof(struct commit
));
94 c
->object
.type
= OBJ_COMMIT
;
95 c
->index
= alloc_commit_index();
96 c
->graph_pos
= COMMIT_NOT_FROM_GRAPH
;
100 static void report(const char *name
, unsigned int count
, size_t size
)
102 fprintf(stderr
, "%10s: %8u (%"PRIuMAX
" kB)\n",
103 name
, count
, (uintmax_t) size
);
106 #define REPORT(name, type) \
107 report(#name, name##_state.count, name##_state.count * sizeof(type) >> 10)
109 void alloc_report(void)
111 REPORT(blob
, struct blob
);
112 REPORT(tree
, struct tree
);
113 REPORT(commit
, struct commit
);
114 REPORT(tag
, struct tag
);
115 REPORT(object
, union any_object
);