svn commit: r195500 - user/des/sizes
Dag-Erling Smorgrav
des at FreeBSD.org
Thu Jul 9 15:41:17 UTC 2009
Author: des
Date: Thu Jul 9 15:41:17 2009
New Revision: 195500
URL: http://svn.freebsd.org/changeset/base/195500
Log:
I've been using this for a long time to check the size and alignment of
various standard types (both C and POSIX).
Added:
user/des/sizes/
user/des/sizes/Makefile (contents, props changed)
user/des/sizes/sizes.c (contents, props changed)
Added: user/des/sizes/Makefile
==============================================================================
--- /dev/null 00:00:00 1970 (empty, because file is newly added)
+++ user/des/sizes/Makefile Thu Jul 9 15:41:17 2009 (r195500)
@@ -0,0 +1,8 @@
+# $FreeBSD$
+
+PROG = sizes
+WARNS ?= 6
+MAN = # none
+
+.include "../Makefile.inc"
+.include <bsd.prog.mk>
Added: user/des/sizes/sizes.c
==============================================================================
--- /dev/null 00:00:00 1970 (empty, because file is newly added)
+++ user/des/sizes/sizes.c Thu Jul 9 15:41:17 2009 (r195500)
@@ -0,0 +1,118 @@
+/*-
+ * Copyright (c) 2009 Dag-Erling Coïdan Smørgrav
+ * 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
+ * in this position and unchanged.
+ * 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 AUTHOR 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 AUTHOR 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.
+ *
+ * $FreeBSD$
+ */
+
+#include <sys/types.h>
+
+#include <assert.h>
+#include <limits.h>
+#include <stddef.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <signal.h>
+#include <time.h>
+
+/*
+ * Determine and display the byte order, signedness of char, and size and
+ * alignment of various scalar types. Assumes two's complement and
+ * eight-bit bytes.
+ */
+
+static void
+assumptions(void)
+{
+ assert(CHAR_BIT == 8);
+ /*
+ * (unsigned char)-1 is
+ * 10000001 (0x81) on sign-and-magnitude
+ * 11111110 (0xfe) on one's complement
+ * 11111111 (0xff) on two's complement
+ */
+ assert((unsigned char)-1 == 0xff);
+}
+
+static void
+byte_order(void)
+{
+ uint32_t e_int = 0x30303030 | 0x04030201;
+ const char *e_char = (const char *)&e_int;
+ printf("%-16s %.4s\n", "byte order", e_char);
+}
+
+static void
+signedness(void)
+{
+ unsigned char uc = 0x80;
+ char *c = (char *)&uc;
+ printf("%-16s %ssigned\n", "char is", (*c > 0) ? "un" : "");
+}
+
+typedef void *void_ptr;
+typedef void (*func_ptr)(void);
+
+#define describe(type) \
+ do { \
+ struct s_##t { char bump; type t; }; \
+ printf("%-12s %12zd %12zd\n", #type, \
+ sizeof(type) * 8, \
+ offsetof(struct s_##t, t) * 8); \
+ } while (0)
+
+static void
+sizes(void)
+{
+ printf("type size alignment\n");
+ printf("--------------------------------------\n");
+ describe(char);
+ describe(short);
+ describe(int);
+ describe(long);
+ describe(long long);
+ describe(float);
+ describe(double);
+ describe(long double);
+ describe(size_t);
+ describe(ptrdiff_t);
+ describe(off_t);
+ describe(time_t);
+ describe(void_ptr);
+ describe(func_ptr);
+ describe(wchar_t);
+ describe(sig_atomic_t);
+}
+
+int
+main(void)
+{
+ assumptions();
+ byte_order();
+ signedness();
+ printf("\n");
+ sizes();
+ return (0);
+}
More information about the svn-src-user
mailing list