xref: /dpdk/drivers/event/dlb2/pf/base/dlb2_osdep_bitmap.h (revision 5433956d51859fa8a9d4d76e2877ee20c8fc6002)
1*5433956dSTimothy McDaniel /* SPDX-License-Identifier: BSD-3-Clause
2*5433956dSTimothy McDaniel  * Copyright(c) 2016-2020 Intel Corporation
3*5433956dSTimothy McDaniel  */
4*5433956dSTimothy McDaniel 
5*5433956dSTimothy McDaniel #ifndef __DLB2_OSDEP_BITMAP_H
6*5433956dSTimothy McDaniel #define __DLB2_OSDEP_BITMAP_H
7*5433956dSTimothy McDaniel 
8*5433956dSTimothy McDaniel #include <stdint.h>
9*5433956dSTimothy McDaniel #include <stdbool.h>
10*5433956dSTimothy McDaniel #include <stdio.h>
11*5433956dSTimothy McDaniel #include <unistd.h>
12*5433956dSTimothy McDaniel #include <rte_bitmap.h>
13*5433956dSTimothy McDaniel #include <rte_string_fns.h>
14*5433956dSTimothy McDaniel #include <rte_malloc.h>
15*5433956dSTimothy McDaniel #include <rte_errno.h>
16*5433956dSTimothy McDaniel #include "../dlb2_main.h"
17*5433956dSTimothy McDaniel 
18*5433956dSTimothy McDaniel /*************************/
19*5433956dSTimothy McDaniel /*** Bitmap operations ***/
20*5433956dSTimothy McDaniel /*************************/
21*5433956dSTimothy McDaniel struct dlb2_bitmap {
22*5433956dSTimothy McDaniel 	struct rte_bitmap *map;
23*5433956dSTimothy McDaniel 	unsigned int len;
24*5433956dSTimothy McDaniel };
25*5433956dSTimothy McDaniel 
26*5433956dSTimothy McDaniel /**
27*5433956dSTimothy McDaniel  * dlb2_bitmap_alloc() - alloc a bitmap data structure
28*5433956dSTimothy McDaniel  * @bitmap: pointer to dlb2_bitmap structure pointer.
29*5433956dSTimothy McDaniel  * @len: number of entries in the bitmap.
30*5433956dSTimothy McDaniel  *
31*5433956dSTimothy McDaniel  * This function allocates a bitmap and initializes it with length @len. All
32*5433956dSTimothy McDaniel  * entries are initially zero.
33*5433956dSTimothy McDaniel  *
34*5433956dSTimothy McDaniel  * Return:
35*5433956dSTimothy McDaniel  * Returns 0 upon success, < 0 otherwise.
36*5433956dSTimothy McDaniel  *
37*5433956dSTimothy McDaniel  * Errors:
38*5433956dSTimothy McDaniel  * EINVAL - bitmap is NULL or len is 0.
39*5433956dSTimothy McDaniel  * ENOMEM - could not allocate memory for the bitmap data structure.
40*5433956dSTimothy McDaniel  */
dlb2_bitmap_alloc(struct dlb2_bitmap ** bitmap,unsigned int len)41*5433956dSTimothy McDaniel static inline int dlb2_bitmap_alloc(struct dlb2_bitmap **bitmap,
42*5433956dSTimothy McDaniel 				    unsigned int len)
43*5433956dSTimothy McDaniel {
44*5433956dSTimothy McDaniel 	struct dlb2_bitmap *bm;
45*5433956dSTimothy McDaniel 	void *mem;
46*5433956dSTimothy McDaniel 	uint32_t alloc_size;
47*5433956dSTimothy McDaniel 	uint32_t nbits = (uint32_t)len;
48*5433956dSTimothy McDaniel 
49*5433956dSTimothy McDaniel 	if (bitmap == NULL || nbits == 0)
50*5433956dSTimothy McDaniel 		return -EINVAL;
51*5433956dSTimothy McDaniel 
52*5433956dSTimothy McDaniel 	/* Allocate DLB2 bitmap control struct */
53*5433956dSTimothy McDaniel 	bm = rte_malloc("DLB2_PF",
54*5433956dSTimothy McDaniel 			sizeof(struct dlb2_bitmap),
55*5433956dSTimothy McDaniel 			RTE_CACHE_LINE_SIZE);
56*5433956dSTimothy McDaniel 
57*5433956dSTimothy McDaniel 	if (bm == NULL)
58*5433956dSTimothy McDaniel 		return -ENOMEM;
59*5433956dSTimothy McDaniel 
60*5433956dSTimothy McDaniel 	/* Allocate bitmap memory */
61*5433956dSTimothy McDaniel 	alloc_size = rte_bitmap_get_memory_footprint(nbits);
62*5433956dSTimothy McDaniel 	mem = rte_malloc("DLB2_PF_BITMAP", alloc_size, RTE_CACHE_LINE_SIZE);
63*5433956dSTimothy McDaniel 	if (mem == NULL) {
64*5433956dSTimothy McDaniel 		rte_free(bm);
65*5433956dSTimothy McDaniel 		return -ENOMEM;
66*5433956dSTimothy McDaniel 	}
67*5433956dSTimothy McDaniel 
68*5433956dSTimothy McDaniel 	bm->map = rte_bitmap_init(len, mem, alloc_size);
69*5433956dSTimothy McDaniel 	if (bm->map == NULL) {
70*5433956dSTimothy McDaniel 		rte_free(mem);
71*5433956dSTimothy McDaniel 		rte_free(bm);
72*5433956dSTimothy McDaniel 		return -ENOMEM;
73*5433956dSTimothy McDaniel 	}
74*5433956dSTimothy McDaniel 
75*5433956dSTimothy McDaniel 	bm->len = len;
76*5433956dSTimothy McDaniel 
77*5433956dSTimothy McDaniel 	*bitmap = bm;
78*5433956dSTimothy McDaniel 
79*5433956dSTimothy McDaniel 	return 0;
80*5433956dSTimothy McDaniel }
81*5433956dSTimothy McDaniel 
82*5433956dSTimothy McDaniel /**
83*5433956dSTimothy McDaniel  * dlb2_bitmap_free() - free a previously allocated bitmap data structure
84*5433956dSTimothy McDaniel  * @bitmap: pointer to dlb2_bitmap structure.
85*5433956dSTimothy McDaniel  *
86*5433956dSTimothy McDaniel  * This function frees a bitmap that was allocated with dlb2_bitmap_alloc().
87*5433956dSTimothy McDaniel  */
dlb2_bitmap_free(struct dlb2_bitmap * bitmap)88*5433956dSTimothy McDaniel static inline void dlb2_bitmap_free(struct dlb2_bitmap *bitmap)
89*5433956dSTimothy McDaniel {
90*5433956dSTimothy McDaniel 	if (bitmap == NULL)
91*5433956dSTimothy McDaniel 		return;
92*5433956dSTimothy McDaniel 
93*5433956dSTimothy McDaniel 	rte_free(bitmap->map);
94*5433956dSTimothy McDaniel 	rte_free(bitmap);
95*5433956dSTimothy McDaniel }
96*5433956dSTimothy McDaniel 
97*5433956dSTimothy McDaniel /**
98*5433956dSTimothy McDaniel  * dlb2_bitmap_fill() - fill a bitmap with all 1s
99*5433956dSTimothy McDaniel  * @bitmap: pointer to dlb2_bitmap structure.
100*5433956dSTimothy McDaniel  *
101*5433956dSTimothy McDaniel  * This function sets all bitmap values to 1.
102*5433956dSTimothy McDaniel  *
103*5433956dSTimothy McDaniel  * Return:
104*5433956dSTimothy McDaniel  * Returns 0 upon success, < 0 otherwise.
105*5433956dSTimothy McDaniel  *
106*5433956dSTimothy McDaniel  * Errors:
107*5433956dSTimothy McDaniel  * EINVAL - bitmap is NULL or is uninitialized.
108*5433956dSTimothy McDaniel  */
dlb2_bitmap_fill(struct dlb2_bitmap * bitmap)109*5433956dSTimothy McDaniel static inline int dlb2_bitmap_fill(struct dlb2_bitmap *bitmap)
110*5433956dSTimothy McDaniel {
111*5433956dSTimothy McDaniel 	unsigned int i;
112*5433956dSTimothy McDaniel 
113*5433956dSTimothy McDaniel 	if (bitmap  == NULL || bitmap->map == NULL)
114*5433956dSTimothy McDaniel 		return -EINVAL;
115*5433956dSTimothy McDaniel 
116*5433956dSTimothy McDaniel 	for (i = 0; i != bitmap->len; i++)
117*5433956dSTimothy McDaniel 		rte_bitmap_set(bitmap->map, i);
118*5433956dSTimothy McDaniel 
119*5433956dSTimothy McDaniel 	return 0;
120*5433956dSTimothy McDaniel }
121*5433956dSTimothy McDaniel 
122*5433956dSTimothy McDaniel /**
123*5433956dSTimothy McDaniel  * dlb2_bitmap_fill() - fill a bitmap with all 0s
124*5433956dSTimothy McDaniel  * @bitmap: pointer to dlb2_bitmap structure.
125*5433956dSTimothy McDaniel  *
126*5433956dSTimothy McDaniel  * This function sets all bitmap values to 0.
127*5433956dSTimothy McDaniel  *
128*5433956dSTimothy McDaniel  * Return:
129*5433956dSTimothy McDaniel  * Returns 0 upon success, < 0 otherwise.
130*5433956dSTimothy McDaniel  *
131*5433956dSTimothy McDaniel  * Errors:
132*5433956dSTimothy McDaniel  * EINVAL - bitmap is NULL or is uninitialized.
133*5433956dSTimothy McDaniel  */
dlb2_bitmap_zero(struct dlb2_bitmap * bitmap)134*5433956dSTimothy McDaniel static inline int dlb2_bitmap_zero(struct dlb2_bitmap *bitmap)
135*5433956dSTimothy McDaniel {
136*5433956dSTimothy McDaniel 	if (bitmap  == NULL || bitmap->map == NULL)
137*5433956dSTimothy McDaniel 		return -EINVAL;
138*5433956dSTimothy McDaniel 
139*5433956dSTimothy McDaniel 	rte_bitmap_reset(bitmap->map);
140*5433956dSTimothy McDaniel 
141*5433956dSTimothy McDaniel 	return 0;
142*5433956dSTimothy McDaniel }
143*5433956dSTimothy McDaniel 
144*5433956dSTimothy McDaniel /**
145*5433956dSTimothy McDaniel  * dlb2_bitmap_set() - set a bitmap entry
146*5433956dSTimothy McDaniel  * @bitmap: pointer to dlb2_bitmap structure.
147*5433956dSTimothy McDaniel  * @bit: bit index.
148*5433956dSTimothy McDaniel  *
149*5433956dSTimothy McDaniel  * Return:
150*5433956dSTimothy McDaniel  * Returns 0 upon success, < 0 otherwise.
151*5433956dSTimothy McDaniel  *
152*5433956dSTimothy McDaniel  * Errors:
153*5433956dSTimothy McDaniel  * EINVAL - bitmap is NULL or is uninitialized, or bit is larger than the
154*5433956dSTimothy McDaniel  *	    bitmap length.
155*5433956dSTimothy McDaniel  */
dlb2_bitmap_set(struct dlb2_bitmap * bitmap,unsigned int bit)156*5433956dSTimothy McDaniel static inline int dlb2_bitmap_set(struct dlb2_bitmap *bitmap,
157*5433956dSTimothy McDaniel 				  unsigned int bit)
158*5433956dSTimothy McDaniel {
159*5433956dSTimothy McDaniel 	if (bitmap  == NULL || bitmap->map == NULL)
160*5433956dSTimothy McDaniel 		return -EINVAL;
161*5433956dSTimothy McDaniel 
162*5433956dSTimothy McDaniel 	if (bitmap->len <= bit)
163*5433956dSTimothy McDaniel 		return -EINVAL;
164*5433956dSTimothy McDaniel 
165*5433956dSTimothy McDaniel 	rte_bitmap_set(bitmap->map, bit);
166*5433956dSTimothy McDaniel 
167*5433956dSTimothy McDaniel 	return 0;
168*5433956dSTimothy McDaniel }
169*5433956dSTimothy McDaniel 
170*5433956dSTimothy McDaniel /**
171*5433956dSTimothy McDaniel  * dlb2_bitmap_set_range() - set a range of bitmap entries
172*5433956dSTimothy McDaniel  * @bitmap: pointer to dlb2_bitmap structure.
173*5433956dSTimothy McDaniel  * @bit: starting bit index.
174*5433956dSTimothy McDaniel  * @len: length of the range.
175*5433956dSTimothy McDaniel  *
176*5433956dSTimothy McDaniel  * Return:
177*5433956dSTimothy McDaniel  * Returns 0 upon success, < 0 otherwise.
178*5433956dSTimothy McDaniel  *
179*5433956dSTimothy McDaniel  * Errors:
180*5433956dSTimothy McDaniel  * EINVAL - bitmap is NULL or is uninitialized, or the range exceeds the bitmap
181*5433956dSTimothy McDaniel  *	    length.
182*5433956dSTimothy McDaniel  */
dlb2_bitmap_set_range(struct dlb2_bitmap * bitmap,unsigned int bit,unsigned int len)183*5433956dSTimothy McDaniel static inline int dlb2_bitmap_set_range(struct dlb2_bitmap *bitmap,
184*5433956dSTimothy McDaniel 					unsigned int bit,
185*5433956dSTimothy McDaniel 					unsigned int len)
186*5433956dSTimothy McDaniel {
187*5433956dSTimothy McDaniel 	unsigned int i;
188*5433956dSTimothy McDaniel 
189*5433956dSTimothy McDaniel 	if (bitmap  == NULL || bitmap->map == NULL)
190*5433956dSTimothy McDaniel 		return -EINVAL;
191*5433956dSTimothy McDaniel 
192*5433956dSTimothy McDaniel 	if (bitmap->len <= bit)
193*5433956dSTimothy McDaniel 		return -EINVAL;
194*5433956dSTimothy McDaniel 
195*5433956dSTimothy McDaniel 	for (i = 0; i != len; i++)
196*5433956dSTimothy McDaniel 		rte_bitmap_set(bitmap->map, bit + i);
197*5433956dSTimothy McDaniel 
198*5433956dSTimothy McDaniel 	return 0;
199*5433956dSTimothy McDaniel }
200*5433956dSTimothy McDaniel 
201*5433956dSTimothy McDaniel /**
202*5433956dSTimothy McDaniel  * dlb2_bitmap_clear() - clear a bitmap entry
203*5433956dSTimothy McDaniel  * @bitmap: pointer to dlb2_bitmap structure.
204*5433956dSTimothy McDaniel  * @bit: bit index.
205*5433956dSTimothy McDaniel  *
206*5433956dSTimothy McDaniel  * Return:
207*5433956dSTimothy McDaniel  * Returns 0 upon success, < 0 otherwise.
208*5433956dSTimothy McDaniel  *
209*5433956dSTimothy McDaniel  * Errors:
210*5433956dSTimothy McDaniel  * EINVAL - bitmap is NULL or is uninitialized, or bit is larger than the
211*5433956dSTimothy McDaniel  *	    bitmap length.
212*5433956dSTimothy McDaniel  */
dlb2_bitmap_clear(struct dlb2_bitmap * bitmap,unsigned int bit)213*5433956dSTimothy McDaniel static inline int dlb2_bitmap_clear(struct dlb2_bitmap *bitmap,
214*5433956dSTimothy McDaniel 				    unsigned int bit)
215*5433956dSTimothy McDaniel {
216*5433956dSTimothy McDaniel 	if (bitmap  == NULL || bitmap->map == NULL)
217*5433956dSTimothy McDaniel 		return -EINVAL;
218*5433956dSTimothy McDaniel 
219*5433956dSTimothy McDaniel 	if (bitmap->len <= bit)
220*5433956dSTimothy McDaniel 		return -EINVAL;
221*5433956dSTimothy McDaniel 
222*5433956dSTimothy McDaniel 	rte_bitmap_clear(bitmap->map, bit);
223*5433956dSTimothy McDaniel 
224*5433956dSTimothy McDaniel 	return 0;
225*5433956dSTimothy McDaniel }
226*5433956dSTimothy McDaniel 
227*5433956dSTimothy McDaniel /**
228*5433956dSTimothy McDaniel  * dlb2_bitmap_clear_range() - clear a range of bitmap entries
229*5433956dSTimothy McDaniel  * @bitmap: pointer to dlb2_bitmap structure.
230*5433956dSTimothy McDaniel  * @bit: starting bit index.
231*5433956dSTimothy McDaniel  * @len: length of the range.
232*5433956dSTimothy McDaniel  *
233*5433956dSTimothy McDaniel  * Return:
234*5433956dSTimothy McDaniel  * Returns 0 upon success, < 0 otherwise.
235*5433956dSTimothy McDaniel  *
236*5433956dSTimothy McDaniel  * Errors:
237*5433956dSTimothy McDaniel  * EINVAL - bitmap is NULL or is uninitialized, or the range exceeds the bitmap
238*5433956dSTimothy McDaniel  *	    length.
239*5433956dSTimothy McDaniel  */
dlb2_bitmap_clear_range(struct dlb2_bitmap * bitmap,unsigned int bit,unsigned int len)240*5433956dSTimothy McDaniel static inline int dlb2_bitmap_clear_range(struct dlb2_bitmap *bitmap,
241*5433956dSTimothy McDaniel 					  unsigned int bit,
242*5433956dSTimothy McDaniel 					  unsigned int len)
243*5433956dSTimothy McDaniel {
244*5433956dSTimothy McDaniel 	unsigned int i;
245*5433956dSTimothy McDaniel 
246*5433956dSTimothy McDaniel 	if (bitmap  == NULL || bitmap->map == NULL)
247*5433956dSTimothy McDaniel 		return -EINVAL;
248*5433956dSTimothy McDaniel 
249*5433956dSTimothy McDaniel 	if (bitmap->len <= bit)
250*5433956dSTimothy McDaniel 		return -EINVAL;
251*5433956dSTimothy McDaniel 
252*5433956dSTimothy McDaniel 	for (i = 0; i != len; i++)
253*5433956dSTimothy McDaniel 		rte_bitmap_clear(bitmap->map, bit + i);
254*5433956dSTimothy McDaniel 
255*5433956dSTimothy McDaniel 	return 0;
256*5433956dSTimothy McDaniel }
257*5433956dSTimothy McDaniel 
258*5433956dSTimothy McDaniel /**
259*5433956dSTimothy McDaniel  * dlb2_bitmap_find_set_bit_range() - find an range of set bits
260*5433956dSTimothy McDaniel  * @bitmap: pointer to dlb2_bitmap structure.
261*5433956dSTimothy McDaniel  * @len: length of the range.
262*5433956dSTimothy McDaniel  *
263*5433956dSTimothy McDaniel  * This function looks for a range of set bits of length @len.
264*5433956dSTimothy McDaniel  *
265*5433956dSTimothy McDaniel  * Return:
266*5433956dSTimothy McDaniel  * Returns the base bit index upon success, < 0 otherwise.
267*5433956dSTimothy McDaniel  *
268*5433956dSTimothy McDaniel  * Errors:
269*5433956dSTimothy McDaniel  * ENOENT - unable to find a length *len* range of set bits.
270*5433956dSTimothy McDaniel  * EINVAL - bitmap is NULL or is uninitialized, or len is invalid.
271*5433956dSTimothy McDaniel  */
dlb2_bitmap_find_set_bit_range(struct dlb2_bitmap * bitmap,unsigned int len)272*5433956dSTimothy McDaniel static inline int dlb2_bitmap_find_set_bit_range(struct dlb2_bitmap *bitmap,
273*5433956dSTimothy McDaniel 						 unsigned int len)
274*5433956dSTimothy McDaniel {
275*5433956dSTimothy McDaniel 	unsigned int i, j = 0;
276*5433956dSTimothy McDaniel 
277*5433956dSTimothy McDaniel 	if (bitmap  == NULL || bitmap->map == NULL || len == 0)
278*5433956dSTimothy McDaniel 		return -EINVAL;
279*5433956dSTimothy McDaniel 
280*5433956dSTimothy McDaniel 	if (bitmap->len < len)
281*5433956dSTimothy McDaniel 		return -ENOENT;
282*5433956dSTimothy McDaniel 
283*5433956dSTimothy McDaniel 	for (i = 0; i != bitmap->len; i++) {
284*5433956dSTimothy McDaniel 		if  (rte_bitmap_get(bitmap->map, i)) {
285*5433956dSTimothy McDaniel 			if (++j == len)
286*5433956dSTimothy McDaniel 				return i - j + 1;
287*5433956dSTimothy McDaniel 		} else {
288*5433956dSTimothy McDaniel 			j = 0;
289*5433956dSTimothy McDaniel 		}
290*5433956dSTimothy McDaniel 	}
291*5433956dSTimothy McDaniel 
292*5433956dSTimothy McDaniel 	/* No set bit range of length len? */
293*5433956dSTimothy McDaniel 	return -ENOENT;
294*5433956dSTimothy McDaniel }
295*5433956dSTimothy McDaniel 
296*5433956dSTimothy McDaniel /**
297*5433956dSTimothy McDaniel  * dlb2_bitmap_find_set_bit() - find an range of set bits
298*5433956dSTimothy McDaniel  * @bitmap: pointer to dlb2_bitmap structure.
299*5433956dSTimothy McDaniel  *
300*5433956dSTimothy McDaniel  * This function looks for a single set bit.
301*5433956dSTimothy McDaniel  *
302*5433956dSTimothy McDaniel  * Return:
303*5433956dSTimothy McDaniel  * Returns the base bit index upon success, -1 if not found, <-1 otherwise.
304*5433956dSTimothy McDaniel  *
305*5433956dSTimothy McDaniel  * Errors:
306*5433956dSTimothy McDaniel  * EINVAL - bitmap is NULL or is uninitialized, or len is invalid.
307*5433956dSTimothy McDaniel  */
dlb2_bitmap_find_set_bit(struct dlb2_bitmap * bitmap)308*5433956dSTimothy McDaniel static inline int dlb2_bitmap_find_set_bit(struct dlb2_bitmap *bitmap)
309*5433956dSTimothy McDaniel {
310*5433956dSTimothy McDaniel 	unsigned int i;
311*5433956dSTimothy McDaniel 
312*5433956dSTimothy McDaniel 	if (bitmap == NULL)
313*5433956dSTimothy McDaniel 		return -EINVAL;
314*5433956dSTimothy McDaniel 
315*5433956dSTimothy McDaniel 	if (bitmap->map == NULL)
316*5433956dSTimothy McDaniel 		return -EINVAL;
317*5433956dSTimothy McDaniel 
318*5433956dSTimothy McDaniel 	for (i = 0; i != bitmap->len; i++) {
319*5433956dSTimothy McDaniel 		if  (rte_bitmap_get(bitmap->map, i))
320*5433956dSTimothy McDaniel 			return i;
321*5433956dSTimothy McDaniel 	}
322*5433956dSTimothy McDaniel 
323*5433956dSTimothy McDaniel 	return -ENOENT;
324*5433956dSTimothy McDaniel }
325*5433956dSTimothy McDaniel 
326*5433956dSTimothy McDaniel /**
327*5433956dSTimothy McDaniel  * dlb2_bitmap_count() - returns the number of set bits
328*5433956dSTimothy McDaniel  * @bitmap: pointer to dlb2_bitmap structure.
329*5433956dSTimothy McDaniel  *
330*5433956dSTimothy McDaniel  * This function looks for a single set bit.
331*5433956dSTimothy McDaniel  *
332*5433956dSTimothy McDaniel  * Return:
333*5433956dSTimothy McDaniel  * Returns the number of set bits upon success, <0 otherwise.
334*5433956dSTimothy McDaniel  *
335*5433956dSTimothy McDaniel  * Errors:
336*5433956dSTimothy McDaniel  * EINVAL - bitmap is NULL or is uninitialized.
337*5433956dSTimothy McDaniel  */
dlb2_bitmap_count(struct dlb2_bitmap * bitmap)338*5433956dSTimothy McDaniel static inline int dlb2_bitmap_count(struct dlb2_bitmap *bitmap)
339*5433956dSTimothy McDaniel {
340*5433956dSTimothy McDaniel 	int weight = 0;
341*5433956dSTimothy McDaniel 	unsigned int i;
342*5433956dSTimothy McDaniel 
343*5433956dSTimothy McDaniel 	if (bitmap == NULL)
344*5433956dSTimothy McDaniel 		return -EINVAL;
345*5433956dSTimothy McDaniel 
346*5433956dSTimothy McDaniel 	if (bitmap->map == NULL)
347*5433956dSTimothy McDaniel 		return -EINVAL;
348*5433956dSTimothy McDaniel 
349*5433956dSTimothy McDaniel 	for (i = 0; i != bitmap->len; i++) {
350*5433956dSTimothy McDaniel 		if  (rte_bitmap_get(bitmap->map, i))
351*5433956dSTimothy McDaniel 			weight++;
352*5433956dSTimothy McDaniel 	}
353*5433956dSTimothy McDaniel 	return weight;
354*5433956dSTimothy McDaniel }
355*5433956dSTimothy McDaniel 
356*5433956dSTimothy McDaniel /**
357*5433956dSTimothy McDaniel  * dlb2_bitmap_longest_set_range() - returns longest contiguous range of set
358*5433956dSTimothy McDaniel  *				      bits
359*5433956dSTimothy McDaniel  * @bitmap: pointer to dlb2_bitmap structure.
360*5433956dSTimothy McDaniel  *
361*5433956dSTimothy McDaniel  * Return:
362*5433956dSTimothy McDaniel  * Returns the bitmap's longest contiguous range of set bits upon success,
363*5433956dSTimothy McDaniel  * <0 otherwise.
364*5433956dSTimothy McDaniel  *
365*5433956dSTimothy McDaniel  * Errors:
366*5433956dSTimothy McDaniel  * EINVAL - bitmap is NULL or is uninitialized.
367*5433956dSTimothy McDaniel  */
dlb2_bitmap_longest_set_range(struct dlb2_bitmap * bitmap)368*5433956dSTimothy McDaniel static inline int dlb2_bitmap_longest_set_range(struct dlb2_bitmap *bitmap)
369*5433956dSTimothy McDaniel {
370*5433956dSTimothy McDaniel 	int max_len = 0, len = 0;
371*5433956dSTimothy McDaniel 	unsigned int i;
372*5433956dSTimothy McDaniel 
373*5433956dSTimothy McDaniel 	if (bitmap == NULL)
374*5433956dSTimothy McDaniel 		return -EINVAL;
375*5433956dSTimothy McDaniel 
376*5433956dSTimothy McDaniel 	if (bitmap->map == NULL)
377*5433956dSTimothy McDaniel 		return -EINVAL;
378*5433956dSTimothy McDaniel 
379*5433956dSTimothy McDaniel 	for (i = 0; i != bitmap->len; i++) {
380*5433956dSTimothy McDaniel 		if  (rte_bitmap_get(bitmap->map, i)) {
381*5433956dSTimothy McDaniel 			len++;
382*5433956dSTimothy McDaniel 		} else {
383*5433956dSTimothy McDaniel 			if (len > max_len)
384*5433956dSTimothy McDaniel 				max_len = len;
385*5433956dSTimothy McDaniel 			len = 0;
386*5433956dSTimothy McDaniel 		}
387*5433956dSTimothy McDaniel 	}
388*5433956dSTimothy McDaniel 
389*5433956dSTimothy McDaniel 	if (len > max_len)
390*5433956dSTimothy McDaniel 		max_len = len;
391*5433956dSTimothy McDaniel 
392*5433956dSTimothy McDaniel 	return max_len;
393*5433956dSTimothy McDaniel }
394*5433956dSTimothy McDaniel 
395*5433956dSTimothy McDaniel /**
396*5433956dSTimothy McDaniel  * dlb2_bitmap_or() - store the logical 'or' of two bitmaps into a third
397*5433956dSTimothy McDaniel  * @dest: pointer to dlb2_bitmap structure, which will contain the results of
398*5433956dSTimothy McDaniel  *	  the 'or' of src1 and src2.
399*5433956dSTimothy McDaniel  * @src1: pointer to dlb2_bitmap structure, will be 'or'ed with src2.
400*5433956dSTimothy McDaniel  * @src2: pointer to dlb2_bitmap structure, will be 'or'ed with src1.
401*5433956dSTimothy McDaniel  *
402*5433956dSTimothy McDaniel  * This function 'or's two bitmaps together and stores the result in a third
403*5433956dSTimothy McDaniel  * bitmap. The source and destination bitmaps can be the same.
404*5433956dSTimothy McDaniel  *
405*5433956dSTimothy McDaniel  * Return:
406*5433956dSTimothy McDaniel  * Returns the number of set bits upon success, <0 otherwise.
407*5433956dSTimothy McDaniel  *
408*5433956dSTimothy McDaniel  * Errors:
409*5433956dSTimothy McDaniel  * EINVAL - One of the bitmaps is NULL or is uninitialized.
410*5433956dSTimothy McDaniel  */
dlb2_bitmap_or(struct dlb2_bitmap * dest,struct dlb2_bitmap * src1,struct dlb2_bitmap * src2)411*5433956dSTimothy McDaniel static inline int dlb2_bitmap_or(struct dlb2_bitmap *dest,
412*5433956dSTimothy McDaniel 				 struct dlb2_bitmap *src1,
413*5433956dSTimothy McDaniel 				 struct dlb2_bitmap *src2)
414*5433956dSTimothy McDaniel {
415*5433956dSTimothy McDaniel 	unsigned int i, min;
416*5433956dSTimothy McDaniel 	int numset = 0;
417*5433956dSTimothy McDaniel 
418*5433956dSTimothy McDaniel 	if (dest  == NULL || dest->map == NULL ||
419*5433956dSTimothy McDaniel 	    src1  == NULL || src1->map == NULL ||
420*5433956dSTimothy McDaniel 	    src2  == NULL || src2->map == NULL)
421*5433956dSTimothy McDaniel 		return -EINVAL;
422*5433956dSTimothy McDaniel 
423*5433956dSTimothy McDaniel 	min = dest->len;
424*5433956dSTimothy McDaniel 	min = (min > src1->len) ? src1->len : min;
425*5433956dSTimothy McDaniel 	min = (min > src2->len) ? src2->len : min;
426*5433956dSTimothy McDaniel 
427*5433956dSTimothy McDaniel 	for (i = 0; i != min; i++) {
428*5433956dSTimothy McDaniel 		if  (rte_bitmap_get(src1->map, i) ||
429*5433956dSTimothy McDaniel 		     rte_bitmap_get(src2->map, i)) {
430*5433956dSTimothy McDaniel 			rte_bitmap_set(dest->map, i);
431*5433956dSTimothy McDaniel 			numset++;
432*5433956dSTimothy McDaniel 		} else {
433*5433956dSTimothy McDaniel 			rte_bitmap_clear(dest->map, i);
434*5433956dSTimothy McDaniel 		}
435*5433956dSTimothy McDaniel 	}
436*5433956dSTimothy McDaniel 
437*5433956dSTimothy McDaniel 	return numset;
438*5433956dSTimothy McDaniel }
439*5433956dSTimothy McDaniel 
440*5433956dSTimothy McDaniel #endif /*  __DLB2_OSDEP_BITMAP_H */
441