1 /* 2 * wmslib/include/wms.h, part of wmslib (Library functions) 3 * Copyright (C) 1994 William Shubert. 4 * See "configure.h.in" for more copyright information. 5 * 6 * This contains a bunch of definitions I always like to have. 7 * This should ALWAYS be AFTER the last system include, BEFORE the first 8 * local include. 9 */ 10 11 #ifndef _WMS_H_ 12 #define _WMS_H_ 1 13 14 #include <configure.h> 15 16 #ifdef STDC_HEADERS 17 # include <stdlib.h> 18 # include <unistd.h> 19 #endif /* STDC_HEADERS */ 20 #include <stdio.h> 21 #include <signal.h> 22 #include <sys/types.h> 23 #include <ctype.h> 24 #if TIME_WITH_SYS_TIME 25 # include <sys/time.h> 26 # include <time.h> 27 #else /* !AC_TIME_WITH_SYS_TIME */ 28 # if HAVE_SYS_TIME_H 29 # include <sys/time.h> 30 # else /* !HAVE_SYS_TIME_H */ 31 # include <time.h> 32 # endif /* HAVE_SYS_TIME_H */ 33 #endif /* AC_TIME_WITH_SYS_TIME */ 34 #if X11_DISP 35 # include <X11/Xlib.h> 36 # include <X11/Xutil.h> 37 # include <X11/keysym.h> 38 #endif /* X11_DISP */ 39 #if SUN_SOUND 40 # include <errno.h> 41 # include <stropts.h> 42 # include <sun/audioio.h> 43 #endif /* SUN_SOUND */ 44 /* Get all these headers out of the way here. */ 45 #include <stdio.h> 46 #include <errno.h> 47 #if HAVE_STDLIB_H 48 # include <stdlib.h> 49 #endif 50 #if HAVE_UNISTD_H 51 # include <unistd.h> 52 #endif /* HAVE_UNISTD_H */ 53 54 #if HAVE_ASSERT_H && DEBUG 55 #include <assert.h> 56 #else 57 #define assert(ignore) 58 #endif /* HAVE_ASSERT_H && DEBUG */ 59 60 #if STDC_HEADERS || HAVE_STRING_H 61 #include <string.h> 62 /* An ANSI string.h and pre-ANSI memory.h might conflict. */ 63 #if !STDC_HEADERS && HAVE_MEMORY_H 64 #include <memory.h> 65 #endif /* not STDC_HEADERS and HAVE_MEMORY_H */ 66 #else /* not STDC_HEADERS and not HAVE_STRING_H */ 67 #include <strings.h> 68 #define memcpy(d,s,n) bcopy((s),(d),(n)) 69 #define strchr index 70 #define strrchr rindex 71 #endif /* Not STDC_HEADERS and not HAVE_STRING_H */ 72 73 #ifdef bool 74 #undef bool 75 #endif /* bool */ 76 #define bool int 77 78 #ifndef TRUE 79 #define FALSE 0 80 #define TRUE 1 81 #endif 82 83 /* int32 must be AT LEAT 32 bits. More is OK. */ 84 #ifdef int32 85 #undef int32 86 #endif /* int32 */ 87 #if (SIZEOF_INT >= 4) 88 #define int32 int 89 #else /* SIZEOF_INT < 4 */ 90 #define int32 long 91 #endif 92 93 #ifdef uint32 94 #undef uint32 95 #endif /* uint32 */ 96 #define uint32 unsigned int32 97 98 #ifdef uchar 99 #undef uchar 100 #endif /* uchar */ 101 #define uchar unsigned char 102 103 #ifdef ushort 104 #undef ushort 105 #endif /* ushort */ 106 #define ushort unsigned short 107 108 #ifdef uint 109 #undef uint 110 #endif /* uint */ 111 #define uint unsigned int 112 113 #ifdef ulong 114 #undef ulong 115 #endif 116 #define ulong unsigned long 117 118 #define int32_max 0x7fffffff 119 #define int32_min (-int32_max-1) 120 121 #define int_max ((int)((((1 << (8*(sizeof(int)-2)))-1)<<1)+1)) 122 #define int_min (-int_max-1) 123 #define uint_max ((((1U << (8*(sizeof(uint)-1)))-1)<<1)+1) 124 125 #define long_max ((long)((((1L << (8*(sizeof(long)-2)))-1)<<1)+1)) 126 #define long_min (-long_max-1) 127 #define ulong_max ((((1UL << (8*(sizeof(ulong)-1)))-1)<<1)+1) 128 129 130 /* 131 * This atoi sets err to TRUE if there's an overflow or a non-digit 132 * character around. If err is NULL it is just like regular atoi(). 133 */ 134 extern int wms_atoi(const char *str, bool *err); 135 extern double wms_atof(const char *str, bool *err); 136 137 138 /* 139 * Debugging aide 1 - The MAGIC macros. 140 * 141 * In every dynamically allocated structure, add this: 142 * #if DEBUG 143 * int magic; 144 * #endif / * DEBUG * / 145 * Then, every time you alloc a structure, _immediately_ do a MAGIC_SET(foo) 146 * on the newly freed buffer. Last thing before you free a structure, do 147 * a MAGIC_UNSET(foo). Then, before you access a structure, always first 148 * say "assert(MAGIC(foo))". This way, if you touch a structure after you 149 * free it, an assertion will trip! And if debug is set to zero, the 150 * checks all disappear and leave you with an efficient program. 151 */ 152 #if DEBUG 153 #define MAGIC_NUM 31415927 /* Any random number will do. */ 154 #define MAGIC_SET(x) ((x)->magic = MAGIC_NUM) 155 #define MAGIC_UNSET(x) ((x)->magic = 0) 156 #define MAGIC(x) ((x)->magic == MAGIC_NUM) 157 #define MAGICNULL(x) (((x) == NULL) || ((x)->magic == MAGIC_NUM)) 158 #define MAGIC_STRUCT int magic; 159 #else 160 #define MAGIC_SET(x) 161 #define MAGIC_UNSET(x) 162 #define MAGIC(x) 1 163 #define MAGIC_STRUCT 164 #endif /* magic */ 165 166 /* 167 * Debugging aide 2 - The wms_malloc routines 168 * 169 * This makes it much easier to spot memory leaks and objects freed multiple 170 * times. Basically, every time you call wms_alloc_stat(), you will see 171 * how much memory is allocated. If this grows indefinitely, you know 172 * that you have a memory leak. Doing wms_alloc_ring_show() will list 173 * EVERY wms_malloc that hasn't yet been freed. Ugh. Probably way 174 * more than you need to see. So when you call wms_alloc_ring_reset(), 175 * the list gets cleared, so call wms_alloc_ring_reset(), do the stuff 176 * that shouldn't leave buffers allocated but does, then call 177 * wms_alloc_ring_show() and voila! The files and lines where your 178 * memory leak is coming from! 179 * As in the MAGIC macros, when DEBUG is set to zero this stuff all 180 * disappears. Good thing, too, because it consumes bunches of memory and 181 * a significant amount of CPU cycles. 182 */ 183 #if DEBUG 184 extern void wms_free(void *deadbuf); 185 extern void wms_alloc_stat(void); 186 #define wms_malloc(x) wms_malloc_debug(x, __FILE__, __LINE__) 187 extern void *wms_malloc_debug(uint bufsize, const char *file, int line); 188 void wms_alloc_ring_reset(void); 189 void wms_alloc_ring_show(void); 190 #else 191 #define wms_free free 192 extern void *wms_malloc(uint bufsize); 193 #endif 194 195 extern const char *wms_progname; 196 197 /* 198 * A StdInt32 is an int that is exactly 4 bytes long and in little endian 199 * format. 200 */ 201 #if SIZEOF_INT == 4 202 typedef int StdInt32; 203 #elif SIZEOF_SHORT == 4 204 typedef short StdInt32; 205 #elif SIZEOF_LONG == 4 206 typedef long StdInt32; 207 #else 208 #error You must have a 4 byte data type to compile this program! 209 #endif 210 211 #if !WORDS_BIGENDIAN 212 213 #define stdInt32_int(i) (i) 214 #define int_stdInt32(i) (i) 215 216 #else /* Big endian machine. */ 217 218 extern int stdInt32_int(StdInt32 i); 219 extern StdInt32 int_stdInt32(int i); 220 221 #endif 222 223 224 #if !HAVE_STRERROR 225 extern const char *strerror(int error); 226 #endif 227 228 #if !HAVE_STRCASECMP 229 extern int strcasecmp(const char *s1, const char *s2); 230 #endif 231 232 #if !HAVE_MEMMOVE 233 extern void *memmove(void *dest, const void *src, size_t n); 234 #endif 235 236 #if !HAVE_GETDTABLESIZE 237 extern int getdtablesize(void); 238 #endif 239 240 #endif /* _WMS_H_ */ 241