From c86a325285f66a5a43a4097a373c890ad4703e87 Mon Sep 17 00:00:00 2001 From: Hiroki Shirokura Date: Thu, 30 Dec 2021 13:13:37 +0000 Subject: [PATCH] lib: add copy function for bitfield_t MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add a function to copy a bitfield_t structure. Add a ‘void *’ to ‘word_t *’ converstion in bf_init() to avoid the following error: > ./lib/bitfield.h: In function ‘bf_copy’: > ./lib/bitfield.h:75:12: error: request for implicit conversion from ‘void *’ to ‘word_t *’ {aka ‘unsigned int *’} not permitted in C++ [-Werror=c++-compat] > (v).data = XCALLOC(MTYPE_BITFIELD, ((v).m * sizeof(word_t))); \ > ^ > ./lib/bitfield.h:278:2: note: in expansion of macro ‘bf_init’ > bf_init(dst, WORD_SIZE * (src.m - 1)); > ^~~~~~~ Signed-off-by: Hiroki Shirokura Signed-off-by: Louis Scalbert --- lib/bitfield.h | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/bitfield.h b/lib/bitfield.h index 9af4053daf..79f6f205c3 100644 --- a/lib/bitfield.h +++ b/lib/bitfield.h @@ -72,7 +72,8 @@ DECLARE_MTYPE(BITFIELD); do { \ (v).n = 0; \ (v).m = ((N) / WORD_SIZE + 1); \ - (v).data = XCALLOC(MTYPE_BITFIELD, ((v).m * sizeof(word_t))); \ + (v).data = (word_t *)XCALLOC(MTYPE_BITFIELD, \ + ((v).m * sizeof(word_t))); \ } while (0) /** @@ -268,6 +269,19 @@ static inline unsigned int bf_find_next_set_bit(bitfield_t v, (v).data = NULL; \ } while (0) +static inline bitfield_t bf_copy(bitfield_t src) +{ + bitfield_t dst; + + assert(bf_is_inited(src)); + bf_init(dst, WORD_SIZE * (src.m - 1)); + for (size_t i = 0; i < src.m; i++) + dst.data[i] = src.data[i]; + dst.n = src.n; + return dst; +} + + #ifdef __cplusplus } #endif