-
Notifications
You must be signed in to change notification settings - Fork 64
/
mcore.c
393 lines (317 loc) · 11.7 KB
/
mcore.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/*!
\file
\brief Functions dealing with creating and allocating mcores
\date Started 5/30/11
\author George
\author Copyright 1997-2011, Regents of the University of Minnesota
\version $Id: mcore.c 13953 2013-03-30 16:20:07Z karypis $
*/
#include <GKlib.h>
/*************************************************************************/
/*! This function creates an mcore
*/
/*************************************************************************/
gk_mcore_t *gk_mcoreCreate(size_t coresize)
{
gk_mcore_t *mcore;
mcore = (gk_mcore_t *)gk_malloc(sizeof(gk_mcore_t), "gk_mcoreCreate: mcore");
memset(mcore, 0, sizeof(gk_mcore_t));
mcore->coresize = coresize;
mcore->corecpos = 0;
mcore->core = (coresize == 0 ? NULL : gk_malloc(mcore->coresize, "gk_mcoreCreate: core"));
/* allocate the memory for keeping track of malloc ops */
mcore->nmops = 2048;
mcore->cmop = 0;
mcore->mops = (gk_mop_t *)gk_malloc(mcore->nmops*sizeof(gk_mop_t), "gk_mcoreCreate: mcore->mops");
return mcore;
}
/*************************************************************************/
/*! This function creates an mcore. This version is used for gkmcore.
*/
/*************************************************************************/
gk_mcore_t *gk_gkmcoreCreate()
{
gk_mcore_t *mcore;
if ((mcore = (gk_mcore_t *)malloc(sizeof(gk_mcore_t))) == NULL)
return NULL;
memset(mcore, 0, sizeof(gk_mcore_t));
/* allocate the memory for keeping track of malloc ops */
mcore->nmops = 2048;
mcore->cmop = 0;
if ((mcore->mops = (gk_mop_t *)malloc(mcore->nmops*sizeof(gk_mop_t))) == NULL) {
free(mcore);
return NULL;
}
return mcore;
}
/*************************************************************************/
/*! This function destroys an mcore.
*/
/*************************************************************************/
void gk_mcoreDestroy(gk_mcore_t **r_mcore, int showstats)
{
gk_mcore_t *mcore = *r_mcore;
if (mcore == NULL)
return;
if (showstats)
printf("\n gk_mcore statistics\n"
" coresize: %12zu nmops: %12zu cmop: %6zu\n"
" num_callocs: %12zu num_hallocs: %12zu\n"
" size_callocs: %12zu size_hallocs: %12zu\n"
" cur_callocs: %12zu cur_hallocs: %12zu\n"
" max_callocs: %12zu max_hallocs: %12zu\n",
mcore->coresize, mcore->nmops, mcore->cmop,
mcore->num_callocs, mcore->num_hallocs,
mcore->size_callocs, mcore->size_hallocs,
mcore->cur_callocs, mcore->cur_hallocs,
mcore->max_callocs, mcore->max_hallocs);
if (mcore->cur_callocs != 0 || mcore->cur_hallocs != 0 || mcore->cmop != 0) {
printf("***Warning: mcore memory was not fully freed when destroyed.\n"
" cur_callocs: %6zu cur_hallocs: %6zu cmop: %6zu\n",
mcore->cur_callocs, mcore->cur_hallocs, mcore->cmop);
}
gk_free((void **)&mcore->core, &mcore->mops, &mcore, LTERM);
*r_mcore = NULL;
}
/*************************************************************************/
/*! This function destroys an mcore. This version is for gkmcore.
*/
/*************************************************************************/
void gk_gkmcoreDestroy(gk_mcore_t **r_mcore, int showstats)
{
gk_mcore_t *mcore = *r_mcore;
if (mcore == NULL)
return;
if (showstats)
printf("\n gk_mcore statistics\n"
" nmops: %12zu cmop: %6zu\n"
" num_hallocs: %12zu\n"
" size_hallocs: %12zu\n"
" cur_hallocs: %12zu\n"
" max_hallocs: %12zu\n",
mcore->nmops, mcore->cmop,
mcore->num_hallocs,
mcore->size_hallocs,
mcore->cur_hallocs,
mcore->max_hallocs);
if (mcore->cur_hallocs != 0 || mcore->cmop != 0) {
printf("***Warning: mcore memory was not fully freed when destroyed.\n"
" cur_hallocs: %6zu cmop: %6zu\n",
mcore->cur_hallocs, mcore->cmop);
}
free(mcore->mops);
free(mcore);
*r_mcore = NULL;
}
/*************************************************************************/
/*! This function allocate space from the core/heap
*/
/*************************************************************************/
void *gk_mcoreMalloc(gk_mcore_t *mcore, size_t nbytes)
{
void *ptr;
/* pad to make pointers 8-byte aligned */
nbytes += (nbytes%8 == 0 ? 0 : 8 - nbytes%8);
if (mcore->corecpos + nbytes < mcore->coresize) {
/* service this request from the core */
ptr = ((char *)mcore->core)+mcore->corecpos;
mcore->corecpos += nbytes;
gk_mcoreAdd(mcore, GK_MOPT_CORE, nbytes, ptr);
}
else {
/* service this request from the heap */
ptr = gk_malloc(nbytes, "gk_mcoremalloc: ptr");
gk_mcoreAdd(mcore, GK_MOPT_HEAP, nbytes, ptr);
}
/*
printf("MCMALLOC: %zu %d %8zu\n", mcore->cmop-1,
mcore->mops[mcore->cmop-1].type, mcore->mops[mcore->cmop-1].nbytes);
*/
return ptr;
}
/*************************************************************************/
/*! This function sets a marker in the stack of malloc ops to be used
subsequently for freeing purposes
*/
/*************************************************************************/
void gk_mcorePush(gk_mcore_t *mcore)
{
gk_mcoreAdd(mcore, GK_MOPT_MARK, 0, NULL);
/* printf("MCPPUSH: %zu\n", mcore->cmop-1); */
}
/*************************************************************************/
/*! This function sets a marker in the stack of malloc ops to be used
subsequently for freeing purposes. This is the gkmcore version.
*/
/*************************************************************************/
void gk_gkmcorePush(gk_mcore_t *mcore)
{
gk_gkmcoreAdd(mcore, GK_MOPT_MARK, 0, NULL);
/* printf("MCPPUSH: %zu\n", mcore->cmop-1); */
}
/*************************************************************************/
/*! This function frees all mops since the last push
*/
/*************************************************************************/
void gk_mcorePop(gk_mcore_t *mcore)
{
while (mcore->cmop > 0) {
mcore->cmop--;
switch (mcore->mops[mcore->cmop].type) {
case GK_MOPT_MARK: /* push marker */
goto DONE;
break;
case GK_MOPT_CORE: /* core free */
if (mcore->corecpos < mcore->mops[mcore->cmop].nbytes)
errexit("Internal Error: wspace's core is about to be over-freed [%zu, %zu, %zd]\n",
mcore->coresize, mcore->corecpos, mcore->mops[mcore->cmop].nbytes);
mcore->corecpos -= mcore->mops[mcore->cmop].nbytes;
mcore->cur_callocs -= mcore->mops[mcore->cmop].nbytes;
break;
case GK_MOPT_HEAP: /* heap free */
gk_free((void **)&mcore->mops[mcore->cmop].ptr, LTERM);
mcore->cur_hallocs -= mcore->mops[mcore->cmop].nbytes;
break;
default:
gk_errexit(SIGMEM, "Unknown mop type of %d\n", mcore->mops[mcore->cmop].type);
}
}
DONE:
;
/*printf("MCPPOP: %zu\n", mcore->cmop); */
}
/*************************************************************************/
/*! This function frees all mops since the last push. This version is
for poping the gkmcore and it uses free instead of gk_free.
*/
/*************************************************************************/
void gk_gkmcorePop(gk_mcore_t *mcore)
{
while (mcore->cmop > 0) {
mcore->cmop--;
switch (mcore->mops[mcore->cmop].type) {
case GK_MOPT_MARK: /* push marker */
goto DONE;
break;
case GK_MOPT_HEAP: /* heap free */
free(mcore->mops[mcore->cmop].ptr);
mcore->cur_hallocs -= mcore->mops[mcore->cmop].nbytes;
break;
default:
gk_errexit(SIGMEM, "Unknown mop type of %d\n", mcore->mops[mcore->cmop].type);
}
}
DONE:
;
}
/*************************************************************************/
/*! Adds a memory allocation at the end of the list.
*/
/*************************************************************************/
void gk_mcoreAdd(gk_mcore_t *mcore, int type, size_t nbytes, void *ptr)
{
if (mcore->cmop == mcore->nmops) {
mcore->nmops *= 2;
mcore->mops = realloc(mcore->mops, mcore->nmops*sizeof(gk_mop_t));
if (mcore->mops == NULL)
gk_errexit(SIGMEM, "***Memory allocation for gkmcore failed.\n");
}
mcore->mops[mcore->cmop].type = type;
mcore->mops[mcore->cmop].nbytes = nbytes;
mcore->mops[mcore->cmop].ptr = ptr;
mcore->cmop++;
switch (type) {
case GK_MOPT_MARK:
break;
case GK_MOPT_CORE:
mcore->num_callocs++;
mcore->size_callocs += nbytes;
mcore->cur_callocs += nbytes;
if (mcore->max_callocs < mcore->cur_callocs)
mcore->max_callocs = mcore->cur_callocs;
break;
case GK_MOPT_HEAP:
mcore->num_hallocs++;
mcore->size_hallocs += nbytes;
mcore->cur_hallocs += nbytes;
if (mcore->max_hallocs < mcore->cur_hallocs)
mcore->max_hallocs = mcore->cur_hallocs;
break;
default:
gk_errexit(SIGMEM, "Incorrect mcore type operation.\n");
}
}
/*************************************************************************/
/*! Adds a memory allocation at the end of the list. This is the gkmcore
version.
*/
/*************************************************************************/
void gk_gkmcoreAdd(gk_mcore_t *mcore, int type, size_t nbytes, void *ptr)
{
if (mcore->cmop == mcore->nmops) {
mcore->nmops *= 2;
mcore->mops = realloc(mcore->mops, mcore->nmops*sizeof(gk_mop_t));
if (mcore->mops == NULL)
gk_errexit(SIGMEM, "***Memory allocation for gkmcore failed.\n");
}
mcore->mops[mcore->cmop].type = type;
mcore->mops[mcore->cmop].nbytes = nbytes;
mcore->mops[mcore->cmop].ptr = ptr;
mcore->cmop++;
switch (type) {
case GK_MOPT_MARK:
break;
case GK_MOPT_HEAP:
mcore->num_hallocs++;
mcore->size_hallocs += nbytes;
mcore->cur_hallocs += nbytes;
if (mcore->max_hallocs < mcore->cur_hallocs)
mcore->max_hallocs = mcore->cur_hallocs;
break;
default:
gk_errexit(SIGMEM, "Incorrect mcore type operation.\n");
}
}
/*************************************************************************/
/*! This function deletes the mop associated with the supplied pointer.
The mop has to be a heap allocation, otherwise it fails violently.
*/
/*************************************************************************/
void gk_mcoreDel(gk_mcore_t *mcore, void *ptr)
{
int i;
for (i=mcore->cmop-1; i>=0; i--) {
if (mcore->mops[i].type == GK_MOPT_MARK)
gk_errexit(SIGMEM, "Could not find pointer %p in mcore\n", ptr);
if (mcore->mops[i].ptr == ptr) {
if (mcore->mops[i].type != GK_MOPT_HEAP)
gk_errexit(SIGMEM, "Trying to delete a non-HEAP mop.\n");
mcore->cur_hallocs -= mcore->mops[i].nbytes;
mcore->mops[i] = mcore->mops[--mcore->cmop];
return;
}
}
gk_errexit(SIGMEM, "mcoreDel should never have been here!\n");
}
/*************************************************************************/
/*! This function deletes the mop associated with the supplied pointer.
The mop has to be a heap allocation, otherwise it fails violently.
This is the gkmcore version.
*/
/*************************************************************************/
void gk_gkmcoreDel(gk_mcore_t *mcore, void *ptr)
{
int i;
for (i=mcore->cmop-1; i>=0; i--) {
if (mcore->mops[i].type == GK_MOPT_MARK)
gk_errexit(SIGMEM, "Could not find pointer %p in mcore\n", ptr);
if (mcore->mops[i].ptr == ptr) {
if (mcore->mops[i].type != GK_MOPT_HEAP)
gk_errexit(SIGMEM, "Trying to delete a non-HEAP mop.\n");
mcore->cur_hallocs -= mcore->mops[i].nbytes;
mcore->mops[i] = mcore->mops[--mcore->cmop];
return;
}
}
gk_errexit(SIGMEM, "gkmcoreDel should never have been here!\n");
}