netdev-dpdk: fix mbuf leaks
[cascardo/ovs.git] / lib / vlan-bitmap.c
1 /* Copyright (c) 2011 Nicira, Inc.
2  *
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:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15
16 #include <config.h>
17
18 #include "vlan-bitmap.h"
19
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'. */
23 unsigned long *
24 vlan_bitmap_from_array(const int64_t *vlans, size_t n_vlans)
25 {
26     unsigned long *b;
27
28     if (!n_vlans) {
29         return NULL;
30     }
31
32     b = bitmap_allocate(4096);
33     if (!vlan_bitmap_from_array__(vlans, n_vlans, b)) {
34         free(b);
35         return NULL;
36     }
37     return b;
38 }
39
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'. */
42 int
43 vlan_bitmap_from_array__(const int64_t *vlans, size_t n_vlans,
44                          unsigned long int *b)
45 {
46     size_t i;
47     int n;
48
49     n = 0;
50     for (i = 0; i < n_vlans; i++) {
51         int64_t vlan = vlans[i];
52
53         if (vlan >= 0 && vlan < 4096 && !bitmap_is_set(b, vlan)) {
54             bitmap_set1(b, vlan);
55             n++;
56         }
57     }
58
59     return n;
60 }
61
62 /* Returns true if 'a' and 'b' are the same: either both null or both the same
63  * 4096-bit bitmap.
64  *
65  * (We assume that a nonnull bitmap is not all 0-bits.) */
66 bool
67 vlan_bitmap_equal(const unsigned long *a, const unsigned long *b)
68 {
69     return (!a && !b) || (a && b && bitmap_equal(a, b, 4096));
70 }