ucx
UAP Common Extensions
Loading...
Searching...
No Matches
Macros | Functions
utils.h File Reference

General purpose utility functions. More...

#include "common.h"

Go to the source code of this file.

Macros

#define cx_for_n(varname, n)   for (size_t varname = 0 ; (varname) < (n) ; (varname)++)
 Convenience macro for a for loop that counts from zero to n-1.
 
#define cx_swap_ptr(left, right)   do {void *cx_tmp_swap_var = left; left = right; right = cx_tmp_swap_var;} while(0)
 Convenience macro for swapping two pointers.
 
#define cx_szmul(a, b, result)   cx_szmul_impl(a, b, result)
 Performs a multiplication of size_t values and checks for overflow.
 
#define cx_stream_bcopy(src, dest, rfnc, wfnc, buf, bufsize)    cx_stream_bncopy(src, dest, rfnc, wfnc, buf, bufsize, SIZE_MAX)
 Reads data from a stream and writes it to another stream.
 
#define cx_stream_copy(src, dest, rfnc, wfnc)    cx_stream_ncopy(src, dest, rfnc, wfnc, SIZE_MAX)
 Reads data from a stream and writes it to another stream.
 

Functions

int cx_szmul_impl (size_t a, size_t b, size_t *result)
 Performs a multiplication of size_t values and checks for overflow.
 
size_t cx_stream_bncopy (void *src, void *dest, cx_read_func rfnc, cx_write_func wfnc, char *buf, size_t bufsize, size_t n)
 Reads data from a stream and writes it to another stream.
 
size_t cx_stream_ncopy (void *src, void *dest, cx_read_func rfnc, cx_write_func wfnc, size_t n)
 Reads data from a stream and writes it to another stream.
 

Detailed Description

General purpose utility functions.

Author
Mike Becker
Olaf Wintermann
Version
3.0

Macro Definition Documentation

◆ cx_stream_bcopy

#define cx_stream_bcopy (   src,
  dest,
  rfnc,
  wfnc,
  buf,
  bufsize 
)     cx_stream_bncopy(src, dest, rfnc, wfnc, buf, bufsize, SIZE_MAX)

Reads data from a stream and writes it to another stream.

Parameters
srcthe source stream
destthe destination stream
rfncthe read function
wfncthe write function
bufa pointer to the copy buffer or NULL if a buffer shall be implicitly created on the heap
bufsizethe size of the copy buffer - if buf is NULL you can set this to zero to let the implementation decide
Returns
total number of bytes copied

◆ cx_stream_copy

#define cx_stream_copy (   src,
  dest,
  rfnc,
  wfnc 
)     cx_stream_ncopy(src, dest, rfnc, wfnc, SIZE_MAX)

Reads data from a stream and writes it to another stream.

The data is temporarily stored in a stack allocated buffer.

Parameters
srcthe source stream
destthe destination stream
rfncthe read function
wfncthe write function
Returns
total number of bytes copied

◆ cx_szmul

#define cx_szmul (   a,
  b,
  result 
)    cx_szmul_impl(a, b, result)

Performs a multiplication of size_t values and checks for overflow.

Parameters
afirst operand
bsecond operand
resulta pointer to a size_t, where the result should be stored
Returns
zero, if no overflow occurred and the result is correct, non-zero otherwise

Function Documentation

◆ cx_stream_bncopy()

size_t cx_stream_bncopy ( void *  src,
void *  dest,
cx_read_func  rfnc,
cx_write_func  wfnc,
char *  buf,
size_t  bufsize,
size_t  n 
)

Reads data from a stream and writes it to another stream.

Parameters
srcthe source stream
destthe destination stream
rfncthe read function
wfncthe write function
bufa pointer to the copy buffer or NULL if a buffer shall be implicitly created on the heap
bufsizethe size of the copy buffer - if buf is NULL you can set this to zero to let the implementation decide
nthe maximum number of bytes that shall be copied. If this is larger than bufsize, the content is copied over multiple iterations.
Returns
the total number of bytes copied

◆ cx_stream_ncopy()

size_t cx_stream_ncopy ( void *  src,
void *  dest,
cx_read_func  rfnc,
cx_write_func  wfnc,
size_t  n 
)

Reads data from a stream and writes it to another stream.

The data is temporarily stored in a stack allocated buffer.

Parameters
srcthe source stream
destthe destination stream
rfncthe read function
wfncthe write function
nthe maximum number of bytes that shall be copied.
Returns
total number of bytes copied

◆ cx_szmul_impl()

int cx_szmul_impl ( size_t  a,
size_t  b,
size_t *  result 
)

Performs a multiplication of size_t values and checks for overflow.

This is a custom implementation in case there is no compiler builtin available.

Parameters
afirst operand
bsecond operand
resulta pointer to a size_t where the result should be stored
Returns
zero, if no overflow occurred and the result is correct, non-zero otherwise