src/glpenv01.c
changeset 1 c445c931472f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/glpenv01.c	Mon Dec 06 13:09:21 2010 +0100
     1.3 @@ -0,0 +1,233 @@
     1.4 +/* glpenv01.c (environment initialization/termination) */
     1.5 +
     1.6 +/***********************************************************************
     1.7 +*  This code is part of GLPK (GNU Linear Programming Kit).
     1.8 +*
     1.9 +*  Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
    1.10 +*  2009, 2010 Andrew Makhorin, Department for Applied Informatics,
    1.11 +*  Moscow Aviation Institute, Moscow, Russia. All rights reserved.
    1.12 +*  E-mail: <mao@gnu.org>.
    1.13 +*
    1.14 +*  GLPK is free software: you can redistribute it and/or modify it
    1.15 +*  under the terms of the GNU General Public License as published by
    1.16 +*  the Free Software Foundation, either version 3 of the License, or
    1.17 +*  (at your option) any later version.
    1.18 +*
    1.19 +*  GLPK is distributed in the hope that it will be useful, but WITHOUT
    1.20 +*  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
    1.21 +*  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
    1.22 +*  License for more details.
    1.23 +*
    1.24 +*  You should have received a copy of the GNU General Public License
    1.25 +*  along with GLPK. If not, see <http://www.gnu.org/licenses/>.
    1.26 +***********************************************************************/
    1.27 +
    1.28 +#include "glpapi.h"
    1.29 +
    1.30 +/***********************************************************************
    1.31 +*  NAME
    1.32 +*
    1.33 +*  glp_init_env - initialize GLPK environment
    1.34 +*
    1.35 +*  SYNOPSIS
    1.36 +*
    1.37 +*  int glp_init_env(void);
    1.38 +*
    1.39 +*  DESCRIPTION
    1.40 +*
    1.41 +*  The routine glp_init_env initializes the GLPK environment. Normally
    1.42 +*  the application program does not need to call this routine, because
    1.43 +*  it is called automatically on the first call to any API routine.
    1.44 +*
    1.45 +*  RETURNS
    1.46 +*
    1.47 +*  The routine glp_init_env returns one of the following codes:
    1.48 +*
    1.49 +*  0 - initialization successful;
    1.50 +*  1 - environment has been already initialized;
    1.51 +*  2 - initialization failed (insufficient memory);
    1.52 +*  3 - initialization failed (unsupported programming model). */
    1.53 +
    1.54 +int glp_init_env(void)
    1.55 +{     ENV *env;
    1.56 +      int ok;
    1.57 +      /* check if the programming model is supported */
    1.58 +      ok = (CHAR_BIT == 8 && sizeof(char) == 1 &&
    1.59 +         sizeof(short) == 2 && sizeof(int) == 4 &&
    1.60 +         (sizeof(void *) == 4 || sizeof(void *) == 8));
    1.61 +      if (!ok) return 3;
    1.62 +      /* check if the environment is already initialized */
    1.63 +      if (tls_get_ptr() != NULL) return 1;
    1.64 +      /* allocate and initialize the environment block */
    1.65 +      env = malloc(sizeof(ENV));
    1.66 +      if (env == NULL) return 2;
    1.67 +      env->magic = ENV_MAGIC;
    1.68 +      sprintf(env->version, "%d.%d",
    1.69 +         GLP_MAJOR_VERSION, GLP_MINOR_VERSION);
    1.70 +      env->term_buf = malloc(TERM_BUF_SIZE);
    1.71 +      if (env->term_buf == NULL)
    1.72 +      {  free(env);
    1.73 +         return 2;
    1.74 +      }
    1.75 +      env->term_out = GLP_ON;
    1.76 +      env->term_hook = NULL;
    1.77 +      env->term_info = NULL;
    1.78 +      env->tee_file = NULL;
    1.79 +      env->err_file = "";
    1.80 +      env->err_line = 0;
    1.81 +      env->err_hook = NULL;
    1.82 +      env->err_info = NULL;
    1.83 +      env->mem_limit.hi = 0x7FFFFFFF, env->mem_limit.lo = 0xFFFFFFFF;
    1.84 +      env->mem_ptr = NULL;
    1.85 +      env->mem_count = env->mem_cpeak = 0;
    1.86 +      env->mem_total = env->mem_tpeak = xlset(0);
    1.87 +      env->file_ptr = NULL;
    1.88 +      env->ioerr_msg = malloc(IOERR_MSG_SIZE);
    1.89 +      if (env->ioerr_msg == NULL)
    1.90 +      {  free(env->term_buf);
    1.91 +         free(env);
    1.92 +         return 2;
    1.93 +      }
    1.94 +      strcpy(env->ioerr_msg, "No error");
    1.95 +      env->h_odbc = env->h_mysql = NULL;
    1.96 +      /* save pointer to the environment block */
    1.97 +      tls_set_ptr(env);
    1.98 +      /* initialization successful */
    1.99 +      return 0;
   1.100 +}
   1.101 +
   1.102 +/***********************************************************************
   1.103 +*  NAME
   1.104 +*
   1.105 +*  get_env_ptr - retrieve pointer to environment block
   1.106 +*
   1.107 +*  SYNOPSIS
   1.108 +*
   1.109 +*  #include "glpenv.h"
   1.110 +*  ENV *get_env_ptr(void);
   1.111 +*
   1.112 +*  DESCRIPTION
   1.113 +*
   1.114 +*  The routine get_env_ptr retrieves and returns a pointer to the GLPK
   1.115 +*  environment block.
   1.116 +*
   1.117 +*  If the GLPK environment has not been initialized yet, the routine
   1.118 +*  performs initialization. If initialization fails, the routine prints
   1.119 +*  an error message to stderr and terminates the program.
   1.120 +*
   1.121 +*  RETURNS
   1.122 +*
   1.123 +*  The routine returns a pointer to the environment block. */
   1.124 +
   1.125 +ENV *get_env_ptr(void)
   1.126 +{     ENV *env = tls_get_ptr();
   1.127 +      /* check if the environment has been initialized */
   1.128 +      if (env == NULL)
   1.129 +      {  /* not initialized yet; perform initialization */
   1.130 +         if (glp_init_env() != 0)
   1.131 +         {  /* initialization failed; display an error message */
   1.132 +            fprintf(stderr, "GLPK initialization failed\n");
   1.133 +            fflush(stderr);
   1.134 +            /* and abnormally terminate the program */
   1.135 +            abort();
   1.136 +         }
   1.137 +         /* initialization successful; retrieve the pointer */
   1.138 +         env = tls_get_ptr();
   1.139 +      }
   1.140 +      /* check if the environment block is valid */
   1.141 +      if (env->magic != ENV_MAGIC)
   1.142 +      {  fprintf(stderr, "Invalid GLPK environment\n");
   1.143 +         fflush(stderr);
   1.144 +         abort();
   1.145 +      }
   1.146 +      return env;
   1.147 +}
   1.148 +
   1.149 +/***********************************************************************
   1.150 +*  NAME
   1.151 +*
   1.152 +*  glp_version - determine library version
   1.153 +*
   1.154 +*  SYNOPSIS
   1.155 +*
   1.156 +*  const char *glp_version(void);
   1.157 +*
   1.158 +*  RETURNS
   1.159 +*
   1.160 +*  The routine glp_version returns a pointer to a null-terminated
   1.161 +*  character string, which specifies the version of the GLPK library in
   1.162 +*  the form "X.Y", where X is the major version number, and Y is the
   1.163 +*  minor version number, for example, "4.16". */
   1.164 +
   1.165 +const char *glp_version(void)
   1.166 +{     ENV *env = get_env_ptr();
   1.167 +      return env->version;
   1.168 +}
   1.169 +
   1.170 +/***********************************************************************
   1.171 +*  NAME
   1.172 +*
   1.173 +*  glp_free_env - free GLPK environment
   1.174 +*
   1.175 +*  SYNOPSIS
   1.176 +*
   1.177 +*  int glp_free_env(void);
   1.178 +*
   1.179 +*  DESCRIPTION
   1.180 +*
   1.181 +*  The routine glp_free_env frees all resources used by GLPK routines
   1.182 +*  (memory blocks, etc.) which are currently still in use.
   1.183 +*
   1.184 +*  Normally the application program does not need to call this routine,
   1.185 +*  because GLPK routines always free all unused resources. However, if
   1.186 +*  the application program even has deleted all problem objects, there
   1.187 +*  will be several memory blocks still allocated for the library needs.
   1.188 +*  For some reasons the application program may want GLPK to free this
   1.189 +*  memory, in which case it should call glp_free_env.
   1.190 +*
   1.191 +*  Note that a call to glp_free_env invalidates all problem objects as
   1.192 +*  if no GLPK routine were called.
   1.193 +*
   1.194 +*  RETURNS
   1.195 +*
   1.196 +*  0 - termination successful;
   1.197 +*  1 - environment is inactive (was not initialized). */
   1.198 +
   1.199 +int glp_free_env(void)
   1.200 +{     ENV *env = tls_get_ptr();
   1.201 +      MEM *desc;
   1.202 +      /* check if the environment is active */
   1.203 +      if (env == NULL) return 1;
   1.204 +      /* check if the environment block is valid */
   1.205 +      if (env->magic != ENV_MAGIC)
   1.206 +      {  fprintf(stderr, "Invalid GLPK environment\n");
   1.207 +         fflush(stderr);
   1.208 +         abort();
   1.209 +      }
   1.210 +      /* close handles to shared libraries */
   1.211 +      if (env->h_odbc != NULL)
   1.212 +         xdlclose(env->h_odbc);
   1.213 +      if (env->h_mysql != NULL)
   1.214 +         xdlclose(env->h_mysql);
   1.215 +      /* close streams which are still open */
   1.216 +      while (env->file_ptr != NULL)
   1.217 +         xfclose(env->file_ptr);
   1.218 +      /* free memory blocks which are still allocated */
   1.219 +      while (env->mem_ptr != NULL)
   1.220 +      {  desc = env->mem_ptr;
   1.221 +         env->mem_ptr = desc->next;
   1.222 +         free(desc);
   1.223 +      }
   1.224 +      /* invalidate the environment block */
   1.225 +      env->magic = -1;
   1.226 +      /* free memory allocated to the environment block */
   1.227 +      free(env->term_buf);
   1.228 +      free(env->ioerr_msg);
   1.229 +      free(env);
   1.230 +      /* reset a pointer to the environment block */
   1.231 +      tls_set_ptr(NULL);
   1.232 +      /* termination successful */
   1.233 +      return 0;
   1.234 +}
   1.235 +
   1.236 +/* eof */