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