File size: 7,001 Bytes
d4035c1 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 | /** @file generic.h
** @author Andrea Vedaldi
** @brief Generic
**/
/* AUTORIGHTS
Copyright (C) 2007-09 Andrea Vedaldi and Brian Fulkerson
This file is part of VLFeat, available in the terms of the GNU
General Public License version 2.
*/
#ifndef VL_GENERIC_H
#define VL_GENERIC_H
#include <stddef.h>
#include <time.h>
#include <assert.h>
#include "host.h"
/** @brief Library version string */
#define VL_VERSION_STRING "0.9.5"
/** ------------------------------------------------------------------
** @name C preprocessor helper macros
** @{ */
/** @brief Convert the argument to a string
**
** @param x value to be stringified.
**
** This macro stringifies the argument @a x by means of the
** <code>#</code> prerpocessor operator.
**
** The standard C preprocessor does not prescan arguments which are
** stringified, so
**
** @code
** #define A B
** char const * str = VL_STRINGIFY(A) ;
** @endcode
**
** initializes <code>str</code> with a pointer to the string
** <code>"A"</code>, which mihgt be unexpected. To fix this issue,
** you can use ::VL_XSTRINGIFY.
**
** @sa ::VL_XSTRINGIFY
**/
#define VL_STRINGIFY(x) # x
/** @brief Expand and then convert the argument to a string
**
** @param x value to be macro-expanded and converted.
**
** This macro macro-expands the argument @a x and stringifies the
** result of the expansion. For instance
**
** @code
** #define A B
** char const * str = VL_STRINGIFY(A) ;
** @endcode
**
** initializes <code>str</code> with a pointer to the string
** <code>"B"</code>.
**
** @sa ::VL_STRINGIFY
**/
#define VL_XSTRINGIFY(x) VL_STRINGIFY(x)
/** @brief Concatenate the arguments into a lexical unit
**
** @param x first argument to be concatenated.
** @param y second argument to be concatenated.
**
** This macro concatenates its arguments into a single lexical unit
** by means of the <code>##</code> preprocessor operator. Notice that
** arguments concatenated by <code>##</code> are not pre-expanded by
** the C preprocessor. To macro-expand the arguments and then
** concatenate them,use ::VL_XCAT.
**
** @see ::VL_XCAT
**/
#define VL_CAT(x,y) x ## y
/** @brief Expand and then concatenate the arguments into a lexical unit
**
** @param x first argument to be concatenated.
** @param y second argument to be concatenated.
**
** This macro is the same as ::VL_CAT, except that the arguments are
** macro expanded before being concatenated.
**
** @see ::VL_CAT
**/
#define VL_XCAT(x,y) VL_CAT(x,y)
/** @} */
/** @brief Convert a boolean to "yes" or "no" strings
** @param x boolean to convert.
** A pointer to either the string "yes" (if @a x is true)
** or the string "no".
** @par Example
** @code
** VL_PRINTF("Is x true? %s.", VL_YESNO(x))
** @endcode
**/
#define VL_YESNO(x) ((x)?"yes":"no")
/** @name Type identidifers for atomic data types
** @{ */
#define VL_TYPE_FLOAT 1
#define VL_TYPE_DOUBLE 2
#define VL_TYPE_INT8 3
#define VL_TYPE_UINT8 4
#define VL_TYPE_INT16 5
#define VL_TYPE_UINT16 6
#define VL_TYPE_INT32 7
#define VL_TYPE_UINT32 8
#define VL_TYPE_INT64 9
#define VL_TYPE_UINT64 10
/** @} */
/** ------------------------------------------------------------------
** @name Memory allocation
** @{ */
VL_EXPORT
void vl_set_alloc_func (void *(*malloc_func) (size_t),
void *(*realloc_func) (void*,size_t),
void *(*calloc_func) (size_t, size_t),
void (*free_func) (void*)) ;
VL_INLINE void *vl_malloc (size_t n) ;
VL_INLINE void *vl_realloc (void *ptr, size_t n) ;
VL_INLINE void *vl_calloc (size_t n, size_t size) ;
VL_INLINE void vl_free (void* ptr) ;
/** @} */
/** ------------------------------------------------------------------
** @name Logging
** @{ */
/** ------------------------------------------------------------------
** @brief Customizable printf function pointer type */
typedef int(*printf_func_t) (char const *format, ...) ;
/** @brief Set printf function
** @param printf_func pointer to @c printf.
** Let @c print_func be NULL to disable printf.
**/
VL_EXPORT void vl_set_printf_func (printf_func_t printf_func) ;
/** @def VL_PRINTF
** @brief Call user-customizable @c printf function
**
** The function calls the user customizable @c printf.
**/
#define VL_PRINTF (*vl_printf_func)
/** @def VL_PRINT
** @brief Same as ::VL_PRINTF (legacy code)
**/
#define VL_PRINT (*vl_printf_func)
/** @} */
/** ------------------------------------------------------------------
** @name Error handling
** @{ */
/** @brief The number of the last error */
extern VL_EXPORT int vl_err_no ;
/** @brief The maximum length of an error description. */
#define VL_ERR_MSG_LEN 1024
/** @brief The description of the last error. */
extern VL_EXPORT char vl_err_msg [VL_ERR_MSG_LEN + 1] ;
#define VL_ERR_OK 0 /**< No error */
#define VL_ERR_OVERFLOW 1 /**< Buffer overflow error */
#define VL_ERR_ALLOC 2 /**< Resource allocation error */
#define VL_ERR_BAD_ARG 3 /**< Bad argument or illegal data error */
#define VL_ERR_IO 4 /**< Input/output error */
#define VL_ERR_EOF 5 /**< End-of-file or end-of-sequence error */
#define VL_ERR_NO_MORE 5 /**< End-of-sequence @deprecated */
/** @} */
/** ------------------------------------------------------------------
** @name Common operations
** @{ */
/** @brief Min operation
** @param x value
** @param y value
** @return the minimum of @a x and @a y.
**/
#define VL_MIN(x,y) (((x)<(y))?(x):(y))
/** @brief Max operation
** @param x value.
** @param y value.
** @return the maximum of @a x and @a y.
**/
#define VL_MAX(x,y) (((x)>(y))?(x):(y))
/** @brief Signed left shift operation
**
** The macro is equivalent to the builtin @c << operator, but it
** supports negative shifts too.
**
** @param x value.
** @param n number of shift positions.
** @return @c x << n .
**/
#define VL_SHIFT_LEFT(x,n) (((n)>=0)?((x)<<(n)):((x)>>-(n)))
/* @} */
/** --------------------------------------------------------------- */
VL_EXPORT
char const * vl_get_version_string () ;
VL_EXPORT
void vl_print_info () ;
/** ------------------------------------------------------------------
** @name Measuring time
** @{
**/
VL_EXPORT void vl_tic() ;
VL_EXPORT double vl_toc() ;
/** @} */
extern VL_EXPORT int (*vl_printf_func) (char const * format, ...) ;
extern VL_EXPORT void *(*vl_malloc_func) (size_t) ;
extern VL_EXPORT void *(*vl_realloc_func) (void*,size_t) ;
extern VL_EXPORT void *(*vl_calloc_func) (size_t, size_t) ;
extern VL_EXPORT void (*vl_free_func) (void*) ;
VL_INLINE
void* vl_malloc (size_t n)
{
return (*vl_malloc_func)(n) ;
}
VL_INLINE
void* vl_realloc (void* ptr, size_t n)
{
return (*vl_realloc_func)(ptr, n) ;
}
VL_INLINE
void* vl_calloc (size_t n, size_t size)
{
return (*vl_calloc_func)(n, size) ;
}
VL_INLINE
void vl_free (void *ptr)
{
(*vl_free_func)(ptr) ;
}
/* VL_GENERIC_H */
#endif
|