From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Subject: Re: [tarantool-patches] Re: [PATCH v1 3/4] box: introduce bitmap_majority_test routine References: <20181227185906.redzzw3hbugxasb6@esperanza> <220114e4-6639-fbe0-e5ff-0c1e1b9705fb@tarantool.org> <20181229131953.z6mkaif2p5zwou2g@esperanza> From: Kirill Shcherbatov Message-ID: <25613688-02dd-7fa8-c200-1b3a9be20972@tarantool.org> Date: Sat, 29 Dec 2018 16:57:00 +0300 MIME-Version: 1.0 In-Reply-To: <20181229131953.z6mkaif2p5zwou2g@esperanza> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit To: tarantool-patches@freelists.org, Vladimir Davydov List-ID: Hi! Ok... Also changed the only place in following patch. =============================== A new bitmap_size routine returns size of bitmap allocation aligned by sizeof(unsigned long) words by count of bits to work with. Memory chunk must be aligned as bit_set/bit_clear functions use unsigned long words to setup bits. We need bitmap_size to make an allocation for format:fields to test compatibility of required fields bitmap with another one that is built for parsed tuple on tuple_init_field_map. Needed for #1012 --- src/lib/bit/bit.h | 13 +++++++++++++ test/unit/CMakeLists.txt | 2 +- test/unit/bit.c | 27 +++++++++++++++++++++++++++ test/unit/bit.result | 8 ++++++++ 4 files changed, 49 insertions(+), 1 deletion(-) diff --git a/src/lib/bit/bit.h b/src/lib/bit/bit.h index 370a0cc5d..04be818b2 100644 --- a/src/lib/bit/bit.h +++ b/src/lib/bit/bit.h @@ -238,6 +238,19 @@ bit_clear(void *data, size_t pos) return prev; } +/** + * Return sizeof(unsigned long)-words aligned size of bitmap by + * bit_count - count of bits to set. Size must be aligned, as + * bit_sit/bit_clear operations use unsigned long words to setup + * bit. + */ +static inline size_t +bitmap_size(size_t bit_count) +{ + size_t word_count = DIV_ROUND_UP(bit_count, CHAR_BIT * sizeof(long)); + return word_count * sizeof(long); +} + /** * @cond false * @brief Naive implementation of ctz. diff --git a/test/unit/CMakeLists.txt b/test/unit/CMakeLists.txt index 0025d3611..5b2f152e3 100644 --- a/test/unit/CMakeLists.txt +++ b/test/unit/CMakeLists.txt @@ -36,7 +36,7 @@ add_executable(rope.test rope.c) target_link_libraries(rope.test salad) add_executable(int96.test int96.cc) add_executable(bit.test bit.c bit.c) -target_link_libraries(bit.test bit) +target_link_libraries(bit.test bit unit) add_executable(bitset_basic.test bitset_basic.c) target_link_libraries(bitset_basic.test bitset) add_executable(bitset_iterator.test bitset_iterator.c) diff --git a/test/unit/bit.c b/test/unit/bit.c index beb89a7e4..67a7ccd63 100644 --- a/test/unit/bit.c +++ b/test/unit/bit.c @@ -206,6 +206,32 @@ test_bit_iter_empty(void) footer(); } +static void +test_bitmap(void) +{ + header(); + plan(5); + + size_t rc = bitmap_size(0); + is(rc, 0, "empty bitmap: have %zu expected %d", rc, 0); + rc = bitmap_size(1); + is(rc, sizeof(unsigned long), "1-item bitmap: have %zu expected %lu", + rc, sizeof(unsigned long)); + rc = bitmap_size(4); + is(rc, sizeof(unsigned long), "4-items bitmap: have %zu expected %lu", + rc, sizeof(unsigned long)); + rc = bitmap_size(CHAR_BIT * sizeof(unsigned long)); + is(rc, sizeof(unsigned long), "%lu-items bitmap: have %zu expected %lu", + CHAR_BIT * sizeof(unsigned long), rc, sizeof(unsigned long)); + rc = bitmap_size(CHAR_BIT * sizeof(unsigned long) + 1); + is(rc, 2 * sizeof(unsigned long), + "%lu-items bitmap: have %zu expected %lu", + CHAR_BIT * sizeof(unsigned long) + 1, rc, 2 * sizeof(unsigned long)); + + check_plan(); + footer(); +} + int main(void) { @@ -216,4 +242,5 @@ main(void) test_index(); test_bit_iter(); test_bit_iter_empty(); + test_bitmap(); } diff --git a/test/unit/bit.result b/test/unit/bit.result index e2c5601f3..d99cfbf01 100644 --- a/test/unit/bit.result +++ b/test/unit/bit.result @@ -891,3 +891,11 @@ Clear: 0, 1, 2, 4, 5, 6, 7, 8, 10, 12, 13, 14, 15, 19, 20, 21, 23, 26, 28, 30, 3 *** test_bit_iter: done *** *** test_bit_iter_empty *** *** test_bit_iter_empty: done *** + *** test_bitmap *** +1..5 +ok 1 - empty bitmap: have 0 expected 0 +ok 2 - 1-item bitmap: have 8 expected 8 +ok 3 - 4-items bitmap: have 8 expected 8 +ok 4 - 64-items bitmap: have 8 expected 8 +ok 5 - 65-items bitmap: have 16 expected 16 + *** test_bitmap: done *** -- 2.19.2