proper types for printf()-ing pointers on amd64 that won't break i386?

2008-09-18 Thread Steve Franks
Hi,

I'm trying to correct some warnings in a port marked
ONLY_FOR_ARCHS=i386.  They stem from casting a pointer (which I assume
is a 64-bit unsigned) to "unsigned int" which is apparently 32 bits?
I sort of thought int was supposed to be the atomic register size, but
no doubt that would break more than it would help, so it's 32-bits.
Anyways, what's the right way to fix this?  The port actually works
fine as-is on amd64, so I can only assume something was fixed for 7.1,
or someone was being extra cautious with the i386 tag.

The code:

   typedef unsigned int cardinal;
   ...
   fprintf(stderr, "Mode Table Offset: $C + $%x\n",
((cardinal)map->mode_table) - ((cardinal)map->bios_ptr));

Can I just ditch the cast+%x and use %p?  I don't have an i386 system
to test on, and I don't want to break anything if I submit a patch...

Thanks,
Steve
___
freebsd-hackers@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
To unsubscribe, send any mail to "[EMAIL PROTECTED]"


Re: proper types for printf()-ing pointers on amd64 that won't break

2008-09-18 Thread Lowell Gilbert
"Steve Franks" <[EMAIL PROTECTED]> writes:

> Hi,
>
> I'm trying to correct some warnings in a port marked
> ONLY_FOR_ARCHS=i386.  They stem from casting a pointer (which I assume
> is a 64-bit unsigned) to "unsigned int" which is apparently 32 bits?
> I sort of thought int was supposed to be the atomic register size, but
> no doubt that would break more than it would help, so it's 32-bits.
> Anyways, what's the right way to fix this?  The port actually works
> fine as-is on amd64, so I can only assume something was fixed for 7.1,
> or someone was being extra cautious with the i386 tag.
>
> The code:
>
>typedef unsigned int cardinal;
>...
>fprintf(stderr, "Mode Table Offset: $C + $%x\n",
> ((cardinal)map->mode_table) - ((cardinal)map->bios_ptr));
>
> Can I just ditch the cast+%x and use %p?  I don't have an i386 system
> to test on, and I don't want to break anything if I submit a patch...

What is actually being printed isn't a pointer, but the difference
between two pointers (I assume from your comments; the code included
isn't enough to show where they come from).  That means the correct
type of what's being printed is size_t, which our printf seems to
support with a "z" modifier.  Removing the explicit casts or (if
necessary), replacing them with something that is big enough will also
be needed.
___
freebsd-hackers@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
To unsubscribe, send any mail to "[EMAIL PROTECTED]"


Re: proper types for printf()-ing pointers on amd64 that won't break

2008-09-18 Thread John Baldwin
On Thursday 18 September 2008 02:26:59 pm Lowell Gilbert wrote:
> "Steve Franks" <[EMAIL PROTECTED]> writes:
> 
> > Hi,
> >
> > I'm trying to correct some warnings in a port marked
> > ONLY_FOR_ARCHS=i386.  They stem from casting a pointer (which I assume
> > is a 64-bit unsigned) to "unsigned int" which is apparently 32 bits?
> > I sort of thought int was supposed to be the atomic register size, but
> > no doubt that would break more than it would help, so it's 32-bits.
> > Anyways, what's the right way to fix this?  The port actually works
> > fine as-is on amd64, so I can only assume something was fixed for 7.1,
> > or someone was being extra cautious with the i386 tag.
> >
> > The code:
> >
> >typedef unsigned int cardinal;
> >...
> >fprintf(stderr, "Mode Table Offset: $C + $%x\n",
> > ((cardinal)map->mode_table) - ((cardinal)map->bios_ptr));
> >
> > Can I just ditch the cast+%x and use %p?  I don't have an i386 system
> > to test on, and I don't want to break anything if I submit a patch...
> 
> What is actually being printed isn't a pointer, but the difference
> between two pointers (I assume from your comments; the code included
> isn't enough to show where they come from).  That means the correct
> type of what's being printed is size_t, which our printf seems to
> support with a "z" modifier.  Removing the explicit casts or (if
> necessary), replacing them with something that is big enough will also
> be needed.

The difference of two pointers is actually a ptrdiff_t rather than a size_t.  
size_t is what sizeof() returns.

-- 
John Baldwin
___
freebsd-hackers@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
To unsubscribe, send any mail to "[EMAIL PROTECTED]"


Re: proper types for printf()-ing pointers on amd64 that won't break i386?

2008-09-18 Thread Jeremy Chadwick
On Thu, Sep 18, 2008 at 10:41:42AM -0700, Steve Franks wrote:
> I'm trying to correct some warnings in a port marked
> ONLY_FOR_ARCHS=i386.  They stem from casting a pointer (which I assume
> is a 64-bit unsigned) to "unsigned int" which is apparently 32 bits?
> I sort of thought int was supposed to be the atomic register size, but
> no doubt that would break more than it would help, so it's 32-bits.
> Anyways, what's the right way to fix this?  The port actually works
> fine as-is on amd64, so I can only assume something was fixed for 7.1,
> or someone was being extra cautious with the i386 tag.
> 
> The code:
> 
>typedef unsigned int cardinal;
>...
>fprintf(stderr, "Mode Table Offset: $C + $%x\n",
> ((cardinal)map->mode_table) - ((cardinal)map->bios_ptr));
> 
> Can I just ditch the cast+%x and use %p?  I don't have an i386 system
> to test on, and I don't want to break anything if I submit a patch...

Yes, use %p!  It works fine on all platforms.

-- 
| Jeremy Chadwickjdc at parodius.com |
| Parodius Networking   http://www.parodius.com/ |
| UNIX Systems Administrator  Mountain View, CA, USA |
| Making life hard for others since 1977.  PGP: 4BD6C0CB |

___
freebsd-hackers@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
To unsubscribe, send any mail to "[EMAIL PROTECTED]"


Re: proper types for printf()-ing pointers on amd64 that won't break i386?

2008-09-18 Thread John Baldwin
On Thursday 18 September 2008 01:41:42 pm Steve Franks wrote:
> Hi,
> 
> I'm trying to correct some warnings in a port marked
> ONLY_FOR_ARCHS=i386.  They stem from casting a pointer (which I assume
> is a 64-bit unsigned) to "unsigned int" which is apparently 32 bits?
> I sort of thought int was supposed to be the atomic register size, but
> no doubt that would break more than it would help, so it's 32-bits.
> Anyways, what's the right way to fix this?  The port actually works
> fine as-is on amd64, so I can only assume something was fixed for 7.1,
> or someone was being extra cautious with the i386 tag.
> 
> The code:
> 
>typedef unsigned int cardinal;
>...
>fprintf(stderr, "Mode Table Offset: $C + $%x\n",
> ((cardinal)map->mode_table) - ((cardinal)map->bios_ptr));
> 
> Can I just ditch the cast+%x and use %p?  I don't have an i386 system
> to test on, and I don't want to break anything if I submit a patch...

If mode_table and bios_ptr are pointers, then their difference is not a 
pointer, but a ptrdiff_t.  You can use %tx to print one of those in hex.  
Note, however, that subtracting pointers only works if they are of the same 
type, and it will return different results in this case unless they are char 
* pointers.  Probably better is to do something like this:

fprintf(stderr, "Mode Table Offset: $C + $%x\n",
(int)((uintptr_t)map->mode_table - (uintptr_t)map->bios_ptr));

Note that %p will include a '0x' prefix for non-NULL pointers which you 
probably don't want.

-- 
John Baldwin
___
freebsd-hackers@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
To unsubscribe, send any mail to "[EMAIL PROTECTED]"