svn commit: r189221 - head/sys/sys

Bjoern A. Zeeb bz at FreeBSD.org
Sun Mar 1 01:35:41 PST 2009


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_ */


More information about the svn-src-all mailing list