alpar@1
|
1 |
/* glpenv01.c (environment initialization/termination) */
|
alpar@1
|
2 |
|
alpar@1
|
3 |
/***********************************************************************
|
alpar@1
|
4 |
* This code is part of GLPK (GNU Linear Programming Kit).
|
alpar@1
|
5 |
*
|
alpar@1
|
6 |
* Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
|
alpar@1
|
7 |
* 2009, 2010 Andrew Makhorin, Department for Applied Informatics,
|
alpar@1
|
8 |
* Moscow Aviation Institute, Moscow, Russia. All rights reserved.
|
alpar@1
|
9 |
* E-mail: <mao@gnu.org>.
|
alpar@1
|
10 |
*
|
alpar@1
|
11 |
* GLPK is free software: you can redistribute it and/or modify it
|
alpar@1
|
12 |
* under the terms of the GNU General Public License as published by
|
alpar@1
|
13 |
* the Free Software Foundation, either version 3 of the License, or
|
alpar@1
|
14 |
* (at your option) any later version.
|
alpar@1
|
15 |
*
|
alpar@1
|
16 |
* GLPK is distributed in the hope that it will be useful, but WITHOUT
|
alpar@1
|
17 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
alpar@1
|
18 |
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
|
alpar@1
|
19 |
* License for more details.
|
alpar@1
|
20 |
*
|
alpar@1
|
21 |
* You should have received a copy of the GNU General Public License
|
alpar@1
|
22 |
* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
|
alpar@1
|
23 |
***********************************************************************/
|
alpar@1
|
24 |
|
alpar@1
|
25 |
#include "glpapi.h"
|
alpar@1
|
26 |
|
alpar@1
|
27 |
/***********************************************************************
|
alpar@1
|
28 |
* NAME
|
alpar@1
|
29 |
*
|
alpar@1
|
30 |
* glp_init_env - initialize GLPK environment
|
alpar@1
|
31 |
*
|
alpar@1
|
32 |
* SYNOPSIS
|
alpar@1
|
33 |
*
|
alpar@1
|
34 |
* int glp_init_env(void);
|
alpar@1
|
35 |
*
|
alpar@1
|
36 |
* DESCRIPTION
|
alpar@1
|
37 |
*
|
alpar@1
|
38 |
* The routine glp_init_env initializes the GLPK environment. Normally
|
alpar@1
|
39 |
* the application program does not need to call this routine, because
|
alpar@1
|
40 |
* it is called automatically on the first call to any API routine.
|
alpar@1
|
41 |
*
|
alpar@1
|
42 |
* RETURNS
|
alpar@1
|
43 |
*
|
alpar@1
|
44 |
* The routine glp_init_env returns one of the following codes:
|
alpar@1
|
45 |
*
|
alpar@1
|
46 |
* 0 - initialization successful;
|
alpar@1
|
47 |
* 1 - environment has been already initialized;
|
alpar@1
|
48 |
* 2 - initialization failed (insufficient memory);
|
alpar@1
|
49 |
* 3 - initialization failed (unsupported programming model). */
|
alpar@1
|
50 |
|
alpar@1
|
51 |
int glp_init_env(void)
|
alpar@1
|
52 |
{ ENV *env;
|
alpar@1
|
53 |
int ok;
|
alpar@1
|
54 |
/* check if the programming model is supported */
|
alpar@1
|
55 |
ok = (CHAR_BIT == 8 && sizeof(char) == 1 &&
|
alpar@1
|
56 |
sizeof(short) == 2 && sizeof(int) == 4 &&
|
alpar@1
|
57 |
(sizeof(void *) == 4 || sizeof(void *) == 8));
|
alpar@1
|
58 |
if (!ok) return 3;
|
alpar@1
|
59 |
/* check if the environment is already initialized */
|
alpar@1
|
60 |
if (tls_get_ptr() != NULL) return 1;
|
alpar@1
|
61 |
/* allocate and initialize the environment block */
|
alpar@1
|
62 |
env = malloc(sizeof(ENV));
|
alpar@1
|
63 |
if (env == NULL) return 2;
|
alpar@1
|
64 |
env->magic = ENV_MAGIC;
|
alpar@1
|
65 |
sprintf(env->version, "%d.%d",
|
alpar@1
|
66 |
GLP_MAJOR_VERSION, GLP_MINOR_VERSION);
|
alpar@1
|
67 |
env->term_buf = malloc(TERM_BUF_SIZE);
|
alpar@1
|
68 |
if (env->term_buf == NULL)
|
alpar@1
|
69 |
{ free(env);
|
alpar@1
|
70 |
return 2;
|
alpar@1
|
71 |
}
|
alpar@1
|
72 |
env->term_out = GLP_ON;
|
alpar@1
|
73 |
env->term_hook = NULL;
|
alpar@1
|
74 |
env->term_info = NULL;
|
alpar@1
|
75 |
env->tee_file = NULL;
|
alpar@1
|
76 |
env->err_file = "";
|
alpar@1
|
77 |
env->err_line = 0;
|
alpar@1
|
78 |
env->err_hook = NULL;
|
alpar@1
|
79 |
env->err_info = NULL;
|
alpar@1
|
80 |
env->mem_limit.hi = 0x7FFFFFFF, env->mem_limit.lo = 0xFFFFFFFF;
|
alpar@1
|
81 |
env->mem_ptr = NULL;
|
alpar@1
|
82 |
env->mem_count = env->mem_cpeak = 0;
|
alpar@1
|
83 |
env->mem_total = env->mem_tpeak = xlset(0);
|
alpar@1
|
84 |
env->file_ptr = NULL;
|
alpar@1
|
85 |
env->ioerr_msg = malloc(IOERR_MSG_SIZE);
|
alpar@1
|
86 |
if (env->ioerr_msg == NULL)
|
alpar@1
|
87 |
{ free(env->term_buf);
|
alpar@1
|
88 |
free(env);
|
alpar@1
|
89 |
return 2;
|
alpar@1
|
90 |
}
|
alpar@1
|
91 |
strcpy(env->ioerr_msg, "No error");
|
alpar@1
|
92 |
env->h_odbc = env->h_mysql = NULL;
|
alpar@1
|
93 |
/* save pointer to the environment block */
|
alpar@1
|
94 |
tls_set_ptr(env);
|
alpar@1
|
95 |
/* initialization successful */
|
alpar@1
|
96 |
return 0;
|
alpar@1
|
97 |
}
|
alpar@1
|
98 |
|
alpar@1
|
99 |
/***********************************************************************
|
alpar@1
|
100 |
* NAME
|
alpar@1
|
101 |
*
|
alpar@1
|
102 |
* get_env_ptr - retrieve pointer to environment block
|
alpar@1
|
103 |
*
|
alpar@1
|
104 |
* SYNOPSIS
|
alpar@1
|
105 |
*
|
alpar@1
|
106 |
* #include "glpenv.h"
|
alpar@1
|
107 |
* ENV *get_env_ptr(void);
|
alpar@1
|
108 |
*
|
alpar@1
|
109 |
* DESCRIPTION
|
alpar@1
|
110 |
*
|
alpar@1
|
111 |
* The routine get_env_ptr retrieves and returns a pointer to the GLPK
|
alpar@1
|
112 |
* environment block.
|
alpar@1
|
113 |
*
|
alpar@1
|
114 |
* If the GLPK environment has not been initialized yet, the routine
|
alpar@1
|
115 |
* performs initialization. If initialization fails, the routine prints
|
alpar@1
|
116 |
* an error message to stderr and terminates the program.
|
alpar@1
|
117 |
*
|
alpar@1
|
118 |
* RETURNS
|
alpar@1
|
119 |
*
|
alpar@1
|
120 |
* The routine returns a pointer to the environment block. */
|
alpar@1
|
121 |
|
alpar@1
|
122 |
ENV *get_env_ptr(void)
|
alpar@1
|
123 |
{ ENV *env = tls_get_ptr();
|
alpar@1
|
124 |
/* check if the environment has been initialized */
|
alpar@1
|
125 |
if (env == NULL)
|
alpar@1
|
126 |
{ /* not initialized yet; perform initialization */
|
alpar@1
|
127 |
if (glp_init_env() != 0)
|
alpar@1
|
128 |
{ /* initialization failed; display an error message */
|
alpar@1
|
129 |
fprintf(stderr, "GLPK initialization failed\n");
|
alpar@1
|
130 |
fflush(stderr);
|
alpar@1
|
131 |
/* and abnormally terminate the program */
|
alpar@1
|
132 |
abort();
|
alpar@1
|
133 |
}
|
alpar@1
|
134 |
/* initialization successful; retrieve the pointer */
|
alpar@1
|
135 |
env = tls_get_ptr();
|
alpar@1
|
136 |
}
|
alpar@1
|
137 |
/* check if the environment block is valid */
|
alpar@1
|
138 |
if (env->magic != ENV_MAGIC)
|
alpar@1
|
139 |
{ fprintf(stderr, "Invalid GLPK environment\n");
|
alpar@1
|
140 |
fflush(stderr);
|
alpar@1
|
141 |
abort();
|
alpar@1
|
142 |
}
|
alpar@1
|
143 |
return env;
|
alpar@1
|
144 |
}
|
alpar@1
|
145 |
|
alpar@1
|
146 |
/***********************************************************************
|
alpar@1
|
147 |
* NAME
|
alpar@1
|
148 |
*
|
alpar@1
|
149 |
* glp_version - determine library version
|
alpar@1
|
150 |
*
|
alpar@1
|
151 |
* SYNOPSIS
|
alpar@1
|
152 |
*
|
alpar@1
|
153 |
* const char *glp_version(void);
|
alpar@1
|
154 |
*
|
alpar@1
|
155 |
* RETURNS
|
alpar@1
|
156 |
*
|
alpar@1
|
157 |
* The routine glp_version returns a pointer to a null-terminated
|
alpar@1
|
158 |
* character string, which specifies the version of the GLPK library in
|
alpar@1
|
159 |
* the form "X.Y", where X is the major version number, and Y is the
|
alpar@1
|
160 |
* minor version number, for example, "4.16". */
|
alpar@1
|
161 |
|
alpar@1
|
162 |
const char *glp_version(void)
|
alpar@1
|
163 |
{ ENV *env = get_env_ptr();
|
alpar@1
|
164 |
return env->version;
|
alpar@1
|
165 |
}
|
alpar@1
|
166 |
|
alpar@1
|
167 |
/***********************************************************************
|
alpar@1
|
168 |
* NAME
|
alpar@1
|
169 |
*
|
alpar@1
|
170 |
* glp_free_env - free GLPK environment
|
alpar@1
|
171 |
*
|
alpar@1
|
172 |
* SYNOPSIS
|
alpar@1
|
173 |
*
|
alpar@1
|
174 |
* int glp_free_env(void);
|
alpar@1
|
175 |
*
|
alpar@1
|
176 |
* DESCRIPTION
|
alpar@1
|
177 |
*
|
alpar@1
|
178 |
* The routine glp_free_env frees all resources used by GLPK routines
|
alpar@1
|
179 |
* (memory blocks, etc.) which are currently still in use.
|
alpar@1
|
180 |
*
|
alpar@1
|
181 |
* Normally the application program does not need to call this routine,
|
alpar@1
|
182 |
* because GLPK routines always free all unused resources. However, if
|
alpar@1
|
183 |
* the application program even has deleted all problem objects, there
|
alpar@1
|
184 |
* will be several memory blocks still allocated for the library needs.
|
alpar@1
|
185 |
* For some reasons the application program may want GLPK to free this
|
alpar@1
|
186 |
* memory, in which case it should call glp_free_env.
|
alpar@1
|
187 |
*
|
alpar@1
|
188 |
* Note that a call to glp_free_env invalidates all problem objects as
|
alpar@1
|
189 |
* if no GLPK routine were called.
|
alpar@1
|
190 |
*
|
alpar@1
|
191 |
* RETURNS
|
alpar@1
|
192 |
*
|
alpar@1
|
193 |
* 0 - termination successful;
|
alpar@1
|
194 |
* 1 - environment is inactive (was not initialized). */
|
alpar@1
|
195 |
|
alpar@1
|
196 |
int glp_free_env(void)
|
alpar@1
|
197 |
{ ENV *env = tls_get_ptr();
|
alpar@1
|
198 |
MEM *desc;
|
alpar@1
|
199 |
/* check if the environment is active */
|
alpar@1
|
200 |
if (env == NULL) return 1;
|
alpar@1
|
201 |
/* check if the environment block is valid */
|
alpar@1
|
202 |
if (env->magic != ENV_MAGIC)
|
alpar@1
|
203 |
{ fprintf(stderr, "Invalid GLPK environment\n");
|
alpar@1
|
204 |
fflush(stderr);
|
alpar@1
|
205 |
abort();
|
alpar@1
|
206 |
}
|
alpar@1
|
207 |
/* close handles to shared libraries */
|
alpar@1
|
208 |
if (env->h_odbc != NULL)
|
alpar@1
|
209 |
xdlclose(env->h_odbc);
|
alpar@1
|
210 |
if (env->h_mysql != NULL)
|
alpar@1
|
211 |
xdlclose(env->h_mysql);
|
alpar@1
|
212 |
/* close streams which are still open */
|
alpar@1
|
213 |
while (env->file_ptr != NULL)
|
alpar@1
|
214 |
xfclose(env->file_ptr);
|
alpar@1
|
215 |
/* free memory blocks which are still allocated */
|
alpar@1
|
216 |
while (env->mem_ptr != NULL)
|
alpar@1
|
217 |
{ desc = env->mem_ptr;
|
alpar@1
|
218 |
env->mem_ptr = desc->next;
|
alpar@1
|
219 |
free(desc);
|
alpar@1
|
220 |
}
|
alpar@1
|
221 |
/* invalidate the environment block */
|
alpar@1
|
222 |
env->magic = -1;
|
alpar@1
|
223 |
/* free memory allocated to the environment block */
|
alpar@1
|
224 |
free(env->term_buf);
|
alpar@1
|
225 |
free(env->ioerr_msg);
|
alpar@1
|
226 |
free(env);
|
alpar@1
|
227 |
/* reset a pointer to the environment block */
|
alpar@1
|
228 |
tls_set_ptr(NULL);
|
alpar@1
|
229 |
/* termination successful */
|
alpar@1
|
230 |
return 0;
|
alpar@1
|
231 |
}
|
alpar@1
|
232 |
|
alpar@1
|
233 |
/* eof */
|