Zephyr API Documentation  2.7.0-rc2
A Scalable Open Source RTOS
util.h File Reference

Misc utilities. More...

#include <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 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)    ((long) (IS_ARRAY(array) + (sizeof(array) / sizeof((array)[0]))))
 Number of elements in the given array. More...
 
#define PART_OF_ARRAY(array, ptr)    ((ptr) && ((ptr) >= &array[0] && (ptr) < &array[ARRAY_SIZE(array)]))
 Check if a pointer ptr lies within array. More...
 
#define CONTAINER_OF(ptr, type, field)    ((type *)(((char *)(ptr)) - offsetof(type, field)))
 Get a pointer to a container structure from an 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))
 The larger value between a and b. More...
 
#define MIN(a, b)   (((a) < (b)) ? (a) : (b))
 The smaller value between a and b. More...
 
#define CLAMP(val, low, high)   (((val) <= (low)) ? (low) : MIN(val, high))
 Clamp a value to a given 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...
 

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...
 
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...
 

Detailed Description

Misc utilities.

Misc utilities usable by the kernel and application code.