glpenv01.c 7.2 KB

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