Add sbuf_new_auto as a shortcut for the very common case of creating a

completely dynamic sbuf.

Obtained from:	Varnish
MFC after:	2 weeks
This commit is contained in:
des 2008-08-09 11:14:05 +00:00
parent 50ef01bba1
commit c2c1c946ae
18 changed files with 30 additions and 28 deletions

View File

@ -68,7 +68,7 @@ StartElement(void *userData, const char *name, const char **attr)
mt = userData;
mt->level++;
mt->sbuf[mt->level] = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
mt->sbuf[mt->level] = sbuf_new_auto();
id = NULL;
ref = NULL;
for (i = 0; attr[i] != NULL; i += 2) {

View File

@ -78,7 +78,7 @@ start_element(void *data, const char *name, const char **attr)
/* XXX force parser to abort */
return;
}
mt->sbuf[mt->level] = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
mt->sbuf[mt->level] = sbuf_new_auto();
id = ref = mode = NULL;
for (i = 0; attr[i] != NULL; i += 2) {
if (iseq(attr[i], "id")) {

View File

@ -565,7 +565,7 @@ acpi_asus_probe(device_t dev)
}
}
sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
sb = sbuf_new_auto();
if (sb == NULL)
return (ENOMEM);

View File

@ -142,7 +142,7 @@ led_write(struct cdev *dev, struct uio *uio, int ioflag)
return(error);
}
sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
sb = sbuf_new_auto();
if (sb == NULL) {
free(s2, M_DEVBUF);
return (ENOMEM);

View File

@ -748,7 +748,7 @@ g_concat_ctl_create(struct gctl_req *req, struct g_class *mp)
}
sc = gp->softc;
sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
sb = sbuf_new_auto();
sbuf_printf(sb, "Can't attach disk(s) to %s:", gp->name);
for (attached = 0, no = 1; no < *nargs; no++) {
snprintf(param, sizeof(param), "arg%u", no);

View File

@ -790,7 +790,7 @@ g_ccd_create(struct gctl_req *req, struct g_class *mp)
pp->sectorsize = sc->sc_secsize;
g_error_provider(pp, 0);
sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
sb = sbuf_new_auto();
sbuf_printf(sb, "ccd%d: %d components ", sc->sc_unit, *nprovider);
for (i = 0; i < *nprovider; i++) {
sbuf_printf(sb, "%s%s",
@ -840,7 +840,7 @@ g_ccd_list(struct gctl_req *req, struct g_class *mp)
up = gctl_get_paraml(req, "unit", sizeof (int));
unit = *up;
sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
sb = sbuf_new_auto();
LIST_FOREACH(gp, &mp->geom, geom) {
cs = gp->softc;
if (cs == NULL || (unit >= 0 && unit != cs->sc_unit))

View File

@ -464,7 +464,7 @@ g_ctl_ioctl_ctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct th
req = (void *)data;
req->nerror = 0;
req->serror = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
req->serror = sbuf_new_auto();
/* It is an error if we cannot return an error text */
if (req->lerror < 2)
return (EINVAL);

View File

@ -517,7 +517,7 @@ sysctl_disks(SYSCTL_HANDLER_ARGS)
int error;
struct sbuf *sb;
sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
sb = sbuf_new_auto();
g_waitfor_event(g_kern_disks, sb, M_WAITOK, NULL);
error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
sbuf_delete(sb);

View File

@ -180,7 +180,7 @@ sysctl_kern_geom_conftxt(SYSCTL_HANDLER_ARGS)
int error;
struct sbuf *sb;
sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
sb = sbuf_new_auto();
g_waitfor_event(g_conftxt, sb, M_WAITOK, NULL);
error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
sbuf_delete(sb);
@ -193,7 +193,7 @@ sysctl_kern_geom_confdot(SYSCTL_HANDLER_ARGS)
int error;
struct sbuf *sb;
sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
sb = sbuf_new_auto();
g_waitfor_event(g_confdot, sb, M_WAITOK, NULL);
error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
sbuf_delete(sb);
@ -206,7 +206,7 @@ sysctl_kern_geom_confxml(SYSCTL_HANDLER_ARGS)
int error;
struct sbuf *sb;
sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
sb = sbuf_new_auto();
g_waitfor_event(g_confxml, sb, M_WAITOK, NULL);
error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
sbuf_delete(sb);

View File

@ -385,7 +385,7 @@ g_slice_config(struct g_geom *gp, u_int idx, int how, off_t offset, off_t length
pp->mediasize = gsl->length;
return (0);
}
sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
sb = sbuf_new_auto();
va_start(ap, fmt);
sbuf_vprintf(sb, fmt, ap);
va_end(ap);

View File

@ -324,7 +324,7 @@ g_new_geomf(struct g_class *mp, const char *fmt, ...)
g_topology_assert();
G_VALID_CLASS(mp);
sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
sb = sbuf_new_auto();
va_start(ap, fmt);
sbuf_vprintf(sb, fmt, ap);
va_end(ap);
@ -566,7 +566,7 @@ g_new_providerf(struct g_geom *gp, const char *fmt, ...)
KASSERT(!(gp->flags & G_GEOM_WITHER),
("new provider on WITHERing geom(%s) (class %s)",
gp->name, gp->class->name));
sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
sb = sbuf_new_auto();
va_start(ap, fmt);
sbuf_vprintf(sb, fmt, ap);
va_end(ap);

View File

@ -506,7 +506,7 @@ g_part_ctl_add(struct gctl_req *req, struct g_part_parms *gpp)
/* Provide feedback if so requested. */
if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
sb = sbuf_new_auto();
sbuf_printf(sb, "%s%s added\n", gp->name,
G_PART_NAME(table, entry, buf, sizeof(buf)));
sbuf_finish(sb);
@ -545,7 +545,7 @@ g_part_ctl_bootcode(struct gctl_req *req, struct g_part_parms *gpp)
/* Provide feedback if so requested. */
if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
sb = sbuf_new_auto();
sbuf_printf(sb, "%s has bootcode\n", gp->name);
sbuf_finish(sb);
gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
@ -749,7 +749,7 @@ g_part_ctl_create(struct gctl_req *req, struct g_part_parms *gpp)
/* Provide feedback if so requested. */
if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
sb = sbuf_new_auto();
sbuf_printf(sb, "%s created\n", gp->name);
sbuf_finish(sb);
gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
@ -821,7 +821,7 @@ g_part_ctl_delete(struct gctl_req *req, struct g_part_parms *gpp)
/* Provide feedback if so requested. */
if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
sb = sbuf_new_auto();
sbuf_printf(sb, "%s%s deleted\n", gp->name,
G_PART_NAME(table, entry, buf, sizeof(buf)));
sbuf_finish(sb);
@ -877,7 +877,7 @@ g_part_ctl_destroy(struct gctl_req *req, struct g_part_parms *gpp)
/* Provide feedback if so requested. */
if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
sb = sbuf_new_auto();
sbuf_printf(sb, "%s destroyed\n", gp->name);
sbuf_finish(sb);
gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
@ -924,7 +924,7 @@ g_part_ctl_modify(struct gctl_req *req, struct g_part_parms *gpp)
/* Provide feedback if so requested. */
if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
sb = sbuf_new_auto();
sbuf_printf(sb, "%s%s modified\n", gp->name,
G_PART_NAME(table, entry, buf, sizeof(buf)));
sbuf_finish(sb);
@ -991,7 +991,7 @@ g_part_ctl_setunset(struct gctl_req *req, struct g_part_parms *gpp,
/* Provide feedback if so requested. */
if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
sb = sbuf_new_auto();
sbuf_printf(sb, "%s%s has %s %sset\n", gp->name,
G_PART_NAME(table, entry, buf, sizeof(buf)),
gpp->gpp_attrib, (set) ? "" : "un");

View File

@ -1069,7 +1069,7 @@ g_stripe_ctl_create(struct gctl_req *req, struct g_class *mp)
}
sc = gp->softc;
sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
sb = sbuf_new_auto();
sbuf_printf(sb, "Can't attach disk(s) to %s:", gp->name);
for (attached = 0, no = 1; no < *nargs; no++) {
snprintf(param, sizeof(param), "arg%u", no);

View File

@ -2179,7 +2179,7 @@ mount_argf(struct mntarg *ma, const char *name, const char *fmt, ...)
ma->v[ma->len].iov_len = strlen(name) + 1;
ma->len++;
sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
sb = sbuf_new_auto();
va_start(ap, fmt);
sbuf_vprintf(sb, fmt, ap);
va_end(ap);

View File

@ -310,7 +310,7 @@ rules_to_string(void)
int needcomma;
char *temp;
sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
sb = sbuf_new_auto();
needcomma = 0;
mtx_lock(&rule_mtx);
for (rule = TAILQ_FIRST(&rule_head); rule != NULL;

View File

@ -56,6 +56,8 @@ __BEGIN_DECLS
* API functions
*/
struct sbuf *sbuf_new(struct sbuf *, char *, int, int);
#define sbuf_new_auto() \
sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND)
void sbuf_clear(struct sbuf *);
int sbuf_setpos(struct sbuf *, int);
int sbuf_bcat(struct sbuf *, const void *, size_t);

View File

@ -82,9 +82,9 @@ new_node(void)
struct node *np;
np = calloc(1, sizeof *np);
np->cont = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
np->cont = sbuf_new_auto();
sbuf_clear(np->cont);
np->key = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
np->key = sbuf_new_auto();
sbuf_clear(np->key);
LIST_INIT(&np->children);
return (np);

View File

@ -208,7 +208,7 @@ g_simdisk_xml_load(const char *file)
int fd, i;
sc = calloc(1, sizeof *sc);
sc->sbuf = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
sc->sbuf = sbuf_new_auto();
LIST_INIT(&sc->sectors);
XML_SetUserData(parser, sc);
XML_SetElementHandler(parser, startElement, endElement);