diff options
author | des <des@FreeBSD.org> | 2001-06-10 15:48:04 +0000 |
---|---|---|
committer | des <des@FreeBSD.org> | 2001-06-10 15:48:04 +0000 |
commit | 23c38e4e7c8ac5370802f5158ea3085e8d58081a (patch) | |
tree | bf847b257ccd40d68502d78a22a262be3f3bcdd9 /sys/kern | |
parent | add2afe54c2e0bce5ea9cd2a11cabc257bd6cbb3 (diff) | |
download | FreeBSD-src-23c38e4e7c8ac5370802f5158ea3085e8d58081a.zip FreeBSD-src-23c38e4e7c8ac5370802f5158ea3085e8d58081a.tar.gz |
sbuf_new(9) now returns a struct sbuf * instead of an int. If the caller
does not provide a struct sbuf, sbuf_new(9) will allocate one and return
a pointer to it.
Diffstat (limited to 'sys/kern')
-rw-r--r-- | sys/kern/subr_sbuf.c | 28 |
1 files changed, 20 insertions, 8 deletions
diff --git a/sys/kern/subr_sbuf.c b/sys/kern/subr_sbuf.c index 11fc64f..6de352d 100644 --- a/sys/kern/subr_sbuf.c +++ b/sys/kern/subr_sbuf.c @@ -55,6 +55,7 @@ MALLOC_DEFINE(M_SBUF, "sbuf", "string buffers"); * Predicates */ #define SBUF_ISDYNAMIC(s) ((s)->s_flags & SBUF_DYNAMIC) +#define SBUF_ISDYNSTRUCT(s) ((s)->s_flags & SBUF_DYNSTRUCT) #define SBUF_ISFINISHED(s) ((s)->s_flags & SBUF_FINISHED) #define SBUF_HASOVERFLOWED(s) ((s)->s_flags & SBUF_OVERFLOWED) #define SBUF_HASROOM(s) ((s)->s_len < (s)->s_size - 1) @@ -99,27 +100,36 @@ _assert_sbuf_state(char *fun, struct sbuf *s, int state) * If buf is non-NULL, it points to a static or already-allocated string * big enough to hold at least length characters. */ -int +struct sbuf * sbuf_new(struct sbuf *s, char *buf, int length, int flags) { KASSERT(length >= 0, ("attempt to create an sbuf of negative length (%d)", length)); KASSERT(flags == 0, (__FUNCTION__ " called with non-zero flags")); - KASSERT(s != NULL, - (__FUNCTION__ " called with a NULL sbuf pointer")); - bzero(s, sizeof *s); + if (s == NULL) { + s = (struct sbuf *)SBMALLOC(sizeof *s); + if (s == NULL) + return (NULL); + bzero(s, sizeof *s); + SBUF_SETFLAG(s, SBUF_DYNSTRUCT); + } else { + bzero(s, sizeof *s); + } s->s_size = length; if (buf) { s->s_buf = buf; - return (0); + return (s); } s->s_buf = (char *)SBMALLOC(s->s_size); - if (s->s_buf == NULL) - return (-1); + if (s->s_buf == NULL) { + if (SBUF_ISDYNSTRUCT(s)) + SBFREE(s); + return (NULL); + } SBUF_SETFLAG(s, SBUF_DYNAMIC); - return (0); + return (s); } /* @@ -315,4 +325,6 @@ sbuf_delete(struct sbuf *s) if (SBUF_ISDYNAMIC(s)) SBFREE(s->s_buf); bzero(s, sizeof *s); + if (SBUF_ISDYNSTRUCT(s)) + SBFREE(s); } |