@matheusbsbs / matheus-bagliano / commits / 2d31347ba5
Fork 0

Use ALLOC_GROW in remote.{c,h}

Signed-off-by: Daniel Barkalow <barkalow@iabervon.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Daniel Barkalow committed Feb 18, 2008 at 23:41 UTC 2d31347ba5c56d43d64dfdfe04a924178ee55b75
2 files changed +50 -72
remote.c
+46 -72
@@ -3,10 +3,12 @@
3 #include "refs.h"
4
5 static struct remote **remotes;
6 -static int allocated_remotes;
6 +static int remotes_alloc;
7 +static int remotes_nr;
8
9 static struct branch **branches;
9 -static int allocated_branches;
10 +static int branches_alloc;
11 +static int branches_nr;
12
13 static struct branch *current_branch;
14 static const char *default_remote_name;
@@ -16,109 +18,81 @@ static char buffer[BUF_SIZE];
18
19 static void add_push_refspec(struct remote *remote, const char *ref)
20 {
19 - int nr = remote->push_refspec_nr + 1;
20 - remote->push_refspec =
21 - xrealloc(remote->push_refspec, nr * sizeof(char *));
22 - remote->push_refspec[nr-1] = ref;
23 - remote->push_refspec_nr = nr;
21 + ALLOC_GROW(remote->push_refspec,
22 + remote->push_refspec_nr + 1,
23 + remote->push_refspec_alloc);
24 + remote->push_refspec[remote->push_refspec_nr++] = ref;
25 }
26
27 static void add_fetch_refspec(struct remote *remote, const char *ref)
28 {
28 - int nr = remote->fetch_refspec_nr + 1;
29 - remote->fetch_refspec =
30 - xrealloc(remote->fetch_refspec, nr * sizeof(char *));
31 - remote->fetch_refspec[nr-1] = ref;
32 - remote->fetch_refspec_nr = nr;
29 + ALLOC_GROW(remote->fetch_refspec,
30 + remote->fetch_refspec_nr + 1,
31 + remote->fetch_refspec_alloc);
32 + remote->fetch_refspec[remote->fetch_refspec_nr++] = ref;
33 }
34
35 static void add_url(struct remote *remote, const char *url)
36 {
37 - int nr = remote->url_nr + 1;
38 - remote->url =
39 - xrealloc(remote->url, nr * sizeof(char *));
40 - remote->url[nr-1] = url;
41 - remote->url_nr = nr;
37 + ALLOC_GROW(remote->url, remote->url_nr + 1, remote->url_alloc);
38 + remote->url[remote->url_nr++] = url;
39 }
40
41 static struct remote *make_remote(const char *name, int len)
42 {
46 - int i, empty = -1;
43 + struct remote *ret;
44 + int i;
45
48 - for (i = 0; i < allocated_remotes; i++) {
49 - if (!remotes[i]) {
50 - if (empty < 0)
51 - empty = i;
52 - } else {
53 - if (len ? (!strncmp(name, remotes[i]->name, len) &&
54 - !remotes[i]->name[len]) :
55 - !strcmp(name, remotes[i]->name))
56 - return remotes[i];
57 - }
46 + for (i = 0; i < remotes_nr; i++) {
47 + if (len ? (!strncmp(name, remotes[i]->name, len) &&
48 + !remotes[i]->name[len]) :
49 + !strcmp(name, remotes[i]->name))
50 + return remotes[i];
51 }
52
60 - if (empty < 0) {
61 - empty = allocated_remotes;
62 - allocated_remotes += allocated_remotes ? allocated_remotes : 1;
63 - remotes = xrealloc(remotes,
64 - sizeof(*remotes) * allocated_remotes);
65 - memset(remotes + empty, 0,
66 - (allocated_remotes - empty) * sizeof(*remotes));
67 - }
68 - remotes[empty] = xcalloc(1, sizeof(struct remote));
53 + ret = xcalloc(1, sizeof(struct remote));
54 + ALLOC_GROW(remotes, remotes_nr + 1, remotes_alloc);
55 + remotes[remotes_nr++] = ret;
56 if (len)
70 - remotes[empty]->name = xstrndup(name, len);
57 + ret->name = xstrndup(name, len);
58 else
72 - remotes[empty]->name = xstrdup(name);
73 - return remotes[empty];
59 + ret->name = xstrdup(name);
60 + return ret;
61 }
62
63 static void add_merge(struct branch *branch, const char *name)
64 {
78 - int nr = branch->merge_nr + 1;
79 - branch->merge_name =
80 - xrealloc(branch->merge_name, nr * sizeof(char *));
81 - branch->merge_name[nr-1] = name;
82 - branch->merge_nr = nr;
65 + ALLOC_GROW(branch->merge_name, branch->merge_nr + 1,
66 + branch->merge_alloc);
67 + branch->merge_name[branch->merge_nr++] = name;
68 }
69
70 static struct branch *make_branch(const char *name, int len)
71 {
87 - int i, empty = -1;
72 + struct branch *ret;
73 + int i;
74 char *refname;
75
90 - for (i = 0; i < allocated_branches; i++) {
91 - if (!branches[i]) {
92 - if (empty < 0)
93 - empty = i;
94 - } else {
95 - if (len ? (!strncmp(name, branches[i]->name, len) &&
96 - !branches[i]->name[len]) :
97 - !strcmp(name, branches[i]->name))
98 - return branches[i];
99 - }
76 + for (i = 0; i < branches_nr; i++) {
77 + if (len ? (!strncmp(name, branches[i]->name, len) &&
78 + !branches[i]->name[len]) :
79 + !strcmp(name, branches[i]->name))
80 + return branches[i];
81 }
82
102 - if (empty < 0) {
103 - empty = allocated_branches;
104 - allocated_branches += allocated_branches ? allocated_branches : 1;
105 - branches = xrealloc(branches,
106 - sizeof(*branches) * allocated_branches);
107 - memset(branches + empty, 0,
108 - (allocated_branches - empty) * sizeof(*branches));
109 - }
110 - branches[empty] = xcalloc(1, sizeof(struct branch));
83 + ALLOC_GROW(branches, branches_nr + 1, branches_alloc);
84 + ret = xcalloc(1, sizeof(struct branch));
85 + branches[branches_nr++] = ret;
86 if (len)
112 - branches[empty]->name = xstrndup(name, len);
87 + ret->name = xstrndup(name, len);
88 else
114 - branches[empty]->name = xstrdup(name);
89 + ret->name = xstrdup(name);
90 refname = malloc(strlen(name) + strlen("refs/heads/") + 1);
91 strcpy(refname, "refs/heads/");
117 - strcpy(refname + strlen("refs/heads/"),
118 - branches[empty]->name);
119 - branches[empty]->refname = refname;
92 + strcpy(refname + strlen("refs/heads/"), ret->name);
93 + ret->refname = refname;
94
121 - return branches[empty];
95 + return ret;
96 }
97
98 static void read_remotes_file(struct remote *remote)
@@ -380,7 +354,7 @@ int for_each_remote(each_remote_fn fn, void *priv)
354 {
355 int i, result = 0;
356 read_config();
383 - for (i = 0; i < allocated_remotes && !result; i++) {
357 + for (i = 0; i < remotes_nr && !result; i++) {
358 struct remote *r = remotes[i];
359 if (!r)
360 continue;
remote.h
+4
@@ -6,14 +6,17 @@ struct remote {
6
7 const char **url;
8 int url_nr;
9 + int url_alloc;
10
11 const char **push_refspec;
12 struct refspec *push;
13 int push_refspec_nr;
14 + int push_refspec_alloc;
15
16 const char **fetch_refspec;
17 struct refspec *fetch;
18 int fetch_refspec_nr;
19 + int fetch_refspec_alloc;
20
21 /*
22 * -1 to never fetch tags
@@ -100,6 +103,7 @@ struct branch {
103 const char **merge_name;
104 struct refspec **merge;
105 int merge_nr;
106 + int merge_alloc;
107 };
108
109 struct branch *branch_get(const char *name);