Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In bitmap.h, why does bitmap_zero need memset?

In include/linux/bitmap.h, in the bitmap_zero(), why use memset?

static inline void bitmap_zero(unsigned long *dst, int nbits)
{
        if (small_const_nbits(nbits))
                *dst = 0UL;
        else {
                int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
                memset(dst, 0, len);
        }
}

IS the *det = OUL not enough?

like image 988
Bill Wang Avatar asked Aug 24 '26 08:08

Bill Wang


1 Answers

The definition of small_const_nbits is:

#define small_const_nbits(nbits) \
    (__builtin_constant_p(nbits) && (nbits) <= BITS_PER_LONG)

BITS_PER_LONG is normally 32 or 64, depending on what machine you're on.

So, if you're trying to clear fewer than that many bits, you can certainly do it in a single operation - that's the first half of the if statement. If it's longer than 32 or 64 bits, you need to set multiple words, and that's done by the memset call.

like image 116
Carl Norum Avatar answered Aug 26 '26 23:08

Carl Norum



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!