1 /* glpenv01.c (environment initialization/termination) */
3 /***********************************************************************
4 * This code is part of GLPK (GNU Linear Programming Kit).
6 * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
7 * 2009, 2010 Andrew Makhorin, Department for Applied Informatics,
8 * Moscow Aviation Institute, Moscow, Russia. All rights reserved.
9 * E-mail: <mao@gnu.org>.
11 * GLPK is free software: you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * GLPK is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
18 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
19 * License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with GLPK. If not, see <http://www.gnu.org/licenses/>.
23 ***********************************************************************/
27 /***********************************************************************
30 * glp_init_env - initialize GLPK environment
34 * int glp_init_env(void);
38 * The routine glp_init_env initializes the GLPK environment. Normally
39 * the application program does not need to call this routine, because
40 * it is called automatically on the first call to any API routine.
44 * The routine glp_init_env returns one of the following codes:
46 * 0 - initialization successful;
47 * 1 - environment has been already initialized;
48 * 2 - initialization failed (insufficient memory);
49 * 3 - initialization failed (unsupported programming model). */
51 int glp_init_env(void)
54 /* check if the programming model is supported */
55 ok = (CHAR_BIT == 8 && sizeof(char) == 1 &&
56 sizeof(short) == 2 && sizeof(int) == 4 &&
57 (sizeof(void *) == 4 || sizeof(void *) == 8));
59 /* check if the environment is already initialized */
60 if (tls_get_ptr() != NULL) return 1;
61 /* allocate and initialize the environment block */
62 env = malloc(sizeof(ENV));
63 if (env == NULL) return 2;
64 env->magic = ENV_MAGIC;
65 sprintf(env->version, "%d.%d",
66 GLP_MAJOR_VERSION, GLP_MINOR_VERSION);
67 env->term_buf = malloc(TERM_BUF_SIZE);
68 if (env->term_buf == NULL)
72 env->term_out = GLP_ON;
73 env->term_hook = NULL;
74 env->term_info = NULL;
80 env->mem_limit.hi = 0x7FFFFFFF, env->mem_limit.lo = 0xFFFFFFFF;
82 env->mem_count = env->mem_cpeak = 0;
83 env->mem_total = env->mem_tpeak = xlset(0);
85 env->ioerr_msg = malloc(IOERR_MSG_SIZE);
86 if (env->ioerr_msg == NULL)
87 { free(env->term_buf);
91 strcpy(env->ioerr_msg, "No error");
92 env->h_odbc = env->h_mysql = NULL;
93 /* save pointer to the environment block */
95 /* initialization successful */
99 /***********************************************************************
102 * get_env_ptr - retrieve pointer to environment block
106 * #include "glpenv.h"
107 * ENV *get_env_ptr(void);
111 * The routine get_env_ptr retrieves and returns a pointer to the GLPK
114 * If the GLPK environment has not been initialized yet, the routine
115 * performs initialization. If initialization fails, the routine prints
116 * an error message to stderr and terminates the program.
120 * The routine returns a pointer to the environment block. */
122 ENV *get_env_ptr(void)
123 { ENV *env = tls_get_ptr();
124 /* check if the environment has been initialized */
126 { /* not initialized yet; perform initialization */
127 if (glp_init_env() != 0)
128 { /* initialization failed; display an error message */
129 fprintf(stderr, "GLPK initialization failed\n");
131 /* and abnormally terminate the program */
134 /* initialization successful; retrieve the pointer */
137 /* check if the environment block is valid */
138 if (env->magic != ENV_MAGIC)
139 { fprintf(stderr, "Invalid GLPK environment\n");
146 /***********************************************************************
149 * glp_version - determine library version
153 * const char *glp_version(void);
157 * The routine glp_version returns a pointer to a null-terminated
158 * character string, which specifies the version of the GLPK library in
159 * the form "X.Y", where X is the major version number, and Y is the
160 * minor version number, for example, "4.16". */
162 const char *glp_version(void)
163 { ENV *env = get_env_ptr();
167 /***********************************************************************
170 * glp_free_env - free GLPK environment
174 * int glp_free_env(void);
178 * The routine glp_free_env frees all resources used by GLPK routines
179 * (memory blocks, etc.) which are currently still in use.
181 * Normally the application program does not need to call this routine,
182 * because GLPK routines always free all unused resources. However, if
183 * the application program even has deleted all problem objects, there
184 * will be several memory blocks still allocated for the library needs.
185 * For some reasons the application program may want GLPK to free this
186 * memory, in which case it should call glp_free_env.
188 * Note that a call to glp_free_env invalidates all problem objects as
189 * if no GLPK routine were called.
193 * 0 - termination successful;
194 * 1 - environment is inactive (was not initialized). */
196 int glp_free_env(void)
197 { ENV *env = tls_get_ptr();
199 /* check if the environment is active */
200 if (env == NULL) return 1;
201 /* check if the environment block is valid */
202 if (env->magic != ENV_MAGIC)
203 { fprintf(stderr, "Invalid GLPK environment\n");
207 /* close handles to shared libraries */
208 if (env->h_odbc != NULL)
209 xdlclose(env->h_odbc);
210 if (env->h_mysql != NULL)
211 xdlclose(env->h_mysql);
212 /* close streams which are still open */
213 while (env->file_ptr != NULL)
214 xfclose(env->file_ptr);
215 /* free memory blocks which are still allocated */
216 while (env->mem_ptr != NULL)
217 { desc = env->mem_ptr;
218 env->mem_ptr = desc->next;
221 /* invalidate the environment block */
223 /* free memory allocated to the environment block */
225 free(env->ioerr_msg);
227 /* reset a pointer to the environment block */
229 /* termination successful */