Ryan's Rambling

Sunday, September 26, 2004

Byte Alignment Bites

We are so spoiled when developing for the Windows platform. It is polite enough to allow us to access mis-aligned data (data which is not aligned on machine word boundaries). I have been tinkering around with some ActiveSync code on a Compaq Aero and just finished getting a fresh reminder that non-aligned data can and does result in worse than sub-optimal performance.

In particular, the data exchange occurs within the GetPacket and SetPacket methods of the IReplObjHandler interface. This interface is implemented on both the desktop and the device. I have written the desktop bits in Delphi and the device bits are in Embedded Visual C++ 3.0. Normal thought processes suggest minizing the volume of data passing over the link (especially when it is serial 19200-56000). So, as is typical,I immediately packed the structures for all text content. Basically it is just a fixed header with a variable portion followed by one or more variable length property entities. This format permits me to send just about anything and is relatively robust except for the overhead associated with the property names travelling in each packet exchange. But that is getting a bit off topic (as I am permitted to at this time of night).

All was fine with marshalling the data on the desktop (where you can debug, have a good tool, etc.) but the ActiveSync connection would literally drop and there was nothing logged as to the failure other than a NAK (obscure at best). With some heavy injection of file based debug statements in the code in question I found that the system simply stopped when trying to access the second property header. For whatever reason as soon as I saw what was happening it just sort of turned on a light bulb for me (gotta love that on those rare instances). So, now the marshalling code does four byte alignment on strings that it passes along. It was quite easy since the data lengths were already being used so I was pretty lucky in terms of length of time to finding the bug and fixing it. Now I can get on with the rest of the tinkering.

So, the lesson, remember that all platforms are not created equal and you still need to know something about what is underneath. Happy coding.

Labels:

3 Comments:

  • Can you tell me how to import IReplObjHandler into projects in vs.net 2003?
    Thanks!

    By ShieldSoldier, At 2:45 AM  

  • You should be able to load up the StockPor example that is included in all the CE platform sdks. It is hidden down in the [installpoint]\wce300\MS Pocket PC\support\ActiveSync\activesync stock portfolio sample\ folder. Mind you it is not a managed .net library you are creating. I don't recall having to do anything other then touch up the include and library search paths the last time I looked at it.

    By Ryan, At 11:26 AM  

  • Hi Ryan,

    just popped in from a google search for IReplObjHandler - the whole topics seems to be a pain. However, as you're surprisingly a delphi guy too, can you provide the community with a good starting point or even some sources?

    Let us know, thanks & bye
    Roman

    By Roman, At 12:38 PM  

Post a Comment



Links to this post:

Create a Link

<< Home