Motr
M0
|
#include <string.h>
#include <limits.h>
#include "lib/user_space/misc.h"
#include "lib/types.h"
#include "lib/assert.h"
#include "lib/buf.h"
Go to the source code of this file.
Data Structures | |
struct | m0_key_val |
Macros | |
#define | __MOTR_LIB_MISC_H__ |
#define | _QUOTE(s) #s |
#define | M0_QUOTE(s) _QUOTE(s) |
#define | M0_SRC_PATH(name) M0_QUOTE(M0_SRC_DIR) "/" name |
#define | M0_MEMBER_SIZE(type, member) sizeof(((type *)0)->member) |
#define | M0_SET0(obj) |
#define | M0_IS0(obj) m0_forall(i, sizeof *(obj), ((char *)obj)[i] == 0) |
#define | M0_SET_ARR0(arr) |
#define | m0_count(var, nr, ...) |
#define | m0_forall(var, nr, ...) |
#define | m0_exists(var, nr, ...) !m0_forall(var, (nr), !(__VA_ARGS__)) |
#define | M0_BITS(...) M0_CAT(__M0_BITS_, M0_COUNT_PARAMS(__VA_ARGS__))(__VA_ARGS__) |
#define | __M0_BITS_0(i) (1ULL << (i)) |
#define | __M0_BITS_1(i, ...) ((1ULL << (i)) | __M0_BITS_0(__VA_ARGS__)) |
#define | __M0_BITS_2(i, ...) ((1ULL << (i)) | __M0_BITS_1(__VA_ARGS__)) |
#define | __M0_BITS_3(i, ...) ((1ULL << (i)) | __M0_BITS_2(__VA_ARGS__)) |
#define | __M0_BITS_4(i, ...) ((1ULL << (i)) | __M0_BITS_3(__VA_ARGS__)) |
#define | __M0_BITS_5(i, ...) ((1ULL << (i)) | __M0_BITS_4(__VA_ARGS__)) |
#define | __M0_BITS_6(i, ...) ((1ULL << (i)) | __M0_BITS_5(__VA_ARGS__)) |
#define | __M0_BITS_7(i, ...) ((1ULL << (i)) | __M0_BITS_6(__VA_ARGS__)) |
#define | __M0_BITS_8(i, ...) ((1ULL << (i)) | __M0_BITS_7(__VA_ARGS__)) |
#define | ergo(a, b) (!(a) || (b)) |
#define | equi(a, b) (!(a) == !(b)) |
#define | AND_NOTHING_ELSE else __dummy_function(); |
#define | m0_is_array(x) (!__builtin_types_compatible_p(typeof(&(x)[0]), typeof(x))) |
#define | IS_IN_ARRAY(idx, array) |
#define | M0_AMB(obj, ptr, field) (container_of((ptr), typeof(*(obj)), field)) |
#define | M0_MEMBER_PTR(ptr, member) |
#define | M0_MEMBER(ptr, member) |
#define | M0_FIELD_VALUE(type, field) (((type *)0)->field) |
#define | M0_HAS_TYPE(expr, type) __builtin_types_compatible_p(typeof(expr), type) |
#define | M0_FIELD_IS(type, field, ftype) M0_HAS_TYPE(M0_FIELD_VALUE(type, field), ftype) |
#define | M0_MAGIX_OFFSET(type, field) |
#define | M0_COUNT_PARAMS(...) M0_COUNT_PARAMS2(__VA_ARGS__, 9,8,7,6,5,4,3,2,1,0) |
#define | M0_COUNT_PARAMS2(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _, ...) _ |
#define | M0_CAT(A, B) M0_CAT2(A, B) |
#define | M0_CAT2(A, B) A ## B |
#define | M0_UNUSED __attribute__((unused)) |
#define | M0_CIRCULAR_SHIFT_LEFT(val, bits) |
#define | M0_BYTES(bits_nr) ((bits_nr + 7) / 8) |
Enumerations | |
enum | { UINT32_STR_LEN = 64 } |
Functions | |
M0_INTERNAL uint64_t | m0_round_up (uint64_t val, uint64_t size) |
M0_INTERNAL uint64_t | m0_round_down (uint64_t val, uint64_t size) |
M0_INTERNAL const char * | m0_bool_to_str (bool b) |
M0_INTERNAL const char * | m0_short_file_name (const char *fname) |
uint64_t | m0_strtou64 (const char *str, char **endptr, int base) |
uint32_t | m0_strtou32 (const char *str, char **endptr, int base) |
void | __dummy_function (void) |
M0_INTERNAL bool | m0_elems_are_unique (const void *array, unsigned nr_elems, size_t elem_size) |
M0_INTERNAL uint32_t | m0_no_of_bits_set (uint64_t val) |
M0_INTERNAL unsigned int | m0_full_name_hash (const unsigned char *name, unsigned int len) |
M0_INTERNAL uint64_t | m0_ptr_wrap (const void *p) |
M0_INTERNAL const void * | m0_ptr_unwrap (uint64_t val) |
M0_INTERNAL void | m0_permute (uint64_t n, uint64_t *k, uint64_t *s, uint64_t *r) |
M0_INTERNAL void | m0_array_sort (uint64_t *arr, uint64_t arr_len) |
M0_INTERNAL bool | m0_bit_get (void *buffer, m0_bcount_t i) |
M0_INTERNAL void | m0_bit_set (void *buffer, m0_bcount_t i, bool val) |
M0_INTERNAL void | m0_key_val_init (struct m0_key_val *kv, const struct m0_buf *key, const struct m0_buf *val) |
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) |
M0_INTERNAL bool | m0_key_val_is_null (struct m0_key_val *kv) |
M0_INTERNAL void | m0_key_val_null_set (struct m0_key_val *kv) |
M0_INTERNAL uint64_t | m0_dummy_id_generate (void) |
Variables | |
M0_EXTERN const struct m0_key_val | M0_KEY_VAL_NULL |
#define __M0_BITS_1 | ( | i, | |
... | |||
) | ((1ULL << (i)) | __M0_BITS_0(__VA_ARGS__)) |
#define __M0_BITS_2 | ( | i, | |
... | |||
) | ((1ULL << (i)) | __M0_BITS_1(__VA_ARGS__)) |
#define __M0_BITS_3 | ( | i, | |
... | |||
) | ((1ULL << (i)) | __M0_BITS_2(__VA_ARGS__)) |
#define __M0_BITS_4 | ( | i, | |
... | |||
) | ((1ULL << (i)) | __M0_BITS_3(__VA_ARGS__)) |
#define __M0_BITS_5 | ( | i, | |
... | |||
) | ((1ULL << (i)) | __M0_BITS_4(__VA_ARGS__)) |
#define __M0_BITS_6 | ( | i, | |
... | |||
) | ((1ULL << (i)) | __M0_BITS_5(__VA_ARGS__)) |
#define __M0_BITS_7 | ( | i, | |
... | |||
) | ((1ULL << (i)) | __M0_BITS_6(__VA_ARGS__)) |
#define __M0_BITS_8 | ( | i, | |
... | |||
) | ((1ULL << (i)) | __M0_BITS_7(__VA_ARGS__)) |
#define AND_NOTHING_ELSE else __dummy_function(); |
#define IS_IN_ARRAY | ( | idx, | |
array | |||
) |
#define M0_BITS | ( | ... | ) | M0_CAT(__M0_BITS_, M0_COUNT_PARAMS(__VA_ARGS__))(__VA_ARGS__) |
Reduces ("aggregates") given expression over an interval.
Example uses
then
returns
#define M0_BYTES | ( | bits_nr | ) | ((bits_nr + 7) / 8) |
#define M0_CIRCULAR_SHIFT_LEFT | ( | val, | |
bits | |||
) |
#define m0_count | ( | var, | |
nr, | |||
... | |||
) |
#define M0_COUNT_PARAMS | ( | ... | ) | M0_COUNT_PARAMS2(__VA_ARGS__, 9,8,7,6,5,4,3,2,1,0) |
Returns the number of parameters given to this variadic macro (up to 9 parameters are supported)
#define M0_COUNT_PARAMS2 | ( | _0, | |
_1, | |||
_2, | |||
_3, | |||
_4, | |||
_5, | |||
_6, | |||
_7, | |||
_8, | |||
_9, | |||
_, | |||
... | |||
) | _ |
Returns a disjunction (logical OR) of an expression evaluated over a range.
#define M0_FIELD_IS | ( | type, | |
field, | |||
ftype | |||
) | M0_HAS_TYPE(M0_FIELD_VALUE(type, field), ftype) |
#define m0_forall | ( | var, | |
nr, | |||
... | |||
) |
Returns a conjunction (logical AND) of an expression evaluated over a range
Declares an unsigned integer variable named "var" in a new scope and evaluates user-supplied expression (the last argument) with "var" iterated over successive elements of [0 .. NR - 1] range, while this expression returns true. Returns true iff the whole range was iterated over.
This function is useful for invariant checking.
Computes offset of "magix" field, iff magix field is of type uint64_t. Otherwise causes compilation failure.
#define M0_MEMBER | ( | ptr, | |
member | |||
) |
#define M0_MEMBER_PTR | ( | ptr, | |
member | |||
) |
#define M0_SET0 | ( | obj | ) |
#define M0_SET_ARR0 | ( | arr | ) |
#define M0_UNUSED __attribute__((unused)) |
M0_INTERNAL void m0_array_sort | ( | uint64_t * | arr, |
uint64_t | arr_len | ||
) |
M0_INTERNAL bool m0_bit_get | ( | void * | buffer, |
m0_bcount_t | i | ||
) |
M0_INTERNAL void m0_bit_set | ( | void * | buffer, |
m0_bcount_t | i, | ||
bool | val | ||
) |
M0_INTERNAL const char* m0_bool_to_str | ( | bool | b | ) |
M0_INTERNAL uint64_t m0_dummy_id_generate | ( | void | ) |
M0_INTERNAL bool m0_elems_are_unique | ( | const void * | array, |
unsigned | nr_elems, | ||
size_t | elem_size | ||
) |
M0_INTERNAL unsigned int m0_full_name_hash | ( | const unsigned char * | name, |
unsigned int | len | ||
) |
M0_INTERNAL void m0_key_val_init | ( | struct m0_key_val * | kv, |
const struct m0_buf * | key, | ||
const struct m0_buf * | val | ||
) |
M0_INTERNAL bool m0_key_val_is_null | ( | struct m0_key_val * | kv | ) |
M0_INTERNAL void m0_key_val_null_set | ( | struct m0_key_val * | kv | ) |
M0_INTERNAL uint32_t m0_no_of_bits_set | ( | uint64_t | val | ) |
M0_INTERNAL void m0_permute | ( | uint64_t | n, |
uint64_t * | k, | ||
uint64_t * | s, | ||
uint64_t * | r | ||
) |
Apply a permutation given by its Lehmer code in k[] to a set s[] of n elements and build inverse permutation in r[].
n | - number of elements in k[], s[] and r[] |
k | - Lehmer code of the permutation |
s | - an array to permute |
r | - an array to build inverse permutation in |
Definition at line 288 of file misc.c.
M0_INTERNAL const void* m0_ptr_unwrap | ( | uint64_t | val | ) |
M0_INTERNAL uint64_t m0_ptr_wrap | ( | const void * | p | ) |
Converts Motr function pointer in a form that can be stored somewhere (e.g., in a trace log or addb2 record) and later decoded back into original pointer.
Such transformation is needed, because function pointers depend on the address at which Motr library is loaded.
Definition at line 277 of file misc.c.
M0_INTERNAL uint64_t m0_round_down | ( | uint64_t | val, |
uint64_t | size | ||
) |
M0_INTERNAL uint64_t m0_round_up | ( | uint64_t | val, |
uint64_t | size | ||
) |
M0_INTERNAL const char* m0_short_file_name | ( | const char * | fname | ) |
Extracts the file name, relative to a motr sources directory, from a full-path file name. A motr source directory is detected by a name "motr/".
For example, given the following full-path file name:
/path/to/motr/lib/ut/finject.c
A short file name, relative to the "motr/" directory, is:
lib/ut/finject.c
/path/to/motr/fs/motr/lib/misc.h => fs/motr/lib/misc.h
}
fname | full path |
Definition at line 212 of file misc.c.
uint32_t m0_strtou32 | ( | const char * | str, |
char ** | endptr, | ||
int | base | ||
) |
uint64_t m0_strtou64 | ( | const char * | str, |
char ** | endptr, | ||
int | base | ||
) |
M0_INTERNAL void* m0_vote_majority_get | ( | struct m0_key_val * | arr, |
uint32_t | len, | ||
bool(*)(const struct m0_buf *, const struct m0_buf *) | cmp, | ||
uint32_t * | vote_nr | ||
) |
This API implements Boyer-Moore Voting Algorithm. Returns the majority element present in an input array. The majority element of an array, if present, is the element that occurs more than n/2 times in the array of length n. It has been assumed that members of the array can be compared for equality and method for the same needs to be provided by the user of the API. Returns null if there is no majority element.
Definition at line 383 of file misc.c.
M0_EXTERN const struct m0_key_val M0_KEY_VAL_NULL |