X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=lib%2Futil.c;h=2402ac6e5a53de28e4702ebdaa347ea81587246c;hb=b86545a659a7146006e229d035f874106303f261;hp=811902afb99bd3f2274fdedbd194db9585b59e42;hpb=ddc4f8e27f231b35df0c73a16bacc17396fb00f3;p=openvswitch diff --git a/lib/util.c b/lib/util.c index 811902af..2402ac6e 100644 --- a/lib/util.c +++ b/lib/util.c @@ -296,7 +296,7 @@ set_program_name__(const char *argv0, const char *date, const char *time) program_name = slash ? slash + 1 : argv0; free(program_version); - program_version = xasprintf("%s (Open vSwitch) "VERSION BUILDNR"\n" + program_version = xasprintf("%s (Open vSwitch) "VERSION"\n" "Compiled %s %s\n", program_name, date, time); } @@ -803,6 +803,54 @@ bitwise_copy(const void *src_, unsigned int src_len, unsigned int src_ofs, } } +/* Zeros the 'n_bits' bits starting from bit 'dst_ofs' in 'dst'. 'dst' is + * 'dst_len' bytes long. + * + * If you consider all of 'dst' to be a single unsigned integer in network byte + * order, then bit N is the bit with value 2**N. That is, bit 0 is the bit + * with value 1 in dst[dst_len - 1], bit 1 is the bit with value 2, bit 2 is + * the bit with value 4, ..., bit 8 is the bit with value 1 in dst[dst_len - + * 2], and so on. + * + * Required invariant: + * dst_ofs + n_bits <= dst_len * 8 + */ +void +bitwise_zero(void *dst_, unsigned int dst_len, unsigned dst_ofs, + unsigned int n_bits) +{ + uint8_t *dst = dst_; + + if (!n_bits) { + return; + } + + dst += dst_len - (dst_ofs / 8 + 1); + dst_ofs %= 8; + + if (dst_ofs) { + unsigned int chunk = MIN(n_bits, 8 - dst_ofs); + + *dst &= ~(((1 << chunk) - 1) << dst_ofs); + + n_bits -= chunk; + if (!n_bits) { + return; + } + + dst--; + } + + while (n_bits >= 8) { + *dst-- = 0; + n_bits -= 8; + } + + if (n_bits) { + *dst &= ~((1 << n_bits) - 1); + } +} + /* Copies the 'n_bits' low-order bits of 'value' into the 'n_bits' bits * starting at bit 'dst_ofs' in 'dst', which is 'dst_len' bytes long. *