Date: Wed, 28 Feb 2001 09:56:00 -0700 From: "Kenneth D. Merry" <ken@kdm.org> To: Dag-Erling Smorgrav <des@ofug.org> Cc: arch@FreeBSD.ORG Subject: Re: sbufs in userland Message-ID: <20010228095600.A41509@panzer.kdm.org> In-Reply-To: <xzp66hvyqo2.fsf@flood.ping.uio.no>; from des@ofug.org on Wed, Feb 28, 2001 at 12:48:13PM %2B0100 References: <20010226003319.A19994@panzer.kdm.org> <xzpelwlc2hm.fsf@flood.ping.uio.no> <20010226143035.A25402@panzer.kdm.org> <xzp66hvyqo2.fsf@flood.ping.uio.no>
next in thread | previous in thread | raw e-mail | index | archive | help
[-- Attachment #1 --]
On Wed, Feb 28, 2001 at 12:48:13 +0100, Dag-Erling Smorgrav wrote:
> "Kenneth D. Merry" <ken@kdm.org> writes:
> > It is using vnsprintf() instead of kvprintf. Diffs for that, and the
> > userland conversion are attached.
>
> Please send unified diffs.
Attached.
Ken
--
Kenneth Merry
ken@kdm.org
[-- Attachment #2 --]
==== //depot/FreeBSD-adaptec/src/sys/kern/subr_sbuf.c#1 - /a/ken/perforce/FreeBSD-adaptec/src/sys/kern/subr_sbuf.c ====
--- /tmp/tmp.81267.0 Wed Feb 28 09:54:42 2001
+++ /a/ken/perforce/FreeBSD-adaptec/src/sys/kern/subr_sbuf.c Wed Feb 14 15:33:36 2001
@@ -29,14 +29,20 @@
*/
#include <sys/param.h>
+#include <sys/sbuf.h>
+
+#ifdef _KERNEL
#include <sys/kernel.h>
#include <sys/malloc.h>
-#include <sys/sbuf.h>
#include <sys/systm.h>
-
#include <machine/stdarg.h>
+#else /* _KERNEL */
+#include <stdarg.h>
+#endif /* _KERNEL */
+#ifdef _KERNEL
MALLOC_DEFINE(M_SBUF, "sbuf", "string buffers");
+#endif /* _KERNEL */
/*
* Predicates
@@ -52,32 +58,38 @@
#define SBUF_SETFLAG(s, f) do { (s)->s_flags |= (f); } while (0)
#define SBUF_CLEARFLAG(s, f) do { (s)->s_flags &= ~(f); } while (0)
+#ifdef _KERNEL
+#define SBASSERT(e, m) KASSERT(e, m)
+#else /* _KERNEL */
+#define SBASSERT(e, m)
+#endif /* _KERNEL */
+
/*
* Debugging support
*/
-#ifdef INVARIANTS
+#if defined(_KERNEL) && defined(INVARIANTS)
static void
assert_sbuf_integrity(struct sbuf *s)
{
- KASSERT(s != NULL,
+ SBASSERT(s != NULL,
(__FUNCTION__ " called with a NULL sbuf pointer"));
- KASSERT(s->s_buf != NULL,
+ SBASSERT(s->s_buf != NULL,
(__FUNCTION__ " called with unitialized or corrupt sbuf"));
- KASSERT(s->s_len < s->s_size,
+ SBASSERT(s->s_len < s->s_size,
("wrote past end of sbuf (%d >= %d)", s->s_len, s->s_size));
}
static void
assert_sbuf_state(struct sbuf *s, int state)
{
- KASSERT((s->s_flags & SBUF_FINISHED) == state,
+ SBASSERT((s->s_flags & SBUF_FINISHED) == state,
(__FUNCTION__ " called with %sfinished or corrupt sbuf",
(state ? "un" : "")));
}
-#else
+#else /* _KERNEL && INVARIANTS */
#define assert_sbuf_integrity(s) do { } while (0)
#define assert_sbuf_state(s, i) do { } while (0)
-#endif
+#endif /* _KERNEL && INVARIANTS */
/*
* Initialize an sbuf.
@@ -87,11 +99,11 @@
int
sbuf_new(struct sbuf *s, char *buf, int length, int flags)
{
- KASSERT(length >= 0,
+ SBASSERT(length >= 0,
("attempt to create an sbuf of negative length (%d)", length));
- KASSERT(flags == 0,
+ SBASSERT(flags == 0,
(__FUNCTION__ " called with non-zero flags"));
- KASSERT(s != NULL,
+ SBASSERT(s != NULL,
(__FUNCTION__ " called with a NULL sbuf pointer"));
bzero(s, sizeof *s);
@@ -100,7 +112,11 @@
s->s_buf = buf;
return (0);
}
+#ifdef _KERNEL
s->s_buf = malloc(s->s_size, M_SBUF, M_WAITOK);
+#else /* _KERNEL */
+ s->s_buf = (char *)malloc(s->s_size);
+#endif /* _KERNEL */
if (s->s_buf == NULL)
return (-1);
SBUF_SETFLAG(s, SBUF_DYNAMIC);
@@ -130,9 +146,9 @@
assert_sbuf_integrity(s);
assert_sbuf_state(s, 0);
- KASSERT(pos >= 0,
+ SBASSERT(pos >= 0,
("attempt to seek to a negative position (%d)", pos));
- KASSERT(pos < s->s_size,
+ SBASSERT(pos < s->s_size,
("attempt to seek past end of sbuf (%d >= %d)", pos, s->s_size));
if (pos < 0 || pos > s->s_len)
@@ -175,6 +191,7 @@
return (sbuf_cat(s, str));
}
+#if 0
/*
* PCHAR function for sbuf_printf()
*/
@@ -193,6 +210,7 @@
else
SBUF_SETFLAG(s, SBUF_OVERFLOWED);
}
+#endif
/*
* Format the given arguments and append the resulting string to an sbuf.
@@ -206,17 +224,26 @@
assert_sbuf_integrity(s);
assert_sbuf_state(s, 0);
- KASSERT(fmt != NULL,
+ SBASSERT(fmt != NULL,
(__FUNCTION__ " called with a NULL format string"));
if (SBUF_HASOVERFLOWED(s))
return (-1);
+#if 0
va_start(ap, fmt);
len = kvprintf(fmt, _sbuf_pchar, s, 10, ap);
va_end(ap);
+#endif
+ va_start(ap, fmt);
+ len = vsnprintf(&s->s_buf[s->s_len], s->s_size - s->s_len, fmt, ap);
+ va_end(ap);
+
+ s->s_len += len;
+ if (!SBUF_HASROOM(s))
+ SBUF_SETFLAG(s, SBUF_OVERFLOWED);
- KASSERT(s->s_len < s->s_size,
+ SBASSERT(s->s_len < s->s_size,
("wrote past end of sbuf (%d >= %d)", s->s_len, s->s_size));
if (SBUF_HASOVERFLOWED(s))
@@ -303,6 +330,10 @@
/* don't care if it's finished or not */
if (SBUF_ISDYNAMIC(s))
+#ifdef _KERNEL
free(s->s_buf, M_SBUF);
+#else /* _KERNEL */
+ free(s->s_buf);
+#endif /* _KERNEL */
bzero(s, sizeof *s);
}
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20010228095600.A41509>
