Allocate string vector in chunks, instead of calling realloc()

every time something is added.
This commit is contained in:
dsl 2009-01-16 21:15:34 +00:00
parent 74a68f7259
commit d020c6ab24
2 changed files with 20 additions and 13 deletions

View File

@ -1,7 +1,7 @@
/* $NetBSD: strlist.c,v 1.2 2009/01/10 16:55:39 dsl Exp $ */
/* $NetBSD: strlist.c,v 1.3 2009/01/16 21:15:34 dsl Exp $ */
/*-
* Copyright (c) 2006 The NetBSD Foundation, Inc.
* Copyright (c) 2008 - 2009 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
@ -33,11 +33,11 @@
*/
#ifndef MAKE_NATIVE
static char rcsid[] = "$NetBSD: strlist.c,v 1.2 2009/01/10 16:55:39 dsl Exp $";
static char rcsid[] = "$NetBSD: strlist.c,v 1.3 2009/01/16 21:15:34 dsl Exp $";
#else
#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: strlist.c,v 1.2 2009/01/10 16:55:39 dsl Exp $");
__RCSID("$NetBSD: strlist.c,v 1.3 2009/01/16 21:15:34 dsl Exp $");
#endif /* not lint */
#endif
@ -50,6 +50,7 @@ void
strlist_init(strlist_t *sl)
{
sl->sl_num = 0;
sl->sl_max = 0;
sl->sl_items = NULL;
}
@ -64,6 +65,7 @@ strlist_clean(strlist_t *sl)
free(sl->sl_items);
sl->sl_num = 0;
sl->sl_max = 0;
sl->sl_items = NULL;
}
@ -71,17 +73,21 @@ void
strlist_add_str(strlist_t *sl, char *str, unsigned int info)
{
unsigned int n;
strlist_item_t *new_items;
strlist_item_t *items;
if (str == NULL)
return;
n = sl->sl_num + 1;
sl->sl_num = n;
new_items = bmake_realloc(sl->sl_items, (n + 1) * sizeof *sl->sl_items);
sl->sl_items = new_items;
new_items += n - 1;
new_items->si_str = str;
new_items->si_info = info;
new_items[1].si_str = NULL; /* STRLIST_FOREACH() terminator */
items = sl->sl_items;
if (n >= sl->sl_max) {
items = bmake_realloc(items, (n + 7) * sizeof *sl->sl_items);
sl->sl_items = items;
sl->sl_max = n + 6;
}
items += n - 1;
items->si_str = str;
items->si_info = info;
items[1].si_str = NULL; /* STRLIST_FOREACH() terminator */
}

View File

@ -1,7 +1,7 @@
/* $NetBSD: strlist.h,v 1.2 2009/01/10 16:55:39 dsl Exp $ */
/* $NetBSD: strlist.h,v 1.3 2009/01/16 21:15:34 dsl Exp $ */
/*-
* Copyright (c) 2006 The NetBSD Foundation, Inc.
* Copyright (c) 2008 - 2009 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
@ -42,6 +42,7 @@ typedef struct {
typedef struct {
unsigned int sl_num;
unsigned int sl_max;
strlist_item_t *sl_items;
} strlist_t;