Verified Commit ef9fffef authored by Alberto Miranda's avatar Alberto Miranda ♨️
Browse files

Rename: chnk_count_for_offset() -> block_count()

parent 02b4ca55
Loading
Loading
Loading
Loading
+12 −12
Original line number Original line Diff line number Diff line
@@ -169,32 +169,32 @@ block_index(const uint64_t offset, const size_t block_size) {
 * overflow.
 * overflow.
 *
 *
 * @param [in] offset the operation's initial offset.
 * @param [in] offset the operation's initial offset.
 * @param [in] count the number of bytes affected by the operation.
 * @param [in] size the number of bytes affected by the operation.
 * @param [in] chnk_size the block size that should be used to compute the
 * @param [in] block_size the block size that should be used to compute the
 * number of blocks.
 * number of blocks.
 * @returns the number of blocks affected by the operation.
 * @returns the number of blocks affected by the operation.
 */
 */
constexpr std::size_t
constexpr std::size_t
chnk_count_for_offset(const uint64_t offset, const size_t count,
block_count(const uint64_t offset, const size_t size, const size_t block_size) {
                      const size_t chnk_size) {


    using gkfs::utils::arithmetic::log2;
    using gkfs::utils::arithmetic::log2;


    // These checks are automatically removed in release builds
    // These checks are automatically removed in release builds
    assert(is_power_of_2(chnk_size));
    assert(is_power_of_2(block_size));


#if defined(__GNUC__) && !defined(__clang__)
#if defined(__GNUC__) && !defined(__clang__)
    assert(!__builtin_add_overflow_p(offset, count, static_cast<uint64_t>(0)));
    assert(!__builtin_add_overflow_p(offset, size, static_cast<uint64_t>(0)));
#else
#else
    assert(offset + count > offset);
    assert(offset + size > offset);
#endif
#endif


    const uint64_t chnk_start = align_left(offset, chnk_size);
    const uint64_t first_block = align_left(offset, block_size);
    const uint64_t chnk_end = align_left(offset + count, chnk_size);
    const uint64_t final_block = align_left(offset + size, block_size);
    const size_t mask = -!!count; // this is either 0 or ~0
    const size_t mask = -!!size; // this is either 0 or ~0


    return (((chnk_end >> log2(chnk_size)) - (chnk_start >> log2(chnk_size)) +
    return (((final_block >> log2(block_size)) -
             !is_divisible(offset + count, chnk_size))) &
             (first_block >> log2(block_size)) +
             !is_divisible(offset + size, block_size))) &
           mask;
           mask;
}
}


+25 −29
Original line number Original line Diff line number Diff line
@@ -512,7 +512,7 @@ SCENARIO(" chunk IDs can be computed correctly ",


SCENARIO(" the number of chunks involved in an operation can be computed "
SCENARIO(" the number of chunks involved in an operation can be computed "
         "correctly ",
         "correctly ",
         "[utils][numeric][chnk_count_for_offset]") {
         "[utils][numeric][block_count]") {


    GIVEN(" an offset, an operation size, and a block size ") {
    GIVEN(" an offset, an operation size, and a block size ") {


@@ -533,7 +533,7 @@ SCENARIO(" the number of chunks involved in an operation can be computed "


                    THEN(" the computed block count == 0 ") {
                    THEN(" the computed block count == 0 ") {
                        const std::size_t n =
                        const std::size_t n =
                                chnk_count_for_offset(offset, size, block_size);
                                block_count(offset, size, block_size);
                        const std::size_t expected_n = 0;
                        const std::size_t expected_n = 0;
                        REQUIRE(n == expected_n);
                        REQUIRE(n == expected_n);
                    }
                    }
@@ -548,7 +548,7 @@ SCENARIO(" the number of chunks involved in an operation can be computed "


                    THEN(" the computed block count == 1 ") {
                    THEN(" the computed block count == 1 ") {
                        const std::size_t n =
                        const std::size_t n =
                                chnk_count_for_offset(offset, size, block_size);
                                block_count(offset, size, block_size);
                        const std::size_t expected_n = 1;
                        const std::size_t expected_n = 1;
                        REQUIRE(n == expected_n);
                        REQUIRE(n == expected_n);
                    }
                    }
@@ -562,7 +562,7 @@ SCENARIO(" the number of chunks involved in an operation can be computed "


                    THEN(" the computed block count == 1 ") {
                    THEN(" the computed block count == 1 ") {
                        const std::size_t n =
                        const std::size_t n =
                                chnk_count_for_offset(offset, size, block_size);
                                block_count(offset, size, block_size);
                        const std::size_t expected_n = 1;
                        const std::size_t expected_n = 1;
                        REQUIRE(n == expected_n);
                        REQUIRE(n == expected_n);
                    }
                    }
@@ -580,7 +580,7 @@ SCENARIO(" the number of chunks involved in an operation can be computed "
                    THEN(" the computed block count corresponds to the number "
                    THEN(" the computed block count corresponds to the number "
                         "of blocks involved in the operation ") {
                         "of blocks involved in the operation ") {
                        const std::size_t n =
                        const std::size_t n =
                                chnk_count_for_offset(offset, size, block_size);
                                block_count(offset, size, block_size);
                        const std::size_t expected_n =
                        const std::size_t expected_n =
                                (offset + size) / block_size -
                                (offset + size) / block_size -
                                offset / block_size +
                                offset / block_size +
@@ -604,7 +604,7 @@ SCENARIO(" the number of chunks involved in an operation can be computed "


                    THEN(" the computed block count == 1 ") {
                    THEN(" the computed block count == 1 ") {
                        const std::size_t n =
                        const std::size_t n =
                                chnk_count_for_offset(offset, size, block_size);
                                block_count(offset, size, block_size);
                        const std::size_t expected_n = 0;
                        const std::size_t expected_n = 0;
                        REQUIRE(n == expected_n);
                        REQUIRE(n == expected_n);
                    }
                    }
@@ -620,7 +620,7 @@ SCENARIO(" the number of chunks involved in an operation can be computed "


                    THEN(" the computed block count equals 1 ") {
                    THEN(" the computed block count equals 1 ") {
                        const std::size_t n =
                        const std::size_t n =
                                chnk_count_for_offset(offset, size, block_size);
                                block_count(offset, size, block_size);
                        const std::size_t expected_n = 1;
                        const std::size_t expected_n = 1;
                        REQUIRE(n == expected_n);
                        REQUIRE(n == expected_n);
                    }
                    }
@@ -634,7 +634,7 @@ SCENARIO(" the number of chunks involved in an operation can be computed "


                    THEN(" the computed block count == 1 ") {
                    THEN(" the computed block count == 1 ") {
                        const std::size_t n =
                        const std::size_t n =
                                chnk_count_for_offset(offset, size, block_size);
                                block_count(offset, size, block_size);
                        const std::size_t expected_n = 1;
                        const std::size_t expected_n = 1;
                        REQUIRE(n == expected_n);
                        REQUIRE(n == expected_n);
                    }
                    }
@@ -652,7 +652,7 @@ SCENARIO(" the number of chunks involved in an operation can be computed "
                    THEN(" the computed block count corresponds to the number "
                    THEN(" the computed block count corresponds to the number "
                         "of blocks involved in the operation ") {
                         "of blocks involved in the operation ") {
                        const std::size_t n =
                        const std::size_t n =
                                chnk_count_for_offset(offset, size, block_size);
                                block_count(offset, size, block_size);
                        const std::size_t expected_n =
                        const std::size_t expected_n =
                                (offset + size) / block_size -
                                (offset + size) / block_size -
                                offset / block_size +
                                offset / block_size +
@@ -675,7 +675,7 @@ SCENARIO(" the number of chunks involved in an operation can be computed "


                    THEN(" the computed block count == 0 ") {
                    THEN(" the computed block count == 0 ") {
                        const std::size_t n =
                        const std::size_t n =
                                chnk_count_for_offset(offset, size, block_size);
                                block_count(offset, size, block_size);
                        const std::size_t expected_n = 0;
                        const std::size_t expected_n = 0;
                        REQUIRE(n == expected_n);
                        REQUIRE(n == expected_n);
                    }
                    }
@@ -689,7 +689,7 @@ SCENARIO(" the number of chunks involved in an operation can be computed "


                    THEN(" the computed block count == 1 ") {
                    THEN(" the computed block count == 1 ") {
                        const std::size_t n =
                        const std::size_t n =
                                chnk_count_for_offset(offset, size, block_size);
                                block_count(offset, size, block_size);
                        const std::size_t expected_n = 1;
                        const std::size_t expected_n = 1;
                        REQUIRE(n == expected_n);
                        REQUIRE(n == expected_n);
                    }
                    }
@@ -707,7 +707,7 @@ SCENARIO(" the number of chunks involved in an operation can be computed "
                    THEN(" the computed block count corresponds to the number "
                    THEN(" the computed block count corresponds to the number "
                         "of blocks involved in the operation ") {
                         "of blocks involved in the operation ") {
                        const std::size_t n =
                        const std::size_t n =
                                chnk_count_for_offset(offset, size, block_size);
                                block_count(offset, size, block_size);
                        const std::size_t expected_n =
                        const std::size_t expected_n =
                                (offset + size) / block_size -
                                (offset + size) / block_size -
                                offset / block_size +
                                offset / block_size +
@@ -730,8 +730,7 @@ SCENARIO(" the number of chunks involved in an operation can be computed "
                CAPTURE(offset, size, block_size);
                CAPTURE(offset, size, block_size);


                THEN(" the computed block count == 1 ") {
                THEN(" the computed block count == 1 ") {
                    const std::size_t n =
                    const std::size_t n = block_count(offset, size, block_size);
                            chnk_count_for_offset(offset, size, block_size);
                    const std::size_t expected_n = 0;
                    const std::size_t expected_n = 0;
                    REQUIRE(n == expected_n);
                    REQUIRE(n == expected_n);
                }
                }
@@ -744,8 +743,7 @@ SCENARIO(" the number of chunks involved in an operation can be computed "
                CAPTURE(offset, size, block_size);
                CAPTURE(offset, size, block_size);


                THEN(" the computed block count == M ") {
                THEN(" the computed block count == M ") {
                    const std::size_t n =
                    const std::size_t n = block_count(offset, size, block_size);
                            chnk_count_for_offset(offset, size, block_size);
                    const std::size_t expected_n = m;
                    const std::size_t expected_n = m;
                    REQUIRE(n == expected_n);
                    REQUIRE(n == expected_n);
                }
                }
@@ -760,8 +758,7 @@ SCENARIO(" the number of chunks involved in an operation can be computed "


                THEN(" the computed block count corresponds to the number "
                THEN(" the computed block count corresponds to the number "
                     "of blocks involved in the operation ") {
                     "of blocks involved in the operation ") {
                    const std::size_t n =
                    const std::size_t n = block_count(offset, size, block_size);
                            chnk_count_for_offset(offset, size, block_size);
                    const std::size_t expected_n =
                    const std::size_t expected_n =
                            (offset + size) / block_size - offset / block_size +
                            (offset + size) / block_size - offset / block_size +
                            ((offset + size) % block_size ? 1u : 0);
                            ((offset + size) % block_size ? 1u : 0);
@@ -777,8 +774,7 @@ SCENARIO(" the number of chunks involved in an operation can be computed "


                THEN(" the computed block count corresponds to the number "
                THEN(" the computed block count corresponds to the number "
                     "of blocks involved in the operation ") {
                     "of blocks involved in the operation ") {
                    const std::size_t n =
                    const std::size_t n = block_count(offset, size, block_size);
                            chnk_count_for_offset(offset, size, block_size);
                    const std::size_t expected_n =
                    const std::size_t expected_n =
                            (offset + size) / block_size - offset / block_size +
                            (offset + size) / block_size - offset / block_size +
                            ((offset + size) % block_size ? 1u : 0);
                            ((offset + size) % block_size ? 1u : 0);
@@ -805,7 +801,7 @@ SCENARIO(" the number of chunks involved in an operation can be computed "


                    THEN(" the computed block count == 1 ") {
                    THEN(" the computed block count == 1 ") {
                        const std::size_t n =
                        const std::size_t n =
                                chnk_count_for_offset(offset, size, block_size);
                                block_count(offset, size, block_size);
                        const std::size_t expected_n = 0;
                        const std::size_t expected_n = 0;
                        REQUIRE(n == expected_n);
                        REQUIRE(n == expected_n);
                    }
                    }
@@ -819,7 +815,7 @@ SCENARIO(" the number of chunks involved in an operation can be computed "


                    THEN(" the computed block count == M ") {
                    THEN(" the computed block count == M ") {
                        const std::size_t n =
                        const std::size_t n =
                                chnk_count_for_offset(offset, size, block_size);
                                block_count(offset, size, block_size);
                        const std::size_t expected_n = m;
                        const std::size_t expected_n = m;
                        REQUIRE(n == expected_n);
                        REQUIRE(n == expected_n);
                    }
                    }
@@ -836,7 +832,7 @@ SCENARIO(" the number of chunks involved in an operation can be computed "
                    THEN(" the computed block count corresponds to the number "
                    THEN(" the computed block count corresponds to the number "
                         "of blocks involved in the operation ") {
                         "of blocks involved in the operation ") {
                        const std::size_t n =
                        const std::size_t n =
                                chnk_count_for_offset(offset, size, block_size);
                                block_count(offset, size, block_size);
                        const std::size_t expected_n =
                        const std::size_t expected_n =
                                (offset + size) / block_size -
                                (offset + size) / block_size -
                                offset / block_size +
                                offset / block_size +
@@ -854,7 +850,7 @@ SCENARIO(" the number of chunks involved in an operation can be computed "
                    THEN(" the computed block count corresponds to the number "
                    THEN(" the computed block count corresponds to the number "
                         "of blocks involved in the operation ") {
                         "of blocks involved in the operation ") {
                        const std::size_t n =
                        const std::size_t n =
                                chnk_count_for_offset(offset, size, block_size);
                                block_count(offset, size, block_size);
                        const std::size_t expected_n =
                        const std::size_t expected_n =
                                (offset + size) / block_size -
                                (offset + size) / block_size -
                                offset / block_size +
                                offset / block_size +
@@ -878,7 +874,7 @@ SCENARIO(" the number of chunks involved in an operation can be computed "


                    THEN(" the computed block count == 1 ") {
                    THEN(" the computed block count == 1 ") {
                        const std::size_t n =
                        const std::size_t n =
                                chnk_count_for_offset(offset, size, block_size);
                                block_count(offset, size, block_size);
                        const std::size_t expected_n = 0;
                        const std::size_t expected_n = 0;
                        REQUIRE(n == expected_n);
                        REQUIRE(n == expected_n);
                    }
                    }
@@ -892,7 +888,7 @@ SCENARIO(" the number of chunks involved in an operation can be computed "


                    THEN(" the computed block count == M ") {
                    THEN(" the computed block count == M ") {
                        const std::size_t n =
                        const std::size_t n =
                                chnk_count_for_offset(offset, size, block_size);
                                block_count(offset, size, block_size);
                        const std::size_t expected_n = m;
                        const std::size_t expected_n = m;
                        REQUIRE(n == expected_n);
                        REQUIRE(n == expected_n);
                    }
                    }
@@ -909,7 +905,7 @@ SCENARIO(" the number of chunks involved in an operation can be computed "
                    THEN(" the computed block count corresponds to the number "
                    THEN(" the computed block count corresponds to the number "
                         "of blocks involved in the operation ") {
                         "of blocks involved in the operation ") {
                        const std::size_t n =
                        const std::size_t n =
                                chnk_count_for_offset(offset, size, block_size);
                                block_count(offset, size, block_size);
                        const std::size_t expected_n =
                        const std::size_t expected_n =
                                (offset + size) / block_size -
                                (offset + size) / block_size -
                                offset / block_size +
                                offset / block_size +
@@ -927,7 +923,7 @@ SCENARIO(" the number of chunks involved in an operation can be computed "
                    THEN(" the computed block count corresponds to the number "
                    THEN(" the computed block count corresponds to the number "
                         "of blocks involved in the operation ") {
                         "of blocks involved in the operation ") {
                        const std::size_t n =
                        const std::size_t n =
                                chnk_count_for_offset(offset, size, block_size);
                                block_count(offset, size, block_size);
                        const std::size_t expected_n =
                        const std::size_t expected_n =
                                (offset + size) / block_size -
                                (offset + size) / block_size -
                                offset / block_size +
                                offset / block_size +
@@ -950,7 +946,7 @@ SCENARIO(" the number of chunks involved in an operation can be computed "


                    THEN(" the computed block count == 1 ") {
                    THEN(" the computed block count == 1 ") {
                        const std::size_t n =
                        const std::size_t n =
                                chnk_count_for_offset(offset, size, block_size);
                                block_count(offset, size, block_size);
                        const std::size_t expected_n = 0;
                        const std::size_t expected_n = 0;
                        REQUIRE(n == expected_n);
                        REQUIRE(n == expected_n);
                    }
                    }