Diff options
authorBryce Kwon <bryce@brycekwon.com>
committerBryce Kwon <bryce@brycekwon.com>
commit
parent
tree
download
Grow the repository and ref lists with ALLOC_GROW
Diffstat (limited to 'source/shared.c')
-rw-r--r--source/shared.c30
1 file changed, 6 insertions, 24 deletions
diff --git a/source/shared.c b/source/shared.c
index 2add244..50ad00b 100644
--- a/source/shared.c
+++ b/source/shared.c
@@ -184,16 +184,8 @@ struct cgit_repo *cgit_add_repo(const char *url)
{
struct cgit_repo *repo;
- if (++cgit_repolist.count > cgit_repolist.length) {
- if (cgit_repolist.length == 0)
- cgit_repolist.length = 8;
- else
- cgit_repolist.length *= 2;
- cgit_repolist.repos =
- xrealloc(cgit_repolist.repos, cgit_repolist.length * sizeof(struct cgit_repo));
- }
-
- repo = &cgit_repolist.repos[cgit_repolist.count - 1];
+ ALLOC_GROW(cgit_repolist.repos, cgit_repolist.count + 1, cgit_repolist.length);
+ repo = &cgit_repolist.repos[cgit_repolist.count++];
memset(repo, 0, sizeof(struct cgit_repo));
repo->url = cgit_trim_end(url, '/');
if (repo->url) {
@@ -300,18 +292,6 @@ void strbuf_ensure_end(struct strbuf *sb, char c)
strbuf_addch(sb, c);
}
-static void add_ref(struct reflist *list, struct refinfo *ref)
-{
- size_t size;
-
- if (list->count >= list->alloc) {
- list->alloc += (list->alloc ? list->alloc : 4);
- size = list->alloc * sizeof(struct refinfo *);
- list->refs = xrealloc(list->refs, size);
- }
- list->refs[list->count++] = ref;
-}
-
void cgit_free_taginfo(struct taginfo *info)
{
// NULL is accepted because cgit_parse_tag returns it for a tag object
@@ -339,8 +319,10 @@ int cgit_refs_cb(const struct reference *ref, void *cb_data)
struct reflist *list = (struct reflist *)cb_data;
struct refinfo *info = make_refinfo(ref->name, ref->oid);
- if (info)
- add_ref(list, info);
+ if (info) {
+ ALLOC_GROW(list->refs, list->count + 1, list->alloc);
+ list->refs[list->count++] = info;
+ }
return 0;
}