Prepare Open vSwitch 1.1.2 release.
[sliver-openvswitch.git] / lib / bitmap.c
1 /*
2  * Copyright (c) 2008, 2009 Nicira Networks.
3  *
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:
7  *
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.
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'. */
23 void
24 bitmap_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. */
41 bool
42 bitmap_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 }
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. */
60 size_t
61 bitmap_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 }