I am working on writing a simple flash file system, and I need to save one of three states for each page of the flash device:
FREE
INVALID (ready to be freed)
VALID
If these were only two possible states, I would probably use a bitmap (memory is a problem). But what is the most compact way to store these values in this case?
The only thing I can think of is to pack four 2-bit values in charand use bitmasks to work with each value.
For example (it is written quickly, so there is no guarantee that it works fine):
#define FREE 0x0
#define INVALID 0x1
#define VALID 0x2
char state[NUM_ITEMS/4];
void set_state(int item_num, int state) {
int idx;
char tmp;
idx = item_num / 4;
tmp = state[idx];
tmp &= ~(0x3 << (item_num % 4));
tmp |= (state << (item_num % 4));
state[idx] = tmp;
}
int main(void) {
set_state(6, INVALID);
return 0;
}
Is there another option that I don't know about?
source
share