1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2016-2020 Intel Corporation
5 #ifndef __DLB2_OSDEP_BITMAP_H
6 #define __DLB2_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 "../dlb2_main.h"
18 /*************************/
19 /*** Bitmap operations ***/
20 /*************************/
22 struct rte_bitmap *map;
27 * dlb2_bitmap_alloc() - alloc a bitmap data structure
28 * @bitmap: pointer to dlb2_bitmap structure pointer.
29 * @len: number of entries in the bitmap.
31 * This function allocates a bitmap and initializes it with length @len. All
32 * entries are initially zero.
35 * Returns 0 upon success, < 0 otherwise.
38 * EINVAL - bitmap is NULL or len is 0.
39 * ENOMEM - could not allocate memory for the bitmap data structure.
41 static inline int dlb2_bitmap_alloc(struct dlb2_bitmap **bitmap,
44 struct dlb2_bitmap *bm;
47 uint32_t nbits = (uint32_t)len;
49 if (bitmap == NULL || nbits == 0)
52 /* Allocate DLB2 bitmap control struct */
53 bm = rte_malloc("DLB2_PF",
54 sizeof(struct dlb2_bitmap),
60 /* Allocate bitmap memory */
61 alloc_size = rte_bitmap_get_memory_footprint(nbits);
62 mem = rte_malloc("DLB2_PF_BITMAP", alloc_size, RTE_CACHE_LINE_SIZE);
68 bm->map = rte_bitmap_init(len, mem, alloc_size);
69 if (bm->map == NULL) {
83 * dlb2_bitmap_free() - free a previously allocated bitmap data structure
84 * @bitmap: pointer to dlb2_bitmap structure.
86 * This function frees a bitmap that was allocated with dlb2_bitmap_alloc().
88 static inline void dlb2_bitmap_free(struct dlb2_bitmap *bitmap)
93 rte_free(bitmap->map);
98 * dlb2_bitmap_fill() - fill a bitmap with all 1s
99 * @bitmap: pointer to dlb2_bitmap structure.
101 * This function sets all bitmap values to 1.
104 * Returns 0 upon success, < 0 otherwise.
107 * EINVAL - bitmap is NULL or is uninitialized.
109 static inline int dlb2_bitmap_fill(struct dlb2_bitmap *bitmap)
113 if (bitmap == NULL || bitmap->map == NULL)
116 for (i = 0; i != bitmap->len; i++)
117 rte_bitmap_set(bitmap->map, i);
123 * dlb2_bitmap_fill() - fill a bitmap with all 0s
124 * @bitmap: pointer to dlb2_bitmap structure.
126 * This function sets all bitmap values to 0.
129 * Returns 0 upon success, < 0 otherwise.
132 * EINVAL - bitmap is NULL or is uninitialized.
134 static inline int dlb2_bitmap_zero(struct dlb2_bitmap *bitmap)
136 if (bitmap == NULL || bitmap->map == NULL)
139 rte_bitmap_reset(bitmap->map);
145 * dlb2_bitmap_set() - set a bitmap entry
146 * @bitmap: pointer to dlb2_bitmap structure.
150 * Returns 0 upon success, < 0 otherwise.
153 * EINVAL - bitmap is NULL or is uninitialized, or bit is larger than the
156 static inline int dlb2_bitmap_set(struct dlb2_bitmap *bitmap,
159 if (bitmap == NULL || bitmap->map == NULL)
162 if (bitmap->len <= bit)
165 rte_bitmap_set(bitmap->map, bit);
171 * dlb2_bitmap_set_range() - set a range of bitmap entries
172 * @bitmap: pointer to dlb2_bitmap structure.
173 * @bit: starting bit index.
174 * @len: length of the range.
177 * Returns 0 upon success, < 0 otherwise.
180 * EINVAL - bitmap is NULL or is uninitialized, or the range exceeds the bitmap
183 static inline int dlb2_bitmap_set_range(struct dlb2_bitmap *bitmap,
189 if (bitmap == NULL || bitmap->map == NULL)
192 if (bitmap->len <= bit)
195 for (i = 0; i != len; i++)
196 rte_bitmap_set(bitmap->map, bit + i);
202 * dlb2_bitmap_clear() - clear a bitmap entry
203 * @bitmap: pointer to dlb2_bitmap structure.
207 * Returns 0 upon success, < 0 otherwise.
210 * EINVAL - bitmap is NULL or is uninitialized, or bit is larger than the
213 static inline int dlb2_bitmap_clear(struct dlb2_bitmap *bitmap,
216 if (bitmap == NULL || bitmap->map == NULL)
219 if (bitmap->len <= bit)
222 rte_bitmap_clear(bitmap->map, bit);
228 * dlb2_bitmap_clear_range() - clear a range of bitmap entries
229 * @bitmap: pointer to dlb2_bitmap structure.
230 * @bit: starting bit index.
231 * @len: length of the range.
234 * Returns 0 upon success, < 0 otherwise.
237 * EINVAL - bitmap is NULL or is uninitialized, or the range exceeds the bitmap
240 static inline int dlb2_bitmap_clear_range(struct dlb2_bitmap *bitmap,
246 if (bitmap == NULL || bitmap->map == NULL)
249 if (bitmap->len <= bit)
252 for (i = 0; i != len; i++)
253 rte_bitmap_clear(bitmap->map, bit + i);
259 * dlb2_bitmap_find_set_bit_range() - find an range of set bits
260 * @bitmap: pointer to dlb2_bitmap structure.
261 * @len: length of the range.
263 * This function looks for a range of set bits of length @len.
266 * Returns the base bit index upon success, < 0 otherwise.
269 * ENOENT - unable to find a length *len* range of set bits.
270 * EINVAL - bitmap is NULL or is uninitialized, or len is invalid.
272 static inline int dlb2_bitmap_find_set_bit_range(struct dlb2_bitmap *bitmap,
275 unsigned int i, j = 0;
277 if (bitmap == NULL || bitmap->map == NULL || len == 0)
280 if (bitmap->len < len)
283 for (i = 0; i != bitmap->len; i++) {
284 if (rte_bitmap_get(bitmap->map, i)) {
292 /* No set bit range of length len? */
297 * dlb2_bitmap_find_set_bit() - find an range of set bits
298 * @bitmap: pointer to dlb2_bitmap structure.
300 * This function looks for a single set bit.
303 * Returns the base bit index upon success, -1 if not found, <-1 otherwise.
306 * EINVAL - bitmap is NULL or is uninitialized, or len is invalid.
308 static inline int dlb2_bitmap_find_set_bit(struct dlb2_bitmap *bitmap)
315 if (bitmap->map == NULL)
318 for (i = 0; i != bitmap->len; i++) {
319 if (rte_bitmap_get(bitmap->map, i))
327 * dlb2_bitmap_count() - returns the number of set bits
328 * @bitmap: pointer to dlb2_bitmap structure.
330 * This function looks for a single set bit.
333 * Returns the number of set bits upon success, <0 otherwise.
336 * EINVAL - bitmap is NULL or is uninitialized.
338 static inline int dlb2_bitmap_count(struct dlb2_bitmap *bitmap)
346 if (bitmap->map == NULL)
349 for (i = 0; i != bitmap->len; i++) {
350 if (rte_bitmap_get(bitmap->map, i))
357 * dlb2_bitmap_longest_set_range() - returns longest contiguous range of set
359 * @bitmap: pointer to dlb2_bitmap structure.
362 * Returns the bitmap's longest contiguous range of set bits upon success,
366 * EINVAL - bitmap is NULL or is uninitialized.
368 static inline int dlb2_bitmap_longest_set_range(struct dlb2_bitmap *bitmap)
370 int max_len = 0, len = 0;
376 if (bitmap->map == NULL)
379 for (i = 0; i != bitmap->len; i++) {
380 if (rte_bitmap_get(bitmap->map, i)) {
396 * dlb2_bitmap_or() - store the logical 'or' of two bitmaps into a third
397 * @dest: pointer to dlb2_bitmap structure, which will contain the results of
398 * the 'or' of src1 and src2.
399 * @src1: pointer to dlb2_bitmap structure, will be 'or'ed with src2.
400 * @src2: pointer to dlb2_bitmap structure, will be 'or'ed with src1.
402 * This function 'or's two bitmaps together and stores the result in a third
403 * bitmap. The source and destination bitmaps can be the same.
406 * Returns the number of set bits upon success, <0 otherwise.
409 * EINVAL - One of the bitmaps is NULL or is uninitialized.
411 static inline int dlb2_bitmap_or(struct dlb2_bitmap *dest,
412 struct dlb2_bitmap *src1,
413 struct dlb2_bitmap *src2)
418 if (dest == NULL || dest->map == NULL ||
419 src1 == NULL || src1->map == NULL ||
420 src2 == NULL || src2->map == NULL)
424 min = (min > src1->len) ? src1->len : min;
425 min = (min > src2->len) ? src2->len : min;
427 for (i = 0; i != min; i++) {
428 if (rte_bitmap_get(src1->map, i) ||
429 rte_bitmap_get(src2->map, i)) {
430 rte_bitmap_set(dest->map, i);
433 rte_bitmap_clear(dest->map, i);
440 #endif /* __DLB2_OSDEP_BITMAP_H */