From: resver@gmail.com Date: Sat, 3 Aug 2013 13:25:28 +0000 (+0000) Subject: Address clusters bitmap using size_t-sized blocks instead of bytes. This should be... X-Git-Url: http://git.osdn.net/view?p=android-x86%2Fexternal-exfat.git;a=commitdiff_plain;h=db8d13fe0464766b33ddaf99711d679bdf2b16c4 Address clusters bitmap using size_t-sized blocks instead of bytes. This should be a bit faster. git-svn-id: http://exfat.googlecode.com/svn/trunk@371 60bc1c72-a15a-11de-b98f-4500b42dc123 --- diff --git a/libexfat/cluster.c b/libexfat/cluster.c index 5a13d23..e84e581 100644 --- a/libexfat/cluster.c +++ b/libexfat/cluster.c @@ -108,18 +108,22 @@ cluster_t exfat_advance_cluster(const struct exfat* ef, 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); @@ -133,7 +137,7 @@ void exfat_flush(struct exfat* ef) { 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; } diff --git a/libexfat/exfat.h b/libexfat/exfat.h index f7825d3..4b0ebc9 100644 --- a/libexfat/exfat.h +++ b/libexfat/exfat.h @@ -51,12 +51,16 @@ #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 { @@ -97,7 +101,7 @@ struct exfat { cluster_t start_cluster; uint32_t size; /* in bits */ - uint8_t* chunk; + bitmap_t* chunk; uint32_t chunk_size; /* in bits */ bool dirty; } diff --git a/libexfat/node.c b/libexfat/node.c index 9eaf0d7..e83ec9e 100644 --- a/libexfat/node.c +++ b/libexfat/node.c @@ -368,16 +368,17 @@ static int readdir(struct exfat* ef, const struct exfat_node* parent, } 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 " @@ -386,7 +387,8 @@ static int readdir(struct exfat* ef, const struct exfat_node* parent, 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;