blob: 957a0af3626432b0c9c2dd1301f20c4ff339a0a4 [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
Stefan Beller14ba97f2018-05-15 14:48:42 -07007 * it maintains all the allocation infrastructure, but even more because it ends
Linus Torvalds855419f2006-06-19 10:44:15 -07008 * up with maximal alignment because it doesn't know what the object alignment
9 * for the new allocation is.
10 */
11#include "cache.h"
12#include "object.h"
13#include "blob.h"
14#include "tree.h"
15#include "commit.h"
16#include "tag.h"
Stefan Beller14ba97f2018-05-15 14:48:42 -070017#include "alloc.h"
Linus Torvalds855419f2006-06-19 10:44:15 -070018
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 */
Stefan Beller14ba97f2018-05-15 14:48:42 -070033
34 /* bookkeeping of allocations */
35 void **slabs;
36 int slab_nr, slab_alloc;
Ramsay Jones225ea222014-07-13 02:41:41 -040037};
38
Elijah Newren17313102018-08-15 10:54:06 -070039struct alloc_state *allocate_alloc_state(void)
Stefan Beller14ba97f2018-05-15 14:48:42 -070040{
41 return xcalloc(1, sizeof(struct alloc_state));
42}
43
44void 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
Ramsay Jones225ea222014-07-13 02:41:41 -040054static inline void *alloc_node(struct alloc_state *s, size_t node_size)
55{
56 void *ret;
57
58 if (!s->nr) {
59 s->nr = BLOCKING;
60 s->p = xmalloc(BLOCKING * node_size);
Stefan Beller14ba97f2018-05-15 14:48:42 -070061
62 ALLOC_GROW(s->slabs, s->slab_nr + 1, s->slab_alloc);
63 s->slabs[s->slab_nr++] = s->p;
Ramsay Jones225ea222014-07-13 02:41:41 -040064 }
65 s->nr--;
66 s->count++;
67 ret = s->p;
68 s->p = (char *)s->p + node_size;
69 memset(ret, 0, node_size);
Stefan Beller14ba97f2018-05-15 14:48:42 -070070
Ramsay Jones225ea222014-07-13 02:41:41 -040071 return ret;
72}
73
Stefan Beller14ba97f2018-05-15 14:48:42 -070074void *alloc_blob_node(struct repository *r)
Jeff King600e2a62014-07-13 02:41:51 -040075{
Stefan Beller14ba97f2018-05-15 14:48:42 -070076 struct blob *b = alloc_node(r->parsed_objects->blob_state, sizeof(struct blob));
Jeff Kingd36f51c2014-07-13 02:41:55 -040077 b->object.type = OBJ_BLOB;
Jeff King600e2a62014-07-13 02:41:51 -040078 return b;
79}
80
Stefan Beller14ba97f2018-05-15 14:48:42 -070081void *alloc_tree_node(struct repository *r)
Jeff King600e2a62014-07-13 02:41:51 -040082{
Stefan Beller14ba97f2018-05-15 14:48:42 -070083 struct tree *t = alloc_node(r->parsed_objects->tree_state, sizeof(struct tree));
Jeff Kingd36f51c2014-07-13 02:41:55 -040084 t->object.type = OBJ_TREE;
Jeff King600e2a62014-07-13 02:41:51 -040085 return t;
86}
87
Stefan Beller14ba97f2018-05-15 14:48:42 -070088void *alloc_tag_node(struct repository *r)
Jeff King600e2a62014-07-13 02:41:51 -040089{
Stefan Beller14ba97f2018-05-15 14:48:42 -070090 struct tag *t = alloc_node(r->parsed_objects->tag_state, sizeof(struct tag));
Jeff Kingd36f51c2014-07-13 02:41:55 -040091 t->object.type = OBJ_TAG;
Jeff King600e2a62014-07-13 02:41:51 -040092 return t;
93}
94
Stefan Beller14ba97f2018-05-15 14:48:42 -070095void *alloc_object_node(struct repository *r)
Jeff King600e2a62014-07-13 02:41:51 -040096{
Stefan Beller14ba97f2018-05-15 14:48:42 -070097 struct object *obj = alloc_node(r->parsed_objects->object_state, sizeof(union any_object));
Jeff Kingd36f51c2014-07-13 02:41:55 -040098 obj->type = OBJ_NONE;
Jeff King600e2a62014-07-13 02:41:51 -040099 return obj;
100}
Linus Torvalds855419f2006-06-19 10:44:15 -0700101
Abhishek Kumar6da43d92020-06-17 14:44:08 +0530102/*
103 * The returned count is to be used as an index into commit slabs,
104 * that are *NOT* maintained per repository, and that is why a single
105 * global counter is used.
106 */
107static unsigned int alloc_commit_index(void)
Jeff King94d5a222014-07-13 02:42:08 -0400108{
Abhishek Kumar6da43d92020-06-17 14:44:08 +0530109 static unsigned int parsed_commits_count;
110 return parsed_commits_count++;
Jeff King94d5a222014-07-13 02:42:08 -0400111}
112
Abhishek Kumar6da43d92020-06-17 14:44:08 +0530113void init_commit_node(struct commit *c)
SZEDER Gábor4468d442019-01-27 14:08:32 +0100114{
115 c->object.type = OBJ_COMMIT;
Abhishek Kumar6da43d92020-06-17 14:44:08 +0530116 c->index = alloc_commit_index();
SZEDER Gábor4468d442019-01-27 14:08:32 +0100117}
118
Stefan Beller14ba97f2018-05-15 14:48:42 -0700119void *alloc_commit_node(struct repository *r)
Jeff King969eba62014-06-10 17:39:04 -0400120{
Stefan Beller14ba97f2018-05-15 14:48:42 -0700121 struct commit *c = alloc_node(r->parsed_objects->commit_state, sizeof(struct commit));
Abhishek Kumar6da43d92020-06-17 14:44:08 +0530122 init_commit_node(c);
Jeff King969eba62014-06-10 17:39:04 -0400123 return c;
124}
125
Felipe Contreras4b25d092009-05-01 12:06:36 +0300126static void report(const char *name, unsigned int count, size_t size)
Ramsay Allan Jones579d1fb2006-07-30 16:38:28 +0100127{
Jonathan Nieder28bd70d2011-03-16 00:15:31 -0500128 fprintf(stderr, "%10s: %8u (%"PRIuMAX" kB)\n",
129 name, count, (uintmax_t) size);
Ramsay Allan Jones579d1fb2006-07-30 16:38:28 +0100130}
131
Jeff Kingc335d742014-06-10 17:38:47 -0400132#define REPORT(name, type) \
Stefan Beller14ba97f2018-05-15 14:48:42 -0700133 report(#name, r->parsed_objects->name##_state->count, \
134 r->parsed_objects->name##_state->count * sizeof(type) >> 10)
Linus Torvalds855419f2006-06-19 10:44:15 -0700135
Stefan Beller14ba97f2018-05-15 14:48:42 -0700136void alloc_report(struct repository *r)
Linus Torvalds855419f2006-06-19 10:44:15 -0700137{
Jeff Kingc335d742014-06-10 17:38:47 -0400138 REPORT(blob, struct blob);
139 REPORT(tree, struct tree);
Ramsay Jones225ea222014-07-13 02:41:41 -0400140 REPORT(commit, struct commit);
Jeff Kingc335d742014-06-10 17:38:47 -0400141 REPORT(tag, struct tag);
142 REPORT(object, union any_object);
Linus Torvalds855419f2006-06-19 10:44:15 -0700143}