Zephyr Project API  3.2.0
A Scalable Open Source RTOS
util.h File Reference

Misc utilities. More...

#include <zephyr/sys/util_macro.h>
#include <stdbool.h>
#include <zephyr/types.h>
#include <stddef.h>

Go to the source code of this file.

Macros

#define POINTER_TO_UINT(x)   ((uintptr_t) (x))
 Cast x, a pointer, to an unsigned integer. More...
 
#define UINT_TO_POINTER(x)   ((void *) (uintptr_t) (x))
 Cast x, an unsigned integer, to a void*. More...
 
#define POINTER_TO_INT(x)   ((intptr_t) (x))
 Cast x, a pointer, to a signed integer. More...
 
#define INT_TO_POINTER(x)   ((void *) (intptr_t) (x))
 Cast x, a signed integer, to a void*. More...
 
#define BITS_PER_LONG   (__CHAR_BIT__ * __SIZEOF_LONG__)
 
#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. More...
 
#define LSB_GET(value)   ((value) & -(value))
 Extract the Least Significant Bit from value. More...
 
#define FIELD_GET(mask, value)   (((value) & (mask)) / LSB_GET(mask))
 Extract a bitfield element from value corresponding to the field mask mask. More...
 
#define FIELD_PREP(mask, value)   (((value) * LSB_GET(mask)) & (mask))
 Prepare a bitfield element using value with mask representing its field position and width. The result should be combined with other fields using a logical OR. More...
 
#define ZERO_OR_COMPILE_ERROR(cond)   ((int) sizeof(char[1 - 2 * !(cond)]) - 1)
 0 if cond is true-ish; causes a compile error otherwise. More...
 
#define IS_ARRAY(array)
 Zero if array has an array type, a compile error otherwise. More...
 
#define ARRAY_SIZE(array)    ((size_t) (IS_ARRAY(array) + (sizeof(array) / sizeof((array)[0]))))
 Number of elements in the given array. More...
 
#define IS_ARRAY_ELEMENT(array, ptr)
 Whether ptr is an element of array. More...
 
#define ARRAY_INDEX(array, ptr)
 Index of ptr within array. More...
 
#define PART_OF_ARRAY(array, ptr)
 Check if a pointer ptr lies within array. More...
 
#define ARRAY_INDEX_FLOOR(array, ptr)
 Array-index of ptr within array, rounded down. More...
 
#define CONTAINER_OF(ptr, type, field)    ((type *)(((char *)(ptr)) - offsetof(type, field)))
 Get a pointer to a structure containing the element. More...
 
#define ROUND_UP(x, align)
 Value of x rounded up to the next multiple of align, which must be a power of 2. More...
 
#define ROUND_DOWN(x, align)    ((unsigned long)(x) & ~((unsigned long)(align) - 1))
 Value of x rounded down to the previous multiple of align, which must be a power of 2. More...
 
#define WB_UP(x)   ROUND_UP(x, sizeof(void *))
 Value of x rounded up to the next word boundary. More...
 
#define WB_DN(x)   ROUND_DOWN(x, sizeof(void *))
 Value of x rounded down to the previous word boundary. More...
 
#define ceiling_fraction(numerator, divider)    (((numerator) + ((divider) - 1)) / (divider))
 Ceiling function applied to numerator / divider as a fraction. More...
 
#define MAX(a, b)   (((a) > (b)) ? (a) : (b))
 Obtain the maximum of two values. More...
 
#define MIN(a, b)   (((a) < (b)) ? (a) : (b))
 Obtain the minimum of two values. More...
 
#define CLAMP(val, low, high)   (((val) <= (low)) ? (low) : MIN(val, high))
 Clamp a value to a given range. More...
 
#define IN_RANGE(val, min, max)   ((val) >= (min) && (val) <= (max))
 Checks if a value is within range. More...
 
#define KB(x)   ((x) << 10)
 Number of bytes in x kibibytes. More...
 
#define MB(x)   (KB(x) << 10)
 Number of bytes in x mebibytes. More...
 
#define GB(x)   (MB(x) << 10)
 Number of bytes in x gibibytes. More...
 
#define KHZ(x)   ((x) * 1000)
 Number of Hz in x kHz. More...
 
#define MHZ(x)   (KHZ(x) * 1000)
 Number of Hz in x MHz. More...
 
#define WAIT_FOR(expr, timeout, delay_stmt)
 Wait for an expression to return true with a timeout. More...
 

Functions

static bool is_power_of_two (unsigned int x)
 Is x a power of two? More...
 
static int64_t arithmetic_shift_right (int64_t value, uint8_t shift)
 Arithmetic shift right. More...
 
static void bytecpy (void *dst, const void *src, size_t size)
 byte by byte memcpy. More...
 
static void byteswp (void *a, void *b, size_t size)
 byte by byte swap. More...
 
int char2hex (char c, uint8_t *x)
 Convert a single character into a hexadecimal nibble. More...
 
int hex2char (uint8_t x, char *c)
 Convert a single hexadecimal nibble into a character. More...
 
size_t bin2hex (const uint8_t *buf, size_t buflen, char *hex, size_t hexlen)
 Convert a binary array into string representation. More...
 
size_t hex2bin (const char *hex, size_t hexlen, uint8_t *buf, size_t buflen)
 Convert a hexadecimal string into a binary array. More...
 
static uint8_t bcd2bin (uint8_t bcd)
 Convert a binary coded decimal (BCD 8421) value to binary. More...
 
static uint8_t bin2bcd (uint8_t bin)
 Convert a binary value to binary coded decimal (BCD 8421). More...
 
uint8_t u8_to_dec (char *buf, uint8_t buflen, uint8_t value)
 Convert a uint8_t into a decimal string representation. More...
 
char * utf8_trunc (char *utf8_str)
 Properly truncate a NULL-terminated UTF-8 string. More...
 
char * utf8_lcpy (char *dst, const char *src, size_t n)
 Copies a UTF-8 encoded string from src to dst. More...
 

Detailed Description

Misc utilities.

Misc utilities usable by the kernel and application code.