- Main Page
- Data Structures
- Files
- Related Pages

# generic.h File Reference

Generic. More...

`#include <stddef.h>`
`#include <assert.h>`

Go to the source code of this file.

## Heap allocation
#define | VL_PRINTF(format,...) ((*vl_printf_func)((format), __VA_ARGS__))
| Call user-customizable `printf` function.
#define | VL_PRINT(string) ((*vl_printf_func)(string))
| Call user-customizable `printf` function (no varags).
void | vl_set_alloc_func (void *(*malloc_func)(vl_size), void *(*realloc_func)(void *, vl_size), void *(*calloc_func)(vl_size, vl_size), void(*free_func)(void *))
| Set memory allocation functions.
VL_INLINE void * | vl_malloc (vl_size n)
| Call customizable `malloc` function.
VL_INLINE void * | vl_realloc (void *ptr, vl_size n)
| Call customizable `resize` function.
VL_INLINE void * | vl_calloc (vl_size n, vl_size size)
| Call customizable `calloc` function.
VL_INLINE void | vl_free (void *ptr)
| Call customizable `free` function.
void | vl_set_printf_func (int(*printf_func)(char const *str,...))
| Call user-customizable `printf` function.
## Error handling
#define | VL_ERR_MSG_LEN 1024
| Error description maximum length.
#define | VL_ERR_OK 0
#define | VL_ERR_OVERFLOW 1
#define | VL_ERR_ALLOC 2
#define | VL_ERR_BAD_ARG 3
#define | VL_ERR_IO 4
#define | VL_ERR_EOF 5
#define | VL_ERR_NO_MORE 5
int | vl_err_no
| Last error code.
char | vl_err_msg [VL_ERR_MSG_LEN]
| Last error description.
## Endianness detection and conversions
#define | VL_LITTLE_ENDIAN 0
#define | VL_BIG_ENDIAN 1
#define | VL_ENDIANNESS VL_BIG_ENDIAN
| Host endianness.
VL_INLINE int | vl_get_endianness ()
| Get endianness.
VL_INLINE void | vl_adapt_endianness_8 (void *dst, void *src)
| Change endianness of 8-byte value if required.
VL_INLINE void | vl_adapt_endianness_4 (void *dst, void *src)
| Change endianness of 4-byte value if required.
VL_INLINE void | vl_adapt_endianness_2 (void *dst, void *src)
| Change endianness of 2-byte value if required.
## Defines
#define | VL_VERSION_STRING "0.1"
| Library version string.
#define | VL_INLINE static __inline__
| Inline function declaration.
#define | VL_STRINGIFY_(x) # x
| Stringify argument helper.
#define | VL_STRINGIFY(x) VL_STRINGIFY_(x)
| Stringify macro.
#define | VL_BIG_INT 2147483647
| Big integer.
#define | VL_SMALL_INT (- VL_BIG_INT - 1)
| Small integer.
#define | VL_LOG_OF_2 0.693147180559945
| Logarithm of 2.
#define | VL_NAN_F (vl_nan_f.value)
| Single NaN (not signaling).
#define | VL_INFINITY_F (vl_infinity_f.value)
| Single Infinity (not signaling).
#define | VL_NAN_D (vl_nan_d.value)
| Double NaN (not signaling).
#define | VL_INFINITY_D (vl_infinity_d.value)
| Double Infinity (not signaling).
Common operations
#define | VL_MIN(x, y) (((x)<(y))?(x):(y))
| Min operation.
#define | VL_MAX(x, y) (((x)>(y))?(x):(y))
| Max operation.
#define | VL_SHIFT_LEFT(x, n) (((n)>=0)?((x)<<(n)):((x)>>-(n)))
| Signed left shift operation.
## Typedefs
Atomic data types
typedef long long | vl_int64
typedef int | vl_int32
typedef short | vl_int16
typedef char | vl_int8
typedef long long unsigned | vl_uint64
typedef int unsigned | vl_uint32
typedef short unsigned | vl_uint16
typedef char unsigned | vl_uint8
typedef int | vl_int
typedef unsigned int | vl_uint
typedef float | vl_single
typedef double | vl_double
typedef unsigned int | vl_uidx
typedef size_t | vl_size
typedef int | vl_bool
## Functions
char const * | vl_get_version_string ()
| Get version string.
## Variables
int(* | vl_printf_func )(char const *format,...)
| Customizable `printf` function pointer.
void *(* | vl_malloc_func )(vl_size)
| Customizable `malloc` function pointer.
void *(* | vl_realloc_func )(void *, vl_size)
| Customizable `realloc` function pointer.
void *(* | vl_calloc_func )(vl_size, vl_size)
| Customizable `calloc` function pointer.
void(* | vl_free_func )(void *)
| Customizable `free` function pointer.

## Detailed Description

Author:
  Andrea Vedaldi

This module provides basic facilities such as atomi types definitions, error handling, endianness detection, serialization, logging.

## Error handling

Error handling uses the same style of the standard C library. Most functions return 0 when they succeed and -1 when they fail, and set the global variable vl_err_no with a code identifying the error occurred. This variable is never set on success and should be examinated right after an error occurred.

## Endinanness detection and conversions

An architecture is big endiand or little endian depending how multi-btye data are stored in memory:

- big endian (big end first - Network order) if it stores the most significant byte has the smaller memory address.
- little endian (small end first - Intel format) if it stores the lesser significant byte at the smaller memory address.

Use the function vl_get_endianness() to detect endianness. To serialize/deserialize data in big endian (network) order, call the functions vl_adapt_endianness_8(), vl_adapt_endianness_4(), vl_adapt_endianness_2() after reading and before writing (the functions change edinanness only if the architecture is little endian and do nothing otherwise).

Author:
  Andrea Vedaldi

## Define Documentation

#define VL_BIG_ENDIAN 1

big endian.

#define VL_ENDIANNESS VL_BIG_ENDIAN

This macro is equal to VL_BIG_ENDIAN or VL_LITTLE_ENDIAN depending on the endianness of the host.

#define VL_ERR_ALLOC 2

Resource allocation error

#define VL_ERR_BAD_ARG 3

Bad argument or illegal data error

#define VL_ERR_EOF 5

End-of-file or end-of-sequence error

#define VL_ERR_IO 4

Input/output error

#define VL_ERR_MSG_LEN 1024

This is the size of the vl_err_msg buffer.

#define VL_ERR_NO_MORE 5

End-of-sequence

Deprecated:

#define VL_ERR_OK 0

No error

#define VL_ERR_OVERFLOW 1

Buffer overflow error

#define VL_LITTLE_ENDIAN 0

little endian.

#define VL_MAX | ( | x,
|  | y |  | ) | (((x)>(y))?(x):(y))

Parameters:

  | x | value.
  | y | value.

Returns:
  the maximum of x and y.

#define VL_MIN | ( | x,
|  | y |  | ) | (((x)<(y))?(x):(y))

Parameters:

  | x | value
  | y | value

Returns:
  the minimum of x and y.

#define VL_PRINT | ( | string |  | ) | ((*vl_printf_func)(string))

Parameters:

  | string | string to print.

This macro is the same as VL_PRINTF() and should be used when the variable list of arguments is empty.

Returns:
  results of the user-customizable `printf`.

#define VL_PRINTF | ( | format,
|  | ... |  | ) | ((*vl_printf_func)((format), __VA_ARGS__))

Parameters:

  | format | format string.
  | ... | `printf` variable arguments.

The function calls the user customizable `printf`.

Returns:
  results of the user-customizable `printf`.

#define VL_SHIFT_LEFT | ( | x,
|  | n |  | ) | (((n)>=0)?((x)<<(n)):((x)>>-(n)))

The macro is equivalent to the builtin `<<` operator, but it supports negative shifts too.

Parameters:

  | x | value.
  | n | number of shift positions.

Returns:
  `x` << n .

#define VL_STRINGIFY | ( | x |  | ) | VL_STRINGIFY_(x)

Parameters:

  | x | macro to stringify

For instance, the following fragment

     #define A x.y.z
     printf(VL_STRINGIFY(A)) ;

will print the string `x.y.z`.

#define VL_STRINGIFY_ | ( | x |  | ) | # x

For internal use only.

See also:
  VL_STRINGIFY

## Typedef Documentation

typedef int vl_bool

boolean

typedef double vl_double

double precision float.

typedef int vl_int

integer.

typedef short vl_int16

signed 16-bit integer.

typedef int vl_int32

signed 32-bit integer.

typedef long long vl_int64

signed 64-bit integer.

typedef char vl_int8

signed 8-bit integer.

typedef float vl_single

single precision float.

typedef size_t vl_size

size data type.

typedef unsigned int vl_uidx

unsigned index data type.

typedef unsigned int vl_uint

unsigned integer.

typedef short unsigned vl_uint16

unsigned 16-bit integer.

typedef int unsigned vl_uint32

unsigned 32-bit integer.

typedef long long unsigned vl_uint64

unsigned 64-bit integer.

typedef char unsigned vl_uint8

unsigned 8-bit integer.

## Function Documentation

VL_INLINE void vl_adapt_endianness_2 | ( | void * | dst,
|  | void * | src |
| ) |  |  |

Parameters:

  | dst | destination 2-byte buffer.
  | src | source 2-byte bufffer.

See also:
  generic-endianness.

VL_INLINE void vl_adapt_endianness_4 | ( | void * | dst,
|  | void * | src |
| ) |  |  |

Parameters:

  | dst | destination 4-byte buffer.
  | src | source 4-byte bufffer.

See also:
  Endinanness detection and conversions

VL_INLINE void vl_adapt_endianness_8 | ( | void * | dst,
|  | void * | src |
| ) |  |  |

Parameters:

  | dst | destination 8-byte buffer.
  | src | source 8-byte bufffer.

See also:
  Endinanness detection and conversions.

VL_INLINE void * vl_calloc | ( | vl_size | n,
|  | vl_size | size |
| ) |  |  |

Parameters:

  | n | size of each element in byte.
  | size | size of the array to allocate (number of elements).

The function calls the user-customizable `calloc`.

Returns:
  result of the user-customizable `calloc`.

VL_INLINE void vl_free | ( | void * | ptr | ) |

Parameters:

  | ptr | buffer to free.

The function calls the user customizable `free`.

VL_INLINE int vl_get_endianness | ( |  | ) |

Returns:
  `VL_BIG_ENDIAN` or VL_LITTLE_ENDIAN depending on the host endianness.

See also:
  Endinanness detection and conversions

char const* vl_get_version_string | ( |  | ) |

Returns:
  library version string

VL_INLINE void * vl_malloc | ( | vl_size | n | ) |

Parameters:

  | n | number of bytes to allocate.

The function calls the user customizable `malloc`.

Returns:
  result of `malloc`

VL_INLINE void * vl_realloc | ( | void * | ptr,
|  | vl_size | n |
| ) |  |  |

Parameters:

  | ptr | buffer to reallocate.
  | n | number of bytes to allocate.

The function calls the user-customizable `realloc`.

Returns:
  result of the user-customizable `realloc`.

void vl_set_alloc_func | ( | void *(*)(vl_size) | malloc_func,
|  | void *(*)(void *, vl_size) | realloc_func,
|  | void *(*)(vl_size, vl_size) | calloc_func,
|  | void(*)(void *) | free_func |
| ) |  |  |

Parameters:

  | malloc_func | pointer to `malloc`.
  | realloc_func | pointer to `realloc`.
  | calloc_func | pointer to `calloc`.
  | free_func | pointer to `free`.

void vl_set_printf_func | ( | int(*)(char const *str,...) | printf_func | ) |

Parameters:

  | format | format string.
  | ... | `printf` variable arguments.

The function calls the user customizable `printf`.

Returns:
  results of the user-customizable `printf`.

## Variable Documentation

void*(* vl_calloc_func)(vl_size, vl_size)

For internal use only.

void(* vl_free_func)(void *)

For internal use only.

void*(* vl_malloc_func)(vl_size)

For internal use only.

int(* vl_printf_func)(char const *format,...)

For internal use only.

void*(* vl_realloc_func)(void *, vl_size)

For internal use only.