1 /* glpdmp.c (dynamic memory pool) */
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 #if 1 /* 29/VIII-2008 */
28 /* some processors need data to be properly aligned; the macro
29 align_datasize enlarges the specified size of a data item to provide
30 a proper alignment of immediately following data */
32 #define align_datasize(size) ((((size) + 7) / 8) * 8)
33 /* 8 bytes is sufficient in both 32- and 64-bit environments */
43 /***********************************************************************
46 * dmp_create_pool - create dynamic memory pool
51 * DMP *dmp_create_pool(void);
55 * The routine dmp_create_pool creates a dynamic memory pool.
59 * The routine returns a pointer to the memory pool created. */
61 DMP *dmp_create_pool(void)
65 xprintf("dmp_create_pool: warning: debug mode enabled\n");
67 pool = xmalloc(sizeof(DMP));
71 for (k = 0; k <= 31; k++) pool->avail[k] = NULL;
73 pool->used = DMP_BLK_SIZE;
74 pool->count.lo = pool->count.hi = 0;
78 /***********************************************************************
81 * dmp_get_atom - get free atom from dynamic memory pool
86 * void *dmp_get_atom(DMP *pool, int size);
90 * The routine dmp_get_atom obtains a free atom (memory block) from the
91 * specified memory pool.
93 * The parameter size is the atom size, in bytes, 1 <= size <= 256.
95 * Note that the free atom contains arbitrary data, not binary zeros.
99 * The routine returns a pointer to the free atom obtained. */
101 void *dmp_get_atom(DMP *pool, int size)
105 int orig_size = size;
107 if (!(1 <= size && size <= 256))
108 xerror("dmp_get_atom: size = %d; invalid atom size\n", size);
110 if (!(pool->size == 0 || pool->size == size))
111 xerror("dmp_get_atom: size = %d; wrong atom size\n", size);
113 /* adjust the size to provide the proper data alignment */
114 size = align_datasize(size);
116 size += align_datasize(sizeof(struct info));
118 /* adjust the size to make it multiple of 8 bytes, if needed */
119 size = ((size + 7) / 8) * 8;
120 /* determine the corresponding list of free cells */
122 xassert(0 <= k && k <= 31);
123 /* obtain a free atom */
124 if (pool->avail[k] == NULL)
125 { /* the list of free cells is empty */
126 if (pool->used + size > DMP_BLK_SIZE)
127 { /* allocate a new memory block */
128 void *block = xmalloc(DMP_BLK_SIZE);
129 *(void **)block = pool->block;
131 pool->used = align_datasize(sizeof(void *));
133 /* place the atom in the current memory block */
134 atom = (char *)pool->block + pool->used;
138 { /* obtain the atom from the list of free cells */
139 atom = pool->avail[k];
140 pool->avail[k] = *(void **)atom;
142 memset(atom, '?', size);
143 /* increase the number of atoms which are currently in use */
145 if (pool->count.lo == 0) pool->count.hi++;
147 ((struct info *)atom)->pool = pool;
148 ((struct info *)atom)->size = orig_size;
149 atom = (char *)atom + align_datasize(sizeof(struct info));
154 /***********************************************************************
157 * dmp_free_atom - return atom to dynamic memory pool
161 * #include "glpdmp.h"
162 * void dmp_free_atom(DMP *pool, void *atom, int size);
166 * The routine dmp_free_atom returns the specified atom (memory block)
167 * to the specified memory pool, making it free.
169 * The parameter size is the atom size, in bytes, 1 <= size <= 256.
171 * Note that the atom can be returned only to the pool, from which it
172 * was obtained, and its size must be exactly the same as on obtaining
173 * it from the pool. */
175 void dmp_free_atom(DMP *pool, void *atom, int size)
177 if (!(1 <= size && size <= 256))
178 xerror("dmp_free_atom: size = %d; invalid atom size\n", size);
180 if (!(pool->size == 0 || pool->size == size))
181 xerror("dmp_free_atom: size = %d; wrong atom size\n", size);
183 if (pool->count.lo == 0 && pool->count.hi == 0)
184 xerror("dmp_free_atom: pool allocation error\n");
186 atom = (char *)atom - align_datasize(sizeof(struct info));
187 xassert(((struct info *)atom)->pool == pool);
188 xassert(((struct info *)atom)->size == size);
190 /* adjust the size to provide the proper data alignment */
191 size = align_datasize(size);
193 size += align_datasize(sizeof(struct info));
195 /* adjust the size to make it multiple of 8 bytes, if needed */
196 size = ((size + 7) / 8) * 8;
197 /* determine the corresponding list of free cells */
199 xassert(0 <= k && k <= 31);
200 /* return the atom to the list of free cells */
201 *(void **)atom = pool->avail[k];
202 pool->avail[k] = atom;
203 /* decrease the number of atoms which are currently in use */
205 if (pool->count.lo == 0xFFFFFFFF) pool->count.hi--;
209 /***********************************************************************
212 * dmp_in_use - determine how many atoms are still in use
216 * #include "glpdmp.h"
217 * glp_long dmp_in_use(DMP *pool);
221 * The routine dmp_in_use determines how many atoms allocated from the
222 * specified memory pool with the routine dmp_get_atom are still in use,
223 * i.e. not returned to the pool with the routine dmp_free_atom.
227 * The routine returns the number of atoms which are still in use. */
229 glp_long dmp_in_use(DMP *pool)
234 /***********************************************************************
237 * dmp_delete_pool - delete dynamic memory pool
241 * #include "glpdmp.h"
242 * void dmp_delete_pool(DMP *pool);
246 * The routine dmp_delete_pool deletes the specified dynamic memory
247 * pool and frees all the memory allocated to this object. */
249 void dmp_delete_pool(DMP *pool)
250 { while (pool->block != NULL)
251 { void *block = pool->block;
252 pool->block = *(void **)block;