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. * All rights reserved.
* *
* This code is derived from software contributed to The NetBSD Foundation * This code is derived from software contributed to The NetBSD Foundation
@ -33,11 +33,11 @@
*/ */
#ifndef MAKE_NATIVE #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 #else
#include <sys/cdefs.h> #include <sys/cdefs.h>
#ifndef lint #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 /* not lint */
#endif #endif
@ -50,6 +50,7 @@ void
strlist_init(strlist_t *sl) strlist_init(strlist_t *sl)
{ {
sl->sl_num = 0; sl->sl_num = 0;
sl->sl_max = 0;
sl->sl_items = NULL; sl->sl_items = NULL;
} }
@ -64,6 +65,7 @@ strlist_clean(strlist_t *sl)
free(sl->sl_items); free(sl->sl_items);
sl->sl_num = 0; sl->sl_num = 0;
sl->sl_max = 0;
sl->sl_items = NULL; sl->sl_items = NULL;
} }
@ -71,17 +73,21 @@ void
strlist_add_str(strlist_t *sl, char *str, unsigned int info) strlist_add_str(strlist_t *sl, char *str, unsigned int info)
{ {
unsigned int n; unsigned int n;
strlist_item_t *new_items; strlist_item_t *items;
if (str == NULL) if (str == NULL)
return; return;
n = sl->sl_num + 1; n = sl->sl_num + 1;
sl->sl_num = n; sl->sl_num = n;
new_items = bmake_realloc(sl->sl_items, (n + 1) * sizeof *sl->sl_items); items = sl->sl_items;
sl->sl_items = new_items; if (n >= sl->sl_max) {
new_items += n - 1; items = bmake_realloc(items, (n + 7) * sizeof *sl->sl_items);
new_items->si_str = str; sl->sl_items = items;
new_items->si_info = info; sl->sl_max = n + 6;
new_items[1].si_str = NULL; /* STRLIST_FOREACH() terminator */ }
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. * All rights reserved.
* *
* This code is derived from software contributed to The NetBSD Foundation * This code is derived from software contributed to The NetBSD Foundation
@ -42,6 +42,7 @@ typedef struct {
typedef struct { typedef struct {
unsigned int sl_num; unsigned int sl_num;
unsigned int sl_max;
strlist_item_t *sl_items; strlist_item_t *sl_items;
} strlist_t; } strlist_t;