OSDN Git Service

Address clusters bitmap using size_t-sized blocks instead of bytes.
authorrelan <relan@users.noreply.github.com>
Sat, 3 Aug 2013 13:25:28 +0000 (13:25 +0000)
committerrelan <relan@users.noreply.github.com>
Mon, 24 Aug 2015 05:26:16 +0000 (08:26 +0300)
This should be a bit faster.

libexfat/cluster.c
libexfat/exfat.h
libexfat/node.c

index 5a13d23..e84e581 100644 (file)
@@ -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;
        }
index f7825d3..4b0ebc9 100644 (file)
 #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;
        }
index 9eaf0d7..e83ec9e 100644 (file)
@@ -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;