From owner-svn-src-all@FreeBSD.ORG Tue May 17 11:04:51 2011 Return-Path: Delivered-To: svn-src-all@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id E778C106564A; Tue, 17 May 2011 11:04:50 +0000 (UTC) (envelope-from phk@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:4f8:fff6::2c]) by mx1.freebsd.org (Postfix) with ESMTP id CCE128FC0C; Tue, 17 May 2011 11:04:50 +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 p4HB4o5Y028310; Tue, 17 May 2011 11:04:50 GMT (envelope-from phk@svn.freebsd.org) Received: (from phk@localhost) by svn.freebsd.org (8.14.4/8.14.4/Submit) id p4HB4oD0028308; Tue, 17 May 2011 11:04:50 GMT (envelope-from phk@svn.freebsd.org) Message-Id: <201105171104.p4HB4oD0028308@svn.freebsd.org> From: Poul-Henning Kamp Date: Tue, 17 May 2011 11:04:50 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cc: Subject: svn commit: r222015 - head/sys/kern X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 17 May 2011 11:04:51 -0000 Author: phk Date: Tue May 17 11:04:50 2011 New Revision: 222015 URL: http://svn.freebsd.org/changeset/base/222015 Log: Use memset() instead of bzero() and memcpy() instead of bcopy(), there is no relevant difference for sbufs, and it increases portability of the source code. Split the actual initialization of the sbuf into a separate local function, so that certain static code checkers can understand what sbuf_new() does, thus eliminating on silly annoyance of MISRA compliance testing. Contributed by: An anonymous company in the last business I expected sbufs to invade. Modified: head/sys/kern/subr_sbuf.c Modified: head/sys/kern/subr_sbuf.c ============================================================================== --- head/sys/kern/subr_sbuf.c Tue May 17 10:44:11 2011 (r222014) +++ head/sys/kern/subr_sbuf.c Tue May 17 11:04:50 2011 (r222015) @@ -144,7 +144,6 @@ sbuf_extendsize(int size) return (newsize); } - /* * Extend an sbuf. */ @@ -160,7 +159,7 @@ sbuf_extend(struct sbuf *s, int addlen) newbuf = SBMALLOC(newsize); if (newbuf == NULL) return (-1); - bcopy(s->s_buf, newbuf, s->s_size); + memcpy(newbuf, s->s_buf, s->s_size); if (SBUF_ISDYNAMIC(s)) SBFREE(s->s_buf); else @@ -171,6 +170,38 @@ sbuf_extend(struct sbuf *s, int addlen) } /* + * Initialize the internals of an sbuf. + * If buf is non-NULL, it points to a static or already-allocated string + * big enough to hold at least length characters. + */ +static struct sbuf * +sbuf_newbuf(struct sbuf *s, char *buf, int length, int flags) +{ + + memset(s, 0, sizeof(*s)); + s->s_flags = flags; + s->s_size = length; + s->s_buf = buf; + + if ((s->s_flags & SBUF_AUTOEXTEND) == 0) { + KASSERT(s->s_size > 1, + ("attempt to create a too small sbuf")); + } + + if (s->s_buf != NULL) + return (s); + + 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) + return (NULL); + SBUF_SETFLAG(s, SBUF_DYNAMIC); + return (s); +} + +/* * Initialize an sbuf. * If buf is non-NULL, it points to a static or already-allocated string * big enough to hold at least length characters. @@ -185,31 +216,17 @@ sbuf_new(struct sbuf *s, char *buf, int ("%s called with invalid flags", __func__)); flags &= SBUF_USRFLAGMSK; - if (s == NULL) { - s = SBMALLOC(sizeof(*s)); - if (s == NULL) - return (NULL); - bzero(s, sizeof(*s)); - s->s_flags = flags; - SBUF_SETFLAG(s, SBUF_DYNSTRUCT); - } else { - bzero(s, sizeof(*s)); - s->s_flags = flags; - } - s->s_size = length; - if (buf != NULL) { - s->s_buf = buf; - return (s); - } - 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) { - if (SBUF_ISDYNSTRUCT(s)) - SBFREE(s); + if (s != NULL) + return (sbuf_newbuf(s, buf, length, flags)); + + s = SBMALLOC(sizeof(*s)); + if (s == NULL) + return (NULL); + if (sbuf_newbuf(s, buf, length, flags) == NULL) { + SBFREE(s); return (NULL); } - SBUF_SETFLAG(s, SBUF_DYNAMIC); + SBUF_SETFLAG(s, SBUF_DYNSTRUCT); return (s); } @@ -727,7 +744,7 @@ sbuf_delete(struct sbuf *s) if (SBUF_ISDYNAMIC(s)) SBFREE(s->s_buf); isdyn = SBUF_ISDYNSTRUCT(s); - bzero(s, sizeof(*s)); + memset(s, 0, sizeof(*s)); if (isdyn) SBFREE(s); }