#ifdef KERNEL
/* MALLOC_DEFINE(M_SBUF, "sbuf", "string buffers"); */
-#define SBMALLOC(size) _MALLOC(size, M_SBUF, M_WAITOK)
-#define SBFREE(buf) FREE(buf, M_SBUF)
+#define SBMALLOC(size) _MALLOC(size, M_SBUF, M_WAITOK)
+#define SBFREE(buf) FREE(buf, M_SBUF)
#else /* KERNEL */
-#define KASSERT(e, m)
-#define SBMALLOC(size) malloc(size)
-#define SBFREE(buf) free(buf)
-#define min(x,y) MIN(x,y)
-
+#define KASSERT(e, m)
+#define SBMALLOC(size) malloc(size)
+#define SBFREE(buf) free(buf)
+#define min(x, y) MIN(x,y)
+
#endif /* KERNEL */
/*
* 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)
-#define SBUF_FREESPACE(s) ((s)->s_size - (s)->s_len - 1)
-#define SBUF_CANEXTEND(s) ((s)->s_flags & SBUF_AUTOEXTEND)
+#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)
+#define SBUF_FREESPACE(s) ((s)->s_size - (s)->s_len - 1)
+#define SBUF_CANEXTEND(s) ((s)->s_flags & SBUF_AUTOEXTEND)
/*
* Set / clear flags
*/
-#define SBUF_SETFLAG(s, f) do { (s)->s_flags |= (f); } while (0)
-#define SBUF_CLEARFLAG(s, f) do { (s)->s_flags &= ~(f); } while (0)
+#define SBUF_SETFLAG(s, f) do { (s)->s_flags |= (f); } while (0)
+#define SBUF_CLEARFLAG(s, f) do { (s)->s_flags &= ~(f); } while (0)
-#define SBUF_MINEXTENDSIZE 16 /* Should be power of 2. */
-#define SBUF_MAXEXTENDSIZE PAGE_SIZE
-#define SBUF_MAXEXTENDINCR PAGE_SIZE
+#define SBUF_MINEXTENDSIZE 16 /* Should be power of 2. */
+#define SBUF_MAXEXTENDSIZE PAGE_SIZE
+#define SBUF_MAXEXTENDINCR PAGE_SIZE
/*
* Debugging support
("%s called with %sfinished or corrupt sbuf", fun,
(state ? "un" : "")));
}
-#define assert_sbuf_integrity(s) _assert_sbuf_integrity(__func__, (s))
-#define assert_sbuf_state(s, i) _assert_sbuf_state(__func__, (s), (i))
+#define assert_sbuf_integrity(s) _assert_sbuf_integrity(__func__, (s))
+#define assert_sbuf_state(s, i) _assert_sbuf_state(__func__, (s), (i))
#else /* KERNEL && INVARIANTS */
-#define assert_sbuf_integrity(s) do { } while (0)
-#define assert_sbuf_state(s, i) do { } while (0)
+#define assert_sbuf_integrity(s) do { } while (0)
+#define assert_sbuf_state(s, i) do { } while (0)
#endif /* KERNEL && INVARIANTS */
static int
newsize = SBUF_MINEXTENDSIZE;
while (newsize < size) {
- if (newsize < (int)SBUF_MAXEXTENDSIZE)
+ if (newsize < (int)SBUF_MAXEXTENDSIZE) {
newsize *= 2;
- else
+ } else {
newsize += SBUF_MAXEXTENDINCR;
+ }
}
- return (newsize);
+ return newsize;
}
char *newbuf;
int newsize;
- if (!SBUF_CANEXTEND(s))
- return (-1);
+ if (!SBUF_CANEXTEND(s)) {
+ return -1;
+ }
newsize = sbuf_extendsize(s->s_size + addlen);
newbuf = (char *)SBMALLOC(newsize);
- if (newbuf == NULL)
- return (-1);
+ if (newbuf == NULL) {
+ return -1;
+ }
bcopy(s->s_buf, newbuf, s->s_size);
- if (SBUF_ISDYNAMIC(s))
+ if (SBUF_ISDYNAMIC(s)) {
SBFREE(s->s_buf);
- else
+ } else {
SBUF_SETFLAG(s, SBUF_DYNAMIC);
+ }
s->s_buf = newbuf;
s->s_size = newsize;
- return (0);
+ return 0;
}
/*
flags &= SBUF_USRFLAGMSK;
if (s == NULL) {
s = (struct sbuf *)SBMALLOC(sizeof *s);
- if (s == NULL)
- return (NULL);
+ if (s == NULL) {
+ return NULL;
+ }
bzero(s, sizeof *s);
s->s_flags = flags;
SBUF_SETFLAG(s, SBUF_DYNSTRUCT);
s->s_size = length;
if (buf) {
s->s_buf = buf;
- return (s);
+ return s;
}
- if (flags & SBUF_AUTOEXTEND)
+ if (flags & SBUF_AUTOEXTEND) {
s->s_size = sbuf_extendsize(s->s_size);
+ }
s->s_buf = (char *)SBMALLOC(s->s_size);
if (s->s_buf == NULL) {
- if (SBUF_ISDYNSTRUCT(s))
+ if (SBUF_ISDYNSTRUCT(s)) {
SBFREE(s);
- return (NULL);
+ }
+ return NULL;
}
SBUF_SETFLAG(s, SBUF_DYNAMIC);
- return (s);
+ return s;
}
#ifdef KERNEL
KASSERT(error != NULL,
("%s called with NULL error pointer", __func__));
- s = sbuf_new(s, NULL, uio->uio_resid + 1, 0);
+ s = sbuf_new(s, NULL, uio_resid(uio) + 1, 0);
if (s == NULL) {
*error = ENOMEM;
- return (NULL);
+ return NULL;
}
- *error = uiomove(s->s_buf, uio->uio_resid, uio);
+ *error = uiomove(s->s_buf, uio_resid(uio), uio);
if (*error != 0) {
sbuf_delete(s);
- return (NULL);
+ return NULL;
}
s->s_len = s->s_size - 1;
*error = 0;
- return (s);
+ return s;
}
#endif
KASSERT(pos < s->s_size,
("attempt to seek past end of sbuf (%d >= %d)", pos, s->s_size));
- if (pos < 0 || pos > s->s_len)
- return (-1);
+ if (pos < 0 || pos > s->s_len) {
+ return -1;
+ }
s->s_len = pos;
- return (0);
+ return 0;
}
/*
assert_sbuf_integrity(s);
assert_sbuf_state(s, 0);
- if (SBUF_HASOVERFLOWED(s))
- return (-1);
+ if (SBUF_HASOVERFLOWED(s)) {
+ return -1;
+ }
for (; len; len--) {
- if (!SBUF_HASROOM(s) && sbuf_extend(s, len) < 0)
+ if (!SBUF_HASROOM(s) && sbuf_extend(s, len) < 0) {
break;
+ }
s->s_buf[s->s_len++] = *str++;
}
if (len) {
SBUF_SETFLAG(s, SBUF_OVERFLOWED);
- return (-1);
+ return -1;
}
- return (0);
+ return 0;
}
#ifdef KERNEL
assert_sbuf_integrity(s);
assert_sbuf_state(s, 0);
- if (SBUF_HASOVERFLOWED(s))
- return (-1);
+ if (SBUF_HASOVERFLOWED(s)) {
+ return -1;
+ }
- if (len == 0)
- return (0);
+ if (len == 0) {
+ return 0;
+ }
if (len > (unsigned) SBUF_FREESPACE(s)) {
sbuf_extend(s, len - SBUF_FREESPACE(s));
len = min(len, SBUF_FREESPACE(s));
}
- if (copyin(CAST_USER_ADDR_T(uaddr), s->s_buf + s->s_len, len) != 0)
- return (-1);
+ if (copyin(CAST_USER_ADDR_T(uaddr), s->s_buf + s->s_len, len) != 0) {
+ return -1;
+ }
s->s_len += len;
- return (0);
+ return 0;
}
#endif
assert_sbuf_state(s, 0);
sbuf_clear(s);
- return (sbuf_bcat(s, buf, len));
+ return sbuf_bcat(s, buf, len);
}
/*
assert_sbuf_integrity(s);
assert_sbuf_state(s, 0);
- if (SBUF_HASOVERFLOWED(s))
- return (-1);
+ if (SBUF_HASOVERFLOWED(s)) {
+ return -1;
+ }
while (*str) {
- if (!SBUF_HASROOM(s) && sbuf_extend(s, strlen(str)) < 0)
+ if (!SBUF_HASROOM(s) && sbuf_extend(s, strlen(str)) < 0) {
break;
+ }
s->s_buf[s->s_len++] = *str++;
}
if (*str) {
SBUF_SETFLAG(s, SBUF_OVERFLOWED);
- return (-1);
+ return -1;
}
- return (0);
+ return 0;
}
#ifdef KERNEL
assert_sbuf_integrity(s);
assert_sbuf_state(s, 0);
- if (SBUF_HASOVERFLOWED(s))
- return (-1);
+ if (SBUF_HASOVERFLOWED(s)) {
+ return -1;
+ }
- if (len == 0)
- len = SBUF_FREESPACE(s); /* XXX return 0? */
+ if (len == 0) {
+ len = SBUF_FREESPACE(s); /* XXX return 0? */
+ }
if (len > (unsigned) SBUF_FREESPACE(s)) {
sbuf_extend(s, len);
len = min(len, SBUF_FREESPACE(s));
switch (copyinstr(CAST_USER_ADDR_T(uaddr), s->s_buf + s->s_len, len + 1, &done)) {
case ENAMETOOLONG:
SBUF_SETFLAG(s, SBUF_OVERFLOWED);
- /* fall through */
+ /* fall through */
case 0:
s->s_len += done - 1;
break;
default:
- return (-1); /* XXX */
+ return -1; /* XXX */
}
- return (done);
+ return done;
}
#endif
assert_sbuf_state(s, 0);
sbuf_clear(s);
- return (sbuf_cat(s, str));
+ return sbuf_cat(s, str);
}
/*
KASSERT(fmt != NULL,
("%s called with a NULL format string", __func__));
- if (SBUF_HASOVERFLOWED(s))
- return (-1);
+ if (SBUF_HASOVERFLOWED(s)) {
+ return -1;
+ }
do {
va_copy(ap_copy, ap);
* given sufficient space, hence the min() calculation below.
*/
s->s_len += min(len, SBUF_FREESPACE(s));
- if (!SBUF_HASROOM(s) && !SBUF_CANEXTEND(s))
+ if (!SBUF_HASROOM(s) && !SBUF_CANEXTEND(s)) {
SBUF_SETFLAG(s, SBUF_OVERFLOWED);
+ }
KASSERT(s->s_len < s->s_size,
("wrote past end of sbuf (%d >= %d)", s->s_len, s->s_size));
- if (SBUF_HASOVERFLOWED(s))
- return (-1);
- return (0);
+ if (SBUF_HASOVERFLOWED(s)) {
+ return -1;
+ }
+ return 0;
}
/*
va_start(ap, fmt);
result = sbuf_vprintf(s, fmt, ap);
va_end(ap);
- return(result);
+ return result;
}
/*
assert_sbuf_integrity(s);
assert_sbuf_state(s, 0);
- if (SBUF_HASOVERFLOWED(s))
- return (-1);
+ if (SBUF_HASOVERFLOWED(s)) {
+ return -1;
+ }
if (!SBUF_HASROOM(s) && sbuf_extend(s, 1) < 0) {
SBUF_SETFLAG(s, SBUF_OVERFLOWED);
- return (-1);
+ return -1;
+ }
+ if (c != '\0') {
+ s->s_buf[s->s_len++] = c;
}
- if (c != '\0')
- s->s_buf[s->s_len++] = c;
- return (0);
+ return 0;
}
static inline int
isspace(char ch)
{
- return (ch == ' ' || ch == '\n' || ch == '\t');
+ return ch == ' ' || ch == '\n' || ch == '\t';
}
/*
assert_sbuf_integrity(s);
assert_sbuf_state(s, 0);
- if (SBUF_HASOVERFLOWED(s))
- return (-1);
+ if (SBUF_HASOVERFLOWED(s)) {
+ return -1;
+ }
- while (s->s_len && isspace(s->s_buf[s->s_len-1]))
+ while (s->s_len && isspace(s->s_buf[s->s_len - 1])) {
--s->s_len;
+ }
- return (0);
+ return 0;
}
/*
int
sbuf_overflowed(struct sbuf *s)
{
- return SBUF_HASOVERFLOWED(s);
+ return SBUF_HASOVERFLOWED(s);
}
/*
assert_sbuf_integrity(s);
/* don't care if it's finished or not */
- if (SBUF_HASOVERFLOWED(s))
- return (-1);
+ if (SBUF_HASOVERFLOWED(s)) {
+ return -1;
+ }
return s->s_len;
}
assert_sbuf_integrity(s);
/* don't care if it's finished or not */
- if (SBUF_ISDYNAMIC(s))
+ if (SBUF_ISDYNAMIC(s)) {
SBFREE(s->s_buf);
+ }
isdyn = SBUF_ISDYNSTRUCT(s);
bzero(s, sizeof *s);
- if (isdyn)
+ if (isdyn) {
SBFREE(s);
+ }
}
/*
int
sbuf_done(struct sbuf *s)
{
-
- return(SBUF_ISFINISHED(s));
+ return SBUF_ISFINISHED(s);
}