{"thread":{"id":"12189","subject":"[PATCH 1/2] Use ALLOC_GROW in remote.{c,h}","startedAt":"2008-02-19T04:41:41Z","lastAt":"2008-02-19T04:41:41Z","messageCount":1,"participants":["Daniel Barkalow"],"isPatch":true,"patchVersion":1,"patchTotal":2},"messages":[{"id":"69232","messageId":"alpine.LNX.1.00.0802182316500.5816@iabervon.org","threadId":"12189","inReplyTo":null,"subject":"[PATCH 1/2] Use ALLOC_GROW in remote.{c,h}","fromName":"Daniel Barkalow","fromEmail":"barkalow@iabervon.org","sentAt":"2008-02-19T04:41:41Z","receivedAt":"2008-02-19T04:41:41Z","isPatch":true,"sender":{"key":"barkalow@iabervon.org","avatar":"https://avatars.githubusercontent.com/u/55364219?v=4"},"body":"Signed-off-by: Daniel Barkalow <barkalow@iabervon.org>\n---\nThe relevance of this patch, which is of general applicability, to this \nseries is that it cleans up the style of this file such that the next \npatch, which now uses the modern style, fits in.\n\n remote.c |  118 ++++++++++++++++++++++++--------------------------------------\n remote.h |    4 ++\n 2 files changed, 50 insertions(+), 72 deletions(-)\n\ndiff --git a/remote.c b/remote.c\nindex 6b56473..457d8a4 100644\n--- a/remote.c\n+++ b/remote.c\n@@ -3,10 +3,12 @@\n #include \"refs.h\"\n \n static struct remote **remotes;\n-static int allocated_remotes;\n+static int remotes_alloc;\n+static int remotes_nr;\n \n static struct branch **branches;\n-static int allocated_branches;\n+static int branches_alloc;\n+static int branches_nr;\n \n static struct branch *current_branch;\n static const char *default_remote_name;\n@@ -16,109 +18,81 @@ static char buffer[BUF_SIZE];\n \n static void add_push_refspec(struct remote *remote, const char *ref)\n {\n-\tint nr = remote->push_refspec_nr + 1;\n-\tremote->push_refspec =\n-\t\txrealloc(remote->push_refspec, nr * sizeof(char *));\n-\tremote->push_refspec[nr-1] = ref;\n-\tremote->push_refspec_nr = nr;\n+\tALLOC_GROW(remote->push_refspec,\n+\t\t   remote->push_refspec_nr + 1,\n+\t\t   remote->push_refspec_alloc);\n+\tremote->push_refspec[remote->push_refspec_nr++] = ref;\n }\n \n static void add_fetch_refspec(struct remote *remote, const char *ref)\n {\n-\tint nr = remote->fetch_refspec_nr + 1;\n-\tremote->fetch_refspec =\n-\t\txrealloc(remote->fetch_refspec, nr * sizeof(char *));\n-\tremote->fetch_refspec[nr-1] = ref;\n-\tremote->fetch_refspec_nr = nr;\n+\tALLOC_GROW(remote->fetch_refspec,\n+\t\t   remote->fetch_refspec_nr + 1,\n+\t\t   remote->fetch_refspec_alloc);\n+\tremote->fetch_refspec[remote->fetch_refspec_nr++] = ref;\n }\n \n static void add_url(struct remote *remote, const char *url)\n {\n-\tint nr = remote->url_nr + 1;\n-\tremote->url =\n-\t\txrealloc(remote->url, nr * sizeof(char *));\n-\tremote->url[nr-1] = url;\n-\tremote->url_nr = nr;\n+\tALLOC_GROW(remote->url, remote->url_nr + 1, remote->url_alloc);\n+\tremote->url[remote->url_nr++] = url;\n }\n \n static struct remote *make_remote(const char *name, int len)\n {\n-\tint i, empty = -1;\n+\tstruct remote *ret;\n+\tint i;\n \n-\tfor (i = 0; i < allocated_remotes; i++) {\n-\t\tif (!remotes[i]) {\n-\t\t\tif (empty < 0)\n-\t\t\t\tempty = i;\n-\t\t} else {\n-\t\t\tif (len ? (!strncmp(name, remotes[i]->name, len) &&\n-\t\t\t\t   !remotes[i]->name[len]) :\n-\t\t\t    !strcmp(name, remotes[i]->name))\n-\t\t\t\treturn remotes[i];\n-\t\t}\n+\tfor (i = 0; i < remotes_nr; i++) {\n+\t\tif (len ? (!strncmp(name, remotes[i]->name, len) &&\n+\t\t\t   !remotes[i]->name[len]) :\n+\t\t    !strcmp(name, remotes[i]->name))\n+\t\t\treturn remotes[i];\n \t}\n \n-\tif (empty < 0) {\n-\t\tempty = allocated_remotes;\n-\t\tallocated_remotes += allocated_remotes ? allocated_remotes : 1;\n-\t\tremotes = xrealloc(remotes,\n-\t\t\t\t   sizeof(*remotes) * allocated_remotes);\n-\t\tmemset(remotes + empty, 0,\n-\t\t       (allocated_remotes - empty) * sizeof(*remotes));\n-\t}\n-\tremotes[empty] = xcalloc(1, sizeof(struct remote));\n+\tret = xcalloc(1, sizeof(struct remote));\n+\tALLOC_GROW(remotes, remotes_nr + 1, remotes_alloc);\n+\tremotes[remotes_nr++] = ret;\n \tif (len)\n-\t\tremotes[empty]->name = xstrndup(name, len);\n+\t\tret->name = xstrndup(name, len);\n \telse\n-\t\tremotes[empty]->name = xstrdup(name);\n-\treturn remotes[empty];\n+\t\tret->name = xstrdup(name);\n+\treturn ret;\n }\n \n static void add_merge(struct branch *branch, const char *name)\n {\n-\tint nr = branch->merge_nr + 1;\n-\tbranch->merge_name =\n-\t\txrealloc(branch->merge_name, nr * sizeof(char *));\n-\tbranch->merge_name[nr-1] = name;\n-\tbranch->merge_nr = nr;\n+\tALLOC_GROW(branch->merge_name, branch->merge_nr + 1,\n+\t\t   branch->merge_alloc);\n+\tbranch->merge_name[branch->merge_nr++] = name;\n }\n \n static struct branch *make_branch(const char *name, int len)\n {\n-\tint i, empty = -1;\n+\tstruct branch *ret;\n+\tint i;\n \tchar *refname;\n \n-\tfor (i = 0; i < allocated_branches; i++) {\n-\t\tif (!branches[i]) {\n-\t\t\tif (empty < 0)\n-\t\t\t\tempty = i;\n-\t\t} else {\n-\t\t\tif (len ? (!strncmp(name, branches[i]->name, len) &&\n-\t\t\t\t   !branches[i]->name[len]) :\n-\t\t\t    !strcmp(name, branches[i]->name))\n-\t\t\t\treturn branches[i];\n-\t\t}\n+\tfor (i = 0; i < branches_nr; i++) {\n+\t\tif (len ? (!strncmp(name, branches[i]->name, len) &&\n+\t\t\t   !branches[i]->name[len]) :\n+\t\t    !strcmp(name, branches[i]->name))\n+\t\t\treturn branches[i];\n \t}\n \n-\tif (empty < 0) {\n-\t\tempty = allocated_branches;\n-\t\tallocated_branches += allocated_branches ? allocated_branches : 1;\n-\t\tbranches = xrealloc(branches,\n-\t\t\t\t   sizeof(*branches) * allocated_branches);\n-\t\tmemset(branches + empty, 0,\n-\t\t       (allocated_branches - empty) * sizeof(*branches));\n-\t}\n-\tbranches[empty] = xcalloc(1, sizeof(struct branch));\n+\tALLOC_GROW(branches, branches_nr + 1, branches_alloc);\n+\tret = xcalloc(1, sizeof(struct branch));\n+\tbranches[branches_nr++] = ret;\n \tif (len)\n-\t\tbranches[empty]->name = xstrndup(name, len);\n+\t\tret->name = xstrndup(name, len);\n \telse\n-\t\tbranches[empty]->name = xstrdup(name);\n+\t\tret->name = xstrdup(name);\n \trefname = malloc(strlen(name) + strlen(\"refs/heads/\") + 1);\n \tstrcpy(refname, \"refs/heads/\");\n-\tstrcpy(refname + strlen(\"refs/heads/\"),\n-\t       branches[empty]->name);\n-\tbranches[empty]->refname = refname;\n+\tstrcpy(refname + strlen(\"refs/heads/\"), ret->name);\n+\tret->refname = refname;\n \n-\treturn branches[empty];\n+\treturn ret;\n }\n \n static void read_remotes_file(struct remote *remote)\n@@ -380,7 +354,7 @@ int for_each_remote(each_remote_fn fn, void *priv)\n {\n \tint i, result = 0;\n \tread_config();\n-\tfor (i = 0; i < allocated_remotes && !result; i++) {\n+\tfor (i = 0; i < remotes_nr && !result; i++) {\n \t\tstruct remote *r = remotes[i];\n \t\tif (!r)\n \t\t\tcontinue;\ndiff --git a/remote.h b/remote.h\nindex 86e036d..0f6033f 100644\n--- a/remote.h\n+++ b/remote.h\n@@ -6,14 +6,17 @@ struct remote {\n \n \tconst char **url;\n \tint url_nr;\n+\tint url_alloc;\n \n \tconst char **push_refspec;\n \tstruct refspec *push;\n \tint push_refspec_nr;\n+\tint push_refspec_alloc;\n \n \tconst char **fetch_refspec;\n \tstruct refspec *fetch;\n \tint fetch_refspec_nr;\n+\tint fetch_refspec_alloc;\n \n \t/*\n \t * -1 to never fetch tags\n@@ -100,6 +103,7 @@ struct branch {\n \tconst char **merge_name;\n \tstruct refspec **merge;\n \tint merge_nr;\n+\tint merge_alloc;\n };\n \n struct branch *branch_get(const char *name);\n-- \n1.5.4.1.191.gfbf10\n"}]}