Skip to content

Commit dfd255d

Browse files
iabervongitster
authored andcommitted
Add allocation and freeing functions for struct refs
Instead of open-coding allocation wherever it happens, have a function. Also, add a function to free a list of refs, which we currently never actually do. Signed-off-by: Daniel Barkalow <barkalow@iabervon.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 54dadbd commit dfd255d

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

connect.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ struct ref **get_remote_heads(int in, struct ref **list,
7272
continue;
7373
if (nr_match && !path_match(name, nr_match, match))
7474
continue;
75-
ref = xcalloc(1, sizeof(*ref) + len - 40);
75+
ref = alloc_ref(len - 40);
7676
hashcpy(ref->old_sha1, old_sha1);
7777
memcpy(ref->name, buffer + 41, len - 40);
7878
*list = ref;

remote.c

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,25 @@ int remote_find_tracking(struct remote *remote, struct refspec *refspec)
320320
return -1;
321321
}
322322

323+
struct ref *alloc_ref(unsigned namelen)
324+
{
325+
struct ref *ret = xmalloc(sizeof(struct ref) + namelen);
326+
memset(ret, 0, sizeof(struct ref) + namelen);
327+
return ret;
328+
}
329+
330+
void free_refs(struct ref *ref)
331+
{
332+
struct ref *next;
333+
while (ref) {
334+
next = ref->next;
335+
if (ref->peer_ref)
336+
free(ref->peer_ref);
337+
free(ref);
338+
ref = next;
339+
}
340+
}
341+
323342
static int count_refspec_match(const char *pattern,
324343
struct ref *refs,
325344
struct ref **matched_ref)
@@ -391,15 +410,15 @@ static struct ref *try_explicit_object_name(const char *name)
391410
int len;
392411

393412
if (!*name) {
394-
ref = xcalloc(1, sizeof(*ref) + 20);
413+
ref = alloc_ref(20);
395414
strcpy(ref->name, "(delete)");
396415
hashclr(ref->new_sha1);
397416
return ref;
398417
}
399418
if (get_sha1(name, sha1))
400419
return NULL;
401420
len = strlen(name) + 1;
402-
ref = xcalloc(1, sizeof(*ref) + len);
421+
ref = alloc_ref(len);
403422
memcpy(ref->name, name, len);
404423
hashcpy(ref->new_sha1, sha1);
405424
return ref;
@@ -411,7 +430,7 @@ static struct ref *make_dst(const char *name, struct ref ***dst_tail)
411430
size_t len;
412431

413432
len = strlen(name) + 1;
414-
dst = xcalloc(1, sizeof(*dst) + len);
433+
dst = alloc_ref(len);
415434
memcpy(dst->name, name, len);
416435
link_dst_tail(dst, dst_tail);
417436
return dst;

remote.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ struct refspec {
3030
char *dst;
3131
};
3232

33+
struct ref *alloc_ref(unsigned namelen);
34+
35+
/*
36+
* Frees the entire list and peers of elements.
37+
*/
38+
void free_refs(struct ref *ref);
39+
3340
int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail,
3441
int nr_refspec, char **refspec, int all);
3542

0 commit comments

Comments
 (0)