Global replace of Nicira Networks.
[sliver-openvswitch.git] / lib / bitmap.c
1 /*
2  * Copyright (c) 2008, 2009, 2011 Nicira, Inc.
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 /* Allocates and returns a bitmap initialized to all-1-bits. */
22 unsigned long *
23 bitmap_allocate1(size_t n_bits)
24 {
25     size_t n_bytes = bitmap_n_bytes(n_bits);
26     size_t n_longs = bitmap_n_longs(n_bits);
27     unsigned long *bitmap;
28
29     /* Allocate and initialize most of the bitmap. */
30     bitmap = xmalloc(n_bytes);
31     memset(bitmap, 0xff, n_bytes);
32
33     /* Ensure that the last "unsigned long" in the bitmap only has as many
34      * 1-bits as there actually should be. */
35     bitmap[n_longs - 1] = (1UL << (n_bits % BITMAP_ULONG_BITS)) - 1;
36
37     return bitmap;
38 }
39
40 /* Sets 'count' consecutive bits in 'bitmap', starting at bit offset 'start',
41  * to 'value'. */
42 void
43 bitmap_set_multiple(unsigned long *bitmap, size_t start, size_t count,
44                     bool value)
45 {
46     for (; count && start % BITMAP_ULONG_BITS; count--) {
47         bitmap_set(bitmap, start++, value);
48     }
49     for (; count >= BITMAP_ULONG_BITS; count -= BITMAP_ULONG_BITS) {
50         *bitmap_unit__(bitmap, start) = -(unsigned long) value;
51         start += BITMAP_ULONG_BITS;
52     }
53     for (; count; count--) {
54         bitmap_set(bitmap, start++, value);
55     }
56 }
57
58 /* Compares the 'n' bits in bitmaps 'a' and 'b'.  Returns true if all bits are
59  * equal, false otherwise. */
60 bool
61 bitmap_equal(const unsigned long *a, const unsigned long *b, size_t n)
62 {
63     size_t i;
64
65     if (memcmp(a, b, n / BITMAP_ULONG_BITS * sizeof(unsigned long))) {
66         return false;
67     }
68     for (i = ROUND_DOWN(n, BITMAP_ULONG_BITS); i < n; i++) {
69         if (bitmap_is_set(a, i) != bitmap_is_set(b, i)) {
70             return false;
71         }
72     }
73     return true;
74 }
75
76 /* Scans 'bitmap' from bit offset 'start' to 'end', excluding 'end' itself.
77  * Returns the bit offset of the lowest-numbered bit set to 1, or 'end' if
78  * all of the bits are set to 0. */
79 size_t
80 bitmap_scan(const unsigned long int *bitmap, size_t start, size_t end)
81 {
82     /* XXX slow */
83     size_t i;
84
85     for (i = start; i < end; i++) {
86         if (bitmap_is_set(bitmap, i)) {
87             break;
88         }
89     }
90     return i;
91 }