You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

230 lines
6.4 KiB

  1. /**
  2. @file
  3. @ingroup util
  4. @brief Low-level utilities.
  5. @copyright@parblock
  6. Copyright (c) 1994-1998 The Regents of the Univ. of California.
  7. All rights reserved.
  8. Permission is hereby granted, without written agreement and without license
  9. or royalty fees, to use, copy, modify, and distribute this software and its
  10. documentation for any purpose, provided that the above copyright notice and
  11. the following two paragraphs appear in all copies of this software.
  12. IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
  13. DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
  14. OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  15. CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  17. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  18. FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
  19. "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO PROVIDE
  20. MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  21. @endparblock
  22. @copyright@parblock
  23. Copyright (c) 1999-2015, Regents of the University of Colorado
  24. All rights reserved.
  25. Redistribution and use in source and binary forms, with or without
  26. modification, are permitted provided that the following conditions
  27. are met:
  28. Redistributions of source code must retain the above copyright
  29. notice, this list of conditions and the following disclaimer.
  30. Redistributions in binary form must reproduce the above copyright
  31. notice, this list of conditions and the following disclaimer in the
  32. documentation and/or other materials provided with the distribution.
  33. Neither the name of the University of Colorado nor the names of its
  34. contributors may be used to endorse or promote products derived from
  35. this software without specific prior written permission.
  36. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  37. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  38. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  39. FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  40. COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  41. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  42. BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  43. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  44. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  45. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  46. ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  47. POSSIBILITY OF SUCH DAMAGE.
  48. @endparblock
  49. */
  50. #ifndef UTIL_H_
  51. #define UTIL_H_
  52. #include "config.h"
  53. #if HAVE_ASSERT_H == 1
  54. #include <assert.h>
  55. #else
  56. #error assert.h is needed to build this package
  57. #endif
  58. #if HAVE_UNISTD_H == 1
  59. #include <unistd.h>
  60. #endif
  61. #include <stdio.h>
  62. #include <ctype.h>
  63. #if HAVE_STDLIB_H
  64. #include <stdlib.h>
  65. #else
  66. #error stdlib.h is needed to build this package
  67. #endif
  68. #if HAVE_STRING_H == 1
  69. #include <string.h>
  70. #else
  71. #error string.h is needed to build this package
  72. #endif
  73. #if HAVE_INTTYPES_H == 1
  74. #include <inttypes.h>
  75. #else
  76. #error inttypes.h is needed to build this package
  77. #endif
  78. /**
  79. * @def PRIszt
  80. * @brief Format string for a size_t value.
  81. */
  82. #if defined(_WIN32) && !defined(__USE_MINGW_ANSI_STDIO)
  83. #ifndef PRIuPTR
  84. #define PRIuPTR "Iu"
  85. #endif
  86. #ifndef PRIxPTR
  87. #define PRIxPTR "Ix"
  88. #endif
  89. #ifndef PRIiPTR
  90. #define PRIiPTR "Id"
  91. #endif
  92. #define PRIszt "Iu"
  93. #else
  94. #define PRIszt "zu"
  95. #endif
  96. /**
  97. * @def UTIL_UNUSED
  98. * @brief Macro to tell gcc that a variable is intentionally unused.
  99. */
  100. #if defined(__GNUC__)
  101. #if __GNUC__ > 2 || __GNUC_MINOR__ >= 7
  102. #define UTIL_UNUSED __attribute__ ((unused))
  103. #else
  104. #define UTIL_UNUSED
  105. #endif
  106. #else
  107. #define UTIL_UNUSED
  108. #endif
  109. /**
  110. * @brief Type-decorated NULL (for documentation).
  111. */
  112. #define NIL(type) ((type *) 0)
  113. /* #define USE_MM */ /* choose default memory allocator */
  114. /**
  115. * @def ALLOC
  116. * @brief Wrapper for either malloc or MMalloc.
  117. * @details Which function is wrapped depends on whether USE_MM is defined.
  118. */
  119. /**
  120. * @def REALLOC
  121. * @brief Wrapper for either realloc or MMrealloc.
  122. * @details Which function is wrapped depends on whether USE_MM is defined.
  123. */
  124. /**
  125. * @def FREE
  126. * @brief Wrapper for free.
  127. * @details Sets its argument to 0 after freeing.
  128. */
  129. #if defined(USE_MM)
  130. /* Assumes the memory manager is default one. */
  131. #define ALLOC(type, num) \
  132. ((type *) malloc(sizeof(type) * (num)))
  133. #define REALLOC(type, obj, num) \
  134. ((type *) realloc(obj, sizeof(type) * (num)))
  135. #else
  136. /* Use replacements that call MMoutOfMemory if allocation fails. */
  137. #define ALLOC(type, num) \
  138. ((type *) MMalloc(sizeof(type) * (size_t) (num)))
  139. #define REALLOC(type, obj, num) \
  140. ((type *) MMrealloc((obj), sizeof(type) * (size_t) (num)))
  141. #endif
  142. /* In any case, set to zero the pointer to freed memory. */
  143. #define FREE(obj) (free(obj), (obj) = 0)
  144. /**
  145. * @brief Prints message and terminates execution.
  146. */
  147. #define fail(why) {\
  148. (void) fprintf(stderr, "Fatal error: file %s, line %d\n%s\n",\
  149. __FILE__, __LINE__, why);\
  150. (void) fflush(stdout);\
  151. abort();\
  152. }
  153. /* These arguably do NOT belong in util.h */
  154. /**
  155. * @brief Computes the absolute value of its argument.
  156. */
  157. #define ABS(a) ((a) < 0 ? -(a) : (a))
  158. /**
  159. * @brief Computes the maximum of its two arguments.
  160. */
  161. #define MAX(a,b) ((a) > (b) ? (a) : (b))
  162. /**
  163. * @brief Computes the minimum of its two arguments.
  164. */
  165. #define MIN(a,b) ((a) < (b) ? (a) : (b))
  166. /**
  167. * @brief Type of comparison functions for util_qsort.
  168. */
  169. typedef int (*QSFP)(void const *, void const *);
  170. #ifdef __cplusplus
  171. extern "C" {
  172. #endif
  173. #ifndef USE_MM
  174. extern void *MMalloc(size_t);
  175. extern void *MMrealloc(void *, size_t);
  176. #endif
  177. extern void MMout_of_memory(size_t);
  178. extern void (*MMoutOfMemory) (size_t);
  179. extern long util_cpu_time(void);
  180. extern long util_cpu_ctime(void);
  181. extern char *util_path_search(char const *);
  182. extern char *util_file_search(char const *, char *, char const *);
  183. extern void util_print_cpu_stats(FILE *);
  184. extern char *util_print_time(unsigned long);
  185. extern char *util_strsav(char const *);
  186. extern char *util_tilde_expand(char const *);
  187. extern size_t getSoftDataLimit(void);
  188. extern void util_qsort (void *vbase, int n, int size, QSFP compar);
  189. extern int util_pipefork(char * const * argv, FILE ** toCommand,
  190. FILE ** fromCommand, int * pid);
  191. #ifdef __cplusplus
  192. }
  193. #endif
  194. #endif /* UTIL_H_ */