COIN-OR::LEMON - Graph Library

source: glpk-cmake/src/glpenv05.c @ 1:c445c931472f

Last change on this file since 1:c445c931472f was 1:c445c931472f, checked in by Alpar Juttner <alpar@…>, 10 years ago

Import glpk-4.45

  • Generated files and doc/notes are removed
File size: 7.1 KB
Line 
1/* glpenv05.c (memory allocation) */
2
3/***********************************************************************
4*  This code is part of GLPK (GNU Linear Programming Kit).
5*
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>.
10*
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.
15*
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.
20*
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***********************************************************************/
24
25#include "glpapi.h"
26
27/* some processors need data to be properly aligned; the macro
28   align_datasize enlarges the specified size of a data item to provide
29   a proper alignment of immediately following data */
30
31#define align_datasize(size) ((((size) + 15) / 16) * 16)
32/* 16 bytes is sufficient in both 32- and 64-bit environments
33   (8 bytes is not sufficient in 64-bit environment due to jmp_buf) */
34
35/***********************************************************************
36*  NAME
37*
38*  glp_malloc - allocate memory block
39*
40*  SYNOPSIS
41*
42*  void *glp_malloc(int size);
43*
44*  DESCRIPTION
45*
46*  The routine glp_malloc allocates a memory block of size bytes long.
47*
48*  Note that being allocated the memory block contains arbitrary data
49*  (not binary zeros).
50*
51*  RETURNS
52*
53*  The routine glp_malloc returns a pointer to the allocated block.
54*  To free this block the routine glp_free (not free!) must be used. */
55
56void *glp_malloc(int size)
57{     ENV *env = get_env_ptr();
58      MEM *desc;
59      int size_of_desc = align_datasize(sizeof(MEM));
60      if (size < 1 || size > INT_MAX - size_of_desc)
61         xerror("glp_malloc: size = %d; invalid parameter\n", size);
62      size += size_of_desc;
63      if (xlcmp(xlset(size),
64          xlsub(env->mem_limit, env->mem_total)) > 0)
65         xerror("glp_malloc: memory limit exceeded\n");
66      if (env->mem_count == INT_MAX)
67         xerror("glp_malloc: too many memory blocks allocated\n");
68      desc = malloc(size);
69      if (desc == NULL)
70         xerror("glp_malloc: no memory available\n");
71      memset(desc, '?', size);
72      desc->flag = MEM_MAGIC;
73      desc->size = size;
74      desc->prev = NULL;
75      desc->next = env->mem_ptr;
76      if (desc->next != NULL) desc->next->prev = desc;
77      env->mem_ptr = desc;
78      env->mem_count++;
79      if (env->mem_cpeak < env->mem_count)
80         env->mem_cpeak = env->mem_count;
81      env->mem_total = xladd(env->mem_total, xlset(size));
82      if (xlcmp(env->mem_tpeak, env->mem_total) < 0)
83         env->mem_tpeak = env->mem_total;
84      return (void *)((char *)desc + size_of_desc);
85}
86
87/***********************************************************************
88*  NAME
89*
90*  glp_calloc - allocate memory block
91*
92*  SYNOPSIS
93*
94*  void *glp_calloc(int n, int size);
95*
96*  DESCRIPTION
97*
98*  The routine glp_calloc allocates a memory block of (n*size) bytes
99*  long.
100*
101*  Note that being allocated the memory block contains arbitrary data
102*  (not binary zeros).
103*
104*  RETURNS
105*
106*  The routine glp_calloc returns a pointer to the allocated block.
107*  To free this block the routine glp_free (not free!) must be used. */
108
109void *glp_calloc(int n, int size)
110{     if (n < 1)
111         xerror("glp_calloc: n = %d; invalid parameter\n", n);
112      if (size < 1)
113         xerror("glp_calloc: size = %d; invalid parameter\n", size);
114      if (n > INT_MAX / size)
115         xerror("glp_calloc: n = %d; size = %d; array too big\n", n,
116            size);
117      return xmalloc(n * size);
118}
119
120/***********************************************************************
121*  NAME
122*
123*  glp_free - free memory block
124*
125*  SYNOPSIS
126*
127*  void glp_free(void *ptr);
128*
129*  DESCRIPTION
130*
131*  The routine glp_free frees a memory block pointed to by ptr, which
132*  was previuosly allocated by the routine glp_malloc or glp_calloc. */
133
134void glp_free(void *ptr)
135{     ENV *env = get_env_ptr();
136      MEM *desc;
137      int size_of_desc = align_datasize(sizeof(MEM));
138      if (ptr == NULL)
139         xerror("glp_free: ptr = %p; null pointer\n", ptr);
140      desc = (void *)((char *)ptr - size_of_desc);
141      if (desc->flag != MEM_MAGIC)
142         xerror("glp_free: ptr = %p; invalid pointer\n", ptr);
143      if (env->mem_count == 0 ||
144          xlcmp(env->mem_total, xlset(desc->size)) < 0)
145         xerror("glp_free: memory allocation error\n");
146      if (desc->prev == NULL)
147         env->mem_ptr = desc->next;
148      else
149         desc->prev->next = desc->next;
150      if (desc->next == NULL)
151         ;
152      else
153         desc->next->prev = desc->prev;
154      env->mem_count--;
155      env->mem_total = xlsub(env->mem_total, xlset(desc->size));
156      memset(desc, '?', size_of_desc);
157      free(desc);
158      return;
159}
160
161/***********************************************************************
162*  NAME
163*
164*  glp_mem_limit - set memory usage limit
165*
166*  SYNOPSIS
167*
168*  void glp_mem_limit(int limit);
169*
170*  DESCRIPTION
171*
172*  The routine glp_mem_limit limits the amount of memory available for
173*  dynamic allocation (in GLPK routines) to limit megabytes. */
174
175void glp_mem_limit(int limit)
176{     ENV *env = get_env_ptr();
177      if (limit < 0)
178         xerror("glp_mem_limit: limit = %d; invalid parameter\n",
179            limit);
180      env->mem_limit = xlmul(xlset(limit), xlset(1 << 20));
181      return;
182}
183
184/***********************************************************************
185*  NAME
186*
187*  glp_mem_usage - get memory usage information
188*
189*  SYNOPSIS
190*
191*  void glp_mem_usage(int *count, int *cpeak, glp_long *total,
192*     glp_long *tpeak);
193*
194*  DESCRIPTION
195*
196*  The routine glp_mem_usage reports some information about utilization
197*  of the memory by GLPK routines. Information is stored to locations
198*  specified by corresponding parameters (see below). Any parameter can
199*  be specified as NULL, in which case corresponding information is not
200*  stored.
201*
202*  *count is the number of the memory blocks currently allocated by the
203*  routines xmalloc and xcalloc (one call to xmalloc or xcalloc results
204*  in allocating one memory block).
205*
206*  *cpeak is the peak value of *count reached since the initialization
207*  of the GLPK library environment.
208*
209*  *total is the total amount, in bytes, of the memory blocks currently
210*  allocated by the routines xmalloc and xcalloc.
211*
212*  *tpeak is the peak value of *total reached since the initialization
213*  of the GLPK library envirionment. */
214
215void glp_mem_usage(int *count, int *cpeak, glp_long *total,
216      glp_long *tpeak)
217{     ENV *env = get_env_ptr();
218      if (count != NULL) *count = env->mem_count;
219      if (cpeak != NULL) *cpeak = env->mem_cpeak;
220      if (total != NULL) *total = env->mem_total;
221      if (tpeak != NULL) *tpeak = env->mem_tpeak;
222      return;
223}
224
225/* eof */
Note: See TracBrowser for help on using the repository browser.