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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
|
%* glpk06.tex *%
\chapter{Miscellaneous API Routines}
\section{GLPK environment routines}
\subsection{glp\_init\_env --- initialize GLPK environment}
\synopsis
\begin{verbatim}
int glp_init_env(void);
\end{verbatim}
\description
The routine \verb|glp_init_env| initializes the GLPK environment.
Normally the application program does not need to call this routine,
because it is called automatically on the first call to any API
routine.
\returns
\begin{retlist}
0 & initialization successful;\\
1 & environment is already initialized;\\
2 & initialization failed (insufficient memory);\\
3 & initialization failed (unsupported programming model).\\
\end{retlist}
\subsection{glp\_version --- determine library version}
\synopsis
\begin{verbatim}
const char *glp_version(void);
\end{verbatim}
\returns
The routine \verb|glp_version| returns a pointer to a null-terminated
character string, which specifies the version of the GLPK library in
the form \verb|"X.Y"|, where `\verb|X|' is the major version number,
and `\verb|Y|' is the minor version number, for example, \verb|"4.16"|.
\newpage
\subsection{glp\_free\_env --- free GLPK environment}
\synopsis
\begin{verbatim}
int glp_free_env(void);
\end{verbatim}
\description
The routine \verb|glp_free_env| frees all resources used by GLPK
routines (memory blocks, etc.) which are currently still in use.
Normally the application program does not need to call this routine,
because GLPK routines always free all unused resources. However, if
the application program even has deleted all problem objects, there
will be several memory blocks still allocated for the internal library
needs. For some reasons the application program may want GLPK to free
this memory, in which case it should call \verb|glp_free_env|.
Note that a call to \verb|glp_free_env| invalidates all problem objects
which still exist.
\returns
\begin{retlist}
0 & termination successful;\\
1 & environment is inactive (was not initialized).\\
\end{retlist}
\subsection{glp\_printf --- write formatted output to terminal}
\synopsis
\begin{verbatim}
void glp_printf(const char *fmt, ...);
\end{verbatim}
\description
The routine \verb|glp_printf| uses the format control string
\verb|fmt| to format its parameters and writes the formatted output to
the terminal.
This routine is a replacement of the standard C function
\verb|printf| and used by all GLPK routines to perform terminal
output. The application program may use \verb|glp_printf| for the same
purpose that allows controlling its terminal output with the routines
\verb|glp_term_out| and \verb|glp_term_hook|.
\subsection{glp\_vprintf --- write formatted output to terminal}
\synopsis
\begin{verbatim}
void glp_vprintf(const char *fmt, va_list arg);
\end{verbatim}
\description
The routine \verb|glp_vprintf| uses the format control string
\verb|fmt| to format its parameters specified by the list \verb|arg|
and writes the formatted output to the terminal.
This routine is a replacement of the standard C function
\verb|vprintf| and used by all GLPK routines to perform terminal
output. The application program may use \verb|glp_vprintf| for the same
purpose that allows controlling its terminal output with the routines
\verb|glp_term_out| and \verb|glp_term_hook|.
\newpage
\subsection{glp\_term\_out --- enable/disable terminal output}
\synopsis
\begin{verbatim}
int glp_term_out(int flag);
\end{verbatim}
\description
Depending on the parameter flag the routine \verb|glp_term_out| enables
or disables terminal output performed by glpk routines:
\verb|GLP_ON | --- enable terminal output;
\verb|GLP_OFF| --- disable terminal output.
\returns
The routine \verb|glp_term_out| returns the previous value of the
terminal output flag.
\subsection{glp\_term\_hook --- intercept terminal output}
\synopsis
\begin{verbatim}
void glp_term_hook(int (*func)(void *info, const char *s), void *info);
\end{verbatim}
\description
The routine \verb|glp_term_hook| installs the user-defined hook routine
to intercept all terminal output performed by GLPK routines.
%This feature can be used to redirect the terminal output to other
%destination, for example, to a file or a text window.
The parameter {\it func} specifies the user-defined hook routine. It is
called from an internal printing routine, which passes to it two
parameters: {\it info} and {\it s}. The parameter {\it info} is a
transit pointer specified in corresponding call to the routine
\verb|glp_term_hook|; it may be used to pass some additional information
to the hook routine. The parameter {\it s} is a pointer to the null
terminated character string, which is intended to be written to the
terminal. If the hook routine returns zero, the printing routine writes
the string {\it s} to the terminal in a usual way; otherwise, if the
hook routine returns non-zero, no terminal output is performed.
To uninstall the hook routine both parameters {\it func} and {\it info}
should be specified as \verb|NULL|.
\para{Example}
\begin{footnotesize}
\begin{verbatim}
static int hook(void *info, const char *s)
{ FILE *foo = info;
fputs(s, foo);
return 1;
}
int main(void)
{ FILE *foo;
. . .
glp_term_hook(hook, foo); /* redirect terminal output */
. . .
glp_term_hook(NULL, NULL); /* resume terminal output */
. . .
}
\end{verbatim}
\end{footnotesize}
\newpage
\subsection{glp\_open\_tee --- start copying terminal output}
\synopsis
\begin{verbatim}
int glp_open_tee(const char *fname);
\end{verbatim}
\description
The routine \verb|glp_open_tee| starts copying all the terminal output
to an output text file, whose name is specified by the character string
\verb|fname|.
\returns
\begin{retlist}
0 & operation successful;\\
1 & copying terminal output is already active;\\
2 & unable to create output file.\\
\end{retlist}
\subsection{glp\_close\_tee --- stop copying terminal output}
\synopsis
\begin{verbatim}
int glp_close_tee(void);
\end{verbatim}
\description
The routine \verb|glp_close_tee| stops copying the terminal output to
the output text file previously open by the routine \verb|glp_open_tee|
closing that file.
\returns
\begin{retlist}
0 & operation successful;\\
1 & copying terminal output was not started.\\
\end{retlist}
\subsection{glp\_error --- display error message and terminate
execution}
\synopsis
\begin{verbatim}
void glp_error(const char *fmt, ...);
\end{verbatim}
\description
The routine \verb|glp_error| (implemented as a macro) formats its
parameters using the format control string \verb|fmt|, writes the
formatted message to the terminal, and then abnormally terminates the
program.
\newpage
\subsection{glp\_at\_error --- check for error state}
\synopsis
\begin{verbatim}
int glp_at_error(void);
\end{verbatim}
\description
The routine \verb|glp_at_error| checks if the GLPK environment is at
error state, i.~e.~if the call to the routine is (indirectly) made from
the \verb|glp_error| routine via an user-defined hook routine.
This routine can be used, for example, by a custom output handler
(installed with the routine \verb|glp_term_hook|) to determine whether
or not the message to be displayed is an error message.
\returns
If the GLPK environment is at error state, the routine returns
non-zero, otherwise zero.
\subsection{glp\_assert --- check logical condition}
\synopsis
\begin{verbatim}
void glp_assert(int expr);
\end{verbatim}
\description
The routine \verb|glp_assert| (implemented as a macro) checks
a logical condition specified by the expression \verb|expr|. If the
condition is true (non-zero), the routine does nothing; otherwise, if
the condition is false (zero), the routine prints an error message and
abnormally terminates the program.
This routine is a replacement of the standard C function \verb|assert|
and used by all GLPK routines to check program logic. The application
program may use \verb|glp_assert| for the same purpose.
\subsection{glp\_error\_hook --- install hook to intercept abnormal
termination}
\synopsis
\begin{verbatim}
void glp_error_hook(void (*func)(void *info), void *info);
\end{verbatim}
\description
The routine \verb|glp_error_hook| installs a user-defined hook routine
to intercept abnormal termination.
The parameter \verb|func| specifies the user-defined hook routine. It
is called from the routine \verb|glp_error| before the latter calls the
abort function to abnormally terminate the application program because
of fatal error. The parameter \verb|info| is a transit pointer,
specified in the corresponding call to the routine
\verb|glp_error_hook|; it may be used to pass some information to the
hook routine.
To uninstall the hook routine the parameters \verb|func| and \verb|info|
should be specified as \verb|NULL|.
If the hook routine returns, the application program is abnormally
terminated. To prevent abnormal termnation the hook routine may perform
a global jump using the standard function \verb|longjmp|, in which case
the application program {\it must} call the routine \verb|glp_free_env|.
\subsection{glp\_alloc --- allocate memory block}
\synopsis
\begin{verbatim}
void *glp_alloc(int n, int size);
\end{verbatim}
\description
The routine \verb|glp_alloc| dynamically allocates a memory block of
\verb|n|$\times$\verb|size| bytes long. Note that:
1) the parameters \verb|n| and \verb|size| must be positive;
2) having been allocated the memory block contains arbitrary data, that
is, it is {\it not} initialized by binary zeros;
3) if the block cannot be allocated due to insufficient memory, the
routine prints an error message and abnormally terminates the program.
This routine is a replacement of the standard C function \verb|malloc|
and used by GLPK routines for dynamic memory allocation. The
application program may use \verb|glp_alloc| for the same purpose.
\returns
The routine \verb|glp_alloc| returns a pointer to the memory block
allocated. To free this block the routine \verb|glp_free| (not the
standard C function \verb|free|!) should be used.
\subsection{glp\_realloc --- reallocate memory block}
\synopsis
\begin{verbatim}
void *glp_realloc(void *ptr, int n, int size);
\end{verbatim}
\description
The routine \verb|glp_realloc| dynamically reallocates a memory block
pointed to by \verb|ptr|, which was previously allocated by the routine
\verb|glp_alloc| or reallocated by this routine. Note that the pointer
\verb|ptr| must be valid and must not be \verb|NULL|. The new size of
the memory block is \verb|n|$\times$\verb|size| bytes long. Note that:
1) both parameters \verb|n| and \verb|size| must be positive;
2) if the block cannot be reallocated due to insufficient memory, the
routine prints an error message and abnormally terminates the program.
This routine is a replacement of the standard C function \verb|realloc|
and used by GLPK routines for dynamic memory allocation. The
application program may use \verb|glp_realloc| for the same purpose.
\returns
The routine \verb|glp_realloc| returns a pointer to the memory block
reallocated. To free this block the routine \verb|glp_free| (not the
standard C function \verb|free|!) should be used.
\newpage
\subsection{glp\_free --- free memory block}
\synopsis
\begin{verbatim}
void glp_free(void *ptr);
\end{verbatim}
\description
The routine \verb|glp_free| deallocates a memory block pointed to by
\verb|ptr|, which was previously allocated by the routine
\verb|glp_malloc| or reallocated by the routine \verb|glp_realloc|.
Note that the pointer \verb|ptr| must be valid and must not be
\verb|NULL|.
This routine is a replacement of the standard C function \verb|free|
and used by GLPK routines for dynamic memory allocation. The
application program may use \verb|glp_free| for the same purpose.
\subsection{glp\_mem\_usage --- get memory usage information}
\synopsis
\begin{verbatim}
void glp_mem_usage(int *count, int *cpeak, size_t *total, size_t *tpeak);
\end{verbatim}
\description
The routine \verb|glp_mem_usage| reports some information about
utilization of the memory by the routines \verb|glp_malloc|,
\verb|glp_calloc|, and \verb|glp_free|. Information is stored to
locations specified by corresponding parameters (see below). Any
parameter can be specified as \verb|NULL|, in which case corresponding
information is not stored.
\verb|*count| is the number of currently allocated memory blocks.
\verb|*cpeak| is the peak value of \verb|*count| reached since the
initialization of the GLPK library environment.
\verb|*total| is the total amount, in bytes, of currently allocated
memory blocks.
\verb|*tpeak| is the peak value of \verb|*total| reached since the
initialization of the GLPK library envirionment.
\para{Example}
\begin{footnotesize}
\begin{verbatim}
glp_mem_usage(&count, NULL, NULL, NULL);
printf("%d memory block(s) are still allocated\n", count);
\end{verbatim}
\end{footnotesize}
\subsection{glp\_mem\_limit --- set memory usage limit}
\synopsis
\begin{verbatim}
void glp_mem_limit(int limit);
\end{verbatim}
\description
The routine \verb|glp_mem_limit| limits the amount of memory available
for dynamic allocation (with the routines \verb|glp_malloc| and
\verb|glp_calloc|) to \verb|limit| megabytes.
%* eof *%
|