-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathglpdmp.c
259 lines (243 loc) · 7.6 KB
/
glpdmp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/* glpdmp.c (dynamic memory pool) */
/***********************************************************************
* This code is part of GLPK (GNU Linear Programming Kit).
*
* Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
* 2009, 2010, 2011, 2013 Andrew Makhorin, Department for Applied
* Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
* reserved. E-mail: <[email protected]>.
*
* GLPK is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* GLPK is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
* License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
***********************************************************************/
#include "glpdmp.h"
#if 1 /* 29/VIII-2008 */
/* some processors need data to be properly aligned; the macro
align_datasize enlarges the specified size of a data item to provide
a proper alignment of immediately following data */
#define align_datasize(size) ((((size) + 7) / 8) * 8)
/* 8 bytes is sufficient in both 32- and 64-bit environments */
#endif
#ifdef GLP_DEBUG
struct info
{ DMP *pool;
int size;
};
#endif
/***********************************************************************
* NAME
*
* dmp_create_pool - create dynamic memory pool
*
* SYNOPSIS
*
* #include "glpdmp.h"
* DMP *dmp_create_pool(void);
*
* DESCRIPTION
*
* The routine dmp_create_pool creates a dynamic memory pool.
*
* RETURNS
*
* The routine returns a pointer to the memory pool created. */
DMP *dmp_create_pool(void)
{ DMP *pool;
int k;
#ifdef GLP_DEBUG
xprintf("dmp_create_pool: warning: debug mode enabled\n");
#endif
pool = xmalloc(sizeof(DMP));
#if 0
pool->size = 0;
#endif
for (k = 0; k <= 31; k++) pool->avail[k] = NULL;
pool->block = NULL;
pool->used = DMP_BLK_SIZE;
pool->count.lo = pool->count.hi = 0;
return pool;
}
/***********************************************************************
* NAME
*
* dmp_get_atom - get free atom from dynamic memory pool
*
* SYNOPSIS
*
* #include "glpdmp.h"
* void *dmp_get_atom(DMP *pool, int size);
*
* DESCRIPTION
*
* The routine dmp_get_atom obtains a free atom (memory block) from the
* specified memory pool.
*
* The parameter size is the atom size, in bytes, 1 <= size <= 256.
*
* Note that the free atom contains arbitrary data, not binary zeros.
*
* RETURNS
*
* The routine returns a pointer to the free atom obtained. */
void *dmp_get_atom(DMP *pool, int size)
{ void *atom;
int k;
#ifdef GLP_DEBUG
int orig_size = size;
#endif
if (!(1 <= size && size <= 256))
xerror("dmp_get_atom: size = %d; invalid atom size\n", size);
#if 0
if (!(pool->size == 0 || pool->size == size))
xerror("dmp_get_atom: size = %d; wrong atom size\n", size);
#endif
/* adjust the size to provide the proper data alignment */
size = align_datasize(size);
#ifdef GLP_DEBUG
size += align_datasize(sizeof(struct info));
#endif
/* adjust the size to make it multiple of 8 bytes, if needed */
size = ((size + 7) / 8) * 8;
/* determine the corresponding list of free cells */
k = size / 8 - 1;
xassert(0 <= k && k <= 31);
/* obtain a free atom */
if (pool->avail[k] == NULL)
{ /* the list of free cells is empty */
if (pool->used + size > DMP_BLK_SIZE)
{ /* allocate a new memory block */
void *block = xmalloc(DMP_BLK_SIZE);
*(void **)block = pool->block;
pool->block = block;
pool->used = align_datasize(sizeof(void *));
}
/* place the atom in the current memory block */
atom = (char *)pool->block + pool->used;
pool->used += size;
}
else
{ /* obtain the atom from the list of free cells */
atom = pool->avail[k];
pool->avail[k] = *(void **)atom;
}
memset(atom, '?', size);
/* increase the number of atoms which are currently in use */
pool->count.lo++;
if (pool->count.lo == 0) pool->count.hi++;
#ifdef GLP_DEBUG
((struct info *)atom)->pool = pool;
((struct info *)atom)->size = orig_size;
atom = (char *)atom + align_datasize(sizeof(struct info));
#endif
return atom;
}
/***********************************************************************
* NAME
*
* dmp_free_atom - return atom to dynamic memory pool
*
* SYNOPSIS
*
* #include "glpdmp.h"
* void dmp_free_atom(DMP *pool, void *atom, int size);
*
* DESCRIPTION
*
* The routine dmp_free_atom returns the specified atom (memory block)
* to the specified memory pool, making it free.
*
* The parameter size is the atom size, in bytes, 1 <= size <= 256.
*
* Note that the atom can be returned only to the pool, from which it
* was obtained, and its size must be exactly the same as on obtaining
* it from the pool. */
void dmp_free_atom(DMP *pool, void *atom, int size)
{ int k;
if (!(1 <= size && size <= 256))
xerror("dmp_free_atom: size = %d; invalid atom size\n", size);
#if 0
if (!(pool->size == 0 || pool->size == size))
xerror("dmp_free_atom: size = %d; wrong atom size\n", size);
#endif
if (pool->count.lo == 0 && pool->count.hi == 0)
xerror("dmp_free_atom: pool allocation error\n");
#ifdef GLP_DEBUG
atom = (char *)atom - align_datasize(sizeof(struct info));
xassert(((struct info *)atom)->pool == pool);
xassert(((struct info *)atom)->size == size);
#endif
/* adjust the size to provide the proper data alignment */
size = align_datasize(size);
#ifdef GLP_DEBUG
size += align_datasize(sizeof(struct info));
#endif
/* adjust the size to make it multiple of 8 bytes, if needed */
size = ((size + 7) / 8) * 8;
/* determine the corresponding list of free cells */
k = size / 8 - 1;
xassert(0 <= k && k <= 31);
/* return the atom to the list of free cells */
*(void **)atom = pool->avail[k];
pool->avail[k] = atom;
/* decrease the number of atoms which are currently in use */
pool->count.lo--;
if (pool->count.lo == 0xFFFFFFFF) pool->count.hi--;
return;
}
/***********************************************************************
* NAME
*
* dmp_in_use - determine how many atoms are still in use
*
* SYNOPSIS
*
* #include "glpdmp.h"
* glp_long dmp_in_use(DMP *pool);
*
* DESCRIPTION
*
* The routine dmp_in_use determines how many atoms allocated from the
* specified memory pool with the routine dmp_get_atom are still in use,
* i.e. not returned to the pool with the routine dmp_free_atom.
*
* RETURNS
*
* The routine returns the number of atoms which are still in use. */
glp_long dmp_in_use(DMP *pool)
{ return
pool->count;
}
/***********************************************************************
* NAME
*
* dmp_delete_pool - delete dynamic memory pool
*
* SYNOPSIS
*
* #include "glpdmp.h"
* void dmp_delete_pool(DMP *pool);
*
* DESCRIPTION
*
* The routine dmp_delete_pool deletes the specified dynamic memory
* pool and frees all the memory allocated to this object. */
void dmp_delete_pool(DMP *pool)
{ while (pool->block != NULL)
{ void *block = pool->block;
pool->block = *(void **)block;
xfree(block);
}
xfree(pool);
return;
}
/* eof */