EM-ODP 4.4.0
Event Machine on ODP
Loading...
Searching...
No Matches
env_bitmask.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2015-2026, Nokia Solutions and Networks
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * * Neither the name of the copyright holder nor the names of its
15 * contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31/**
32 * @file
33 * Env bit mask functions - don't include this file directly,
34 * instead include "environment.h"
35 */
36
37#ifndef _ENV_BITMASK_H_
38#define _ENV_BITMASK_H_
39
40#pragma GCC visibility push(default)
41
42#include <stdint.h>
43#include <stdio.h>
44
45#include <odp_api.h>
46
47#include <event_machine/platform/env/env_macros.h>
48
49#ifdef __cplusplus
50extern "C" {
51#endif
52
53/*
54 * Type for a bit mask.
55 */
56typedef struct {
57 odp_cpumask_t odp_cpumask;
59
60/**
61 * Zero the whole mask.
62 *
63 * @param mask Bit mask
64 */
65static inline void env_bitmask_zero(env_bitmask_t *mask)
66{
67 odp_cpumask_zero(&mask->odp_cpumask);
68}
69
70/**
71 * Set a bit in the mask.
72 *
73 * @param bit Bit id
74 * @param mask Bit mask
75 */
76static inline void env_bitmask_set(int bit, env_bitmask_t *mask)
77{
78 odp_cpumask_set(&mask->odp_cpumask, bit);
79}
80
81/**
82 * Clear a bit in the mask.
83 *
84 * @param bit Bit id
85 * @param mask Bit mask
86 */
87static inline void env_bitmask_clr(int bit, env_bitmask_t *mask)
88{
89 odp_cpumask_clr(&mask->odp_cpumask, bit);
90}
91
92/**
93 * Test if a bit is set in the mask.
94 *
95 * @param bit Bit id
96 * @param mask Bit mask
97 *
98 * @return Non-zero if bit id is set in the mask
99 */
100static inline int env_bitmask_isset(int bit, const env_bitmask_t *mask)
101{
102 return odp_cpumask_isset(&mask->odp_cpumask, bit);
103}
104
105/**
106 * Test if the mask is all zero.
107 *
108 * @param mask Bit mask
109 *
110 * @return Non-zero if the mask is all zero
111 */
112static inline int env_bitmask_iszero(const env_bitmask_t *mask)
113{
114 odp_cpumask_t zero_mask;
115
116 odp_cpumask_zero(&zero_mask);
117
118 return odp_cpumask_equal(&zero_mask, &mask->odp_cpumask);
119}
120
121/**
122 * Test if two masks are equal
123 *
124 * @param mask1 First bit mask
125 * @param mask2 Second bit mask
126 *
127 * @return Non-zero if the two masks are equal
128 */
129static inline int env_bitmask_equal(const env_bitmask_t *mask1,
130 const env_bitmask_t *mask2)
131{
132 return odp_cpumask_equal(&mask1->odp_cpumask, &mask2->odp_cpumask);
133}
134
135/**
136 * Set a range (0...count-1) of bits in the mask.
137 *
138 * @param count Number of bits to set
139 * @param mask Bit mask
140 */
141static inline void env_bitmask_set_count(int count, env_bitmask_t *mask)
142{
143 for (int i = 0; i < count; i++)
144 odp_cpumask_set(&mask->odp_cpumask, i);
145}
146
147/**
148 * Copy bit mask
149 *
150 * @param dst Destination bit mask
151 * @param src Source bit mask
152 */
153static inline void env_bitmask_copy(env_bitmask_t *dst,
154 const env_bitmask_t *src)
155{
156 odp_cpumask_copy(&dst->odp_cpumask, &src->odp_cpumask);
157}
158
159/**
160 * Count the number of bits set in the mask.
161 *
162 * @param mask Bit mask
163 *
164 * @return Number of bits set
165 */
166static inline int env_bitmask_count(const env_bitmask_t *mask)
167{
168 return odp_cpumask_count(&mask->odp_cpumask);
169}
170
171/**
172 * Set specified bits from 'bits[]' in bit mask.
173 *
174 * bit 0: bits[0] = 0x1 (len = 1)
175 * bit 1: bits[0] = 0x2 (len = 1)
176 * ...
177 * bit 64: bits[0] = 0x0, bits[1] = 0x1 (len = 2)
178 * bit 65: bits[0] = 0x0, bits[1] = 0x2 (len = 2)
179 * ...
180 * cores 0-127: bits[0]=0xffffffffffffffff, bits[1]=0xffffffffffffffff (len=2)
181 * ...
182 * @param bits[] array of uint64_t:s containing the bits to set in the bit mask
183 * @param len number of array elements in bits[].
184 * @param mask bit mask to set.
185 *
186 * @note bits ar 'or'ed into mask, so any previously set bits will remain set.
187 */
188static inline void env_bitmask_set_bits(const uint64_t bits[], int len,
189 env_bitmask_t *mask)
190{
191 (void)bits;
192 (void)len;
193 (void)mask;
194
195 fprintf(stderr, "%s() function not implemented!\n", __func__);
196}
197
198/**
199 * Get bit mask, stored in a uint64_t array for the user
200 *
201 * bit 0: bits[0] = 0x1 (len = 1)
202 * bit 1: bits[0] = 0x2 (len = 1)
203 * ...
204 * bit 64: bits[0] = 0x0, bits[1] = 0x1 (len = 2)
205 * bit 65: bits[0] = 0x0, bits[1] = 0x2 (len = 2)
206 * ...
207 * cores 0-127: bits[0]=0xffffffffffffffff, bits[1]=0xffffffffffffffff (len=2)
208 * ...
209 * @param[out] bits[] array of uint64_t:s that the bit mask will be stored in
210 * @param len number of array elements in bits[].
211 * @param mask bit mask to get bits from.
212 *
213 * @return The number of uint64_t:s written into bits[].
214 */
215static inline int env_bitmask_get_bits(uint64_t bits[/*out*/], int len,
216 const env_bitmask_t *mask)
217{
218 (void)bits;
219 (void)len;
220 (void)mask;
221
222 fprintf(stderr, "%s() function not implemented!\n", __func__);
223
224 return 0;
225}
226
227/**
228 * Return the index (position) of the Nth set bit in the bit mask
229 *
230 * @param n Nth set bit, note n=1 means first set bit, n=[1...MaxCores]
231 * @param mask bit mask
232 *
233 * @return Index of the Nth set bit, <0 on error or if no such bit.
234 */
235static inline int env_bitmask_idx(int n, const env_bitmask_t *mask)
236{
237 if (unlikely((unsigned int)(n - 1) >= ODP_CPUMASK_SIZE))
238 return -1;
239
240 int i = 1;
241 int cpu = odp_cpumask_first(&mask->odp_cpumask);
242
243 while (cpu >= 0 && i < n) {
244 cpu = odp_cpumask_next(&mask->odp_cpumask, cpu);
245 i++;
246 }
247
248 return cpu;
249}
250
251/**
252 * Bitwise AND operation on two masks, store the result in 'dst'
253 *
254 * dst = src1 & src2
255 *
256 * @param dst destination bit mask, result is stored here
257 * @param src1 source mask #1
258 * @param scr2 source mask #2
259 */
260static inline void env_bitmask_and(env_bitmask_t *dst,
261 const env_bitmask_t *src1,
262 const env_bitmask_t *src2)
263{
264 odp_cpumask_and(&dst->odp_cpumask,
265 &src1->odp_cpumask, &src2->odp_cpumask);
266}
267
268/**
269 * Bitwise OR operation on two masks, store the result in 'dst'
270 *
271 * dst = src1 | src2
272 *
273 * @param dst destination bit mask, result is stored here
274 * @param src1 source mask #1
275 * @param scr2 source mask #2
276 */
277static inline void env_bitmask_or(env_bitmask_t *dst,
278 const env_bitmask_t *src1,
279 const env_bitmask_t *src2)
280{
281 odp_cpumask_or(&dst->odp_cpumask,
282 &src1->odp_cpumask, &src2->odp_cpumask);
283}
284
285/**
286 * Bitwise XOR operation on two masks, store the result in 'dst'
287 *
288 * dst = src1 ^ src2
289 *
290 * @param dst destination bit mask, result is stored here
291 * @param src1 source mask #1
292 * @param scr2 source mask #2
293 */
294static inline void env_bitmask_xor(env_bitmask_t *dst,
295 const env_bitmask_t *src1,
296 const env_bitmask_t *src2)
297{
298 odp_cpumask_xor(&dst->odp_cpumask,
299 &src1->odp_cpumask, &src2->odp_cpumask);
300}
301
302#ifdef __cplusplus
303}
304#endif
305
306#pragma GCC visibility pop
307#endif /* _ENV_BITMASK_H_ */