1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2016-2020 Intel Corporation
5 #ifndef __DLB_OSDEP_BITMAP_H__
6 #define __DLB_OSDEP_BITMAP_H__
12 #include <rte_bitmap.h>
13 #include <rte_string_fns.h>
14 #include <rte_malloc.h>
15 #include <rte_errno.h>
16 #include "../dlb_main.h"
18 /*************************/
19 /*** Bitmap operations ***/
20 /*************************/
22 struct rte_bitmap *map;
28 * dlb_bitmap_alloc() - alloc a bitmap data structure
29 * @bitmap: pointer to dlb_bitmap structure pointer.
30 * @len: number of entries in the bitmap.
32 * This function allocates a bitmap and initializes it with length @len. All
33 * entries are initially zero.
36 * Returns 0 upon success, < 0 otherwise.
39 * EINVAL - bitmap is NULL or len is 0.
40 * ENOMEM - could not allocate memory for the bitmap data structure.
42 static inline int dlb_bitmap_alloc(struct dlb_hw *hw,
43 struct dlb_bitmap **bitmap,
46 struct dlb_bitmap *bm;
49 uint32_t nbits = (uint32_t) len;
52 if (bitmap == NULL || nbits == 0)
55 /* Allocate DLB bitmap control struct */
56 bm = rte_malloc("DLB_PF",
57 sizeof(struct dlb_bitmap),
63 /* Allocate bitmap memory */
64 alloc_size = rte_bitmap_get_memory_footprint(nbits);
65 mem = rte_malloc("DLB_PF_BITMAP", alloc_size, RTE_CACHE_LINE_SIZE);
71 bm->map = rte_bitmap_init(len, mem, alloc_size);
72 if (bm->map == NULL) {
86 * dlb_bitmap_free() - free a previously allocated bitmap data structure
87 * @bitmap: pointer to dlb_bitmap structure.
89 * This function frees a bitmap that was allocated with dlb_bitmap_alloc().
91 static inline void dlb_bitmap_free(struct dlb_bitmap *bitmap)
96 rte_free(bitmap->map);
101 * dlb_bitmap_fill() - fill a bitmap with all 1s
102 * @bitmap: pointer to dlb_bitmap structure.
104 * This function sets all bitmap values to 1.
107 * Returns 0 upon success, < 0 otherwise.
110 * EINVAL - bitmap is NULL or is uninitialized.
112 static inline int dlb_bitmap_fill(struct dlb_bitmap *bitmap)
116 if (bitmap == NULL || bitmap->map == NULL)
119 for (i = 0; i != bitmap->len; i++)
120 rte_bitmap_set(bitmap->map, i);
126 * dlb_bitmap_zero() - fill a bitmap with all 0s
127 * @bitmap: pointer to dlb_bitmap structure.
129 * This function sets all bitmap values to 0.
132 * Returns 0 upon success, < 0 otherwise.
135 * EINVAL - bitmap is NULL or is uninitialized.
137 static inline int dlb_bitmap_zero(struct dlb_bitmap *bitmap)
139 if (bitmap == NULL || bitmap->map == NULL)
142 rte_bitmap_reset(bitmap->map);
148 * dlb_bitmap_set() - set a bitmap entry
149 * @bitmap: pointer to dlb_bitmap structure.
153 * Returns 0 upon success, < 0 otherwise.
156 * EINVAL - bitmap is NULL or is uninitialized, or bit is larger than the
159 static inline int dlb_bitmap_set(struct dlb_bitmap *bitmap,
162 if (bitmap == NULL || bitmap->map == NULL)
165 if (bitmap->len <= bit)
168 rte_bitmap_set(bitmap->map, bit);
174 * dlb_bitmap_set_range() - set a range of bitmap entries
175 * @bitmap: pointer to dlb_bitmap structure.
176 * @bit: starting bit index.
177 * @len: length of the range.
180 * Returns 0 upon success, < 0 otherwise.
183 * EINVAL - bitmap is NULL or is uninitialized, or the range exceeds the bitmap
186 static inline int dlb_bitmap_set_range(struct dlb_bitmap *bitmap,
192 if (bitmap == NULL || bitmap->map == NULL)
195 if (bitmap->len <= bit)
198 for (i = 0; i != len; i++)
199 rte_bitmap_set(bitmap->map, bit + i);
205 * dlb_bitmap_clear() - clear a bitmap entry
206 * @bitmap: pointer to dlb_bitmap structure.
210 * Returns 0 upon success, < 0 otherwise.
213 * EINVAL - bitmap is NULL or is uninitialized, or bit is larger than the
216 static inline int dlb_bitmap_clear(struct dlb_bitmap *bitmap,
219 if (bitmap == NULL || bitmap->map == NULL)
222 if (bitmap->len <= bit)
225 rte_bitmap_clear(bitmap->map, bit);
231 * dlb_bitmap_clear_range() - clear a range of bitmap entries
232 * @bitmap: pointer to dlb_bitmap structure.
233 * @bit: starting bit index.
234 * @len: length of the range.
237 * Returns 0 upon success, < 0 otherwise.
240 * EINVAL - bitmap is NULL or is uninitialized, or the range exceeds the bitmap
243 static inline int dlb_bitmap_clear_range(struct dlb_bitmap *bitmap,
249 if (bitmap == NULL || bitmap->map == NULL)
252 if (bitmap->len <= bit)
255 for (i = 0; i != len; i++)
256 rte_bitmap_clear(bitmap->map, bit + i);
262 * dlb_bitmap_find_set_bit_range() - find a range of set bits
263 * @bitmap: pointer to dlb_bitmap structure.
264 * @len: length of the range.
266 * This function looks for a range of set bits of length @len.
269 * Returns the base bit index upon success, < 0 otherwise.
272 * ENOENT - unable to find a length *len* range of set bits.
273 * EINVAL - bitmap is NULL or is uninitialized, or len is invalid.
275 static inline int dlb_bitmap_find_set_bit_range(struct dlb_bitmap *bitmap,
278 unsigned int i, j = 0;
280 if (bitmap == NULL || bitmap->map == NULL || len == 0)
283 if (bitmap->len < len)
286 for (i = 0; i != bitmap->len; i++) {
287 if (rte_bitmap_get(bitmap->map, i)) {
294 /* No set bit range of length len? */
299 * dlb_bitmap_find_set_bit() - find the first set bit
300 * @bitmap: pointer to dlb_bitmap structure.
302 * This function looks for a single set bit.
305 * Returns the base bit index upon success, < 0 otherwise.
308 * ENOENT - the bitmap contains no set bits.
309 * EINVAL - bitmap is NULL or is uninitialized, or len is invalid.
311 static inline int dlb_bitmap_find_set_bit(struct dlb_bitmap *bitmap)
318 if (bitmap->map == NULL)
321 for (i = 0; i != bitmap->len; i++) {
322 if (rte_bitmap_get(bitmap->map, i))
330 * dlb_bitmap_count() - returns the number of set bits
331 * @bitmap: pointer to dlb_bitmap structure.
333 * This function looks for a single set bit.
336 * Returns the number of set bits upon success, <0 otherwise.
339 * EINVAL - bitmap is NULL or is uninitialized.
341 static inline int dlb_bitmap_count(struct dlb_bitmap *bitmap)
349 if (bitmap->map == NULL)
352 for (i = 0; i != bitmap->len; i++) {
353 if (rte_bitmap_get(bitmap->map, i))
360 * dlb_bitmap_longest_set_range() - returns longest contiguous range of set bits
361 * @bitmap: pointer to dlb_bitmap structure.
364 * Returns the bitmap's longest contiguous range of set bits upon success,
368 * EINVAL - bitmap is NULL or is uninitialized.
370 static inline int dlb_bitmap_longest_set_range(struct dlb_bitmap *bitmap)
372 int max_len = 0, len = 0;
378 if (bitmap->map == NULL)
381 for (i = 0; i != bitmap->len; i++) {
382 if (rte_bitmap_get(bitmap->map, i)) {
398 * dlb_bitmap_or() - store the logical 'or' of two bitmaps into a third
399 * @dest: pointer to dlb_bitmap structure, which will contain the results of
400 * the 'or' of src1 and src2.
401 * @src1: pointer to dlb_bitmap structure, will be 'or'ed with src2.
402 * @src2: pointer to dlb_bitmap structure, will be 'or'ed with src1.
404 * This function 'or's two bitmaps together and stores the result in a third
405 * bitmap. The source and destination bitmaps can be the same.
408 * Returns the number of set bits upon success, <0 otherwise.
411 * EINVAL - One of the bitmaps is NULL or is uninitialized.
413 static inline int dlb_bitmap_or(struct dlb_bitmap *dest,
414 struct dlb_bitmap *src1,
415 struct dlb_bitmap *src2)
420 if (dest == NULL || dest->map == NULL ||
421 src1 == NULL || src1->map == NULL ||
422 src2 == NULL || src2->map == NULL)
426 min = (min > src1->len) ? src1->len : min;
427 min = (min > src2->len) ? src2->len : min;
429 for (i = 0; i != min; i++) {
430 if (rte_bitmap_get(src1->map, i) ||
431 rte_bitmap_get(src2->map, i)) {
432 rte_bitmap_set(dest->map, i);
435 rte_bitmap_clear(dest->map, i);
441 #endif /* __DLB_OSDEP_BITMAP_H__ */