alloc.c: remove the alloc_raw_commit_node() function
[git/git.git] / alloc.c
CommitLineData
855419f7
LT
1/*
2 * alloc.c - specialized allocator for internal objects
3 *
4 * Copyright (C) 2006 Linus Torvalds
5 *
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.
11 */
12#include "cache.h"
13#include "object.h"
14#include "blob.h"
15#include "tree.h"
16#include "commit.h"
17#include "tag.h"
18
19#define BLOCKING 1024
20
2c1cbec1 21#define DEFINE_ALLOCATOR(name, type) \
225ea220 22static struct alloc_state name##_state; \
100c5f3b 23void *alloc_##name##_node(void) \
855419f7 24{ \
225ea220 25 return alloc_node(&name##_state, sizeof(type)); \
855419f7
LT
26}
27
2c1cbec1
LT
28union any_object {
29 struct object object;
30 struct blob blob;
31 struct tree tree;
32 struct commit commit;
33 struct tag tag;
34};
35
225ea220
RJ
36struct alloc_state {
37 int count; /* total number of nodes allocated */
38 int nr; /* number of nodes left in current allocation */
39 void *p; /* first free node in current allocation */
40};
41
42static inline void *alloc_node(struct alloc_state *s, size_t node_size)
43{
44 void *ret;
45
46 if (!s->nr) {
47 s->nr = BLOCKING;
48 s->p = xmalloc(BLOCKING * node_size);
49 }
50 s->nr--;
51 s->count++;
52 ret = s->p;
53 s->p = (char *)s->p + node_size;
54 memset(ret, 0, node_size);
55 return ret;
56}
57
2c1cbec1
LT
58DEFINE_ALLOCATOR(blob, struct blob)
59DEFINE_ALLOCATOR(tree, struct tree)
2c1cbec1
LT
60DEFINE_ALLOCATOR(tag, struct tag)
61DEFINE_ALLOCATOR(object, union any_object)
855419f7 62
225ea220
RJ
63static struct alloc_state commit_state;
64
969eba63
JK
65void *alloc_commit_node(void)
66{
67 static int commit_count;
225ea220 68 struct commit *c = alloc_node(&commit_state, sizeof(struct commit));
969eba63
JK
69 c->index = commit_count++;
70 return c;
71}
72
4b25d091 73static void report(const char *name, unsigned int count, size_t size)
579d1fbf 74{
28bd70d8
JN
75 fprintf(stderr, "%10s: %8u (%"PRIuMAX" kB)\n",
76 name, count, (uintmax_t) size);
579d1fbf
RJ
77}
78
c335d74d 79#define REPORT(name, type) \
225ea220 80 report(#name, name##_state.count, name##_state.count * sizeof(type) >> 10)
855419f7
LT
81
82void alloc_report(void)
83{
c335d74d
JK
84 REPORT(blob, struct blob);
85 REPORT(tree, struct tree);
225ea220 86 REPORT(commit, struct commit);
c335d74d
JK
87 REPORT(tag, struct tag);
88 REPORT(object, union any_object);
855419f7 89}