2 * Copyright (c) 2015 Cisco and/or its affiliates.
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.
16 * Modifications to this file
17 * Copyright (c) 2006-2009 by cisco Systems, Inc.
18 * All rights reserved.
22 Copyright (c) 2001, 2002, 2003, 2005 Eliot Dresselhaus
24 Permission is hereby granted, free of charge, to any person obtaining
25 a copy of this software and associated documentation files (the
26 "Software"), to deal in the Software without restriction, including
27 without limitation the rights to use, copy, modify, merge, publish,
28 distribute, sublicense, and/or sell copies of the Software, and to
29 permit persons to whom the Software is furnished to do so, subject to
30 the following conditions:
32 The above copyright notice and this permission notice shall be
33 included in all copies or substantial portions of the Software.
35 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
36 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
37 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
38 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
39 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
40 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
41 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
44 #ifndef __CGN_BITMAP_H__
45 #define __CGN_BITMAP_H__
47 /* Bitmaps built as vectors of machine words. */
50 #include <vlib/vlib.h>
51 #include <vnet/vnet.h>
52 #include <vppinfra/vec.h>
53 #include <vppinfra/random.h>
55 #define clib_bitmap_dup(v) vec_dup(v)
56 #define clib_bitmap_free(v) vec_free(v)
57 #define clib_bitmap_bytes(v) vec_bytes(v)
58 #define clib_bitmap_zero(v) vec_zero(v)
60 /* Allocate bitmap with given number of bits. */
61 #define clib_bitmap_alloc(v,n_bits) \
62 v = vec_new (uword, ((n_bits) + BITS (uword) - 1) / BITS (uword))
64 /* Sets given bit. Returns old value. */
66 cgn_clib_bitmap_set_no_check (uword * a, uword i)
68 uword i0 = i / BITS (a[0]);
69 uword bit = (uword) 1 << (i % BITS (a[0]));
72 /* ASSERT (i0 < vec_len (a)); */
76 return (ai & bit) != 0;
79 /* Clears given bit. Returns old value. */
81 uword cgn_clib_bitmap_clear_no_check (uword * a, uword i)
83 uword i0 = i / BITS (a[0]);
84 uword bit = (uword) 1 << (i % BITS (a[0]));
87 /* ASSERT (i0 < vec_len (a)); */
91 return (ai & bit) != 0;
94 /* Gets num_bits from ai start at start. assume that all bits are
97 static inline uword cgn_clib_bitmap_get_bits (uword *ai, u16 start,
98 unsigned char num_bits)
100 uword i0 = start / BITS (ai[0]);
101 uword i1 = start % BITS (ai[0]);
102 uword result = ai[i0] >> i1;
103 if(num_bits >= BITS(ai[0])) return result;
104 /* Else, we have to trim the bits */
105 result = result & (((uword)1 << num_bits) - 1);
109 /* Check if all of the bits from start to numb_bits are avaiable */
110 static inline uword cgn_clib_bitmap_check_if_all (uword *ai, u16 start,
113 /* Now check if any bits are zero.. if yes, return false */
115 if(num_bits >= BITS(ai[0])) {
116 /* assume that its going to be multiples of BUTS(ai[0]) */
117 uword i0 = start / BITS (ai[0]);
118 bitmask = ~0; /* set all bits to 1 */
120 if(ai[i0] ^ bitmask) return 0;
121 num_bits = num_bits - BITS (ai[0]);
123 } while (num_bits > 0);
127 uword result = cgn_clib_bitmap_get_bits (ai, start, num_bits);
128 bitmask = ((uword)1 << num_bits) -1; /* set only num_bits */
129 return (!(result ^ bitmask));