{"thread":{"id":"8954","subject":"[PATCH 1/2] Add allocation and freeing functions for struct refs","startedAt":"2007-07-10T04:47:23Z","lastAt":"2007-07-10T04:47:23Z","messageCount":1,"participants":["Daniel Barkalow"],"isPatch":true,"patchVersion":1,"patchTotal":2},"messages":[{"id":"46912","messageId":"Pine.LNX.4.64.0707100042150.6977@iabervon.org","threadId":"8954","inReplyTo":null,"subject":"[PATCH 1/2] Add allocation and freeing functions for struct refs","fromName":"Daniel Barkalow","fromEmail":"barkalow@iabervon.org","sentAt":"2007-07-10T04:47:23Z","receivedAt":"2007-07-10T04:47:23Z","isPatch":true,"sender":{"key":"barkalow@iabervon.org","avatar":"https://avatars.githubusercontent.com/u/55364219?v=4"},"body":"Instead of open-coding allocation wherever it happens, have a function. \nAlso, add a function to free a list of refs, which we currently never \nactually do.\n\nSigned-off-by: Daniel Barkalow <barkalow@iabervon.org>\n---\n connect.c |    2 +-\n remote.c  |   25 ++++++++++++++++++++++---\n remote.h  |    7 +++++++\n 3 files changed, 30 insertions(+), 4 deletions(-)\n\ndiff --git a/connect.c b/connect.c\nindex 65e79ed..715cdc0 100644\n--- a/connect.c\n+++ b/connect.c\n@@ -72,7 +72,7 @@ struct ref **get_remote_heads(int in, struct ref **list,\n \t\t\tcontinue;\n \t\tif (nr_match && !path_match(name, nr_match, match))\n \t\t\tcontinue;\n-\t\tref = xcalloc(1, sizeof(*ref) + len - 40);\n+\t\tref = alloc_ref(len - 40);\n \t\thashcpy(ref->old_sha1, old_sha1);\n \t\tmemcpy(ref->name, buffer + 41, len - 40);\n \t\t*list = ref;\ndiff --git a/remote.c b/remote.c\nindex 500ca4d..ed39764 100644\n--- a/remote.c\n+++ b/remote.c\n@@ -320,6 +320,25 @@ int remote_find_tracking(struct remote *remote, struct refspec *refspec)\n \treturn -1;\n }\n \n+struct ref *alloc_ref(unsigned namelen)\n+{\n+\tstruct ref *ret = xmalloc(sizeof(struct ref) + namelen);\n+\tmemset(ret, 0, sizeof(struct ref) + namelen);\n+\treturn ret;\n+}\n+\n+void free_refs(struct ref *ref)\n+{\n+\tstruct ref *next;\n+\twhile (ref) {\n+\t\tnext = ref->next;\n+\t\tif (ref->peer_ref)\n+\t\t\tfree(ref->peer_ref);\n+\t\tfree(ref);\n+\t\tref = next;\n+\t}\n+}\n+\n static int count_refspec_match(const char *pattern,\n \t\t\t       struct ref *refs,\n \t\t\t       struct ref **matched_ref)\n@@ -391,7 +410,7 @@ static struct ref *try_explicit_object_name(const char *name)\n \tint len;\n \n \tif (!*name) {\n-\t\tref = xcalloc(1, sizeof(*ref) + 20);\n+\t\tref = alloc_ref(20);\n \t\tstrcpy(ref->name, \"(delete)\");\n \t\thashclr(ref->new_sha1);\n \t\treturn ref;\n@@ -399,7 +418,7 @@ static struct ref *try_explicit_object_name(const char *name)\n \tif (get_sha1(name, sha1))\n \t\treturn NULL;\n \tlen = strlen(name) + 1;\n-\tref = xcalloc(1, sizeof(*ref) + len);\n+\tref = alloc_ref(len);\n \tmemcpy(ref->name, name, len);\n \thashcpy(ref->new_sha1, sha1);\n \treturn ref;\n@@ -411,7 +430,7 @@ static struct ref *make_dst(const char *name, struct ref ***dst_tail)\n \tsize_t len;\n \n \tlen = strlen(name) + 1;\n-\tdst = xcalloc(1, sizeof(*dst) + len);\n+\tdst = alloc_ref(len);\n \tmemcpy(dst->name, name, len);\n \tlink_dst_tail(dst, dst_tail);\n \treturn dst;\ndiff --git a/remote.h b/remote.h\nindex 01dbcef..080b7da 100644\n--- a/remote.h\n+++ b/remote.h\n@@ -30,6 +30,13 @@ struct refspec {\n \tchar *dst;\n };\n \n+struct ref *alloc_ref(unsigned namelen);\n+\n+/*\n+ * Frees the entire list and peers of elements.\n+ */\n+void free_refs(struct ref *ref);\n+\n int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail,\n \t       int nr_refspec, char **refspec, int all);\n \n-- \n1.5.2.2.1399.g097d5-dirty\n"}]}