- const cluster_t mid_start = (start + 7) / 8 * 8;
- const cluster_t mid_end = end / 8 * 8;
- cluster_t c;
- cluster_t byte;
-
- for (c = start; c < mid_start; c++)
- if (BMAP_GET(bitmap, c) == 0)
- {
- BMAP_SET(bitmap, c);
- return c + EXFAT_FIRST_DATA_CLUSTER;
- }
+ 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] == ~((bitmap_t) 0))
+ continue;
+ 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);
+ return c + EXFAT_FIRST_DATA_CLUSTER;
+ }
+ }
+ return EXFAT_CLUSTER_END;
+}