From owner-svn-src-stable-8@FreeBSD.ORG Wed Jun 1 16:34:18 2011 Return-Path: Delivered-To: svn-src-stable-8@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 93206106566B; Wed, 1 Jun 2011 16:34:18 +0000 (UTC) (envelope-from mdf@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:4f8:fff6::2c]) by mx1.freebsd.org (Postfix) with ESMTP id 77C088FC0A; Wed, 1 Jun 2011 16:34:18 +0000 (UTC) Received: from svn.freebsd.org (localhost [127.0.0.1]) by svn.freebsd.org (8.14.4/8.14.4) with ESMTP id p51GYIhu077888; Wed, 1 Jun 2011 16:34:18 GMT (envelope-from mdf@svn.freebsd.org) Received: (from mdf@localhost) by svn.freebsd.org (8.14.4/8.14.4/Submit) id p51GYI8D077886; Wed, 1 Jun 2011 16:34:18 GMT (envelope-from mdf@svn.freebsd.org) Message-Id: <201106011634.p51GYI8D077886@svn.freebsd.org> From: Matthew D Fleming Date: Wed, 1 Jun 2011 16:34:18 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-8@freebsd.org X-SVN-Group: stable-8 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cc: Subject: svn commit: r222570 - stable/8/sys/kern X-BeenThere: svn-src-stable-8@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: SVN commit messages for only the 8-stable src tree List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 01 Jun 2011 16:34:18 -0000 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);