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>
Daniel Barkalow committed
Jul 10, 2007 at 00:47 UTC
dfd255dd1a7c04ad24891db50e5d80d96f93fd4a
3 files changed
+30
-4
connect.c
+1
-1
@@ -72,7 +72,7 @@ struct ref **get_remote_heads(int in, struct ref **list,
72
continue;
73
if (nr_match && !path_match(name, nr_match, match))
74
continue;
75
- ref = xcalloc(1, sizeof(*ref) + len - 40);
75
+ ref = alloc_ref(len - 40);
76
hashcpy(ref->old_sha1, old_sha1);
77
memcpy(ref->name, buffer + 41, len - 40);
78
*list = ref;
remote.c
+22
-3
@@ -320,6 +320,25 @@ int remote_find_tracking(struct remote *remote, struct refspec *refspec)
320
return -1;
321
}
322
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
+
342
static int count_refspec_match(const char *pattern,
343
struct ref *refs,
344
struct ref **matched_ref)
@@ -391,7 +410,7 @@ static struct ref *try_explicit_object_name(const char *name)
410
int len;
411
412
if (!*name) {
394
- ref = xcalloc(1, sizeof(*ref) + 20);
413
+ ref = alloc_ref(20);
414
strcpy(ref->name, "(delete)");
415
hashclr(ref->new_sha1);
416
return ref;
@@ -399,7 +418,7 @@ static struct ref *try_explicit_object_name(const char *name)
418
if (get_sha1(name, sha1))
419
return NULL;
420
len = strlen(name) + 1;
402
- ref = xcalloc(1, sizeof(*ref) + len);
421
+ ref = alloc_ref(len);
422
memcpy(ref->name, name, len);
423
hashcpy(ref->new_sha1, sha1);
424
return ref;
@@ -411,7 +430,7 @@ static struct ref *make_dst(const char *name, struct ref ***dst_tail)
430
size_t len;
431
432
len = strlen(name) + 1;
414
- dst = xcalloc(1, sizeof(*dst) + len);
433
+ dst = alloc_ref(len);
434
memcpy(dst->name, name, len);
435
link_dst_tail(dst, dst_tail);
436
return dst;
remote.h
+7
@@ -30,6 +30,13 @@ struct refspec {
30
char *dst;
31
};
32
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
+
40
int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail,
41
int nr_refspec, char **refspec, int all);
42