J
James Harris
A file allocation table is an on-disk structure that was designed in the
days before people took much care with field alignment. FAT file systems are
still prevalent today. I need to write some code to work with such
structures so have to deal with fields that will not be aligned.
To illustrate, the FAT header starts like this:
8-byte BS_OEMName
2-byte BPB_BytsPerSec
1-byte BPB_SecPerClus
2-byte BPB_RsvdSecCnt
...
As you can see, it is impossible to align the last of those fields,
BPB_RsvdSecCnt. The same is true of some of the fields that follow. (For
anyone who is interested the full list of fields can be seen in a document
called fatgen103. Copies are freely available on the internet.)
I've seen the c faq at http://c-faq.com/struct/padding.html and know that
there are no ideal solutions. This post is to ask for suggestions as to how
to address this well and portably.
I could do something basic and slow but what makes this an interesting
challenge is that: 1) many of the fields *will* be naturally aligned so
don't need any special access mechanism and 2) some of the machines the code
will run on will support unaligned accessed and some will not. It would be
good to use the same C source to run on any type of machine and get the
compiler to emit the faster code where it can.
Anyone already addressed the same sort of issue? I have some ideas but is
there a standard approach?
James
days before people took much care with field alignment. FAT file systems are
still prevalent today. I need to write some code to work with such
structures so have to deal with fields that will not be aligned.
To illustrate, the FAT header starts like this:
8-byte BS_OEMName
2-byte BPB_BytsPerSec
1-byte BPB_SecPerClus
2-byte BPB_RsvdSecCnt
...
As you can see, it is impossible to align the last of those fields,
BPB_RsvdSecCnt. The same is true of some of the fields that follow. (For
anyone who is interested the full list of fields can be seen in a document
called fatgen103. Copies are freely available on the internet.)
I've seen the c faq at http://c-faq.com/struct/padding.html and know that
there are no ideal solutions. This post is to ask for suggestions as to how
to address this well and portably.
I could do something basic and slow but what makes this an interesting
challenge is that: 1) many of the fields *will* be naturally aligned so
don't need any special access mechanism and 2) some of the machines the code
will run on will support unaligned accessed and some will not. It would be
good to use the same C source to run on any type of machine and get the
compiler to emit the faster code where it can.
Anyone already addressed the same sort of issue? I have some ideas but is
there a standard approach?
James