1 /* Copyright (c) 2011 Nicira, Inc.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
18 #include "vlan-bitmap.h"
20 /* Allocates and returns a new 4096-bit bitmap that has 1-bit in positions in
21 * the 'n_vlans' bits indicated in 'vlans' and 0-bits everywhere else. Returns
22 * a null pointer if there are no (valid) VLANs in 'vlans'. */
24 vlan_bitmap_from_array(const int64_t *vlans, size_t n_vlans)
32 b = bitmap_allocate(4096);
33 if (!vlan_bitmap_from_array__(vlans, n_vlans, b)) {
40 /* Adds to 4096-bit VLAN bitmap 'b' a 1-bit in each position in the 'n_vlans'
41 * bits indicated in 'vlans'. Returns the number of 1-bits added to 'b'. */
43 vlan_bitmap_from_array__(const int64_t *vlans, size_t n_vlans,
50 for (i = 0; i < n_vlans; i++) {
51 int64_t vlan = vlans[i];
53 if (vlan >= 0 && vlan < 4096 && !bitmap_is_set(b, vlan)) {
62 /* Returns true if 'a' and 'b' are the same: either both null or both the same
65 * (We assume that a nonnull bitmap is not all 0-bits.) */
67 vlan_bitmap_equal(const unsigned long *a, const unsigned long *b)
69 return (!a && !b) || (a && b && bitmap_equal(a, b, 4096));