Zephyr Project API 3.7.0
A Scalable Open Source RTOS
|
Misc utilities. More...
#include <zephyr/sys/util_macro.h>
#include <zephyr/toolchain.h>
#include <stdbool.h>
#include <zephyr/sys/__assert.h>
#include <zephyr/types.h>
#include <stddef.h>
#include <stdint.h>
#include <zephyr/sys/time_units.h>
Go to the source code of this file.
Macros | |
#define | NUM_BITS(t) (sizeof(t) * 8) |
Number of bits that make up a type. | |
#define | POINTER_TO_UINT(x) ((uintptr_t) (x)) |
Cast x , a pointer, to an unsigned integer. | |
#define | UINT_TO_POINTER(x) ((void *) (uintptr_t) (x)) |
Cast x , an unsigned integer, to a void* . | |
#define | POINTER_TO_INT(x) ((intptr_t) (x)) |
Cast x , a pointer, to a signed integer. | |
#define | INT_TO_POINTER(x) ((void *) (intptr_t) (x)) |
Cast x , a signed integer, to a void* . | |
#define | BITS_PER_LONG (__CHAR_BIT__ * __SIZEOF_LONG__) |
Number of bits in a long int. | |
#define | BITS_PER_LONG_LONG (__CHAR_BIT__ * __SIZEOF_LONG_LONG__) |
Number of bits in a long long int. | |
#define | GENMASK(h, l) (((~0UL) - (1UL << (l)) + 1) & (~0UL >> (BITS_PER_LONG - 1 - (h)))) |
Create a contiguous bitmask starting at bit position l and ending at position h . | |
#define | GENMASK64(h, l) (((~0ULL) - (1ULL << (l)) + 1) & (~0ULL >> (BITS_PER_LONG_LONG - 1 - (h)))) |
Create a contiguous 64-bit bitmask starting at bit position l and ending at position h . | |
#define | LSB_GET(value) ((value) & -(value)) |
Extract the Least Significant Bit from value . | |
#define | FIELD_GET(mask, value) (((value) & (mask)) / LSB_GET(mask)) |
Extract a bitfield element from value corresponding to the field mask mask . | |
#define | FIELD_PREP(mask, value) (((value) * LSB_GET(mask)) & (mask)) |
Prepare a bitfield element using value with mask representing its field position and width. | |
#define | ZERO_OR_COMPILE_ERROR(cond) ((int) sizeof(char[1 - 2 * !(cond)]) - 1) |
0 if cond is true-ish; causes a compile error otherwise. | |
#define | IS_ARRAY(array) |
Zero if array has an array type, a compile error otherwise. | |
#define | ARRAY_SIZE(array) ((size_t) (IS_ARRAY(array) + (sizeof(array) / sizeof((array)[0])))) |
Number of elements in the given array . | |
#define | IS_ARRAY_ELEMENT(array, ptr) |
Whether ptr is an element of array . | |
#define | ARRAY_INDEX(array, ptr) |
Index of ptr within array . | |
#define | PART_OF_ARRAY(array, ptr) |
Check if a pointer ptr lies within array . | |
#define | ARRAY_INDEX_FLOOR(array, ptr) |
Array-index of ptr within array , rounded down. | |
#define | ARRAY_FOR_EACH(array, idx) for (size_t idx = 0; (idx) < ARRAY_SIZE(array); ++(idx)) |
Iterate over members of an array using an index variable. | |
#define | ARRAY_FOR_EACH_PTR(array, ptr) |
Iterate over members of an array using a pointer. | |
#define | SAME_TYPE(a, b) __builtin_types_compatible_p(__typeof__(a), __typeof__(b)) |
Validate if two entities have a compatible type. | |
#define | CONTAINER_OF_VALIDATE(ptr, type, field) |
Validate CONTAINER_OF parameters, only applies to C mode. | |
#define | CONTAINER_OF(ptr, type, field) |
Get a pointer to a structure containing the element. | |
#define | SIZEOF_FIELD(type, member) sizeof((((type *)0)->member)) |
Report the size of a struct field in bytes. | |
#define | CONCAT(...) UTIL_CAT(_CONCAT_, NUM_VA_ARGS_LESS_1(__VA_ARGS__))(__VA_ARGS__) |
Concatenate input arguments. | |
#define | IS_ALIGNED(ptr, align) (((uintptr_t)(ptr)) % (align) == 0) |
Check if ptr is aligned to align alignment. | |
#define | ROUND_UP(x, align) |
Value of x rounded up to the next multiple of align . | |
#define | ROUND_DOWN(x, align) (((unsigned long)(x) / (unsigned long)(align)) * (unsigned long)(align)) |
Value of x rounded down to the previous multiple of align . | |
#define | WB_UP(x) ROUND_UP(x, sizeof(void *)) |
Value of x rounded up to the next word boundary. | |
#define | WB_DN(x) ROUND_DOWN(x, sizeof(void *)) |
Value of x rounded down to the previous word boundary. | |
#define | DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d)) |
Divide and round up. | |
#define | DIV_ROUND_CLOSEST(n, d) |
Divide and round to the nearest integer. | |
#define | ceiling_fraction(numerator, divider) |
Ceiling function applied to numerator / divider as a fraction. | |
#define | MAX(a, b) (((a) > (b)) ? (a) : (b)) |
Obtain the maximum of two values. | |
#define | MIN(a, b) (((a) < (b)) ? (a) : (b)) |
Obtain the minimum of two values. | |
#define | CLAMP(val, low, high) (((val) <= (low)) ? (low) : MIN(val, high)) |
Clamp a value to a given range. | |
#define | IN_RANGE(val, min, max) ((val) >= (min) && (val) <= (max)) |
Checks if a value is within range. | |
#define | LOG2(x) ((x) < 1 ? -1 : __z_log2(x)) |
Compute log2(x) | |
#define | LOG2CEIL(x) ((x) < 1 ? 0 : __z_log2((x)-1) + 1) |
Compute ceil(log2(x)) | |
#define | NHPOT(x) ((x) < 1 ? 1 : ((x) > (1ULL<<63) ? 0 : 1ULL << LOG2CEIL(x))) |
Compute next highest power of two. | |
#define | KB(x) ((x) << 10) |
Number of bytes in x kibibytes. | |
#define | MB(x) (KB(x) << 10) |
Number of bytes in x mebibytes. | |
#define | GB(x) (MB(x) << 10) |
Number of bytes in x gibibytes. | |
#define | KHZ(x) ((x) * 1000) |
Number of Hz in x kHz. | |
#define | MHZ(x) (KHZ(x) * 1000) |
Number of Hz in x MHz. | |
#define | WAIT_FOR(expr, timeout, delay_stmt) |
Wait for an expression to return true with a timeout. | |
Functions | |
static bool | is_power_of_two (unsigned int x) |
Is x a power of two? | |
static ALWAYS_INLINE bool | is_null_no_warn (void *p) |
Is p equal to NULL ? | |
static int64_t | arithmetic_shift_right (int64_t value, uint8_t shift) |
Arithmetic shift right. | |
static void | bytecpy (void *dst, const void *src, size_t size) |
byte by byte memcpy. | |
static void | byteswp (void *a, void *b, size_t size) |
byte by byte swap. | |
int | char2hex (char c, uint8_t *x) |
Convert a single character into a hexadecimal nibble. | |
int | hex2char (uint8_t x, char *c) |
Convert a single hexadecimal nibble into a character. | |
size_t | bin2hex (const uint8_t *buf, size_t buflen, char *hex, size_t hexlen) |
Convert a binary array into string representation. | |
size_t | hex2bin (const char *hex, size_t hexlen, uint8_t *buf, size_t buflen) |
Convert a hexadecimal string into a binary array. | |
static uint8_t | bcd2bin (uint8_t bcd) |
Convert a binary coded decimal (BCD 8421) value to binary. | |
static uint8_t | bin2bcd (uint8_t bin) |
Convert a binary value to binary coded decimal (BCD 8421). | |
uint8_t | u8_to_dec (char *buf, uint8_t buflen, uint8_t value) |
Convert a uint8_t into a decimal string representation. | |
static int32_t | sign_extend (uint32_t value, uint8_t index) |
Sign extend an 8, 16 or 32 bit value using the index bit as sign bit. | |
static int64_t | sign_extend_64 (uint64_t value, uint8_t index) |
Sign extend a 64 bit value using the index bit as sign bit. | |
char * | utf8_trunc (char *utf8_str) |
Properly truncate a NULL-terminated UTF-8 string. | |
char * | utf8_lcpy (char *dst, const char *src, size_t n) |
Copies a UTF-8 encoded string from src to dst . | |
static void | mem_xor_n (uint8_t *dst, const uint8_t *src1, const uint8_t *src2, size_t len) |
XOR n bytes. | |
static void | mem_xor_32 (uint8_t dst[4], const uint8_t src1[4], const uint8_t src2[4]) |
XOR 32 bits. | |
static void | mem_xor_128 (uint8_t dst[16], const uint8_t src1[16], const uint8_t src2[16]) |
XOR 128 bits. | |
Misc utilities.
Misc utilities usable by the kernel and application code.