Initial commit of vpp code.
[vpp.git] / vnet / vnet / vcgn / cgn_bitmap.h
1 /*
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:
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  * Modifications to this file
17  * Copyright (c) 2006-2009 by cisco Systems, Inc.
18  * All rights reserved.
19  */
20
21 /*
22   Copyright (c) 2001, 2002, 2003, 2005 Eliot Dresselhaus
23
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:
31
32   The above copyright notice and this permission notice shall be
33   included in all copies or substantial portions of the Software.
34
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.
42 */
43
44 #ifndef __CGN_BITMAP_H__
45 #define __CGN_BITMAP_H__
46
47 /* Bitmaps built as vectors of machine words. */
48
49 #include <string.h>
50 #include <vlib/vlib.h>
51 #include <vnet/vnet.h>
52 #include <vppinfra/vec.h>
53 #include <vppinfra/random.h>
54
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)
59
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))
63
64 /* Sets given bit.  Returns old value. */
65 static inline uword
66 cgn_clib_bitmap_set_no_check (uword * a, uword i)
67 {
68   uword i0 = i / BITS (a[0]);
69   uword bit = (uword) 1 << (i % BITS (a[0]));
70   uword ai;
71
72 /*  ASSERT (i0 < vec_len (a)); */
73   ai = a[i0];
74   a[i0] = ai | bit;
75
76   return (ai & bit) != 0;
77 }
78
79 /* Clears given bit.  Returns old value. */
80 static inline 
81 uword cgn_clib_bitmap_clear_no_check (uword * a, uword i)
82 {
83   uword i0 = i / BITS (a[0]);
84   uword bit = (uword) 1 << (i % BITS (a[0]));
85   uword ai;
86
87 /*  ASSERT (i0 < vec_len (a)); */
88   ai = a[i0];
89   a[i0] = ai & ~bit;
90
91   return (ai & bit) != 0;
92 }
93
94 /* Gets num_bits from ai start at start. assume that all bits are
95  * in the same uword.
96  */
97 static inline uword cgn_clib_bitmap_get_bits (uword *ai, u16 start,
98             unsigned char num_bits)
99 {
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);
106   return result;
107 }
108
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,
111             i16 num_bits)
112 {
113   /* Now check if any bits are zero.. if yes, return false */
114   uword bitmask;
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 */
119     do {
120       if(ai[i0] ^ bitmask) return 0;
121       num_bits = num_bits - BITS (ai[0]);
122       i0++;
123     } while (num_bits > 0);
124     return 1;
125   }
126   else {
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));
130   }
131 }
132
133 #endif