]> git.proxmox.com Git - ovs.git/blame - lib/bitmap.c
ovs-ofctl: Fix small typo about nw_tos in man page.
[ovs.git] / lib / bitmap.c
CommitLineData
064af421 1/*
7cc48aed 2 * Copyright (c) 2008, 2009 Nicira Networks.
064af421 3 *
a14bc59f
BP
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
064af421 7 *
a14bc59f
BP
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
064af421
BP
15 */
16
17#include <config.h>
18#include "bitmap.h"
19#include <string.h>
20
21/* Sets 'count' consecutive bits in 'bitmap', starting at bit offset 'start',
22 * to 'value'. */
23void
24bitmap_set_multiple(unsigned long *bitmap, size_t start, size_t count,
25 bool value)
26{
27 for (; count && start % BITMAP_ULONG_BITS; count--) {
28 bitmap_set(bitmap, start++, value);
29 }
30 for (; count >= BITMAP_ULONG_BITS; count -= BITMAP_ULONG_BITS) {
31 *bitmap_unit__(bitmap, start) = -(unsigned long) value;
32 start += BITMAP_ULONG_BITS;
33 }
34 for (; count; count--) {
35 bitmap_set(bitmap, start++, value);
36 }
37}
38
39/* Compares the 'n' bits in bitmaps 'a' and 'b'. Returns true if all bits are
40 * equal, false otherwise. */
41bool
42bitmap_equal(const unsigned long *a, const unsigned long *b, size_t n)
43{
44 size_t i;
45
46 if (memcmp(a, b, n / BITMAP_ULONG_BITS * sizeof(unsigned long))) {
47 return false;
48 }
49 for (i = ROUND_DOWN(n, BITMAP_ULONG_BITS); i < n; i++) {
50 if (bitmap_is_set(a, i) != bitmap_is_set(b, i)) {
51 return false;
52 }
53 }
54 return true;
55}
7cc48aed
BP
56
57/* Scans 'bitmap' from bit offset 'start' to 'end', excluding 'end' itself.
58 * Returns the bit offset of the lowest-numbered bit set to 1, or 'end' if
59 * all of the bits are set to 0. */
60size_t
61bitmap_scan(const unsigned long int *bitmap, size_t start, size_t end)
62{
63 /* XXX slow */
64 size_t i;
65
66 for (i = start; i < end; i++) {
67 if (bitmap_is_set(bitmap, i)) {
68 break;
69 }
70 }
71 return i;
72}