File: mxstdlib.h

package info (click to toggle)
python-mxstack 0.3.0-2
  • links: PTS
  • area: main
  • in suites: potato
  • size: 204 kB
  • ctags: 272
  • sloc: ansic: 1,329; python: 163; makefile: 44; sh: 19
file content (201 lines) | stat: -rw-r--r-- 5,433 bytes parent folder | download | duplicates (2)
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
#ifndef MXSTDLIB_H
#define MXSTDLIB_H

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

   (c) Marc-Andre Lemburg; All Rights Reserved; mailto: mal@lemburg.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 "#x" at %X to size "#c"=%i (%s:%i)\n",x,c,__FILE__,__LINE__),\
	  (x *)realloc((void*)(var),sizeof(x)*(c)))
# 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 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
# define MAL_DEBUG
#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 "stderr"
#endif

/* Log id to be used */

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

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

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

	now = time(NULL);
	filename = getenv(MAL_DEBUG_OUTPUTFILE_ENV_VARIABLE);
	if (!filename)
	    filename = MAL_DEBUG_OUTPUTFILE;
	if (strcmp(filename,"stdout") == 0)
	    mxDebugPrintf_file = stdout;
	else if (strcmp(filename,"stderr") == 0)
	    mxDebugPrintf_file = stderr;
	else
	    mxDebugPrintf_file = fopen(filename,"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",filename);
	}
	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