diff options
author | Peter Xu <peterx@redhat.com> | 2017-08-30 16:32:00 +0800 |
---|---|---|
committer | Juan Quintela <quintela@redhat.com> | 2017-09-22 14:11:25 +0200 |
commit | d7788151a0807d5d2d410e3f8944d8c8a651f8d2 (patch) | |
tree | 3e6ed934ea387d91404551d32c087f504f022d99 /util/bitmap.c | |
parent | fc7deeea26af3d08f45bad85b8bd3fc3d790a090 (diff) |
bitmap: provide to_le/from_le helpers
Provide helpers to convert bitmaps to little endian format. It can be
used when we want to send one bitmap via network to some other hosts.
One thing to mention is that, these helpers only solve the problem of
endianess, but it does not solve the problem of different word size on
machines (the bitmaps managing same count of bits may contains different
size when malloced). So we need to take care of the size alignment issue
on the callers for now.
Signed-off-by: Peter Xu <peterx@redhat.com>
Reviewed-by: Juan Quintela <quintela@redhat.com>
Signed-off-by: Juan Quintela <quintela@redhat.com>
Diffstat (limited to 'util/bitmap.c')
-rw-r--r-- | util/bitmap.c | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/util/bitmap.c b/util/bitmap.c index 90a42ff625..cb618c65a5 100644 --- a/util/bitmap.c +++ b/util/bitmap.c @@ -370,3 +370,35 @@ long slow_bitmap_count_one(const unsigned long *bitmap, long nbits) return result; } + +static void bitmap_to_from_le(unsigned long *dst, + const unsigned long *src, long nbits) +{ + long len = BITS_TO_LONGS(nbits); + +#ifdef HOST_WORDS_BIGENDIAN + long index; + + for (index = 0; index < len; index++) { +# if HOST_LONG_BITS == 64 + dst[index] = bswap64(src[index]); +# else + dst[index] = bswap32(src[index]); +# endif + } +#else + memcpy(dst, src, len * sizeof(unsigned long)); +#endif +} + +void bitmap_from_le(unsigned long *dst, const unsigned long *src, + long nbits) +{ + bitmap_to_from_le(dst, src, nbits); +} + +void bitmap_to_le(unsigned long *dst, const unsigned long *src, + long nbits) +{ + bitmap_to_from_le(dst, src, nbits); +} |