1 /* glpenv05.c (memory allocation) */
3 /***********************************************************************
4 * This code is part of GLPK (GNU Linear Programming Kit).
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>.
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.
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.
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 ***********************************************************************/
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 */
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) */
35 /***********************************************************************
38 * glp_malloc - allocate memory block
42 * void *glp_malloc(int size);
46 * The routine glp_malloc allocates a memory block of size bytes long.
48 * Note that being allocated the memory block contains arbitrary data
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. */
56 void *glp_malloc(int size)
57 { ENV *env = get_env_ptr();
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);
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");
70 xerror("glp_malloc: no memory available\n");
71 memset(desc, '?', size);
72 desc->flag = MEM_MAGIC;
75 desc->next = env->mem_ptr;
76 if (desc->next != NULL) desc->next->prev = desc;
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);
87 /***********************************************************************
90 * glp_calloc - allocate memory block
94 * void *glp_calloc(int n, int size);
98 * The routine glp_calloc allocates a memory block of (n*size) bytes
101 * Note that being allocated the memory block contains arbitrary data
102 * (not binary zeros).
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. */
109 void *glp_calloc(int n, int size)
111 xerror("glp_calloc: n = %d; invalid parameter\n", n);
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,
117 return xmalloc(n * size);
120 /***********************************************************************
123 * glp_free - free memory block
127 * void glp_free(void *ptr);
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. */
134 void glp_free(void *ptr)
135 { ENV *env = get_env_ptr();
137 int size_of_desc = align_datasize(sizeof(MEM));
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;
149 desc->prev->next = desc->next;
150 if (desc->next == NULL)
153 desc->next->prev = desc->prev;
155 env->mem_total = xlsub(env->mem_total, xlset(desc->size));
156 memset(desc, '?', size_of_desc);
161 /***********************************************************************
164 * glp_mem_limit - set memory usage limit
168 * void glp_mem_limit(int limit);
172 * The routine glp_mem_limit limits the amount of memory available for
173 * dynamic allocation (in GLPK routines) to limit megabytes. */
175 void glp_mem_limit(int limit)
176 { ENV *env = get_env_ptr();
178 xerror("glp_mem_limit: limit = %d; invalid parameter\n",
180 env->mem_limit = xlmul(xlset(limit), xlset(1 << 20));
184 /***********************************************************************
187 * glp_mem_usage - get memory usage information
191 * void glp_mem_usage(int *count, int *cpeak, glp_long *total,
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
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).
206 * *cpeak is the peak value of *count reached since the initialization
207 * of the GLPK library environment.
209 * *total is the total amount, in bytes, of the memory blocks currently
210 * allocated by the routines xmalloc and xcalloc.
212 * *tpeak is the peak value of *total reached since the initialization
213 * of the GLPK library envirionment. */
215 void glp_mem_usage(int *count, int *cpeak, glp_long *total,
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;