1 /* env.h (GLPK environment) */
2 
3 /***********************************************************************
4 *  This code is part of GLPK (GNU Linear Programming Kit).
5 *  Copyright (C) 2000-2017 Free Software Foundation, Inc.
6 *  Written by Andrew Makhorin <mao@gnu.org>.
7 *
8 *  GLPK is free software: you can redistribute it and/or modify it
9 *  under the terms of the GNU General Public License as published by
10 *  the Free Software Foundation, either version 3 of the License, or
11 *  (at your option) any later version.
12 *
13 *  GLPK is distributed in the hope that it will be useful, but WITHOUT
14 *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 *  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
16 *  License for more details.
17 *
18 *  You should have received a copy of the GNU General Public License
19 *  along with GLPK. If not, see <http://www.gnu.org/licenses/>.
20 ***********************************************************************/
21 
22 #ifndef ENV_H
23 #define ENV_H
24 
25 #include "stdc.h"
26 
27 typedef struct ENV ENV;
28 typedef struct MBD MBD;
29 
30 #define SIZE_T_MAX (~(size_t)0)
31 /* largest value of size_t type */
32 
33 #define TBUF_SIZE 4096
34 /* terminal output buffer size, in bytes */
35 
36 #define EBUF_SIZE 1024
37 /* error message buffer size, in bytes */
38 
39 /* enable/disable flag: */
40 #define GLP_ON  1
41 #define GLP_OFF 0
42 
43 struct ENV
44 {     /* GLPK environment block */
45 #if 0 /* 14/I-2007 */
46       char version[7+1];
47       /* version string returned by the routine glp_version */
48 #endif
49       ENV *self;
50       /* pointer to this block to check its validity */
51       /*--------------------------------------------------------------*/
52       /* terminal output */
53       char *term_buf; /* char term_buf[TBUF_SIZE]; */
54       /* terminal output buffer */
55       int term_out;
56       /* flag to enable/disable terminal output */
57       int (*term_hook)(void *info, const char *s);
58       /* user-defined routine to intercept terminal output */
59       void *term_info;
60       /* transit pointer (cookie) passed to the routine term_hook */
61       FILE *tee_file;
62       /* output stream used to copy terminal output */
63       /*--------------------------------------------------------------*/
64       /* error handling */
65 #if 1 /* 07/XI-2015 */
66       int err_st;
67       /* error state flag; set on entry to glp_error */
68 #endif
69       const char *err_file;
70       /* value of the __FILE__ macro passed to glp_error */
71       int err_line;
72       /* value of the __LINE__ macro passed to glp_error */
73       void (*err_hook)(void *info);
74       /* user-defined routine to intercept abnormal termination */
75       void *err_info;
76       /* transit pointer (cookie) passed to the routine err_hook */
77       char *err_buf; /* char err_buf[EBUF_SIZE]; */
78       /* buffer to store error messages (used by I/O routines) */
79       /*--------------------------------------------------------------*/
80       /* dynamic memory allocation */
81       size_t mem_limit;
82       /* maximal amount of memory, in bytes, available for dynamic
83        * allocation */
84       MBD *mem_ptr;
85       /* pointer to the linked list of allocated memory blocks */
86       int mem_count;
87       /* total number of currently allocated memory blocks */
88       int mem_cpeak;
89       /* peak value of mem_count */
90       size_t mem_total;
91       /* total amount of currently allocated memory, in bytes; it is
92        * the sum of the size field over all memory block descriptors */
93       size_t mem_tpeak;
94       /* peak value of mem_total */
95 #if 1 /* 23/XI-2015 */
96       /*--------------------------------------------------------------*/
97       /* bignum module working area */
98       void *gmp_pool; /* DMP *gmp_pool; */
99       /* working memory pool */
100       int gmp_size;
101       /* size of working array */
102       unsigned short *gmp_work; /* ushort gmp_work[gmp_size]; */
103       /* working array */
104 #endif
105       /*--------------------------------------------------------------*/
106       /* dynamic linking support (optional) */
107       void *h_odbc;
108       /* handle to ODBC shared library */
109       void *h_mysql;
110       /* handle to MySQL shared library */
111 };
112 
113 struct MBD
114 {     /* memory block descriptor */
115       size_t size;
116       /* size of block, in bytes, including descriptor */
117       MBD *self;
118       /* pointer to this descriptor to check its validity */
119       MBD *prev;
120       /* pointer to previous memory block descriptor */
121       MBD *next;
122       /* pointer to next memory block descriptor */
123 };
124 
125 #define get_env_ptr _glp_get_env_ptr
126 ENV *get_env_ptr(void);
127 /* retrieve pointer to environment block */
128 
129 #define tls_set_ptr _glp_tls_set_ptr
130 void tls_set_ptr(void *ptr);
131 /* store global pointer in TLS */
132 
133 #define tls_get_ptr _glp_tls_get_ptr
134 void *tls_get_ptr(void);
135 /* retrieve global pointer from TLS */
136 
137 #define xputs glp_puts
138 void glp_puts(const char *s);
139 /* write string on terminal */
140 
141 #define xprintf glp_printf
142 void glp_printf(const char *fmt, ...);
143 /* write formatted output on terminal */
144 
145 #define xvprintf glp_vprintf
146 void glp_vprintf(const char *fmt, va_list arg);
147 /* write formatted output on terminal */
148 
149 int glp_term_out(int flag);
150 /* enable/disable terminal output */
151 
152 void glp_term_hook(int (*func)(void *info, const char *s), void *info);
153 /* install hook to intercept terminal output */
154 
155 int glp_open_tee(const char *fname);
156 /* start copying terminal output to text file */
157 
158 int glp_close_tee(void);
159 /* stop copying terminal output to text file */
160 
161 #ifndef GLP_ERRFUNC_DEFINED
162 #define GLP_ERRFUNC_DEFINED
163 typedef void (*glp_errfunc)(const char *fmt, ...);
164 #endif
165 
166 #define xerror glp_error_(__FILE__, __LINE__)
167 glp_errfunc glp_error_(const char *file, int line);
168 /* display fatal error message and terminate execution */
169 
170 #define xassert(expr) \
171       ((void)((expr) || (glp_assert_(#expr, __FILE__, __LINE__), 1)))
172 void glp_assert_(const char *expr, const char *file, int line);
173 /* check for logical condition */
174 
175 void glp_error_hook(void (*func)(void *info), void *info);
176 /* install hook to intercept abnormal termination */
177 
178 #define put_err_msg _glp_put_err_msg
179 void put_err_msg(const char *msg);
180 /* provide error message string */
181 
182 #define get_err_msg _glp_get_err_msg
183 const char *get_err_msg(void);
184 /* obtain error message string */
185 
186 #define xmalloc(size) glp_alloc(1, size)
187 /* allocate memory block (obsolete) */
188 
189 #define xcalloc(n, size) glp_alloc(n, size)
190 /* allocate memory block (obsolete) */
191 
192 #define xalloc(n, size) glp_alloc(n, size)
193 #define talloc(n, type) ((type *)glp_alloc(n, sizeof(type)))
194 void *glp_alloc(int n, int size);
195 /* allocate memory block */
196 
197 #define xrealloc(ptr, n, size) glp_realloc(ptr, n, size)
198 #define trealloc(ptr, n, type) ((type *)glp_realloc(ptr, n, \
199       sizeof(type)))
200 void *glp_realloc(void *ptr, int n, int size);
201 /* reallocate memory block */
202 
203 #define xfree(ptr) glp_free(ptr)
204 #define tfree(ptr) glp_free(ptr)
205 void glp_free(void *ptr);
206 /* free memory block */
207 
208 void glp_mem_limit(int limit);
209 /* set memory usage limit */
210 
211 void glp_mem_usage(int *count, int *cpeak, size_t *total,
212       size_t *tpeak);
213 /* get memory usage information */
214 
215 typedef struct glp_file glp_file;
216 /* sequential stream descriptor */
217 
218 #define glp_open _glp_open
219 glp_file *glp_open(const char *name, const char *mode);
220 /* open stream */
221 
222 #define glp_eof _glp_eof
223 int glp_eof(glp_file *f);
224 /* test end-of-file indicator */
225 
226 #define glp_ioerr _glp_ioerr
227 int glp_ioerr(glp_file *f);
228 /* test I/O error indicator */
229 
230 #define glp_read _glp_read
231 int glp_read(glp_file *f, void *buf, int nnn);
232 /* read data from stream */
233 
234 #define glp_getc _glp_getc
235 int glp_getc(glp_file *f);
236 /* read character from stream */
237 
238 #define glp_write _glp_write
239 int glp_write(glp_file *f, const void *buf, int nnn);
240 /* write data to stream */
241 
242 #define glp_format _glp_format
243 int glp_format(glp_file *f, const char *fmt, ...);
244 /* write formatted data to stream */
245 
246 #define glp_close _glp_close
247 int glp_close(glp_file *f);
248 /* close stream */
249 
250 #define xtime glp_time
251 double glp_time(void);
252 /* determine current universal time */
253 
254 #define xdifftime glp_difftime
255 double glp_difftime(double t1, double t0);
256 /* compute difference between two time values */
257 
258 #define xdlopen _glp_dlopen
259 void *xdlopen(const char *module);
260 /* open dynamically linked library */
261 
262 #define xdlsym _glp_dlsym
263 void *xdlsym(void *h, const char *symbol);
264 /* obtain address of symbol from dynamically linked library */
265 
266 #define xdlclose _glp_dlclose
267 void xdlclose(void *h);
268 /* close dynamically linked library */
269 
270 #endif
271 
272 /* eof */
273