Re: portability question
- From: Owen Taylor <otaylor redhat com>
- To: Sheldon Simms <wsxyz6294 yahoo com>
- Cc: gtk-devel-list gnome org
- Subject: Re: portability question
- Date: Tue, 16 Dec 2003 16:10:49 -0500
On Tue, 2003-12-16 at 14:47, Sheldon Simms wrote:
> What I meant by assumption of arbitrary pointer/int
> interchangeability was things like this (from
> gtype.c):
>
> static inline TypeNode*
> lookup_type_node_I (register GType utype)
> {
> if (utype > G_TYPE_FUNDAMENTAL_MAX)
> return (TypeNode*) (utype & ~TYPE_ID_MASK);
> else
> return static_fundamental_type_nodes[utype >>
> G_TYPE_FUNDAMENTAL_SHIFT];
> }
>
> Since GType is actually an unsigned long and there is
> no guarantee in the
> C standard that an arbitrary pointer value can be
> stored in a value of
> unsigned integer type. This could be done portably
> like this:
>
> typedef uintptr_t GType;
> ...
> static inline TypeNode*
> lookup_type_node_I (register GType utype)
> {
> if (utype > G_TYPE_FUNDAMENTAL_MAX)
> return (TypeNode*)(void *)(utype &
> ~TYPE_ID_MASK);
> else
> return static_fundamental_type_nodes[utype >>
> G_TYPE_FUNDAMENTAL_SHIFT];
> }
>
> Because a uintptr_t is guaranteed to be able to hold a
> void * and the double
> cast makes sure that any necessary conversions between
> a void * and a
> TypeNode * are done.
You'll find that uintptr_t is vastly less portable than
what we are doing now. It's a C99'ism...
Part of the GLib porting process is making sure that the
size of the GType integral type is the same as the size of
a pointer. Luckily for the ease of the porting process,
all common platforms have long and pointers with the
same size.
We're playing a bunch of "non-portable" tricks with GType
because the speed and compactness of GType is really important.
Note that we're assuming as well:
- No pointers are found in the first 1024 bytes of memory
- Pointers to TypeNode structures are aligned on 4-byte
boundaries.
Regards,
Owen
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]