Re: [NtDev] Re: Silly question on IA64

I’m sorry, but that is an insidious programming habit that leads straight to the
gates of Silicon Hell. Even if you’re writing an application for a known hardware
platform, there’s still no valid reason to cast a pointer to directly to a type
designed for numerical values. You should never assume the size of a pointer is
explicitly the same size as any other datatype other than another pointer. A
similar sin is to assume the size of ‘int’.

I think one possible solution to avoid these kinds of programming traps is to
define a global macro somewhere such that you have a correctly sized cast
available depending on whether you’re compiling 32 or 64bit. Or, you could always
cast to a PUCHAR.

CONTAINING_RECORD64 is only used on explicit 64bit pointers, so using a 64bit
datatype is appropriate since the pointer-size is guaranteed.

Regards,

Paul Bunn, UltraBac.com, 425-644-6000
Microsoft MVP - WindowsNT/2000
http://www.ultrabac.com

-----Original Message-----
From: James Antognini [mailto:antognini@us.ibm.com]
Sent: Wednesday, June 06, 2001 8:08 AM
To: NT Developers Interest List
Subject: [ntdev] Re: [NtDev] Re: Silly question on IA64

Sorry, I disagree with your assertion that only a pointer variable
should hold a pointer. I am continually doing pointer arithmetic, and I
have to cast a pointer into what works, typically a ULONG.
Interestingly, the macro CONTAINING_RECORD64 does arithmetic on
ULONGLONG, which is not a pointer type. You may say Microsoft ought to
have defined a ULONGLONG_PTR type to kept things pure, but I feel doing
so or not doing so is a matter of taste.

That said, in product code where I used ULONG for pointer values, I
always appended a comment that the technique was not
64-bit-address-safe. Matters of taste aside, it’s wise to leave a
warning.


You are currently subscribed to ntdev as: $subst(‘Recip.EmailAddr’)
To unsubscribe send a blank email to leave-ntdev-$subst(‘Recip.MemberIDChar’)@lists.osr.com

I found this (rounding down to a 64K boundary):

PVOID zits = 0x12345678,
zits1;

zits1 = (PVOID)((ULONG)zits&0xffff0000);

PBYTE and PCHAR won’t work. But I found that ULONG_PTR does! I’m not
sure that type was available when I did that piece of code. Certainly I
wasn’t aware of it. Now I know better, and for all the reasons given in
the discussion, I prefer to use it.


James Antognini
IBM Watson Research


You are currently subscribed to ntdev as: $subst(‘Recip.EmailAddr’)
To unsubscribe send a blank email to leave-ntdev-$subst(‘Recip.MemberIDChar’)@lists.osr.com

OK, I’ll admit to doing something similarly, though my workaround was to do
everything on a ULONG, then cast it to a pointer as the last thing I did.

Anyone else have a better way of masking an address?

One way would be:
PUCHAR pAddress = 0x12345678;
pAddress /= 0x10000;
pAddress *= 0x10000;

But this is far worse, as far as I am concerned.

Anyone have an elegant and typesafe way to do this?

Phil

-----Original Message-----
From: James Antognini [mailto:antognini@us.ibm.com]
Sent: Wednesday, June 06, 2001 2:45 PM
To: NT Developers Interest List
Subject: [ntdev] Re: Silly question on IA64

I found this (rounding down to a 64K boundary):

PVOID zits = 0x12345678,
zits1;

zits1 = (PVOID)((ULONG)zits&0xffff0000);

PBYTE and PCHAR won’t work. But I found that ULONG_PTR does! I’m not
sure that type was available when I did that piece of code. Certainly I
wasn’t aware of it. Now I know better, and for all the reasons given in
the discussion, I prefer to use it.


James Antognini
IBM Watson Research


You are currently subscribed to ntdev as: xxxxx@intel.com
To unsubscribe send a blank email to leave-ntdev-$subst(‘Recip.MemberIDChar’)@lists.osr.com


You are currently subscribed to ntdev as: $subst(‘Recip.EmailAddr’)
To unsubscribe send a blank email to leave-ntdev-$subst(‘Recip.MemberIDChar’)@lists.osr.com

zits1 = (PVOID)((ULONG)zits&0xffff0000);

Well, I admit that I’ve never felt the need to round down to a 64K
boundary, so I never thought of that. Of course, I don’t do segmented
address-space operating systems so I guess I’m just lucky. All the
systems I do work with have a macro in the system header files that
does the rounding to page boundaries.

Steve Williams “The woods are lovely, dark and deep.
xxxxx@icarus.com But I have promises to keep,
xxxxx@picturel.com and lines to code before I sleep,
http://www.picturel.com And lines to code before I sleep.”


You are currently subscribed to ntdev as: $subst(‘Recip.EmailAddr’)
To unsubscribe send a blank email to leave-ntdev-$subst(‘Recip.MemberIDChar’)@lists.osr.com

All the systems I do work with have a macro in the system header
files that
does the rounding to page boundaries.

I’ve missed that one, I guess. Would you mind telling us what it is?

Phil


You are currently subscribed to ntdev as: $subst(‘Recip.EmailAddr’)
To unsubscribe send a blank email to leave-ntdev-$subst(‘Recip.MemberIDChar’)@lists.osr.com

Processor bit-count/endian orientation independent way of doing this is:

PVOID zits1 = (PVOID)((ULONG_PTR)zits & (ULONG_PTR)~0xffff);

… to mask off last 4 bits.

Regards,

Paul Bunn, UltraBac.com, 425-644-6000
Microsoft MVP - WindowsNT/2000
http://www.ultrabac.com

-----Original Message-----
From: Barila, Phil [mailto:xxxxx@intel.com]
Sent: Wednesday, June 06, 2001 4:18 PM
To: NT Developers Interest List
Subject: [ntdev] Re: Silly question on IA64

OK, I’ll admit to doing something similarly, though my workaround was to do
everything on a ULONG, then cast it to a pointer as the last thing I did.

Anyone else have a better way of masking an address?

One way would be:
PUCHAR pAddress = 0x12345678;
pAddress /= 0x10000;
pAddress *= 0x10000;

But this is far worse, as far as I am concerned.

Anyone have an elegant and typesafe way to do this?

Phil

-----Original Message-----
From: James Antognini [mailto:antognini@us.ibm.com]
Sent: Wednesday, June 06, 2001 2:45 PM
To: NT Developers Interest List
Subject: [ntdev] Re: Silly question on IA64

I found this (rounding down to a 64K boundary):

PVOID zits = 0x12345678,
zits1;

zits1 = (PVOID)((ULONG)zits&0xffff0000);

PBYTE and PCHAR won’t work. But I found that ULONG_PTR does! I’m not
sure that type was available when I did that piece of code. Certainly I
wasn’t aware of it. Now I know better, and for all the reasons given in
the discussion, I prefer to use it.


You are currently subscribed to ntdev as: $subst(‘Recip.EmailAddr’)
To unsubscribe send a blank email to leave-ntdev-$subst(‘Recip.MemberIDChar’)@lists.osr.com

All the systems I do work with have a macro in the system header
files that does the rounding to page boundaries.

xxxxx@intel.com said:

I’ve missed that one, I guess. Would you mind telling us what it is?

In Linux (and if I remember correctly other unices) and Windows 2000,
PAGE_ALIGN(x) does the trick.

Steve Williams “The woods are lovely, dark and deep.
xxxxx@icarus.com But I have promises to keep,
xxxxx@picturel.com and lines to code before I sleep,
http://www.picturel.com And lines to code before I sleep.”


You are currently subscribed to ntdev as: $subst(‘Recip.EmailAddr’)
To unsubscribe send a blank email to leave-ntdev-$subst(‘Recip.MemberIDChar’)@lists.osr.com

Thats because ULONG_PTR is defined as a unsigned long.

As far as I know, U’re not allowed to do any arithmetic like & etc on any
pointer, you need to type cast it to do that.

Ramit.

----- Original Message -----
From: “James Antognini”
Newsgroups: ntdev
To: “NT Developers Interest List”
Sent: Thursday, June 07, 2001 3:14 AM
Subject: [ntdev] Re: Silly question on IA64

> I found this (rounding down to a 64K boundary):
>
> PVOID zits = 0x12345678,
> zits1;
>
> zits1 = (PVOID)((ULONG)zits&0xffff0000);
>
> PBYTE and PCHAR won’t work. But I found that ULONG_PTR does! I’m not
> sure that type was available when I did that piece of code. Certainly I
> wasn’t aware of it. Now I know better, and for all the reasons given in
> the discussion, I prefer to use it.
>
> –
> James Antognini
> IBM Watson Research
>
>
>
> —
> You are currently subscribed to ntdev as: xxxxx@wipro.com
> To unsubscribe send a blank email to leave-ntdev-$subst(‘Recip.MemberIDChar’)@lists.osr.com


You are currently subscribed to ntdev as: $subst(‘Recip.EmailAddr’)
To unsubscribe send a blank email to leave-ntdev-$subst(‘Recip.MemberIDChar’)@lists.osr.com

> What is the difference between casting to a ULONG, and casting to a PVOID?

If you ++ either one, they both increment by one. If you add 0x10, they

Can PVOID be ++ ed? At least C++ prohibits this because sizeof(void) is a
nonsense and not zero.

Max


You are currently subscribed to ntdev as: $subst(‘Recip.EmailAddr’)
To unsubscribe send a blank email to leave-ntdev-$subst(‘Recip.MemberIDChar’)@lists.osr.com