CMagic  0.5.0
Portable C library of utilities and data structures
Macros | Functions
utils.h File Reference

General purpose utilities. More...

#include <stddef.h>
#include <stdint.h>

Go to the source code of this file.

Macros

#define CMAGIC_UTILS_DIV_CEIL(dividend, divisor)   (((dividend) + (divisor) - 1) / (divisor))
 Performs division of two integers and returns ceiling of the result. More...
 
#define CMAGIC_UTILS_ARRAY_SIZE(array)   (sizeof(array) / sizeof((array)[0]))
 Returns the capacity of the array. More...
 
#define CMAGIC_UTILS_ASSERT_SAME_TYPE(expr1, expr2)   ((void)sizeof(&(expr1)-&(expr2)))
 Checks if two L-value expressions have the same type. More...
 
#define CMAGIC_UTILS_MIN(val1, val2)   ((val1) < (val2) ? (val1) : (val2))
 Returns smaller of two values. More...
 
#define CMAGIC_UTILS_MAX(val1, val2)   ((val1) > (val2) ? (val1) : (val2))
 Returns greater of two values. More...
 

Functions

uintptr_t cmagic_utils_align_address_up (uintptr_t unaligned_addr, size_t required_alignment)
 Increases the address if it's not aligned. More...
 
uintptr_t cmagic_utils_align_address_down (uintptr_t unaligned_addr, size_t required_alignment)
 Decreases the address if it's not aligned. More...
 

Detailed Description

General purpose utilities.

Macro Definition Documentation

◆ CMAGIC_UTILS_ARRAY_SIZE

#define CMAGIC_UTILS_ARRAY_SIZE (   array)    (sizeof(array) / sizeof((array)[0]))

Returns the capacity of the array.

Warning
Use only when have access to the array definition. This won't work for an array passed via a pointer.

◆ CMAGIC_UTILS_ASSERT_SAME_TYPE

#define CMAGIC_UTILS_ASSERT_SAME_TYPE (   expr1,
  expr2 
)    ((void)sizeof(&(expr1)-&(expr2)))

Checks if two L-value expressions have the same type.

The expressions must be L-values because this macro needs addresses of them. Subtraction of two pointers without any casting causes compilation error if the types don't match.

◆ CMAGIC_UTILS_DIV_CEIL

#define CMAGIC_UTILS_DIV_CEIL (   dividend,
  divisor 
)    (((dividend) + (divisor) - 1) / (divisor))

Performs division of two integers and returns ceiling of the result.

◆ CMAGIC_UTILS_MAX

#define CMAGIC_UTILS_MAX (   val1,
  val2 
)    ((val1) > (val2) ? (val1) : (val2))

Returns greater of two values.

◆ CMAGIC_UTILS_MIN

#define CMAGIC_UTILS_MIN (   val1,
  val2 
)    ((val1) < (val2) ? (val1) : (val2))

Returns smaller of two values.

Function Documentation

◆ cmagic_utils_align_address_down()

uintptr_t cmagic_utils_align_address_down ( uintptr_t  unaligned_addr,
size_t  required_alignment 
)

Decreases the address if it's not aligned.

Parameters
unaligned_addroriginal address
required_alignmentrequired alignment of the resulting address
Returns
unaligned_addr rounded DOWN so it's divisible by required_alignment

◆ cmagic_utils_align_address_up()

uintptr_t cmagic_utils_align_address_up ( uintptr_t  unaligned_addr,
size_t  required_alignment 
)

Increases the address if it's not aligned.

Parameters
unaligned_addroriginal address
required_alignmentrequired alignment of the resulting address
Returns
unaligned_addr rounded UP so it's divisible by required_alignment