From owner-svn-src-head@FreeBSD.ORG Sun Mar 1 09:35:41 2009 Return-Path: Delivered-To: svn-src-head@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 71F0B106566C; Sun, 1 Mar 2009 09:35:41 +0000 (UTC) (envelope-from bz@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:4f8:fff6::2c]) by mx1.freebsd.org (Postfix) with ESMTP id 468048FC13; Sun, 1 Mar 2009 09:35:41 +0000 (UTC) (envelope-from bz@FreeBSD.org) Received: from svn.freebsd.org (localhost [127.0.0.1]) by svn.freebsd.org (8.14.3/8.14.3) with ESMTP id n219ZfPS037466; Sun, 1 Mar 2009 09:35:41 GMT (envelope-from bz@svn.freebsd.org) Received: (from bz@localhost) by svn.freebsd.org (8.14.3/8.14.3/Submit) id n219ZfwD037465; Sun, 1 Mar 2009 09:35:41 GMT (envelope-from bz@svn.freebsd.org) Message-Id: <200903010935.n219ZfwD037465@svn.freebsd.org> From: "Bjoern A. Zeeb" Date: Sun, 1 Mar 2009 09:35:41 +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: r189221 - head/sys/sys X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 01 Mar 2009 09:35:41 -0000 Author: bz Date: Sun Mar 1 09:35:41 2009 New Revision: 189221 URL: http://svn.freebsd.org/changeset/base/189221 Log: Add the new compile-time assertion macro CTASSERT_EQUAL(). It takes a positive integer constant (the expected value) and another positive integer, usually compile-time evaluated, e.g. CTASSERT_EQUAL(FOO_EXPECTED_SIZE, sizeof (struct foo)); While the classic CTASSERT() gives: error: size of array '__assert60' is negative this gives you: In function '__ctassert_equal_at_line_60': warning: '__expected_42_but_got[464ul]' is used uninitialized in this function and you can directly see the difference in the expected and the real value. CTASSERT_EQUAL() needs special compile time options to trigger thus keep it locally to this header. If it proves to be of general interest it can be moved to systm.h. Submitted by: jmallett Reviewed by: sam, warner, rwatson, jmallett (earlier versions) Modified: head/sys/sys/vimage.h Modified: head/sys/sys/vimage.h ============================================================================== --- head/sys/sys/vimage.h Sun Mar 1 08:01:38 2009 (r189220) +++ head/sys/sys/vimage.h Sun Mar 1 09:35:41 2009 (r189221) @@ -112,4 +112,28 @@ struct vnet_modlink { int vi_symlookup(struct kld_sym_lookup *, char *); void vnet_mod_register(const struct vnet_modinfo *); +/* + * x must be a positive integer constant (expected value), + * y must be compile-time evaluated to a positive integer, + * e.g. CTASSERT_EQUAL(FOO_EXPECTED_SIZE, sizeof (struct foo)); + * One needs to compile with -Wuninitialized and thus at least -O + * for this to trigger and -Werror if it should be fatal. + */ +#define CTASSERT_EQUAL(x, y) \ + static int __attribute__((__used__)) \ + __attribute__((__section__(".debug_ctassert_equal"))) \ + __CONCAT(__ctassert_equal_at_line_, __LINE__)(void); \ + \ + static int __attribute__((__used__)) \ + __attribute__((__section__(".debug_ctassert_equal"))) \ + __CONCAT(__ctassert_equal_at_line_, __LINE__)(void) \ + { \ + int __CONCAT(__CONCAT(__expected_, x), \ + _but_got)[(y) + (x)]; \ + __CONCAT(__CONCAT(__expected_, x), _but_got)[(x)] = 1; \ + return (__CONCAT(__CONCAT(__expected_, x), \ + _but_got)[(y)]); \ + } \ + struct __hack + #endif /* !_SYS_VIMAGE_H_ */