LCOV - code coverage report
Current view: top level - lib - vlan-bitmap.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 16 18 88.9 %
Date: 2016-09-14 01:02:56 Functions: 3 3 100.0 %
Branches: 13 22 59.1 %

           Branch data     Line data    Source code
       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                 :       4725 : vlan_bitmap_from_array(const int64_t *vlans, size_t n_vlans)
      25                 :            : {
      26                 :            :     unsigned long *b;
      27                 :            : 
      28         [ +  + ]:       4725 :     if (!n_vlans) {
      29                 :       4719 :         return NULL;
      30                 :            :     }
      31                 :            : 
      32                 :          6 :     b = bitmap_allocate(4096);
      33         [ -  + ]:          6 :     if (!vlan_bitmap_from_array__(vlans, n_vlans, b)) {
      34                 :          0 :         free(b);
      35                 :          0 :         return NULL;
      36                 :            :     }
      37                 :          6 :     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                 :          6 : 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                 :          6 :     n = 0;
      50         [ +  + ]:         15 :     for (i = 0; i < n_vlans; i++) {
      51                 :          9 :         int64_t vlan = vlans[i];
      52                 :            : 
      53 [ +  - ][ +  - ]:          9 :         if (vlan >= 0 && vlan < 4096 && !bitmap_is_set(b, vlan)) {
                 [ +  - ]
      54                 :          9 :             bitmap_set1(b, vlan);
      55                 :          9 :             n++;
      56                 :            :         }
      57                 :            :     }
      58                 :            : 
      59                 :          6 :     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                 :      36502 : vlan_bitmap_equal(const unsigned long *a, const unsigned long *b)
      68                 :            : {
      69 [ +  + ][ -  + ]:      36502 :     return (!a && !b) || (a && b && bitmap_equal(a, b, 4096));
         [ +  - ][ -  + ]
                 [ #  # ]
      70                 :            : }

Generated by: LCOV version 1.12