From owner-svn-src-all@freebsd.org Fri Jun 24 21:44:47 2016 Return-Path: Delivered-To: svn-src-all@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 7A2A1B74E71; Fri, 24 Jun 2016 21:44:47 +0000 (UTC) (envelope-from asomers@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 3D3CE25FF; Fri, 24 Jun 2016 21:44:47 +0000 (UTC) (envelope-from asomers@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id u5OLikcJ096478; Fri, 24 Jun 2016 21:44:46 GMT (envelope-from asomers@FreeBSD.org) Received: (from asomers@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id u5OLikjj096476; Fri, 24 Jun 2016 21:44:46 GMT (envelope-from asomers@FreeBSD.org) Message-Id: <201606242144.u5OLikjj096476@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: asomers set sender to asomers@FreeBSD.org using -f From: Alan Somers Date: Fri, 24 Jun 2016 21:44:46 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r302180 - in head: sys/sys tests/sys/sys X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.22 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: Fri, 24 Jun 2016 21:44:47 -0000 Author: asomers Date: Fri Jun 24 21:44:46 2016 New Revision: 302180 URL: https://svnweb.freebsd.org/changeset/base/302180 Log: Fix bitstring allocation on 32-bit platforms sys/sys/bitstring.h Fix a rounding calculation that could undersize a bitstring on 32-bit platforms. tests/sys/sys/bitstring_test.h Add a test for bitstr_size PR: 210260 Reported by: Mark Millard Reviewed by: gibbs Approved by: re (marius) Sponsored by: Spectra Logic Corp Differential Revision: https://reviews.freebsd.org/D6848 Modified: head/sys/sys/bitstring.h head/tests/sys/sys/bitstring_test.c Modified: head/sys/sys/bitstring.h ============================================================================== --- head/sys/sys/bitstring.h Fri Jun 24 20:21:32 2016 (r302179) +++ head/sys/sys/bitstring.h Fri Jun 24 21:44:46 2016 (r302180) @@ -75,6 +75,12 @@ typedef unsigned long bitstr_t; #define _BITSTR_MASK (~0UL) #define _BITSTR_BITS (sizeof(bitstr_t) * 8) +#ifdef roundup2 +#define _bit_roundup2 roundup2 +#else +#define _bit_roundup2(x, y) (((x)+((y)-1))&(~((y)-1))) /* if y is powers of two */ +#endif + /* bitstr_t in bit string containing the bit. */ static inline int _bit_idx(int _bit) @@ -104,9 +110,8 @@ _bit_make_mask(int _start, int _stop) } /*----------------------------- Public Interface -----------------------------*/ -/* Number of bytes consumed by a bit string of nbits bits */ -#define bitstr_size(_nbits) \ - (((_nbits) + _BITSTR_BITS - 1) / 8) +/* Number of bytes allocated for a bit string of nbits bits */ +#define bitstr_size(_nbits) (_bit_roundup2(_nbits, _BITSTR_BITS) / 8) /* Allocate a bit string initialized with no bits set. */ #ifdef _KERNEL @@ -123,7 +128,7 @@ bit_alloc(int _nbits) } #endif -/* Allocate a bit string on the stack with no bits set. */ +/* Allocate a bit string on the stack */ #define bit_decl(name, nbits) \ ((name)[bitstr_size(nbits) / sizeof(bitstr_t)]) Modified: head/tests/sys/sys/bitstring_test.c ============================================================================== --- head/tests/sys/sys/bitstring_test.c Fri Jun 24 20:21:32 2016 (r302179) +++ head/tests/sys/sys/bitstring_test.c Fri Jun 24 21:44:46 2016 (r302180) @@ -102,6 +102,17 @@ ATF_TC_BODY(bitstr_in_struct, tc) bit_nclear(test_struct.bitstr, 0, 8); } +ATF_TC_WITHOUT_HEAD(bitstr_size); +ATF_TC_BODY(bitstr_size, tc) +{ + size_t sob = sizeof(bitstr_t); + + ATF_CHECK_EQ(0, bitstr_size(0)); + ATF_CHECK_EQ(sob, bitstr_size(1)); + ATF_CHECK_EQ(sob, bitstr_size(sob * 8)); + ATF_CHECK_EQ(2 * sob, bitstr_size(sob * 8 + 1)); +} + BITSTRING_TC_DEFINE(bit_set) /* bitstr_t *bitstr, int nbits, const char *memloc */ { @@ -407,6 +418,7 @@ ATF_TP_ADD_TCS(tp) { ATF_TP_ADD_TC(tp, bitstr_in_struct); + ATF_TP_ADD_TC(tp, bitstr_size); BITSTRING_TC_ADD(tp, bit_set); BITSTRING_TC_ADD(tp, bit_clear); BITSTRING_TC_ADD(tp, bit_ffs);