File: mxstdlib.h

package info (click to toggle)
egenix-mx-base 2.0.6-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 3,028 kB
  • ctags: 4,762
  • sloc: ansic: 14,965; python: 11,739; sh: 313; makefile: 117
file content (254 lines) | stat: -rw-r--r-- 7,248 bytes parent folder | download | duplicates (16)
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
#ifndef MXSTDLIB_H
#define MXSTDLIB_H

/* Standard stuff I use often -- not Python specific 

   Copyright (c) 2000, Marc-Andre Lemburg; mailto:mal@lemburg.com
   Copyright (c) 2000-2004, eGenix.com Software GmbH; mailto:info@egenix.com
   See the documentation for further copyright information or contact
   the author.

 */

#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <time.h>
#include <math.h>
#ifdef HAVE_LIMITS_H
#include <limits.h>
#else
#ifndef INT_MAX
# define INT_MAX 2147483647
#endif
#ifndef LONG_MAX
# define LONG_MAX INT_MAX
#endif
#endif

/* --- My own macros for memory allocation... --------------------------- */

#ifdef MAL_MEM_DEBUG
# define newstruct(x) \
         (mxDebugPrintf("* malloc for struct "#x" (%s:%i)\n",__FILE__,__LINE__),\
	  (x *)malloc(sizeof(x)))
# define cnewstruct(x) \
         (mxDebugPrintf("* calloc for struct "#x" (%s:%i)\n",c,__FILE__,__LINE__),\
	  (x *)calloc(sizeof(x),1))
# define new(x,c) \
         (mxDebugPrintf("* malloc for "#c"=%i '"#x"'s (%s:%i)\n",c,__FILE__,__LINE__),\
	  (x *)malloc(sizeof(x)*(c)))
# define cnew(x,c) \
         (mxDebugPrintf("* calloc for "#c"=%i '"#x"'s (%s:%i)\n",c,__FILE__,__LINE__),\
	  (x *)calloc((c),sizeof(x)))
# define resize(var,x,c) \
         (mxDebugPrintf("* realloc array "#var" ("#x") at %X to size "#c"=%i (%s:%i)\n",var,c,__FILE__,__LINE__),\
	  (x *)realloc((void*)(var),sizeof(x)*(c)))
# define varresize(var,x,bytes) \
         (mxDebugPrintf("* realloc var "#var" ("#x") at %X to %i bytes (%s:%i)\n",var,bytes,__FILE__,__LINE__),\
	  (x *)realloc((void*)(var),(bytes)))
# define free(x) \
         (mxDebugPrintf("* freeing "#x" at %X (%s:%i)\n",x,__FILE__,__LINE__),\
	  free((void*)(x)))
#else
# define newstruct(x)		((x *)malloc(sizeof(x)))
# define cnewstruct(x)		((x *)calloc(sizeof(x),1))
# define new(x,c)		((x *)malloc(sizeof(x)*(c)))
# define cnew(x,c)		((x *)calloc((c),sizeof(x)))
# define resize(var,x,c)	((x *)realloc((void*)(var),sizeof(x)*(c)))
# define varresize(var,x,bytes)	((x *)realloc((void*)(var),(bytes)))
# define free(x) 		free((void*)(x))
#endif

/* --- Debugging output ------------------------------------------------- */

/* Use the flag MAL_DEBUG to enable debug processing.

   The flag MAL_DEBUG_WITH_PYTHON can be used to indicate that the
   object file will be linked with Python, so we can use Python APIs
   for the debug processing here.

*/
#ifdef MAL_DEBUG_WITH_PYTHON
# ifndef PYTHON_API_VERSION
#  error "mx.h must be included when compiling with MAL_DEBUG_WITH_PYTHON"
# endif
# ifndef MAL_DEBUG
#  define MAL_DEBUG
# endif
#else
# if defined(PYTHON_API_VERSION) && defined(MAL_DEBUG)
#  define MAL_DEBUG_WITH_PYTHON
# endif
#endif

/* Indicator for the availability of these interfaces: */

#define HAVE_MAL_DEBUG

/* Name of the environment variable defining the log file name
   to be used: */

#ifndef MAL_DEBUG_OUTPUTFILE_ENV_VARIABLE
# define MAL_DEBUG_OUTPUTFILE_ENV_VARIABLE "mxLogFile"
#endif

/* File name to be used for debug logging (each object file using this
   facility may set its own logging file) if no environment variable
   is set: */

#ifndef MAL_DEBUG_OUTPUTFILE
# define MAL_DEBUG_OUTPUTFILE "mx.log"
#endif

/* Name of the environment variable defining the log file prefix to be
   used (e.g. to direct all log files into a separate directory): */

#ifndef MAL_DEBUG_OUTPUTFILEPREFIX_ENV_VARIABLE
# define MAL_DEBUG_OUTPUTFILEPREFIX_ENV_VARIABLE "mxLogFileDir"
#endif

/* File name prefix to be used for log files, if no environment
   variable is set: */

#ifndef MAL_DEBUG_OUTPUTFILEPREFIX
# define MAL_DEBUG_OUTPUTFILEPREFIX ""
#endif

/* Log id to be used */

#ifndef MAL_DEBUG_LOGID
# define MAL_DEBUG_LOGID "New Log Session"
#endif

/* Debug printf() API

   Output is written to a log file or stream. If the output file is
   not yet open, the function will try to open the file as defined by
   the environment or the program defines.  The file remains open
   until the program terminates. Subsequent changes to the environment
   are not taken into account.

   The output file is deduced in the following way:

   1. get the filename from the environment, revert to the predefined
      value

   2. get the filename prefix from the environment, revert to
      the predefined value
   
   3. if filename is one of "stderr" or "stdout" use the native
      streams for output; otherwise try to open fileprefix + filename
      reverting to stderr in case this fails.

 */

static
int mxDebugPrintf(const char *format, ...)
{
    va_list args;
    static FILE *mxDebugPrintf_file;

    if (!mxDebugPrintf_file) {
	time_t now;
	char *filename,*fileprefix;

	now = time(NULL);
	filename = getenv(MAL_DEBUG_OUTPUTFILE_ENV_VARIABLE);
	if (!filename)
	    filename = MAL_DEBUG_OUTPUTFILE;
	fileprefix = getenv(MAL_DEBUG_OUTPUTFILEPREFIX_ENV_VARIABLE);
	if (!fileprefix)
	    fileprefix = MAL_DEBUG_OUTPUTFILEPREFIX;
	if (strcmp(filename,"stdout") == 0)
	    mxDebugPrintf_file = stdout;
	else if (strcmp(filename,"stderr") == 0)
	    mxDebugPrintf_file = stderr;
	else {
	    char logfile[512];

	    strncpy(logfile,fileprefix,sizeof(logfile));
	    strncat(logfile,filename,sizeof(logfile));
	    mxDebugPrintf_file = fopen(logfile,"ab");
	    if (!mxDebugPrintf_file) {
		/* Hack to shut up "cc -Wall" warning that this
		   function is not used... */
		static void *mxDebugPrintf_used;
		mxDebugPrintf_used = (void *)mxDebugPrintf;
		/* Default to stderr in case the log file cannot be
                   opened */
		mxDebugPrintf_file = stderr;
		fprintf(mxDebugPrintf_file,
			"\n*** Failed to open log file '%s'; "
			"using stderr\n",logfile);
	    }
	}
	fprintf(mxDebugPrintf_file,
		"\n--- "MAL_DEBUG_LOGID" --- %s\n",
		ctime(&now));
    }

    va_start(args,format);
    vfprintf(mxDebugPrintf_file,format,args);
    fflush(mxDebugPrintf_file);
    va_end(args);
    return 1;
}

#ifdef MAL_DEBUG

# ifdef MAL_DEBUG_WITH_PYTHON
/* Use the Python debug flag to enable debugging output (python -d) */
#  define DPRINTF if (Py_DebugFlag) mxDebugPrintf
#  define IF_DEBUGGING if (Py_DebugFlag) 
#  define DEBUGGING (Py_DebugFlag > 0)
# else

/* Always output debugging information */
#  define DPRINTF mxDebugPrintf
#  define IF_DEBUGGING  
#  define DEBUGGING (1)
# endif

#else

# ifndef _MSC_VER
/* This assumes that you are using an optimizing compiler which
   eliminates the resulting debug code. */
#  define DPRINTF if (0) mxDebugPrintf
#  define IF_DEBUGGING if (0) 
#  define DEBUGGING (0)
# else

/* MSVC doesn't do a good job here, so we use a different approach. */
#  define DPRINTF 0 && mxDebugPrintf
#  define IF_DEBUGGING if (0) 
#  define DEBUGGING (0)
# endif

#endif

/* --- Misc ------------------------------------------------------------- */

/* The usual bunch... */
#ifndef max
# define max(a,b) ((a>b)?(a):(b))
#endif
#ifndef min
# define min(a,b) ((a<b)?(a):(b))
#endif

/* Bit testing... returns 1 iff bit is on in value, 0 otherwise */
#ifndef testbit
# define testbit(value,bit) (((value) & (1<<(bit))) != 0)
#endif

/* Flag testing... returns 1 iff flag is on in value, 0 otherwise */
#ifndef testflag
# define testflag(value,flag) (((value) & (flag)) != 0)
#endif

/* EOF */
#endif