blob: 12afadfacdd6094912a6e18a217a9aa6318b47b2 [file] [log] [blame]
Linus Torvalds855419f2006-06-19 10:44:15 -07001/*
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
Linus Torvalds2c1cbec2007-04-16 22:10:19 -070021union any_object {
22 struct object object;
23 struct blob blob;
24 struct tree tree;
25 struct commit commit;
26 struct tag tag;
27};
28
Ramsay Jones225ea222014-07-13 02:41:41 -040029struct 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
35static 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
Jeff King600e2a62014-07-13 02:41:51 -040051static struct alloc_state blob_state;
52void *alloc_blob_node(void)
53{
54 struct blob *b = alloc_node(&blob_state, sizeof(struct blob));
Jeff Kingd36f51c2014-07-13 02:41:55 -040055 b->object.type = OBJ_BLOB;
Jeff King600e2a62014-07-13 02:41:51 -040056 return b;
57}
58
59static struct alloc_state tree_state;
60void *alloc_tree_node(void)
61{
62 struct tree *t = alloc_node(&tree_state, sizeof(struct tree));
Jeff Kingd36f51c2014-07-13 02:41:55 -040063 t->object.type = OBJ_TREE;
Jeff King600e2a62014-07-13 02:41:51 -040064 return t;
65}
66
67static struct alloc_state tag_state;
68void *alloc_tag_node(void)
69{
70 struct tag *t = alloc_node(&tag_state, sizeof(struct tag));
Jeff Kingd36f51c2014-07-13 02:41:55 -040071 t->object.type = OBJ_TAG;
Jeff King600e2a62014-07-13 02:41:51 -040072 return t;
73}
74
75static struct alloc_state object_state;
76void *alloc_object_node(void)
77{
78 struct object *obj = alloc_node(&object_state, sizeof(union any_object));
Jeff Kingd36f51c2014-07-13 02:41:55 -040079 obj->type = OBJ_NONE;
Jeff King600e2a62014-07-13 02:41:51 -040080 return obj;
81}
Linus Torvalds855419f2006-06-19 10:44:15 -070082
Ramsay Jones225ea222014-07-13 02:41:41 -040083static struct alloc_state commit_state;
84
Jeff King94d5a222014-07-13 02:42:08 -040085unsigned int alloc_commit_index(void)
86{
87 static unsigned int count;
88 return count++;
89}
90
Jeff King969eba62014-06-10 17:39:04 -040091void *alloc_commit_node(void)
92{
Ramsay Jones225ea222014-07-13 02:41:41 -040093 struct commit *c = alloc_node(&commit_state, sizeof(struct commit));
Jeff Kingd36f51c2014-07-13 02:41:55 -040094 c->object.type = OBJ_COMMIT;
Jeff King94d5a222014-07-13 02:42:08 -040095 c->index = alloc_commit_index();
Jeff King969eba62014-06-10 17:39:04 -040096 return c;
97}
98
Felipe Contreras4b25d092009-05-01 12:06:36 +030099static void report(const char *name, unsigned int count, size_t size)
Ramsay Allan Jones579d1fb2006-07-30 16:38:28 +0100100{
Jonathan Nieder28bd70d2011-03-16 00:15:31 -0500101 fprintf(stderr, "%10s: %8u (%"PRIuMAX" kB)\n",
102 name, count, (uintmax_t) size);
Ramsay Allan Jones579d1fb2006-07-30 16:38:28 +0100103}
104
Jeff Kingc335d742014-06-10 17:38:47 -0400105#define REPORT(name, type) \
Ramsay Jones225ea222014-07-13 02:41:41 -0400106 report(#name, name##_state.count, name##_state.count * sizeof(type) >> 10)
Linus Torvalds855419f2006-06-19 10:44:15 -0700107
108void alloc_report(void)
109{
Jeff Kingc335d742014-06-10 17:38:47 -0400110 REPORT(blob, struct blob);
111 REPORT(tree, struct tree);
Ramsay Jones225ea222014-07-13 02:41:41 -0400112 REPORT(commit, struct commit);
Jeff Kingc335d742014-06-10 17:38:47 -0400113 REPORT(tag, struct tag);
114 REPORT(object, union any_object);
Linus Torvalds855419f2006-06-19 10:44:15 -0700115}