January, 2005

  • The Old New Thing

    Why did the Win64 team choose the LLP64 model?


    Over on Channel 9, member Beer28 wrote, "I can't imagine there are too many problems with programs that have type widths changed." I got a good chuckle out of that and made a note to write up an entry on the Win64 data model.

    The Win64 team selected the LLP64 data model, in which all integral types remain 32-bit values and only pointers expand to 64-bit values. Why?

    In addition to the reasons give on that web page, another reason is that doing so avoids breaking persistence formats. For example, part of the header data for a bitmap file is defined by the following structure:

    typedef struct tagBITMAPINFOHEADER {
            DWORD      biSize;
            LONG       biWidth;
            LONG       biHeight;
            WORD       biPlanes;
            WORD       biBitCount;
            DWORD      biCompression;
            DWORD      biSizeImage;
            LONG       biXPelsPerMeter;
            LONG       biYPelsPerMeter;
            DWORD      biClrUsed;
            DWORD      biClrImportant;

    If a LONG expanded from a 32-bit value to a 64-bit value, it would not be possible for a 64-bit program to use this structure to parse a bitmap file.

    There are persistence formats other than files. In addition to the obvious things like RPC and DCOM, registry binary blobs and shared memory blocks can also be used to transfer information between processes. If the source and destination processes are different bitness, any change to the integer sizes would result in a mismatch.

    Notice that in these inter-process communication scenarios, we don't have to worry as much about the effect of a changed pointer size. Nobody in their right mind would transfer a pointer across processes: Separate address spaces mean that the pointer value is useless in any process other than the one that generated it, so why share it?

  • The Old New Thing

    Capturing the current directory from a batch file


    Sometimes people go to great lengths to get information which is available in a much simpler way. We saw it a few days ago when we found a 200+-line C# program that could be replaced with a 90-byte batch file. Here's another example of a rather roundabout way of capturing the current directory from a batch file.

    The easy way is to use the %CD% pseudo-variable. It expands to the current working directory.

    set OLDDIR=%CD%
    .. do stuff ..
    chdir /d %OLDDIR% &rem restore current directory

    (Of course, directory save/restore could more easily have been done with pushd/popd, but that's not the point here.)

    The %CD% trick is handy even from the command line. For example, I often find myself in a directory where there's a file that I want to operate on but... oh, I need to chdir to some other directory in order to perform that operation.

    set _=%CD%\curfile.txt
    cd ... some other directory ...
    somecommand args %_% args

    (I like to use %_% as my scratch environment variable.)

    Type SET /? to see the other pseudo-variables provided by the command processor.

  • The Old New Thing

    Control how much network bandwith Automatic Updates will use


    By default, the Background Intelligent Transfer Service (BITS) which is used by Automatic Updates will use idle network bandwidth for downloading updates. This is normally not a problem.

    One case where it can be a problem is you have a large LAN that shares a single DSL connection. BITS doesn't see that that DSL connection is shared. Consequently, each computer on the LAN will be using its idle network bandwidth to download updates and the total of all the LAN computers doing this will oversaturate the DSL connection. [Typo fixed. 31-Jan-05.]

    Another example where this can be a problem is if you have a network card that connects to a hardware firewall which in turn uses a dial-up modem to connect to the Internet. (For example, you might connect through a classic Apple AirPort which is in turn connected to a modem.) BITS sees your fast network card and can't see that there is a bottleneck further downstream. As a result, it oversaturates the dial-up connection.

    To tweak the BITS settings, you can fire up the Group Policy Editor by typing "gpedit.msc" into the Run dialog. From there, go to Computer Configuration, Administrative Templates, Network, then Background Intelligent Transfer Service. From there you can configure the maximum network bandwidth that BITS will use. You can even specify different BITS download rates based on time of day, so that it downloads more aggressively while you're sleeping, for example.

  • The Old New Thing

    The strangest way of rounding down to the nearest quarter


    In a previous life, I wrote database software. A customer complained that one of their reports was taking an unacceptably long amount of time to generate, and I was asked to take a look at it even though it wasn't my account.

    The report was a vacation-days report, listing the number of vacation days taken and available for each employee. Vacation days accrued at a fixed rate but were granted only in quarter-day increments. For example, if you earned 15 vacation days per year and the year was 32% complete, then you had accrued 32% × 15 = 4.8 vacation days, of which 4.75 were available to use.

    The existing code to round the number of accrued days down to the nearest quarter-day went something like this:

    * assume that at this point, ACCRUED is the number
    * of accrued days.
    * STR(ACCRUED,6,2) converts ACCRUED to a 6-character
    * string: 3 integer digits, a decimal point, and two
    * fractional digits.  Excess fractional digits are rounded.
    STORE RIGHT(S,2) TO F        && extract digits after decimal
    IF F < "25"
     F = "00"                    && 00 to 24 becomes 00
     IF F < "50"
      F = "25"                   && 25 to 49 becomes 25
      IF F < "75"
       F = "50"                  && 50 to 74 becomes 50
       F = "75"                  && 75 to 99 becomes 75
    ROUNDED = VAL(LEFT(S,4) + F) && reconstruct value and convert

    In other words, the code converted the number to a string, extracted the digits after the decimal point, did string comparisons to figure out which quartile the fraction resided in, then created a new string with the replacement fraction and converted that string back to a number. And all this in an interpreted language.

    This code fragment was repeated each time rounding-down was needed because the language supported only 32 subroutines, and this procedure wasn't important enough to be worth kicking out one of the other existing subroutines.

    I replaced this seventeen-line monstrosity with the one-line equivalent each time it occurred, and the report ran much faster.

    (This is nowhere near the strangest way of implementing rounding. There are far worse examples.)

    Exercise: What is the one-line equivalent?

    Exercise: What is the double-rounding bug in the original code?

  • The Old New Thing

    Why do files and directories with no time/date mess up sorting in Explorer?


    If you have a file or directory that does not have a last-modified date, you may find that it causes Explorer to sort very strangely. (How do you get a file or directory with no last-modifiied date? It's hard to do; you need the help of an external file system.) Why is this?

    As we learned earlier, a sort comparison function must impose a total order in order to produce consistent results. The problem is that Explorer's comparison function mis-handles files and directories with no last-modified date.

    To get some more of the background behind this, you need to know about so-called "simple pidls". A simple pidl is an item ID list that refers to a file or directory that does not actually exist.

    The problem is that a valid file or directory with no last-modified date looks just like one of these simple pidls becauses Explorer uses the last-modified date to distinguish whether it is manipulating a real pidl or a simple one.

    The problems with sorting occur when it comes time to decide where in the list these "real pidls that are mistaken for simple pidls" go into the sorted list. Explorer tries to keep all folders together, but if it sees a "simple pidl" it can't tell whether that item is a folder or a file (after all, something that doesn't exist is neither a file nor a folder) and it ends up producing inconsistent comparison results.

    Moral of the story: Be careful with your sort functions. If you produce inconsistent results in your sort function, you will get inconsistent results in your "sorted" output.

  • The Old New Thing

    Bringing cryptic command lines to Windows


    The CMD.EXE batch language can be awfully cryptic, but for those who miss the richness of command lines like

    kill -1 $(ps -ef | grep inetd | grep -v grep | tr -s " " | cut -f2 -d " ")
    or bursts of line noise masquerading as a pipeline of "find", "sed", and "awk" processes, Microsoft Windows Services for Unix is available for free download.

  • The Old New Thing

    Alton Brown book appearance report


    Right on schedule, Alton Brown appeared at the Elliot Bay Book Company bookstore in downtown Seattle. One of my friends wondered aloud, "Wait a second, he's promoting his cookbook. How do you do a reading from a cookbook?"

    He didn't read from his cookbook.

    To an overflow crowd that probably violated a few fire codes, Alton Brown discussed what inspired him to tackle a book on baking, riffed with the audience (he's quite funny when interacting with a crowd), then fielded questions. Alton Brown trivia:

    • At the New England Culinary Institute, he drove his teachers crazy by constantly asking the sorts of questions that he answers on his show. "What is going on chemically?" "What is the purpose of eggs in this recipe?" "Where does the water go?" They thought they were rid of him when he graduated, but AB got the last laugh: The school now gets applications which say "I want to cook like Alton Brown."
    • Why does he wear Hawaiian shirts on Good Eats? Because the material they're made from doesn't rustle against the microphone.
    • The wacko camera angles come from his background in directing television commercials. The show was originally up against The West Wing and used its irreverent style as a form of counter-programming.
    • AB claims that all the actors on the show are really production crew members brought in front of the camera. How much of this you choose to believe is up to you.
    • Each episode takes about three days to film. (Note: This doesn't count all the writing and research time.) Compare this to traditional cooking shows which film three episodes in one day!
    • The Good Eats theme is exactly ten notes long, at AB's specific request. It was allegedly inspired by the last track on the Get Shorty soundtrack CD.
    • On Iron Chef America he does not himself taste any of the dishes. Any more. He learned this lesson the hard way after trying Hiroyuki Sakai's trout ice cream.
    • Before trying any of the recipes in the book, check AB's web site for corrections. The most notorious misprint is the mysterious "aspirin" substitution on page 238. (You also have to watch out for the spelling mistakes. "Nickle"?)

    Afterwards, he signed books for ages and managed to be a good sport about it throughout. Then again, this is a book tour, after all. During that time, he's mastered the ability to sign a book and talk at the same time. I, on the other hand, am a rank amateur and couldn't even talk and watch him sign my book at the same time.

  • The Old New Thing

    Why are kernel HANDLEs always a multiple of four?


    Not very well known is that the bottom two bits of kernel HANDLEs are always zero; in other words, their numeric value is always a multiple of 4. Note that this applies only to kernel HANDLEs; it does not apply to pseudo-handles or to any other type of handle (USER handles, GDI handles, multimedia handles...) Kernel handles are things you can pass to the CloseHandle function.

    The availability of the bottom two bits is buried in the ntdef.h header file:

    // Low order two bits of a handle are ignored by the system and available
    // for use by application code as tag bits.  The remaining bits are opaque
    // and used to store a serial number and table index.
    #define OBJ_HANDLE_TAGBITS  0x00000003L

    That at least the bottom bit of kernel HANDLEs is always zero is implied by the GetQueuedCompletionStatus function, which indicates that you can set the bottom bit of the event handle to suppress completion port notification. In order for this to work, the bottom bit must normally be zero.

    This information is not useful for most application writers, which should continue to treat HANDLEs as opaque values. The people who would be interested in tag bits are those who are implementing low-level class libraries or are wrapping kernel objects inside a larger framework.

  • The Old New Thing

    Hyperlinking to Hutchison Whampoa Limited forbidden


    Maybe they don't want people to find them.

    The copyright notice for the web site of Hutchison Whampoa Limited states,

    Copyright Hutchison Whampoa Limited. 2003. All rights reserved.

    No person, whether an individual or a body corporate, shall create or establish a hyperlink to the HWL Corporate Website by hypertext reference or imaging without the written permission of Hutchison.

    I can't create a hyperlink so you'll have to find it yourself.

    This isn't an issue of deep linking; they are banning even links to their home page.

  • The Old New Thing

    A 90-byte "whereis" program


    Sometimes people try too hard.

    You can download a C# program to look for a file on your PATH, or you can use a 90-character batch file:

    @for %%e in (%PATHEXT%) do @for %%i in (%1%%e) do @if NOT "%%~$PATH:i"=="" echo %%~$PATH:i
Page 1 of 3 (27 items) 123