Skip to content

Commit 14ba97f

Browse files
stefanbellergitster
authored andcommitted
alloc: allow arbitrary repositories for alloc functions
We have to convert all of the alloc functions at once, because alloc_report uses a funky macro for reporting. It is better for the sake of mechanical conversion to convert multiple functions at once rather than changing the structure of the reporting function. We record all memory allocation in alloc.c, and free them in clear_alloc_state, which is called for all repositories except the_repository. Signed-off-by: Stefan Beller <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 341e45e commit 14ba97f

13 files changed

+140
-42
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_the_repository(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_the_repository(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_the_repository(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_the_repository(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_the_repository(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_the_repository(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(the_repository);
111+
c->index = alloc_commit_index(r);
96112
return c;
97113
}
98114

@@ -103,9 +119,10 @@ static void report(const char *name, unsigned int count, size_t size)
103119
}
104120

105121
#define REPORT(name, type) \
106-
report(#name, name##_state.count, name##_state.count * sizeof(type) >> 10)
122+
report(#name, r->parsed_objects->name##_state->count, \
123+
r->parsed_objects->name##_state->count * sizeof(type) >> 10)
107124

108-
void alloc_report_the_repository(void)
125+
void alloc_report(struct repository *r)
109126
{
110127
REPORT(blob, struct blob);
111128
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: 1 addition & 0 deletions
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

1011
void blame_origin_decref(struct blame_origin *o)
1112
{

blob.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "cache.h"
22
#include "blob.h"
33
#include "repository.h"
4+
#include "alloc.h"
45

56
const char *blob_type = "blob";
67

cache.h

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1763,22 +1763,6 @@ extern const char *excludes_file;
17631763
int decode_85(char *dst, const char *line, int linelen);
17641764
void encode_85(char *buf, const unsigned char *data, int bytes);
17651765

1766-
/* alloc.c */
1767-
#define alloc_blob_node(r) alloc_blob_node_##r()
1768-
extern void *alloc_blob_node_the_repository(void);
1769-
#define alloc_tree_node(r) alloc_tree_node_##r()
1770-
extern void *alloc_tree_node_the_repository(void);
1771-
#define alloc_commit_node(r) alloc_commit_node_##r()
1772-
extern void *alloc_commit_node_the_repository(void);
1773-
#define alloc_tag_node(r) alloc_tag_node_##r()
1774-
extern void *alloc_tag_node_the_repository(void);
1775-
#define alloc_object_node(r) alloc_object_node_##r()
1776-
extern void *alloc_object_node_the_repository(void);
1777-
#define alloc_report(r) alloc_report_##r()
1778-
extern void alloc_report_the_repository(void);
1779-
#define alloc_commit_index(r) alloc_commit_index_##r()
1780-
extern unsigned int alloc_commit_index_the_repository(void);
1781-
17821766
/* pkt-line.c */
17831767
void packet_trace_identity(const char *prog);
17841768

commit.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "diff.h"
77
#include "revision.h"
88
#include "notes.h"
9+
#include "alloc.h"
910
#include "gpg-interface.h"
1011
#include "mergesort.h"
1112
#include "commit-slab.h"
@@ -296,6 +297,17 @@ void free_commit_buffer(struct commit *commit)
296297
}
297298
}
298299

300+
void release_commit_memory(struct commit *c)
301+
{
302+
c->tree = NULL;
303+
c->index = 0;
304+
free_commit_buffer(c);
305+
free_commit_list(c->parents);
306+
/* TODO: what about commit->util? */
307+
308+
c->object.parsed = 0;
309+
}
310+
299311
const void *detach_commit_buffer(struct commit *commit, unsigned long *sizep)
300312
{
301313
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
@@ -99,6 +99,12 @@ void unuse_commit_buffer(const struct commit *, const void *buffer);
9999
*/
100100
void free_commit_buffer(struct commit *);
101101

102+
/*
103+
* Release memory related to a commit, including the parent list and
104+
* any cached object buffer.
105+
*/
106+
void release_commit_memory(struct commit *c);
107+
102108
/*
103109
* Disassociate any cached object buffer from the commit, but do not free it.
104110
* The buffer (or NULL, if none) is returned.

merge-recursive.c

Lines changed: 1 addition & 0 deletions
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"

object.c

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "tree.h"
66
#include "commit.h"
77
#include "tag.h"
8+
#include "alloc.h"
89
#include "object-store.h"
910
#include "packfile.h"
1011

@@ -455,6 +456,13 @@ struct parsed_object_pool *parsed_object_pool_new(void)
455456
{
456457
struct parsed_object_pool *o = xmalloc(sizeof(*o));
457458
memset(o, 0, sizeof(*o));
459+
460+
o->blob_state = allocate_alloc_state();
461+
o->tree_state = allocate_alloc_state();
462+
o->commit_state = allocate_alloc_state();
463+
o->tag_state = allocate_alloc_state();
464+
o->object_state = allocate_alloc_state();
465+
458466
return o;
459467
}
460468

@@ -501,9 +509,39 @@ void raw_object_store_clear(struct raw_object_store *o)
501509
void parsed_object_pool_clear(struct parsed_object_pool *o)
502510
{
503511
/*
504-
* TOOD free objects in o->obj_hash.
505-
*
506512
* As objects are allocated in slabs (see alloc.c), we do
507513
* not need to free each object, but each slab instead.
514+
*
515+
* Before doing so, we need to free any additional memory
516+
* the objects may hold.
508517
*/
518+
unsigned i;
519+
520+
for (i = 0; i < o->obj_hash_size; i++) {
521+
struct object *obj = o->obj_hash[i];
522+
523+
if (!obj)
524+
continue;
525+
526+
if (obj->type == OBJ_TREE)
527+
free_tree_buffer((struct tree*)obj);
528+
else if (obj->type == OBJ_COMMIT)
529+
release_commit_memory((struct commit*)obj);
530+
else if (obj->type == OBJ_TAG)
531+
release_tag_memory((struct tag*)obj);
532+
}
533+
534+
FREE_AND_NULL(o->obj_hash);
535+
o->obj_hash_size = 0;
536+
537+
clear_alloc_state(o->blob_state);
538+
clear_alloc_state(o->tree_state);
539+
clear_alloc_state(o->commit_state);
540+
clear_alloc_state(o->tag_state);
541+
clear_alloc_state(o->object_state);
542+
FREE_AND_NULL(o->blob_state);
543+
FREE_AND_NULL(o->tree_state);
544+
FREE_AND_NULL(o->commit_state);
545+
FREE_AND_NULL(o->tag_state);
546+
FREE_AND_NULL(o->object_state);
509547
}

object.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@
44
struct parsed_object_pool {
55
struct object **obj_hash;
66
int nr_objs, obj_hash_size;
7+
8+
/* TODO: migrate alloc_states to mem-pool? */
9+
struct alloc_state *blob_state;
10+
struct alloc_state *tree_state;
11+
struct alloc_state *commit_state;
12+
struct alloc_state *tag_state;
13+
struct alloc_state *object_state;
14+
unsigned commit_count;
715
};
816

917
struct parsed_object_pool *parsed_object_pool_new(void);

tag.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "commit.h"
44
#include "tree.h"
55
#include "blob.h"
6+
#include "alloc.h"
67
#include "gpg-interface.h"
78

89
const char *tag_type = "tag";
@@ -115,6 +116,14 @@ static timestamp_t parse_tag_date(const char *buf, const char *tail)
115116
return parse_timestamp(dateptr, NULL, 10);
116117
}
117118

119+
void release_tag_memory(struct tag *t)
120+
{
121+
free(t->tag);
122+
t->tagged = NULL;
123+
t->object.parsed = 0;
124+
t->date = 0;
125+
}
126+
118127
int parse_tag_buffer(struct tag *item, const void *data, unsigned long size)
119128
{
120129
struct object_id oid;

tag.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ struct tag {
1515
extern struct tag *lookup_tag(const struct object_id *oid);
1616
extern int parse_tag_buffer(struct tag *item, const void *data, unsigned long size);
1717
extern int parse_tag(struct tag *item);
18+
extern void release_tag_memory(struct tag *t);
1819
extern struct object *deref_tag(struct object *, const char *, int);
1920
extern struct object *deref_tag_noverify(struct object *);
2021
extern int gpg_verify_tag(const struct object_id *oid,

tree.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "blob.h"
66
#include "commit.h"
77
#include "tag.h"
8+
#include "alloc.h"
89
#include "tree-walk.h"
910

1011
const char *tree_type = "tree";

0 commit comments

Comments
 (0)