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.

204 lines
5.3 KiB

  1. /* $Id: util.h,v 1.10 2012/02/05 05:34:04 fabio Exp fabio $ */
  2. #ifndef UTIL_H
  3. #define UTIL_H
  4. #ifdef __cplusplus
  5. extern "C" {
  6. #endif
  7. #if defined(__GNUC__)
  8. # define UTIL_INLINE __inline__
  9. # if __GNUC__ > 2 || __GNUC_MINOR__ >= 7
  10. # define UTIL_UNUSED __attribute__ ((unused))
  11. # else
  12. # define UTIL_UNUSED
  13. # endif
  14. #else
  15. # define UTIL_INLINE
  16. # define UTIL_UNUSED
  17. #endif
  18. #ifndef SIZEOF_VOID_P
  19. #define SIZEOF_VOID_P 4
  20. #endif
  21. #ifndef SIZEOF_INT
  22. #define SIZEOF_INT 4
  23. #endif
  24. #ifndef SIZEOF_LONG
  25. #define SIZEOF_LONG 4
  26. #endif
  27. #if SIZEOF_VOID_P == 8 && SIZEOF_INT == 4
  28. typedef long util_ptrint;
  29. #else
  30. typedef int util_ptrint;
  31. #endif
  32. /* #define USE_MM */ /* choose libmm.a as the memory allocator */
  33. /* these are too entrenched to get away with changing the name */
  34. #define strsav util_strsav
  35. #include <unistd.h>
  36. #define NIL(type) ((type *) 0)
  37. #if defined(USE_MM) || defined(MNEMOSYNE)
  38. /*
  39. * assumes the memory manager is either libmm.a or libmnem.a
  40. * libmm.a:
  41. * - allows malloc(0) or realloc(obj, 0)
  42. * - catches out of memory (and calls MMout_of_memory())
  43. * - catch free(0) and realloc(0, size) in the macros
  44. * libmnem.a:
  45. * - reports memory leaks
  46. * - is used in conjunction with the mnemalyse postprocessor
  47. */
  48. #ifdef MNEMOSYNE
  49. #include "mnemosyne.h"
  50. #define ALLOC(type, num) \
  51. ((num) ? ((type *) malloc(sizeof(type) * (num))) : \
  52. ((type *) malloc(sizeof(long))))
  53. #else
  54. #define ALLOC(type, num) \
  55. ((type *) malloc(sizeof(type) * (num)))
  56. #endif
  57. #define REALLOC(type, obj, num) \
  58. (obj) ? ((type *) realloc((char *) obj, sizeof(type) * (num))) : \
  59. ((type *) malloc(sizeof(type) * (num)))
  60. #define FREE(obj) \
  61. ((obj) ? (free((char *) (obj)), (obj) = 0) : 0)
  62. #else
  63. /*
  64. * enforce strict semantics on the memory allocator
  65. * - when in doubt, delete the '#define USE_MM' above
  66. */
  67. #define ALLOC(type, num) \
  68. ((type *) MMalloc((long) sizeof(type) * (long) (num)))
  69. #define REALLOC(type, obj, num) \
  70. ((type *) MMrealloc((char *) (obj), (long) sizeof(type) * (long) (num)))
  71. #define FREE(obj) \
  72. ((obj) ? (free((char *) (obj)), (obj) = 0) : 0)
  73. #endif
  74. /* Ultrix (and SABER) have 'fixed' certain functions which used to be int */
  75. #if defined(ultrix) || defined(SABER) || defined(aiws) || defined(hpux) || defined(apollo) || defined(__osf__) || defined(__SVR4) || defined(__GNUC__)
  76. #define VOID_OR_INT void
  77. #define VOID_OR_CHAR void
  78. #else
  79. #define VOID_OR_INT int
  80. #define VOID_OR_CHAR char
  81. #endif
  82. /* No machines seem to have much of a problem with these */
  83. #include <stdio.h>
  84. #include <ctype.h>
  85. /* Some machines fail to define some functions in stdio.h */
  86. #if !defined(__STDC__) && !defined(__cplusplus)
  87. extern FILE *popen(), *tmpfile();
  88. extern int pclose();
  89. #endif
  90. /* most machines don't give us a header file for these */
  91. #if (defined(__STDC__) || defined(__cplusplus) || defined(ultrix)) && !defined(MNEMOSYNE) || defined(__SVR4)
  92. # include <stdlib.h>
  93. #else
  94. # ifndef _IBMR2
  95. extern VOID_OR_INT abort(), exit();
  96. # endif
  97. # if !defined(MNEMOSYNE) && !defined(_IBMR2)
  98. extern VOID_OR_INT free (void *);
  99. extern VOID_OR_CHAR *malloc(), *realloc();
  100. # endif
  101. extern char *getenv();
  102. extern int system();
  103. extern double atof();
  104. #endif
  105. /* some call it strings.h, some call it string.h; others, also have memory.h */
  106. #if defined(__STDC__) || defined(__cplusplus) || defined(_IBMR2) || defined(ultrix)
  107. #include <string.h>
  108. #else
  109. /* ANSI C string.h -- 1/11/88 Draft Standard */
  110. extern char *strcpy(), *strncpy(), *strcat(), *strncat(), *strerror();
  111. extern char *strpbrk(), *strtok(), *strchr(), *strrchr(), *strstr();
  112. extern int strcoll(), strxfrm(), strncmp(), strlen(), strspn(), strcspn();
  113. extern char *memmove(), *memccpy(), *memchr(), *memcpy(), *memset();
  114. extern int memcmp(), strcmp();
  115. #endif
  116. #ifdef __STDC__
  117. #include <assert.h>
  118. #else
  119. #ifndef NDEBUG
  120. #define assert(ex) {\
  121. if (! (ex)) {\
  122. (void) fprintf(stderr,\
  123. "Assertion failed: file %s, line %d\n\"%s\"\n",\
  124. __FILE__, __LINE__, "ex");\
  125. (void) fflush(stdout);\
  126. abort();\
  127. }\
  128. }
  129. #else
  130. #define assert(ex) ;
  131. #endif
  132. #endif
  133. #define fail(why) {\
  134. (void) fprintf(stderr, "Fatal error: file %s, line %d\n%s\n",\
  135. __FILE__, __LINE__, why);\
  136. (void) fflush(stdout);\
  137. abort();\
  138. }
  139. #ifdef lint
  140. #undef putc /* correct lint '_flsbuf' bug */
  141. #undef ALLOC /* allow for lint -h flag */
  142. #undef REALLOC
  143. #define ALLOC(type, num) (((type *) 0) + (num))
  144. #define REALLOC(type, obj, num) ((obj) + (num))
  145. #endif
  146. /* These arguably do NOT belong in util.h */
  147. #define ABS(a) ((a) < 0 ? -(a) : (a))
  148. #define MAX(a,b) ((a) > (b) ? (a) : (b))
  149. #define MIN(a,b) ((a) < (b) ? (a) : (b))
  150. #ifndef USE_MM
  151. extern char *MMalloc (long);
  152. extern void MMout_of_memory (long);
  153. extern void (*MMoutOfMemory) (long);
  154. extern char *MMrealloc (char *, long);
  155. #endif
  156. extern long util_cpu_time (void);
  157. extern char *util_path_search (char const *);
  158. extern char *util_file_search (char const *, char *, char const *);
  159. extern int util_pipefork (char * const *, FILE **, FILE **, int *);
  160. extern void util_print_cpu_stats (FILE *);
  161. extern char *util_print_time (unsigned long);
  162. extern int util_save_image (char const *, char const *);
  163. extern char *util_strsav (char const *);
  164. extern char *util_tilde_expand (char const *);
  165. extern void util_restart (char const *, char const *, int);
  166. extern unsigned long getSoftDataLimit (void);
  167. #ifdef __cplusplus
  168. }
  169. #endif
  170. #endif /* UTIL_H */