Motr  M0
misc.h
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2013-2020 Seagate Technology LLC and/or its Affiliates
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * For any questions about this software or licensing,
18  * please email opensource@seagate.com or cortx-questions@seagate.com.
19  *
20  */
21 
22 #pragma once
23 
24 #ifndef __MOTR_LIB_MISC_H__
25 #define __MOTR_LIB_MISC_H__
26 
27 #ifdef __KERNEL__
28 # include <linux/string.h> /* memset, strstr */
29 # include <linux/bitops.h> /* ffs */
30 # include "lib/linux_kernel/misc.h"
31 #else
32 # include <string.h> /* memset, ffs, strstr */
33 # include <limits.h> /* CHAR_BIT */
34 # include "lib/user_space/misc.h"
35 #endif
36 #include "lib/types.h"
37 #include "lib/assert.h" /* M0_CASSERT */
38 #include "lib/buf.h" /* m0_buf */
39 
40 #define _QUOTE(s) #s
41 #define M0_QUOTE(s) _QUOTE(s)
42 
48 #define M0_SRC_PATH(name) M0_QUOTE(M0_SRC_DIR) "/" name
49 
54 M0_INTERNAL uint64_t m0_round_up(uint64_t val, uint64_t size);
55 
60 M0_INTERNAL uint64_t m0_round_down(uint64_t val, uint64_t size);
61 
62 #define M0_MEMBER_SIZE(type, member) sizeof(((type *)0)->member)
63 
64 #define M0_SET0(obj) \
65 ({ \
66  M0_CASSERT(!m0_is_array(obj)); \
67  memset((obj), 0, sizeof *(obj)); \
68 })
69 
70 #define M0_IS0(obj) m0_forall(i, sizeof *(obj), ((char *)obj)[i] == 0)
71 
72 #define M0_SET_ARR0(arr) \
73 ({ \
74  M0_CASSERT(m0_is_array(arr)); \
75  memset((arr), 0, sizeof (arr)); \
76 })
77 
79 #define m0_count(var, nr, ...) \
80 ({ \
81  unsigned __nr = (nr); \
82  unsigned var; \
83  unsigned count; \
84  \
85  for (count = var = 0; var < __nr; ++var) { \
86  if (__VA_ARGS__) \
87  ++count; \
88  } \
89  count; \
90 })
91 
112 #define m0_forall(var, nr, ...) \
113 ({ \
114  unsigned __nr = (nr); \
115  unsigned var; \
116  \
117  for (var = 0; var < __nr && ({ __VA_ARGS__ ; }); ++var) \
118  ; \
119  var == __nr; \
120 })
121 
134 #define m0_exists(var, nr, ...) !m0_forall(var, (nr), !(__VA_ARGS__))
135 
150 #define m0_reduce(var, nr, init, exp) \
151 ({ \
152  unsigned __nr = (nr); \
153  unsigned var; \
154  typeof(init) __accum = (init); \
155  \
156  for (var = 0; var < __nr; ++var) { \
157  __accum = __accum exp; \
158  } \
159  __accum; \
160 })
161 
178 #define m0_fold(var, accum, nr, init, exp) \
179 ({ \
180  unsigned __nr = (nr); \
181  unsigned var; \
182  typeof(init) accum = (init); \
183  \
184  for (var = 0; var < __nr; ++var) { \
185  accum = exp; \
186  } \
187  accum; \
188 })
189 
199 #define M0_IN(x, set) \
200  ({ typeof (x) __x = (x); \
201  M0_IN0(__x, M0_UNPACK set); })
202 
203 #define M0_UNPACK(...) __VA_ARGS__
204 
205 #define M0_IN0(...) \
206  M0_CAT(M0_IN_, M0_COUNT_PARAMS(__VA_ARGS__))(__VA_ARGS__)
207 
208 #define M0_IN_1(x, v) ((x) == (v))
209 #define M0_IN_2(x, v, ...) ((x) == (v) || M0_IN_1(x, __VA_ARGS__))
210 #define M0_IN_3(x, v, ...) ((x) == (v) || M0_IN_2(x, __VA_ARGS__))
211 #define M0_IN_4(x, v, ...) ((x) == (v) || M0_IN_3(x, __VA_ARGS__))
212 #define M0_IN_5(x, v, ...) ((x) == (v) || M0_IN_4(x, __VA_ARGS__))
213 #define M0_IN_6(x, v, ...) ((x) == (v) || M0_IN_5(x, __VA_ARGS__))
214 #define M0_IN_7(x, v, ...) ((x) == (v) || M0_IN_6(x, __VA_ARGS__))
215 #define M0_IN_8(x, v, ...) ((x) == (v) || M0_IN_7(x, __VA_ARGS__))
216 #define M0_IN_9(x, v, ...) ((x) == (v) || M0_IN_8(x, __VA_ARGS__))
217 
236 #define M0_BITS(...) \
237  M0_CAT(__M0_BITS_, M0_COUNT_PARAMS(__VA_ARGS__))(__VA_ARGS__)
238 
239 #define __M0_BITS_0(i) (1ULL << (i))
240 #define __M0_BITS_1(i, ...) ((1ULL << (i)) | __M0_BITS_0(__VA_ARGS__))
241 #define __M0_BITS_2(i, ...) ((1ULL << (i)) | __M0_BITS_1(__VA_ARGS__))
242 #define __M0_BITS_3(i, ...) ((1ULL << (i)) | __M0_BITS_2(__VA_ARGS__))
243 #define __M0_BITS_4(i, ...) ((1ULL << (i)) | __M0_BITS_3(__VA_ARGS__))
244 #define __M0_BITS_5(i, ...) ((1ULL << (i)) | __M0_BITS_4(__VA_ARGS__))
245 #define __M0_BITS_6(i, ...) ((1ULL << (i)) | __M0_BITS_5(__VA_ARGS__))
246 #define __M0_BITS_7(i, ...) ((1ULL << (i)) | __M0_BITS_6(__VA_ARGS__))
247 #define __M0_BITS_8(i, ...) ((1ULL << (i)) | __M0_BITS_7(__VA_ARGS__))
248 
249 M0_INTERNAL const char *m0_bool_to_str(bool b);
250 
278 M0_INTERNAL const char *m0_short_file_name(const char *fname);
279 
280 /* strtoull for user- and kernel-space */
281 uint64_t m0_strtou64(const char *str, char **endptr, int base);
282 
283 /* strtoul for user- and kernel-space */
284 uint32_t m0_strtou32(const char *str, char **endptr, int base);
285 
286 /*
287  * Helper macros for implication and equivalence.
288  *
289  * Unfortunately, name clashes are possible and m0_ prefix is too awkward. See
290  * M0_BASSERT() checks in lib/misc.c
291  */
292 #ifndef ergo
293 #define ergo(a, b) (!(a) || (b))
294 #endif
295 
296 #ifndef equi
297 #define equi(a, b) (!(a) == !(b))
298 #endif
299 
300 void __dummy_function(void);
301 
306 #define AND_NOTHING_ELSE else __dummy_function();
307 
308 #define m0_is_array(x) \
309  (!__builtin_types_compatible_p(typeof(&(x)[0]), typeof(x)))
310 
311 #define IS_IN_ARRAY(idx, array) \
312 ({ \
313  M0_CASSERT(m0_is_array(array)); \
314  ((unsigned long)(idx)) < ARRAY_SIZE(array); \
315 })
316 
317 M0_INTERNAL bool m0_elems_are_unique(const void *array, unsigned nr_elems,
318  size_t elem_size);
319 
320 #define M0_AMB(obj, ptr, field) \
321  (container_of((ptr), typeof(*(obj)), field))
322 
323 #define M0_MEMBER_PTR(ptr, member) \
324 ({ \
325  typeof(ptr) __ptr = (ptr); \
326  __ptr == NULL ? NULL : &__ptr->member; \
327 })
328 
329 #define M0_MEMBER(ptr, member) \
330 ({ \
331  typeof(ptr) __ptr = (ptr); \
332  __ptr == NULL ? NULL : __ptr->member; \
333 })
334 
339 #define M0_FIELD_VALUE(type, field) (((type *)0)->field)
340 
344 #define M0_HAS_TYPE(expr, type) __builtin_types_compatible_p(typeof(expr), type)
345 
349 #define M0_FIELD_IS(type, field, ftype) \
350  M0_HAS_TYPE(M0_FIELD_VALUE(type, field), ftype)
351 
356 #define M0_MAGIX_OFFSET(type, field) \
357 M0_FIELD_IS(type, field, uint64_t) ? \
358  offsetof(type, field) : \
359  sizeof(char [M0_FIELD_IS(type, field, uint64_t) - 1])
360 
370 #define M0_COUNT_PARAMS(...) \
371  M0_COUNT_PARAMS2(__VA_ARGS__, 9,8,7,6,5,4,3,2,1,0)
372 #define M0_COUNT_PARAMS2(_0,_1,_2,_3,_4,_5,_6,_7,_8,_9,_, ...) _
373 
377 #define M0_CAT(A, B) M0_CAT2(A, B)
378 #define M0_CAT2(A, B) A ## B
379 
380 #define M0_UNUSED __attribute__((unused))
381 
382 M0_INTERNAL uint32_t m0_no_of_bits_set(uint64_t val);
383 
384 M0_INTERNAL unsigned int
385 m0_full_name_hash(const unsigned char *name, unsigned int len);
386 
396 M0_INTERNAL uint64_t m0_ptr_wrap(const void *p);
397 
398 M0_INTERNAL const void *m0_ptr_unwrap(uint64_t val);
399 
403 #define M0_CIRCULAR_SHIFT_LEFT(val, bits) \
404 ({ \
405  typeof(val) __v = (val); \
406  typeof(bits) __b = (bits); \
407  \
408  (__v << __b) | (__v >> (sizeof(__v) * CHAR_BIT - __b)); \
409 })
410 
411 enum {
413 };
414 
415 
417 struct m0_key_val {
418  struct m0_buf kv_key;
419  struct m0_buf kv_val;
420 };
421 
436 M0_INTERNAL void m0_permute(uint64_t n, uint64_t *k, uint64_t *s, uint64_t *r);
437 
439 M0_INTERNAL void m0_array_sort(uint64_t *arr, uint64_t arr_len);
440 
442 #define M0_BYTES(bits_nr) ((bits_nr + 7) / 8)
443 
445 M0_INTERNAL bool m0_bit_get(void *buffer, m0_bcount_t i);
446 
448 M0_INTERNAL void m0_bit_set(void *buffer, m0_bcount_t i, bool val);
449 
451 M0_INTERNAL void m0_key_val_init(struct m0_key_val *kv, const struct m0_buf *key,
452  const struct m0_buf *val);
453 
463 M0_INTERNAL void *m0_vote_majority_get(struct m0_key_val *arr, uint32_t len,
464  bool (*cmp)(const struct m0_buf *,
465  const struct m0_buf *),
466  uint32_t *vote_nr);
467 
471 M0_INTERNAL bool m0_key_val_is_null(struct m0_key_val *kv);
472 
476 M0_INTERNAL void m0_key_val_null_set(struct m0_key_val *kv);
477 
478 M0_EXTERN const struct m0_key_val M0_KEY_VAL_NULL;
479 
483 M0_INTERNAL uint64_t m0_dummy_id_generate(void);
484 
485 #endif /* __MOTR_LIB_MISC_H__ */
486 
487 /*
488  * Local variables:
489  * c-indentation-style: "K&R"
490  * c-basic-offset: 8
491  * tab-width: 8
492  * fill-column: 80
493  * scroll-step: 1
494  * End:
495  */
static struct m0_addb2_philter p
Definition: consumer.c:40
M0_INTERNAL bool m0_elems_are_unique(const void *array, unsigned nr_elems, size_t elem_size)
Definition: misc.c:257
M0_INTERNAL uint64_t m0_ptr_wrap(const void *p)
Definition: misc.c:277
M0_INTERNAL bool m0_bit_get(void *buffer, m0_bcount_t i)
Definition: misc.c:340
Definition: idx_mock.c:52
M0_INTERNAL unsigned int m0_full_name_hash(const unsigned char *name, unsigned int len)
Definition: misc.c:266
M0_INTERNAL const char * m0_bool_to_str(bool b)
Definition: misc.c:207
uint64_t m0_bcount_t
Definition: types.h:77
M0_INTERNAL const void * m0_ptr_unwrap(uint64_t val)
Definition: misc.c:282
M0_INTERNAL uint32_t m0_no_of_bits_set(uint64_t val)
Definition: misc.c:227
Definition: buf.h:37
int i
Definition: dir.c:1033
M0_INTERNAL const char * m0_short_file_name(const char *fname)
Definition: misc.c:212
M0_INTERNAL void m0_permute(uint64_t n, uint64_t *k, uint64_t *s, uint64_t *r)
Definition: misc.c:288
const char * name
Definition: trace.c:110
M0_INTERNAL void m0_key_val_init(struct m0_key_val *kv, const struct m0_buf *key, const struct m0_buf *val)
Definition: misc.c:369
M0_INTERNAL uint64_t m0_round_down(uint64_t val, uint64_t size)
Definition: misc.c:187
M0_INTERNAL uint64_t m0_round_up(uint64_t val, uint64_t size)
Definition: misc.c:181
static int cmp(const struct m0_ut_suite **s0, const struct m0_ut_suite **s1)
Definition: ut.c:654
uint64_t m0_strtou64(const char *str, char **endptr, int base)
Definition: kmisc.c:26
M0_INTERNAL uint64_t m0_dummy_id_generate(void)
Definition: misc.c:425
uint64_t n
Definition: fops.h:107
struct m0_buf kv_val
Definition: misc.h:419
uint32_t m0_strtou32(const char *str, char **endptr, int base)
Definition: kmisc.c:32
M0_INTERNAL void m0_array_sort(uint64_t *arr, uint64_t arr_len)
Definition: misc.c:325
M0_INTERNAL bool m0_key_val_is_null(struct m0_key_val *kv)
Definition: misc.c:363
struct m0_buf kv_key
Definition: misc.h:418
M0_INTERNAL void m0_bit_set(void *buffer, m0_bcount_t i, bool val)
Definition: misc.c:349
static int r[NR]
Definition: thread.c:46
Definition: addb2.c:200
M0_EXTERN const struct m0_key_val M0_KEY_VAL_NULL
Definition: misc.h:478
m0_bcount_t size
Definition: di.c:39
void __dummy_function(void)
Definition: misc.c:32
static uint64_t base
Definition: dump.c:1504
M0_INTERNAL void * m0_vote_majority_get(struct m0_key_val *arr, uint32_t len, bool(*cmp)(const struct m0_buf *, const struct m0_buf *), uint32_t *vote_nr)
Definition: misc.c:383
static struct m0_addb2_source * s
Definition: consumer.c:39
M0_INTERNAL void m0_key_val_null_set(struct m0_key_val *kv)
Definition: misc.c:378
Definition: idx_mock.c:47