From owner-freebsd-arch Sun Feb 25 23:49:55 2001 Delivered-To: freebsd-arch@freebsd.org Received: from earth.backplane.com (earth-nat-cw.backplane.com [208.161.114.67]) by hub.freebsd.org (Postfix) with ESMTP id 2BAEE37B4EC for ; Sun, 25 Feb 2001 23:49:52 -0800 (PST) (envelope-from dillon@earth.backplane.com) Received: (from dillon@localhost) by earth.backplane.com (8.11.2/8.9.3) id f1Q7nGZ30306; Sun, 25 Feb 2001 23:49:16 -0800 (PST) (envelope-from dillon) Date: Sun, 25 Feb 2001 23:49:16 -0800 (PST) From: Matt Dillon Message-Id: <200102260749.f1Q7nGZ30306@earth.backplane.com> To: "Kenneth D. Merry" Cc: arch@FreeBSD.ORG Subject: Re: sbufs in userland References: <20010226003319.A19994@panzer.kdm.org> Sender: owner-freebsd-arch@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG :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