DWORD Alignment
hi!
i found this function in some sample from the microsoft platform SDK , the sample shows how to create dialog templates on runtime , and its using this function to align some pointer to DWORD boundary.
now , i dont understand anything from this function and how its done what its have to do, so can someone expline to me how its works, and what is DWORD Alignment?
i will be very greatfull if somebody will , Thanks .
/*+++
Helper routine. Take an input pointer, return closest
pointer that is aligned on a DWORD (4 byte) boundary.
---*/
LPWORD lpwAlign ( LPWORD lpIn)
{
DWORD_PTR ul;
ul = (DWORD_PTR) lpIn;
ul +=3;
ul >>=2;
ul <<=2;
return (LPWORD) ul;
}
------------------------------- Goblineye Entertainment------------------------------
anyone , please?
thanks
thanks
------------------------------- Goblineye Entertainment------------------------------
Ok, I''ll try
Aligning to Dwords basically means that pointers should be multiples of four - 1(four bytes for dword , starting on 0)
the "ul +=3;" actually is puzzling me; seems that it''s there to perform some form of rounding;
Anyway, the shift right and shift left are made to fill the rightmost two bits with zeros (why they didn''t just "anded" with ...FFFFFFC is anybody''s guess), so the +3 seems to be to make the pointer jump to the higher dword aligned value whenever it''s not aligned (I don''t understand why, but anyway...)
example:
a pointer value of 4 (100 in binary, from now on) -> 100+011 -> 111 -> 111>>2 -> 1(the two bits go out of the register) -> 1<<2 -> 100
so an allready aligned pointer returns an equal value.
now a pointer with a value of 5 (not alligned)
101-> 101+ 011->1000 -> 1000>>2 -> 10 -> 10<<2 ->1000
so now the pointer jumps to the next dowrd alligned pointer: value 8
with a value of six, to finish:
110->110 + 011->1001->1001>>2 -> 10 -> 10 << 2 -> 1000
The way i see it any value that is not alligned returns a pointer to the next alligned value (don''t ask me why they don''t truncate it, maybe to ensure that the pointer is valid (before the initial pointer it won''t be valid for sure, after it might still be))
And I really don''t get why they don''t just mask the pointer with a 0xFFFFFFFC mask (maybe to make it pointer variable length independent?)
Aligning to Dwords basically means that pointers should be multiples of four - 1(four bytes for dword , starting on 0)
the "ul +=3;" actually is puzzling me; seems that it''s there to perform some form of rounding;
Anyway, the shift right and shift left are made to fill the rightmost two bits with zeros (why they didn''t just "anded" with ...FFFFFFC is anybody''s guess), so the +3 seems to be to make the pointer jump to the higher dword aligned value whenever it''s not aligned (I don''t understand why, but anyway...)
example:
a pointer value of 4 (100 in binary, from now on) -> 100+011 -> 111 -> 111>>2 -> 1(the two bits go out of the register) -> 1<<2 -> 100
so an allready aligned pointer returns an equal value.
now a pointer with a value of 5 (not alligned)
101-> 101+ 011->1000 -> 1000>>2 -> 10 -> 10<<2 ->1000
so now the pointer jumps to the next dowrd alligned pointer: value 8
with a value of six, to finish:
110->110 + 011->1001->1001>>2 -> 10 -> 10 << 2 -> 1000
The way i see it any value that is not alligned returns a pointer to the next alligned value (don''t ask me why they don''t truncate it, maybe to ensure that the pointer is valid (before the initial pointer it won''t be valid for sure, after it might still be))
And I really don''t get why they don''t just mask the pointer with a 0xFFFFFFFC mask (maybe to make it pointer variable length independent?)
ok alignment.... well on a 32-bit Intel procesor its fast to read 32-bit(a dword) from memory than it is to read say a byte or a word....
the resion is that the procesor will read in a 32-bit dword anyways then extract the part you want....
now lets say you have a word(16-bits) aligned on a dword boundery...
it reads the dword then it extracts the word from that...
if it no aligned.... like this
(location in memory expresed in bytes)
256 word(high byte) (this is a dword boundery)
255 word(low byte)
254
253
252 dword boundery
if you divied 255 by 4 it doesn''t divied evenly... what the processor does in this case it to read the word in is to read in the dword at 252(bytes 252-255)... this gets the low byte then it reads the dword at 256(bytes 256-260)... so its 2 dwords that it reads in...
if it is aligned like this:
(location in memory expresed in bytes)
256 (dword boundery)
255
254
253 high byte of word
252 low byte of word (dword boundery)
the processor reads the dword at 252 so it thericacly twice as fast to algin them(probably is I haven''t tested it my self to see=)...
so what the function lpwAlign does is make sure that pointer is on a dword boundery.... because when variables are created it doesn''t care if they are aligned... it uses alittle more memory(gaps are created), but its faster and in some cases with hardware it cann''t handle stuff that isn''t aligned...
Great Milenko
the resion is that the procesor will read in a 32-bit dword anyways then extract the part you want....
now lets say you have a word(16-bits) aligned on a dword boundery...
it reads the dword then it extracts the word from that...
if it no aligned.... like this
(location in memory expresed in bytes)
256 word(high byte) (this is a dword boundery)
255 word(low byte)
254
253
252 dword boundery
if you divied 255 by 4 it doesn''t divied evenly... what the processor does in this case it to read the word in is to read in the dword at 252(bytes 252-255)... this gets the low byte then it reads the dword at 256(bytes 256-260)... so its 2 dwords that it reads in...
if it is aligned like this:
(location in memory expresed in bytes)
256 (dword boundery)
255
254
253 high byte of word
252 low byte of word (dword boundery)
the processor reads the dword at 252 so it thericacly twice as fast to algin them(probably is I haven''t tested it my self to see=)...
so what the function lpwAlign does is make sure that pointer is on a dword boundery.... because when variables are created it doesn''t care if they are aligned... it uses alittle more memory(gaps are created), but its faster and in some cases with hardware it cann''t handle stuff that isn''t aligned...
Great Milenko
Words Of Wisdom:
"Never Stick A Pretzel In Your Butt It Might Break Off In There."
http://www.crosswinds.net/~milenko
http://www.crosswinds.net/~pirotech
The Great Milenko"Don't stick a pretzel up your ass, it might get stuck in there.""Computer Programming is findding the right wrench to hammer in the correct screw."
very very tnx to each of u!
i start to understand now , but i still dont understand for what is the "ul +=3;" part.....
if somebody can tell for what it used i will be very greatfull
i start to understand now , but i still dont understand for what is the "ul +=3;" part.....
if somebody can tell for what it used i will be very greatfull
------------------------------- Goblineye Entertainment------------------------------
The +3 is to round UP to the nearest DWORD address.
Let''s say you allocated an unaligned block of bytes, and you round the begin address down - you enter memory outside of the allocated block.
(ps: I think all allocated blocks are already aligned - this is just for example)
Let''s say you allocated an unaligned block of bytes, and you round the begin address down - you enter memory outside of the allocated block.
(ps: I think all allocated blocks are already aligned - this is just for example)
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement