Skip to content

Commit 1102405

Browse files
committed
Merge branch 'sb/object-store-alloc'
The conversion to pass "the_repository" and then "a_repository" throughout the object access API continues. * sb/object-store-alloc: alloc: allow arbitrary repositories for alloc functions object: allow create_object to handle arbitrary repositories object: allow grow_object_hash to handle arbitrary repositories alloc: add repository argument to alloc_commit_index alloc: add repository argument to alloc_report alloc: add repository argument to alloc_object_node alloc: add repository argument to alloc_tag_node alloc: add repository argument to alloc_commit_node alloc: add repository argument to alloc_tree_node alloc: add repository argument to alloc_blob_node object: add repository argument to grow_object_hash object: add repository argument to create_object repository: introduce parsed objects field
2 parents fa82bb7 + 14ba97f commit 1102405

15 files changed

+221
-68
lines changed

alloc.c

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
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
*/
@@ -15,6 +14,7 @@
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+
3554
static 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(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(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(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(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(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(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();
111+
c->index = alloc_commit_index(r);
96112
c->graph_pos = COMMIT_NOT_FROM_GRAPH;
97113
c->generation = GENERATION_NUMBER_INFINITY;
98114
return c;
@@ -105,9 +121,10 @@ static void report(const char *name, unsigned int count, size_t size)
105121
}
106122

107123
#define REPORT(name, type) \
108-
report(#name, name##_state.count, name##_state.count * sizeof(type) >> 10)
124+
report(#name, r->parsed_objects->name##_state->count, \
125+
r->parsed_objects->name##_state->count * sizeof(type) >> 10)
109126

110-
void alloc_report(void)
127+
void alloc_report(struct repository *r)
111128
{
112129
REPORT(blob, struct blob);
113130
REPORT(tree, struct tree);

alloc.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#ifndef ALLOC_H
2+
#define ALLOC_H
3+
4+
struct tree;
5+
struct commit;
6+
struct tag;
7+
8+
void *alloc_blob_node(struct repository *r);
9+
void *alloc_tree_node(struct repository *r);
10+
void *alloc_commit_node(struct repository *r);
11+
void *alloc_tag_node(struct repository *r);
12+
void *alloc_object_node(struct repository *r);
13+
void alloc_report(struct repository *r);
14+
unsigned int alloc_commit_index(struct repository *r);
15+
16+
void *allocate_alloc_state(void);
17+
void clear_alloc_state(struct alloc_state *s);
18+
19+
#endif

blame.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "diffcore.h"
77
#include "tag.h"
88
#include "blame.h"
9+
#include "alloc.h"
910
#include "commit-slab.h"
1011

1112
define_commit_slab(blame_suspects, struct blame_origin *);
@@ -179,7 +180,7 @@ static struct commit *fake_working_tree_commit(struct diff_options *opt,
179180

180181
read_cache();
181182
time(&now);
182-
commit = alloc_commit_node();
183+
commit = alloc_commit_node(the_repository);
183184
commit->object.parsed = 1;
184185
commit->date = now;
185186
parent_tail = &commit->parents;

blob.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
#include "cache.h"
22
#include "blob.h"
3+
#include "repository.h"
4+
#include "alloc.h"
35

46
const char *blob_type = "blob";
57

68
struct blob *lookup_blob(const struct object_id *oid)
79
{
810
struct object *obj = lookup_object(oid->hash);
911
if (!obj)
10-
return create_object(oid->hash, alloc_blob_node());
12+
return create_object(the_repository, oid->hash,
13+
alloc_blob_node(the_repository));
1114
return object_as_type(obj, OBJ_BLOB, 0);
1215
}
1316

cache.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1770,15 +1770,6 @@ extern const char *excludes_file;
17701770
int decode_85(char *dst, const char *line, int linelen);
17711771
void encode_85(char *buf, const unsigned char *data, int bytes);
17721772

1773-
/* alloc.c */
1774-
extern void *alloc_blob_node(void);
1775-
extern void *alloc_tree_node(void);
1776-
extern void *alloc_commit_node(void);
1777-
extern void *alloc_tag_node(void);
1778-
extern void *alloc_object_node(void);
1779-
extern void alloc_report(void);
1780-
extern unsigned int alloc_commit_index(void);
1781-
17821773
/* pkt-line.c */
17831774
void packet_trace_identity(const char *prog);
17841775

commit.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "diff.h"
88
#include "revision.h"
99
#include "notes.h"
10+
#include "alloc.h"
1011
#include "gpg-interface.h"
1112
#include "mergesort.h"
1213
#include "commit-slab.h"
@@ -52,7 +53,8 @@ struct commit *lookup_commit(const struct object_id *oid)
5253
{
5354
struct object *obj = lookup_object(oid->hash);
5455
if (!obj)
55-
return create_object(oid->hash, alloc_commit_node());
56+
return create_object(the_repository, oid->hash,
57+
alloc_commit_node(the_repository));
5658
return object_as_type(obj, OBJ_COMMIT, 0);
5759
}
5860

@@ -325,6 +327,17 @@ struct object_id *get_commit_tree_oid(const struct commit *commit)
325327
return &get_commit_tree(commit)->object.oid;
326328
}
327329

330+
void release_commit_memory(struct commit *c)
331+
{
332+
c->maybe_tree = NULL;
333+
c->index = 0;
334+
free_commit_buffer(c);
335+
free_commit_list(c->parents);
336+
/* TODO: what about commit->util? */
337+
338+
c->object.parsed = 0;
339+
}
340+
328341
const void *detach_commit_buffer(struct commit *commit, unsigned long *sizep)
329342
{
330343
struct commit_buffer *v = buffer_slab_peek(&buffer_slab, commit);

commit.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,12 @@ void free_commit_buffer(struct commit *);
119119
struct tree *get_commit_tree(const struct commit *);
120120
struct object_id *get_commit_tree_oid(const struct commit *);
121121

122+
/*
123+
* Release memory related to a commit, including the parent list and
124+
* any cached object buffer.
125+
*/
126+
void release_commit_memory(struct commit *c);
127+
122128
/*
123129
* Disassociate any cached object buffer from the commit, but do not free it.
124130
* The buffer (or NULL, if none) is returned.

merge-recursive.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "diff.h"
1616
#include "diffcore.h"
1717
#include "tag.h"
18+
#include "alloc.h"
1819
#include "unpack-trees.h"
1920
#include "string-list.h"
2021
#include "xdiff-interface.h"
@@ -160,7 +161,7 @@ static struct tree *shift_tree_object(struct tree *one, struct tree *two,
160161

161162
static struct commit *make_virtual_commit(struct tree *tree, const char *comment)
162163
{
163-
struct commit *commit = alloc_commit_node();
164+
struct commit *commit = alloc_commit_node(the_repository);
164165

165166
set_merge_remote_desc(commit, comment, (struct object *)commit);
166167
commit->maybe_tree = tree;

0 commit comments

Comments
 (0)