1: #line 2686 "mxTools.pak"
2: #ifndef MXSTDLIB_H
3: #define MXSTDLIB_H
4:
5: /* Standard stuff I use often -- not Python specific
6:
7: (c) Marc-Andre Lemburg; All Rights Reserved; mailto: mal@lemburg.com
8: See the documentation for further copyright information or contact
9: the author.
10:
11: */
12:
13: #include <stdlib.h>
14: #include <stdio.h>
15: #include <stdarg.h>
16: #include <string.h>
17: #include <time.h>
18: #include <math.h>
19: #ifdef HAVE_LIMITS_H
20: #include <limits.h>
21: #else
22: #define INT_MAX 2147483647
23: #endif
24:
25: /* --- My own macros for memory allocation... --------------------------- */
26:
27: #ifdef MAL_MEM_DEBUG
28: # define newstruct(x) \
29: (mxDebugPrintf("* malloc for struct "#x" (%s:%i)\n",__FILE__,__LINE__),\
30: (x *)malloc(sizeof(x)))
31: # define cnewstruct(x) \
32: (mxDebugPrintf("* calloc for struct "#x" (%s:%i)\n",c,__FILE__,__LINE__),\
33: (x *)calloc(sizeof(x),1))
34: # define new(x,c) \
35: (mxDebugPrintf("* malloc for "#c"=%i '"#x"'s (%s:%i)\n",c,__FILE__,__LINE__),\
36: (x *)malloc(sizeof(x)*(c)))
37: # define cnew(x,c) \
38: (mxDebugPrintf("* calloc for "#c"=%i '"#x"'s (%s:%i)\n",c,__FILE__,__LINE__),\
39: (x *)calloc((c),sizeof(x)))
40: # define resize(var,x,c) \
41: (mxDebugPrintf("* realloc "#x" at %X to size "#c"=%i (%s:%i)\n",x,c,__FILE__,__LINE__),\
42: (x *)realloc((void*)(var),sizeof(x)*(c)))
43: # define free(x) \
44: (mxDebugPrintf("* freeing "#x" at %X (%s:%i)\n",x,__FILE__,__LINE__),\
45: free((void*)(x)))
46: #else
47: # define newstruct(x) (x *)malloc(sizeof(x))
48: # define cnewstruct(x) (x *)calloc(sizeof(x),1)
49: # define new(x,c) (x *)malloc(sizeof(x)*(c))
50: # define cnew(x,c) (x *)calloc((c),sizeof(x))
51: # define resize(var,x,c) (x *)realloc((void*)(var),sizeof(x)*(c))
52: # define free(x) free((void*)(x))
53: #endif
54:
55: /* --- Debugging output ------------------------------------------------- */
56:
57: /* Indicator for the availability of these interfaces: */
58:
59: #define HAVE_MAL_DEBUG
60:
61: /* File name to be used for debug logging (each object file using this
62: facility may set its own logging file): */
63:
64: #ifndef MAL_DEBUG_OUTPUTFILE
65: # define MAL_DEBUG_OUTPUTFILE "stderr"
66: #endif
67:
68: /* Log id to be used */
69:
70: #ifndef MAL_DEBUG_LOGID
71: # define MAL_DEBUG_LOGID "New Log Session"
72: #endif
73:
74: static
75: void mxDebugPrintf(const char *format, ...)
76: {
77: va_list args;
78: static FILE *mxDebugPrintf_file;
79:
80: if (!mxDebugPrintf_file) {
81: time_t now;
82:
83: now = time(NULL);
84: if (strcmp(MAL_DEBUG_OUTPUTFILE,"stdout") == 0)
85: mxDebugPrintf_file = stdout;
86: else if (strcmp(MAL_DEBUG_OUTPUTFILE,"stderr") == 0)
87: mxDebugPrintf_file = stderr;
88: else
89: mxDebugPrintf_file = fopen(MAL_DEBUG_OUTPUTFILE,"ab");
90: if (!mxDebugPrintf_file) {
91: /* Hack to shut up "cc -Wall" warning that this function
92: was not used... */
93: static void *mxDebugPrintf_used;
94: mxDebugPrintf_used = (void *)mxDebugPrintf;
95: /* Default to stderr in case the log file cannot be opened */
96: mxDebugPrintf_file = stderr;
97: fprintf(mxDebugPrintf_file,
98: "\n*** Failed to open log file '"MAL_DEBUG_LOGID"'; "
99: "using stderr\n");
100: }
101: fprintf(mxDebugPrintf_file,
102: "\n--- "MAL_DEBUG_LOGID" --- %s\n",
103: ctime(&now));
104: }
105:
106: va_start(args,format);
107: vfprintf(mxDebugPrintf_file,format,args);
108: fflush(mxDebugPrintf_file);
109: va_end(args);
110: return;
111: }
112:
113: #ifdef MAL_DEBUG
114: # if defined(PyFloat_AS_DOUBLE) /* check whether we're compiling a Python ext. */
115: # define DPRINTF if (Py_DebugFlag) mxDebugPrintf
116: # define IF_DEBUGGING if (Py_DebugFlag)
117: # else
118: # define DPRINTF mxDebugPrintf
119: # define IF_DEBUGGING
120: # endif
121: #else
122: # define DPRINTF if (0) mxDebugPrintf
123: # define IF_DEBUGGING if (0)
124: #endif
125:
126: /* --- Misc ------------------------------------------------------------- */
127:
128: /* The usual bunch... */
129: #ifndef max
130: #define max(a,b) ((a>b)?(a):(b))
131: #endif
132: #ifndef min
133: #define min(a,b) ((a<b)?(a):(b))
134: #endif
135:
136: /* EOF */
137: #endif
138: