Source-Changes-D archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: POINTER_ALIGNED_P (was: Re: CVS commit: src/sys)



On 16.02.2021 19:46, Roy Marples wrote:
I suggest we change POINTER_ALIGNED_P to accept the alignment value we
want rather than the bitwise test we supply, like so:

#define    POINTER_ALIGNED_P(p, a)        (((uintptr_t)(p) & ((a) - 1))

That's a good definition, clear, simple, obvious, without any surprises.

To make sure that this macro doesn't get broken again, I have written
the attached tests.  I will commit them after making sure I got the
changes to distrib/sets/lists/tests/mi correct.  All the rest I already
tested successfully.

Is my assumption correct that on each platform supported by NetBSD, a
variable of type double gets aligned to a multiple of 8, even on the
stack?  I wanted to keep the test as simple as possible, therefore I
didn't want to call malloc just to get an aligned pointer.

A downside of this test is that the macro from <sys/param.h> gets only
evaluated at compile time of the test.  The test therefore cannot cover
future updates to <sys/param.h> without being reinstalled itself.  But
at least for the release builds, it ensures a correct definition.

Roland
Index: distrib/sets/lists/tests/mi
===================================================================
RCS file: /cvsroot/src/distrib/sets/lists/tests/mi,v
retrieving revision 1.1019
diff -u -p -r1.1019 mi
--- distrib/sets/lists/tests/mi	16 Feb 2021 09:46:24 -0000	1.1019
+++ distrib/sets/lists/tests/mi	16 Feb 2021 21:45:40 -0000
@@ -4396,6 +4396,10 @@
 ./usr/tests/sys/rc/h_args				tests-sys-tests		compattestfile,atf
 ./usr/tests/sys/rc/h_simple				tests-sys-tests		compattestfile,atf
 ./usr/tests/sys/rc/t_rc_d_cli				tests-sys-tests		compattestfile,atf
+./usr/tests/sys/sys					tests-sys-tests		compattestfile,atf
+./usr/tests/sys/sys/Atffile				tests-sys-tests		compattestfile,atf
+./usr/tests/sys/sys/Kyuafile				tests-sys-tests		compattestfile,atf,kyua
+./usr/tests/sys/sys/t_pointer_aligned_p			tests-sys-tests		compattestfile,atf
 ./usr/tests/sys/x86					tests-sys-tests		compattestfile,atf
 ./usr/tests/syscall					tests-obsolete		obsolete
 ./usr/tests/syscall/Atffile				tests-obsolete		obsolete
Index: tests/sys/Makefile
===================================================================
RCS file: /cvsroot/src/tests/sys/Makefile,v
retrieving revision 1.5
diff -u -p -r1.5 Makefile
--- tests/sys/Makefile	15 Oct 2020 17:44:44 -0000	1.5
+++ tests/sys/Makefile	16 Feb 2021 21:45:40 -0000
@@ -10,6 +10,7 @@ TESTS_SUBDIRS+=	netatalk
 TESTS_SUBDIRS+=	netinet
 TESTS_SUBDIRS+=	netinet6
 TESTS_SUBDIRS+=	rc
+TESTS_SUBDIRS+=	sys
 .if ${MACHINE} == amd64 || ${MACHINE} == i386
 TESTS_SUBDIRS+=	x86
 .endif
Index: tests/sys/sys/Makefile
===================================================================
RCS file: tests/sys/sys/Makefile
diff -N tests/sys/sys/Makefile
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ tests/sys/sys/Makefile	16 Feb 2021 21:45:40 -0000
@@ -0,0 +1,11 @@
+# $NetBSD$
+
+TESTSDIR=	${TESTSBASE}/sys/sys
+WARNS=		6
+
+TESTS_C=	t_pointer_aligned_p
+
+.PATH:		${NETBSDSRCDIR}/sys
+CPPFLAGS+=	-I${NETBSDSRCDIR}/sys
+
+.include <bsd.test.mk>
Index: tests/sys/sys/t_pointer_aligned_p.c
===================================================================
RCS file: tests/sys/sys/t_pointer_aligned_p.c
diff -N tests/sys/sys/t_pointer_aligned_p.c
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ tests/sys/sys/t_pointer_aligned_p.c	16 Feb 2021 21:45:40 -0000
@@ -0,0 +1,77 @@
+/*	$NetBSD$	*/
+
+/*-
+ * Copyright (c) 2021 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <sys/types.h>
+#undef __NO_STRICT_ALIGNMENT	/* force the interesting calculation */
+#include <sys/param.h>
+
+#include <atf-c.h>
+
+ATF_TC(pointer_aligned_p);
+ATF_TC_HEAD(pointer_aligned_p, tc)
+{
+
+	atf_tc_set_md_var(tc, "descr", "Tests for POINTER_ALIGNED_P");
+}
+ATF_TC_BODY(pointer_aligned_p, tc)
+{
+	double d;		/* force alignment to a multiple of 8 */
+
+	char *p = (void *)&d;
+
+	if (!POINTER_ALIGNED_P(p, 1))
+		atf_tc_fail("p must be aligned to 1");
+	if (!POINTER_ALIGNED_P(p + 1, 1))
+		atf_tc_fail("p + 1 must be aligned to 1");
+
+	if (!POINTER_ALIGNED_P(p, 2))
+		atf_tc_fail("p must be aligned to 2");
+	if (POINTER_ALIGNED_P(p + 1, 2))
+		atf_tc_fail("p + 1 must not be aligned to 2");
+	if (!POINTER_ALIGNED_P(p + 2, 2))
+		atf_tc_fail("p + 2 must be aligned to 2");
+
+	if (!POINTER_ALIGNED_P(p, 4))
+		atf_tc_fail("p must be aligned to 4");
+	if (POINTER_ALIGNED_P(p + 1, 4))
+		atf_tc_fail("p + 1 must not be aligned to 4");
+	if (POINTER_ALIGNED_P(p + 2, 4))
+		atf_tc_fail("p + 2 must not be aligned to 4");
+	if (POINTER_ALIGNED_P(p + 3, 4))
+		atf_tc_fail("p + 3 must not be aligned to 4");
+	if (!POINTER_ALIGNED_P(p + 4, 4))
+		atf_tc_fail("p + 4 must be aligned to 4");
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+
+	ATF_TP_ADD_TC(tp, pointer_aligned_p);
+
+	return atf_no_error();
+}


Home | Main Index | Thread Index | Old Index