Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 25 Feb 2001 23:49:16 -0800 (PST)
From:      Matt Dillon <dillon@earth.backplane.com>
To:        "Kenneth D. Merry" <ken@kdm.org>
Cc:        arch@FreeBSD.ORG
Subject:   Re: sbufs in userland
Message-ID:  <200102260749.f1Q7nGZ30306@earth.backplane.com>
References:   <20010226003319.A19994@panzer.kdm.org>

next in thread | previous in thread | raw e-mail | index | archive | help
:As part of a re-write of the CAM error recovery code that Justin and I have
:been working on, I have been re-doing the CAM error printing code.
:
:One of the things I'm working on is making the error printing code in the
:kernel and in userland as similar as possible.
:
:I would like to use the sbuf(9) interface to do the string formatting,
:since it is fairly superior to my current string formatting method (see
:scsi_sense_string() in sys/cam/scsi/scsi_all.c).

    Well, I'm on record as really hating the sbuf concept.

    For userland, why not simply use asprintf()?   I use asprintf() for 
    just about everything, though I give it a nifty little wrapper.

char *
safe_replacef(char **pptr, const char *ctl, ...)
{
    va_list va;
    char *optr = *pptr;
        
    if (ctl) {
        va_start(va, ctl);
        if (vasprintf(pptr, ctl, va) < 0)
            fatalmem();
        va_end(va);
    } 
    safe_free(&optr);
    return(*pptr);
}

void
safe_free(char **ptr)
{
    if (*ptr) {
	free(*ptr);
	*ptr = NULL;
    }
}

    So you can do this:

	char *tmpstr = NULL;	/* preinitialization to NULL necessary */

	safe_replacef(&tmpstr, "bah bah %s", "bah");
	use tmpstr locally
	...
	safe_replacef(&tmpstr, "black black %s", "black");
	use tmpstr locally
	..
	for ( ...) {
	    safe_replacef(&tmpstr, "sheep sheep %s", "sheep");
	    use tmpstr locally
	    ...
	}
	...
	safe_free(&tmpstr);

    In anycase, I'd just use an appropriately wrapped asprintf() for
    userland.  I used to be paranoid about malloc()'ing and free()ing
    temporary strings for performance reasons but I got over it, and it
    turns out not to be a performance issue.  Now I use a safe_replacef()
    or a safe_asprintf() type of wrapper almost exclusively for just
    about all my string formatting.

						-Matt


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-arch" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200102260749.f1Q7nGZ30306>