Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 1 Jun 2011 16:34:18 +0000 (UTC)
From:      Matthew D Fleming <mdf@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-8@freebsd.org
Subject:   svn commit: r222570 - stable/8/sys/kern
Message-ID:  <201106011634.p51GYI8D077886@svn.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: mdf
Date: Wed Jun  1 16:34:18 2011
New Revision: 222570
URL: http://svn.freebsd.org/changeset/base/222570

Log:
  MFC r212180-r212184:
  
  Use math rather than iteration when the desired sbuf size is larger than
  SBUF_MAXEXTENDSIZE.
  
  Fix brain fart when converting an if statement into a KASSERT.
  
  Fix user-space libsbuf build.  Why isn't CTASSERT available to
  user-space?
  
  Style(9) fixes and eliminate the use of min().
  
  Use a better #if guard.

Modified:
  stable/8/sys/kern/subr_sbuf.c
Directory Properties:
  stable/8/sys/   (props changed)
  stable/8/sys/amd64/include/xen/   (props changed)
  stable/8/sys/cddl/contrib/opensolaris/   (props changed)
  stable/8/sys/contrib/dev/acpica/   (props changed)
  stable/8/sys/contrib/pf/   (props changed)

Modified: stable/8/sys/kern/subr_sbuf.c
==============================================================================
--- stable/8/sys/kern/subr_sbuf.c	Wed Jun  1 16:07:14 2011	(r222569)
+++ stable/8/sys/kern/subr_sbuf.c	Wed Jun  1 16:34:18 2011	(r222570)
@@ -56,7 +56,6 @@ static MALLOC_DEFINE(M_SBUF, "sbuf", "st
 #define	KASSERT(e, m)
 #define	SBMALLOC(size)		malloc(size)
 #define	SBFREE(buf)		free(buf)
-#define	min(x,y)		MIN(x,y)
 #endif /* _KERNEL */
 
 /*
@@ -116,18 +115,24 @@ _assert_sbuf_state(const char *fun, stru
 
 #endif /* _KERNEL && INVARIANTS */
 
+#ifdef CTASSERT
+CTASSERT(powerof2(SBUF_MAXEXTENDSIZE));
+CTASSERT(powerof2(SBUF_MAXEXTENDINCR));
+#endif
+
 static int
 sbuf_extendsize(int size)
 {
 	int newsize;
 
-	newsize = SBUF_MINEXTENDSIZE;
-	while (newsize < size) {
-		if (newsize < (int)SBUF_MAXEXTENDSIZE)
+	if (size < (int)SBUF_MAXEXTENDSIZE) {
+		newsize = SBUF_MINEXTENDSIZE;
+		while (newsize < size)
 			newsize *= 2;
-		else
-			newsize += SBUF_MAXEXTENDINCR;
+	} else {
+		newsize = roundup2(size, SBUF_MAXEXTENDINCR);
 	}
+	KASSERT(newsize >= size, ("%s: %d < %d\n", __func__, newsize, size));
 	return (newsize);
 }
 
@@ -184,11 +189,11 @@ sbuf_new(struct sbuf *s, char *buf, int 
 		s->s_flags = flags;
 	}
 	s->s_size = length;
-	if (buf) {
+	if (buf != NULL) {
 		s->s_buf = buf;
 		return (s);
 	}
-	if (flags & SBUF_AUTOEXTEND)
+	if ((flags & SBUF_AUTOEXTEND) != 0)
 		s->s_size = sbuf_extendsize(s->s_size);
 	s->s_buf = SBMALLOC(s->s_size);
 	if (s->s_buf == NULL) {
@@ -284,7 +289,7 @@ sbuf_bcat(struct sbuf *s, const void *bu
 			break;
 		s->s_buf[s->s_len++] = *str++;
 	}
-	if (len) {
+	if (len > 0) {
 		SBUF_SETFLAG(s, SBUF_OVERFLOWED);
 		return (-1);
 	}
@@ -308,7 +313,8 @@ sbuf_bcopyin(struct sbuf *s, const void 
 		return (0);
 	if (len > SBUF_FREESPACE(s)) {
 		sbuf_extend(s, len - SBUF_FREESPACE(s));
-		len = min(len, SBUF_FREESPACE(s));
+		if (SBUF_FREESPACE(s) < len)
+			len = SBUF_FREESPACE(s);
 	}
 	if (copyin(uaddr, s->s_buf + s->s_len, len) != 0)
 		return (-1);
@@ -345,12 +351,12 @@ sbuf_cat(struct sbuf *s, const char *str
 	if (SBUF_HASOVERFLOWED(s))
 		return (-1);
 
-	while (*str) {
+	while (*str != '\0') {
 		if (!SBUF_HASROOM(s) && sbuf_extend(s, strlen(str)) < 0)
 			break;
 		s->s_buf[s->s_len++] = *str++;
 	}
-	if (*str) {
+	if (*str != '\0') {
 		SBUF_SETFLAG(s, SBUF_OVERFLOWED);
 		return (-1);
 	}
@@ -376,7 +382,8 @@ sbuf_copyin(struct sbuf *s, const void *
 		len = SBUF_FREESPACE(s);	/* XXX return 0? */
 	if (len > SBUF_FREESPACE(s)) {
 		sbuf_extend(s, len);
-		len = min(len, SBUF_FREESPACE(s));
+		if (SBUF_FREESPACE(s) < len)
+			len = SBUF_FREESPACE(s);
 	}
 	switch (copyinstr(uaddr, s->s_buf + s->s_len, len + 1, &done)) {
 	case ENAMETOOLONG:
@@ -440,9 +447,11 @@ sbuf_vprintf(struct sbuf *s, const char 
 	 * terminating nul.
 	 *
 	 * vsnprintf() returns the amount that would have been copied,
-	 * given sufficient space, hence the min() calculation below.
+	 * given sufficient space, so don't over-increment s_len.
 	 */
-	s->s_len += min(len, SBUF_FREESPACE(s));
+	if (SBUF_FREESPACE(s) < len)
+		len = SBUF_FREESPACE(s);
+	s->s_len += len;
 	if (!SBUF_HASROOM(s) && !SBUF_CANEXTEND(s))
 		SBUF_SETFLAG(s, SBUF_OVERFLOWED);
 
@@ -486,7 +495,7 @@ sbuf_putc(struct sbuf *s, int c)
 		return (-1);
 	}
 	if (c != '\0')
-	    s->s_buf[s->s_len++] = c;
+		s->s_buf[s->s_len++] = c;
 	return (0);
 }
 
@@ -503,7 +512,7 @@ sbuf_trim(struct sbuf *s)
 	if (SBUF_HASOVERFLOWED(s))
 		return (-1);
 
-	while (s->s_len && isspace(s->s_buf[s->s_len-1]))
+	while (s->s_len > 0 && isspace(s->s_buf[s->s_len-1]))
 		--s->s_len;
 
 	return (0);



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