Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 9 Jul 2001 17:48:19 +1000 (EST)
From:      Bruce Evans <bde@zeta.org.au>
To:        Dag-Erling Smorgrav <des@ofug.org>
Cc:        arch@FreeBSD.ORG
Subject:   Re: Type differences
Message-ID:  <Pine.BSF.4.21.0107091706090.86109-100000@besplex.bde.org>
In-Reply-To: <xzpbsmvqe73.fsf@flood.ping.uio.no>

next in thread | previous in thread | raw e-mail | index | archive | help
On 8 Jul 2001, Dag-Erling Smorgrav wrote:

> des@des /usr/src/sys% grep 'define.*BSD_SIZE_T' {i386,alpha}/include/ansi.h
> i386/include/ansi.h:#define     _BSD_SIZE_T_    unsigned int            /* sizeof() */
> alpha/include/ansi.h:#define    _BSD_SIZE_T_    unsigned long           /* sizeof() */
> des@des /usr/src/sys% grep 'define.*BSD_OFF_T' {i386,alpha}/include/ansi.h
> i386/include/ansi.h:#define     _BSD_OFF_T_     __int64_t               /* file offset */
> alpha/include/ansi.h:#define    _BSD_OFF_T_     long                    /* file offset */
> 
> What's the idea here?  off_t is the exact same size and signedness on
> both platforms, so why not use the same definition for both?

The idea is that _BSD_FOO_T_ should have a basic (ISO C90) type so
that <machine/ansi.h> doesn't have convoluted dependencies on the files
that declare typedefed types.  This is not possible for _BSD_OFF_T_,
so it is handled specially (see rev.15 of the i386 version).

> And why
> make size_t an int on i386 but a long on alpha, when on both these
> platforms int and long are identical?

_BSD_SIZE_T_ is different because it _is_ different.
1. int and long aren't identical.  The difference may be just in their
   spelling, but it affects the whole type system.
2. int and long don't even have the same size on alphas under FreeBSD.
3. <machine/ansi.h> doesn't get to choose the type of _BSD_SIZE_T_.  It
   must match the type of the compiler's sizeof() operator.  This is
   specified by the SIZE_TYPE macro in gcc/cppdefault.h and
   gcc/config/${MACHINE_ARCH}/*.  It defaults to "long unsigned int",
   but for historical reasons it is "unsigned int" on i386's.

> Also, gcc (correctly) complains about a "comparison between signed and
> unsigned" when you try to compare a size_t to an off_t on alpha - but
> doesn't complain on i386.  What gives?

On i386's, size_t is 32 bits unsigned.  For the comparison, size_t's are
promoted to a common type.  off_t is longer, so size_t is first promoted
to 64 bits signed (it loses its unsignedness due to ISO C's (BAD)
value-preserving (signedness-clobbering) promotion rule).  off_t is also
64 bit signed, so no further promotion occurs.  Both operands are now
signed, so gcc doesn't complain.  Not that there is still a bug if it
is possible for the off_t to have a negative value; signedness-clobbering
promotion has mainly broken the warning for i386's.

> And while we're on this subject, what's the Officially Approved way of
> printf()ing an off_t or a size_t so it does not generate warnings on
> either platform?

Implement the ISO C99 [u]intmax_t and %j[diouxX] and use them.  You
could also implement and use %z[diouxX] for size_t's and %t[diouxX] for
ptrdiff_t's.

Bruce


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-arch" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?Pine.BSF.4.21.0107091706090.86109-100000>