rev |
line source |
alpar@9
|
1 /* glpenv05.c (memory allocation) */
|
alpar@9
|
2
|
alpar@9
|
3 /***********************************************************************
|
alpar@9
|
4 * This code is part of GLPK (GNU Linear Programming Kit).
|
alpar@9
|
5 *
|
alpar@9
|
6 * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
|
alpar@9
|
7 * 2009, 2010, 2011 Andrew Makhorin, Department for Applied Informatics,
|
alpar@9
|
8 * Moscow Aviation Institute, Moscow, Russia. All rights reserved.
|
alpar@9
|
9 * E-mail: <mao@gnu.org>.
|
alpar@9
|
10 *
|
alpar@9
|
11 * GLPK is free software: you can redistribute it and/or modify it
|
alpar@9
|
12 * under the terms of the GNU General Public License as published by
|
alpar@9
|
13 * the Free Software Foundation, either version 3 of the License, or
|
alpar@9
|
14 * (at your option) any later version.
|
alpar@9
|
15 *
|
alpar@9
|
16 * GLPK is distributed in the hope that it will be useful, but WITHOUT
|
alpar@9
|
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
alpar@9
|
18 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
|
alpar@9
|
19 * License for more details.
|
alpar@9
|
20 *
|
alpar@9
|
21 * You should have received a copy of the GNU General Public License
|
alpar@9
|
22 * along with GLPK. If not, see <http://www.gnu.org/licenses/>.
|
alpar@9
|
23 ***********************************************************************/
|
alpar@9
|
24
|
alpar@9
|
25 #include "glpapi.h"
|
alpar@9
|
26
|
alpar@9
|
27 /* some processors need data to be properly aligned; the macro
|
alpar@9
|
28 align_datasize enlarges the specified size of a data item to provide
|
alpar@9
|
29 a proper alignment of immediately following data */
|
alpar@9
|
30
|
alpar@9
|
31 #define align_datasize(size) ((((size) + 15) / 16) * 16)
|
alpar@9
|
32 /* 16 bytes is sufficient in both 32- and 64-bit environments
|
alpar@9
|
33 (8 bytes is not sufficient in 64-bit environment due to jmp_buf) */
|
alpar@9
|
34
|
alpar@9
|
35 /***********************************************************************
|
alpar@9
|
36 * NAME
|
alpar@9
|
37 *
|
alpar@9
|
38 * glp_malloc - allocate memory block
|
alpar@9
|
39 *
|
alpar@9
|
40 * SYNOPSIS
|
alpar@9
|
41 *
|
alpar@9
|
42 * void *glp_malloc(int size);
|
alpar@9
|
43 *
|
alpar@9
|
44 * DESCRIPTION
|
alpar@9
|
45 *
|
alpar@9
|
46 * The routine glp_malloc allocates a memory block of size bytes long.
|
alpar@9
|
47 *
|
alpar@9
|
48 * Note that being allocated the memory block contains arbitrary data
|
alpar@9
|
49 * (not binary zeros).
|
alpar@9
|
50 *
|
alpar@9
|
51 * RETURNS
|
alpar@9
|
52 *
|
alpar@9
|
53 * The routine glp_malloc returns a pointer to the allocated block.
|
alpar@9
|
54 * To free this block the routine glp_free (not free!) must be used. */
|
alpar@9
|
55
|
alpar@9
|
56 void *glp_malloc(int size)
|
alpar@9
|
57 { ENV *env = get_env_ptr();
|
alpar@9
|
58 MEM *desc;
|
alpar@9
|
59 int size_of_desc = align_datasize(sizeof(MEM));
|
alpar@9
|
60 if (size < 1 || size > INT_MAX - size_of_desc)
|
alpar@9
|
61 xerror("glp_malloc: size = %d; invalid parameter\n", size);
|
alpar@9
|
62 size += size_of_desc;
|
alpar@9
|
63 if (xlcmp(xlset(size),
|
alpar@9
|
64 xlsub(env->mem_limit, env->mem_total)) > 0)
|
alpar@9
|
65 xerror("glp_malloc: memory limit exceeded\n");
|
alpar@9
|
66 if (env->mem_count == INT_MAX)
|
alpar@9
|
67 xerror("glp_malloc: too many memory blocks allocated\n");
|
alpar@9
|
68 desc = malloc(size);
|
alpar@9
|
69 if (desc == NULL)
|
alpar@9
|
70 xerror("glp_malloc: no memory available\n");
|
alpar@9
|
71 memset(desc, '?', size);
|
alpar@9
|
72 desc->flag = MEM_MAGIC;
|
alpar@9
|
73 desc->size = size;
|
alpar@9
|
74 desc->prev = NULL;
|
alpar@9
|
75 desc->next = env->mem_ptr;
|
alpar@9
|
76 if (desc->next != NULL) desc->next->prev = desc;
|
alpar@9
|
77 env->mem_ptr = desc;
|
alpar@9
|
78 env->mem_count++;
|
alpar@9
|
79 if (env->mem_cpeak < env->mem_count)
|
alpar@9
|
80 env->mem_cpeak = env->mem_count;
|
alpar@9
|
81 env->mem_total = xladd(env->mem_total, xlset(size));
|
alpar@9
|
82 if (xlcmp(env->mem_tpeak, env->mem_total) < 0)
|
alpar@9
|
83 env->mem_tpeak = env->mem_total;
|
alpar@9
|
84 return (void *)((char *)desc + size_of_desc);
|
alpar@9
|
85 }
|
alpar@9
|
86
|
alpar@9
|
87 /***********************************************************************
|
alpar@9
|
88 * NAME
|
alpar@9
|
89 *
|
alpar@9
|
90 * glp_calloc - allocate memory block
|
alpar@9
|
91 *
|
alpar@9
|
92 * SYNOPSIS
|
alpar@9
|
93 *
|
alpar@9
|
94 * void *glp_calloc(int n, int size);
|
alpar@9
|
95 *
|
alpar@9
|
96 * DESCRIPTION
|
alpar@9
|
97 *
|
alpar@9
|
98 * The routine glp_calloc allocates a memory block of (n*size) bytes
|
alpar@9
|
99 * long.
|
alpar@9
|
100 *
|
alpar@9
|
101 * Note that being allocated the memory block contains arbitrary data
|
alpar@9
|
102 * (not binary zeros).
|
alpar@9
|
103 *
|
alpar@9
|
104 * RETURNS
|
alpar@9
|
105 *
|
alpar@9
|
106 * The routine glp_calloc returns a pointer to the allocated block.
|
alpar@9
|
107 * To free this block the routine glp_free (not free!) must be used. */
|
alpar@9
|
108
|
alpar@9
|
109 void *glp_calloc(int n, int size)
|
alpar@9
|
110 { if (n < 1)
|
alpar@9
|
111 xerror("glp_calloc: n = %d; invalid parameter\n", n);
|
alpar@9
|
112 if (size < 1)
|
alpar@9
|
113 xerror("glp_calloc: size = %d; invalid parameter\n", size);
|
alpar@9
|
114 if (n > INT_MAX / size)
|
alpar@9
|
115 xerror("glp_calloc: n = %d; size = %d; array too big\n", n,
|
alpar@9
|
116 size);
|
alpar@9
|
117 return xmalloc(n * size);
|
alpar@9
|
118 }
|
alpar@9
|
119
|
alpar@9
|
120 /***********************************************************************
|
alpar@9
|
121 * NAME
|
alpar@9
|
122 *
|
alpar@9
|
123 * glp_free - free memory block
|
alpar@9
|
124 *
|
alpar@9
|
125 * SYNOPSIS
|
alpar@9
|
126 *
|
alpar@9
|
127 * void glp_free(void *ptr);
|
alpar@9
|
128 *
|
alpar@9
|
129 * DESCRIPTION
|
alpar@9
|
130 *
|
alpar@9
|
131 * The routine glp_free frees a memory block pointed to by ptr, which
|
alpar@9
|
132 * was previuosly allocated by the routine glp_malloc or glp_calloc. */
|
alpar@9
|
133
|
alpar@9
|
134 void glp_free(void *ptr)
|
alpar@9
|
135 { ENV *env = get_env_ptr();
|
alpar@9
|
136 MEM *desc;
|
alpar@9
|
137 int size_of_desc = align_datasize(sizeof(MEM));
|
alpar@9
|
138 if (ptr == NULL)
|
alpar@9
|
139 xerror("glp_free: ptr = %p; null pointer\n", ptr);
|
alpar@9
|
140 desc = (void *)((char *)ptr - size_of_desc);
|
alpar@9
|
141 if (desc->flag != MEM_MAGIC)
|
alpar@9
|
142 xerror("glp_free: ptr = %p; invalid pointer\n", ptr);
|
alpar@9
|
143 if (env->mem_count == 0 ||
|
alpar@9
|
144 xlcmp(env->mem_total, xlset(desc->size)) < 0)
|
alpar@9
|
145 xerror("glp_free: memory allocation error\n");
|
alpar@9
|
146 if (desc->prev == NULL)
|
alpar@9
|
147 env->mem_ptr = desc->next;
|
alpar@9
|
148 else
|
alpar@9
|
149 desc->prev->next = desc->next;
|
alpar@9
|
150 if (desc->next == NULL)
|
alpar@9
|
151 ;
|
alpar@9
|
152 else
|
alpar@9
|
153 desc->next->prev = desc->prev;
|
alpar@9
|
154 env->mem_count--;
|
alpar@9
|
155 env->mem_total = xlsub(env->mem_total, xlset(desc->size));
|
alpar@9
|
156 memset(desc, '?', size_of_desc);
|
alpar@9
|
157 free(desc);
|
alpar@9
|
158 return;
|
alpar@9
|
159 }
|
alpar@9
|
160
|
alpar@9
|
161 /***********************************************************************
|
alpar@9
|
162 * NAME
|
alpar@9
|
163 *
|
alpar@9
|
164 * glp_mem_limit - set memory usage limit
|
alpar@9
|
165 *
|
alpar@9
|
166 * SYNOPSIS
|
alpar@9
|
167 *
|
alpar@9
|
168 * void glp_mem_limit(int limit);
|
alpar@9
|
169 *
|
alpar@9
|
170 * DESCRIPTION
|
alpar@9
|
171 *
|
alpar@9
|
172 * The routine glp_mem_limit limits the amount of memory available for
|
alpar@9
|
173 * dynamic allocation (in GLPK routines) to limit megabytes. */
|
alpar@9
|
174
|
alpar@9
|
175 void glp_mem_limit(int limit)
|
alpar@9
|
176 { ENV *env = get_env_ptr();
|
alpar@9
|
177 if (limit < 0)
|
alpar@9
|
178 xerror("glp_mem_limit: limit = %d; invalid parameter\n",
|
alpar@9
|
179 limit);
|
alpar@9
|
180 env->mem_limit = xlmul(xlset(limit), xlset(1 << 20));
|
alpar@9
|
181 return;
|
alpar@9
|
182 }
|
alpar@9
|
183
|
alpar@9
|
184 /***********************************************************************
|
alpar@9
|
185 * NAME
|
alpar@9
|
186 *
|
alpar@9
|
187 * glp_mem_usage - get memory usage information
|
alpar@9
|
188 *
|
alpar@9
|
189 * SYNOPSIS
|
alpar@9
|
190 *
|
alpar@9
|
191 * void glp_mem_usage(int *count, int *cpeak, glp_long *total,
|
alpar@9
|
192 * glp_long *tpeak);
|
alpar@9
|
193 *
|
alpar@9
|
194 * DESCRIPTION
|
alpar@9
|
195 *
|
alpar@9
|
196 * The routine glp_mem_usage reports some information about utilization
|
alpar@9
|
197 * of the memory by GLPK routines. Information is stored to locations
|
alpar@9
|
198 * specified by corresponding parameters (see below). Any parameter can
|
alpar@9
|
199 * be specified as NULL, in which case corresponding information is not
|
alpar@9
|
200 * stored.
|
alpar@9
|
201 *
|
alpar@9
|
202 * *count is the number of the memory blocks currently allocated by the
|
alpar@9
|
203 * routines xmalloc and xcalloc (one call to xmalloc or xcalloc results
|
alpar@9
|
204 * in allocating one memory block).
|
alpar@9
|
205 *
|
alpar@9
|
206 * *cpeak is the peak value of *count reached since the initialization
|
alpar@9
|
207 * of the GLPK library environment.
|
alpar@9
|
208 *
|
alpar@9
|
209 * *total is the total amount, in bytes, of the memory blocks currently
|
alpar@9
|
210 * allocated by the routines xmalloc and xcalloc.
|
alpar@9
|
211 *
|
alpar@9
|
212 * *tpeak is the peak value of *total reached since the initialization
|
alpar@9
|
213 * of the GLPK library envirionment. */
|
alpar@9
|
214
|
alpar@9
|
215 void glp_mem_usage(int *count, int *cpeak, glp_long *total,
|
alpar@9
|
216 glp_long *tpeak)
|
alpar@9
|
217 { ENV *env = get_env_ptr();
|
alpar@9
|
218 if (count != NULL) *count = env->mem_count;
|
alpar@9
|
219 if (cpeak != NULL) *cpeak = env->mem_cpeak;
|
alpar@9
|
220 if (total != NULL) *total = env->mem_total;
|
alpar@9
|
221 if (tpeak != NULL) *tpeak = env->mem_tpeak;
|
alpar@9
|
222 return;
|
alpar@9
|
223 }
|
alpar@9
|
224
|
alpar@9
|
225 /* eof */
|