10-06-08 - 2

From the MSDN :

Because buffer addresses for read and write operations must be sector-aligned, the application must have direct 
control of how these buffers are allocated. One way to sector-align buffers is to use the VirtualAlloc function 
to allocate the buffers. Consider the following:

    * VirtualAlloc allocates memory that is aligned on addresses that are integer multiples of the system's page size.
	 Page size is 4,096 bytes on x64 and x86 or 8,192 bytes for the Intel Itanium processor family. For additional
	 information, see the GetSystemInfo function.
    * Sector size is typically 512 to 4,096 bytes for direct-access storage devices (hard drives) and 2,048 bytes for CD-ROMs.
    * Both page and sector sizes are powers of 2.

I'm guessing that if you ever tried to use a sector size larger than 4096, a lot of apps would fail. And by "a lot" I mean 100% of apps that do non-buffered IO.

This is an example of terrible API's, IMO. It's extremely hard to do the "right" thing (for each file name, figure out what drive that file is on, ask for the sector size of that drive, allocate buffers aligned to the sector size of that drive), and it's very easy to do something that will almost always work - just use VirtualAlloc to get 4k aligned pages and use that. Which is why everyone just uses VirtualAlloc. Until one day it doesn't work.

No comments:

old rants