This should be a bit faster.
return node->fptr_cluster;
}
-static cluster_t find_bit_and_set(uint8_t* bitmap, size_t start, size_t end)
+static cluster_t find_bit_and_set(bitmap_t* bitmap, size_t start, size_t end)
{
- const size_t start_index = start / 8;
- const size_t end_index = DIV_ROUND_UP(end, 8);
+ const size_t start_index = start / sizeof(bitmap_t) / 8;
+ const size_t end_index = DIV_ROUND_UP(end, sizeof(bitmap_t) * 8);
size_t i;
+ size_t start_bitindex;
+ size_t end_bitindex;
size_t c;
for (i = start_index; i < end_index; i++)
{
- if (bitmap[i] == 0xff)
+ if (bitmap[i] == ~((bitmap_t) 0))
continue;
- for (c = MAX(i * 8, start); c < MIN((i + 1) * 8, end); c++)
+ start_bitindex = MAX(i * sizeof(bitmap_t) * 8, start);
+ end_bitindex = MIN((i + 1) * sizeof(bitmap_t) * 8, end);
+ for (c = start_bitindex; c < end_bitindex; c++)
if (BMAP_GET(bitmap, c) == 0)
{
BMAP_SET(bitmap, c);
{
if (ef->cmap.dirty)
{
- exfat_pwrite(ef->dev, ef->cmap.chunk, (ef->cmap.chunk_size + 7) / 8,
+ exfat_pwrite(ef->dev, ef->cmap.chunk, BMAP_SIZE(ef->cmap.chunk_size),
exfat_c2o(ef, ef->cmap.start_cluster));
ef->cmap.dirty = false;
}
#define ROUND_UP(x, d) (DIV_ROUND_UP(x, d) * (d))
#define UTF8_BYTES(c) ((c) * 6) /* UTF-8 character can occupy up to 6 bytes */
+typedef size_t bitmap_t;
+#define BMAP_SIZE(count) (ROUND_UP(count, sizeof(bitmap_t) * 8) / 8)
+#define BMAP_BLOCK(index) ((index) / sizeof(bitmap_t) / 8)
+#define BMAP_MASK(index) ((bitmap_t) 1 << ((index) % (sizeof(bitmap_t) * 8)))
#define BMAP_GET(bitmap, index) \
- (((uint8_t*) bitmap)[(index) / 8] & (1u << ((index) % 8)))
+ ((bitmap)[BMAP_BLOCK(index)] & BMAP_MASK(index))
#define BMAP_SET(bitmap, index) \
- ((uint8_t*) bitmap)[(index) / 8] |= (1u << ((index) % 8))
+ ((bitmap)[BMAP_BLOCK(index)] |= BMAP_MASK(index))
#define BMAP_CLR(bitmap, index) \
- ((uint8_t*) bitmap)[(index) / 8] &= ~(1u << ((index) % 8))
+ ((bitmap)[BMAP_BLOCK(index)] &= ~BMAP_MASK(index))
struct exfat_node
{
{
cluster_t start_cluster;
uint32_t size; /* in bits */
- uint8_t* chunk;
+ bitmap_t* chunk;
uint32_t chunk_size; /* in bits */
bool dirty;
}
}
ef->cmap.size = le32_to_cpu(ef->sb->cluster_count) -
EXFAT_FIRST_DATA_CLUSTER;
- if (le64_to_cpu(bitmap->size) < (ef->cmap.size + 7) / 8)
+ if (le64_to_cpu(bitmap->size) < DIV_ROUND_UP(ef->cmap.size, 8))
{
exfat_error("invalid clusters bitmap size: %"PRIu64
" (expected at least %u)",
- le64_to_cpu(bitmap->size), (ef->cmap.size + 7) / 8);
+ le64_to_cpu(bitmap->size),
+ DIV_ROUND_UP(ef->cmap.size, 8));
goto error;
}
/* FIXME bitmap can be rather big, up to 512 MB */
ef->cmap.chunk_size = ef->cmap.size;
- ef->cmap.chunk = malloc(le64_to_cpu(bitmap->size));
+ ef->cmap.chunk = malloc(BMAP_SIZE(ef->cmap.chunk_size));
if (ef->cmap.chunk == NULL)
{
exfat_error("failed to allocate clusters bitmap chunk "
goto error;
}
- exfat_pread(ef->dev, ef->cmap.chunk, le64_to_cpu(bitmap->size),
+ exfat_pread(ef->dev, ef->cmap.chunk,
+ BMAP_SIZE(ef->cmap.chunk_size),
exfat_c2o(ef, ef->cmap.start_cluster));
break;