lemon-project-template-glpk

diff deps/glpk/src/glpenv05.c @ 9:33de93886c88

Import GLPK 4.47
author Alpar Juttner <alpar@cs.elte.hu>
date Sun, 06 Nov 2011 20:59:10 +0100
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/deps/glpk/src/glpenv05.c	Sun Nov 06 20:59:10 2011 +0100
     1.3 @@ -0,0 +1,225 @@
     1.4 +/* glpenv05.c (memory allocation) */
     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, 2011 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 +/* some processors need data to be properly aligned; the macro
    1.31 +   align_datasize enlarges the specified size of a data item to provide
    1.32 +   a proper alignment of immediately following data */
    1.33 +
    1.34 +#define align_datasize(size) ((((size) + 15) / 16) * 16)
    1.35 +/* 16 bytes is sufficient in both 32- and 64-bit environments
    1.36 +   (8 bytes is not sufficient in 64-bit environment due to jmp_buf) */
    1.37 +
    1.38 +/***********************************************************************
    1.39 +*  NAME
    1.40 +*
    1.41 +*  glp_malloc - allocate memory block
    1.42 +*
    1.43 +*  SYNOPSIS
    1.44 +*
    1.45 +*  void *glp_malloc(int size);
    1.46 +*
    1.47 +*  DESCRIPTION
    1.48 +*
    1.49 +*  The routine glp_malloc allocates a memory block of size bytes long.
    1.50 +*
    1.51 +*  Note that being allocated the memory block contains arbitrary data
    1.52 +*  (not binary zeros).
    1.53 +*
    1.54 +*  RETURNS
    1.55 +*
    1.56 +*  The routine glp_malloc returns a pointer to the allocated block.
    1.57 +*  To free this block the routine glp_free (not free!) must be used. */
    1.58 +
    1.59 +void *glp_malloc(int size)
    1.60 +{     ENV *env = get_env_ptr();
    1.61 +      MEM *desc;
    1.62 +      int size_of_desc = align_datasize(sizeof(MEM));
    1.63 +      if (size < 1 || size > INT_MAX - size_of_desc)
    1.64 +         xerror("glp_malloc: size = %d; invalid parameter\n", size);
    1.65 +      size += size_of_desc;
    1.66 +      if (xlcmp(xlset(size),
    1.67 +          xlsub(env->mem_limit, env->mem_total)) > 0)
    1.68 +         xerror("glp_malloc: memory limit exceeded\n");
    1.69 +      if (env->mem_count == INT_MAX)
    1.70 +         xerror("glp_malloc: too many memory blocks allocated\n");
    1.71 +      desc = malloc(size);
    1.72 +      if (desc == NULL)
    1.73 +         xerror("glp_malloc: no memory available\n");
    1.74 +      memset(desc, '?', size);
    1.75 +      desc->flag = MEM_MAGIC;
    1.76 +      desc->size = size;
    1.77 +      desc->prev = NULL;
    1.78 +      desc->next = env->mem_ptr;
    1.79 +      if (desc->next != NULL) desc->next->prev = desc;
    1.80 +      env->mem_ptr = desc;
    1.81 +      env->mem_count++;
    1.82 +      if (env->mem_cpeak < env->mem_count)
    1.83 +         env->mem_cpeak = env->mem_count;
    1.84 +      env->mem_total = xladd(env->mem_total, xlset(size));
    1.85 +      if (xlcmp(env->mem_tpeak, env->mem_total) < 0)
    1.86 +         env->mem_tpeak = env->mem_total;
    1.87 +      return (void *)((char *)desc + size_of_desc);
    1.88 +}
    1.89 +
    1.90 +/***********************************************************************
    1.91 +*  NAME
    1.92 +*
    1.93 +*  glp_calloc - allocate memory block
    1.94 +*
    1.95 +*  SYNOPSIS
    1.96 +*
    1.97 +*  void *glp_calloc(int n, int size);
    1.98 +*
    1.99 +*  DESCRIPTION
   1.100 +*
   1.101 +*  The routine glp_calloc allocates a memory block of (n*size) bytes
   1.102 +*  long.
   1.103 +*
   1.104 +*  Note that being allocated the memory block contains arbitrary data
   1.105 +*  (not binary zeros).
   1.106 +*
   1.107 +*  RETURNS
   1.108 +*
   1.109 +*  The routine glp_calloc returns a pointer to the allocated block.
   1.110 +*  To free this block the routine glp_free (not free!) must be used. */
   1.111 +
   1.112 +void *glp_calloc(int n, int size)
   1.113 +{     if (n < 1)
   1.114 +         xerror("glp_calloc: n = %d; invalid parameter\n", n);
   1.115 +      if (size < 1)
   1.116 +         xerror("glp_calloc: size = %d; invalid parameter\n", size);
   1.117 +      if (n > INT_MAX / size)
   1.118 +         xerror("glp_calloc: n = %d; size = %d; array too big\n", n,
   1.119 +            size);
   1.120 +      return xmalloc(n * size);
   1.121 +}
   1.122 +
   1.123 +/***********************************************************************
   1.124 +*  NAME
   1.125 +*
   1.126 +*  glp_free - free memory block
   1.127 +*
   1.128 +*  SYNOPSIS
   1.129 +*
   1.130 +*  void glp_free(void *ptr);
   1.131 +*
   1.132 +*  DESCRIPTION
   1.133 +*
   1.134 +*  The routine glp_free frees a memory block pointed to by ptr, which
   1.135 +*  was previuosly allocated by the routine glp_malloc or glp_calloc. */
   1.136 +
   1.137 +void glp_free(void *ptr)
   1.138 +{     ENV *env = get_env_ptr();
   1.139 +      MEM *desc;
   1.140 +      int size_of_desc = align_datasize(sizeof(MEM));
   1.141 +      if (ptr == NULL)
   1.142 +         xerror("glp_free: ptr = %p; null pointer\n", ptr);
   1.143 +      desc = (void *)((char *)ptr - size_of_desc);
   1.144 +      if (desc->flag != MEM_MAGIC)
   1.145 +         xerror("glp_free: ptr = %p; invalid pointer\n", ptr);
   1.146 +      if (env->mem_count == 0 ||
   1.147 +          xlcmp(env->mem_total, xlset(desc->size)) < 0)
   1.148 +         xerror("glp_free: memory allocation error\n");
   1.149 +      if (desc->prev == NULL)
   1.150 +         env->mem_ptr = desc->next;
   1.151 +      else
   1.152 +         desc->prev->next = desc->next;
   1.153 +      if (desc->next == NULL)
   1.154 +         ;
   1.155 +      else
   1.156 +         desc->next->prev = desc->prev;
   1.157 +      env->mem_count--;
   1.158 +      env->mem_total = xlsub(env->mem_total, xlset(desc->size));
   1.159 +      memset(desc, '?', size_of_desc);
   1.160 +      free(desc);
   1.161 +      return;
   1.162 +}
   1.163 +
   1.164 +/***********************************************************************
   1.165 +*  NAME
   1.166 +*
   1.167 +*  glp_mem_limit - set memory usage limit
   1.168 +*
   1.169 +*  SYNOPSIS
   1.170 +*
   1.171 +*  void glp_mem_limit(int limit);
   1.172 +*
   1.173 +*  DESCRIPTION
   1.174 +*
   1.175 +*  The routine glp_mem_limit limits the amount of memory available for
   1.176 +*  dynamic allocation (in GLPK routines) to limit megabytes. */
   1.177 +
   1.178 +void glp_mem_limit(int limit)
   1.179 +{     ENV *env = get_env_ptr();
   1.180 +      if (limit < 0)
   1.181 +         xerror("glp_mem_limit: limit = %d; invalid parameter\n",
   1.182 +            limit);
   1.183 +      env->mem_limit = xlmul(xlset(limit), xlset(1 << 20));
   1.184 +      return;
   1.185 +}
   1.186 +
   1.187 +/***********************************************************************
   1.188 +*  NAME
   1.189 +*
   1.190 +*  glp_mem_usage - get memory usage information
   1.191 +*
   1.192 +*  SYNOPSIS
   1.193 +*
   1.194 +*  void glp_mem_usage(int *count, int *cpeak, glp_long *total,
   1.195 +*     glp_long *tpeak);
   1.196 +*
   1.197 +*  DESCRIPTION
   1.198 +*
   1.199 +*  The routine glp_mem_usage reports some information about utilization
   1.200 +*  of the memory by GLPK routines. Information is stored to locations
   1.201 +*  specified by corresponding parameters (see below). Any parameter can
   1.202 +*  be specified as NULL, in which case corresponding information is not
   1.203 +*  stored.
   1.204 +*
   1.205 +*  *count is the number of the memory blocks currently allocated by the
   1.206 +*  routines xmalloc and xcalloc (one call to xmalloc or xcalloc results
   1.207 +*  in allocating one memory block).
   1.208 +*
   1.209 +*  *cpeak is the peak value of *count reached since the initialization
   1.210 +*  of the GLPK library environment.
   1.211 +*
   1.212 +*  *total is the total amount, in bytes, of the memory blocks currently
   1.213 +*  allocated by the routines xmalloc and xcalloc.
   1.214 +*
   1.215 +*  *tpeak is the peak value of *total reached since the initialization
   1.216 +*  of the GLPK library envirionment. */
   1.217 +
   1.218 +void glp_mem_usage(int *count, int *cpeak, glp_long *total,
   1.219 +      glp_long *tpeak)
   1.220 +{     ENV *env = get_env_ptr();
   1.221 +      if (count != NULL) *count = env->mem_count;
   1.222 +      if (cpeak != NULL) *cpeak = env->mem_cpeak;
   1.223 +      if (total != NULL) *total = env->mem_total;
   1.224 +      if (tpeak != NULL) *tpeak = env->mem_tpeak;
   1.225 +      return;
   1.226 +}
   1.227 +
   1.228 +/* eof */