-
Notifications
You must be signed in to change notification settings - Fork 64
/
memory.c
307 lines (242 loc) · 8.37 KB
/
memory.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
/*!
\file memory.c
\brief This file contains various allocation routines
The allocation routines included are for 1D and 2D arrays of the
most datatypes that GKlib support. Many of these routines are
defined with the help of the macros in gk_memory.h. These macros
can be used to define other memory allocation routines.
\date Started 4/3/2007
\author George
\version\verbatim $Id: memory.c 21050 2017-05-25 03:53:58Z karypis $ \endverbatim
*/
#include <GKlib.h>
/* This is for the global mcore that tracks all heap allocations */
static __thread gk_mcore_t *gkmcore = NULL;
/*************************************************************************/
/*! Define the set of memory allocation routines for each data type */
/**************************************************************************/
GK_MKALLOC(gk_c, char)
GK_MKALLOC(gk_i, int)
GK_MKALLOC(gk_i8, int8_t)
GK_MKALLOC(gk_i16, int16_t)
GK_MKALLOC(gk_i32, int32_t)
GK_MKALLOC(gk_i64, int64_t)
GK_MKALLOC(gk_ui8, uint8_t)
GK_MKALLOC(gk_ui16, uint16_t)
GK_MKALLOC(gk_ui32, uint32_t)
GK_MKALLOC(gk_ui64, uint64_t)
GK_MKALLOC(gk_z, ssize_t)
GK_MKALLOC(gk_zu, size_t)
GK_MKALLOC(gk_f, float)
GK_MKALLOC(gk_d, double)
GK_MKALLOC(gk_idx, gk_idx_t)
GK_MKALLOC(gk_ckv, gk_ckv_t)
GK_MKALLOC(gk_ikv, gk_ikv_t)
GK_MKALLOC(gk_i8kv, gk_i8kv_t)
GK_MKALLOC(gk_i16kv, gk_i16kv_t)
GK_MKALLOC(gk_i32kv, gk_i32kv_t)
GK_MKALLOC(gk_i64kv, gk_i64kv_t)
GK_MKALLOC(gk_zkv, gk_zkv_t)
GK_MKALLOC(gk_zukv, gk_zukv_t)
GK_MKALLOC(gk_fkv, gk_fkv_t)
GK_MKALLOC(gk_dkv, gk_dkv_t)
GK_MKALLOC(gk_skv, gk_skv_t)
GK_MKALLOC(gk_idxkv, gk_idxkv_t)
/*************************************************************************/
/*! This function allocates a two-dimensional matrix.
*/
/*************************************************************************/
void gk_AllocMatrix(void ***r_matrix, size_t elmlen, size_t ndim1, size_t ndim2)
{
size_t i, j;
void **matrix;
*r_matrix = NULL;
if ((matrix = (void **)gk_malloc(ndim1*sizeof(void *), "gk_AllocMatrix: matrix")) == NULL)
return;
for (i=0; i<ndim1; i++) {
if ((matrix[i] = (void *)gk_malloc(ndim2*elmlen, "gk_AllocMatrix: matrix[i]")) == NULL) {
for (j=0; j<i; j++)
gk_free((void **)&matrix[j], LTERM);
return;
}
}
*r_matrix = matrix;
}
/*************************************************************************/
/*! This function frees a two-dimensional matrix.
*/
/*************************************************************************/
void gk_FreeMatrix(void ***r_matrix, size_t ndim1, size_t ndim2)
{
size_t i;
void **matrix;
if ((matrix = *r_matrix) == NULL)
return;
for (i=0; i<ndim1; i++)
gk_free((void **)&matrix[i], LTERM);
gk_free((void **)r_matrix, LTERM);
}
/*************************************************************************/
/*! This function initializes tracking of heap allocations.
*/
/*************************************************************************/
int gk_malloc_init()
{
if (gkmcore == NULL)
gkmcore = gk_gkmcoreCreate();
if (gkmcore == NULL)
return 0;
gk_gkmcorePush(gkmcore);
return 1;
}
/*************************************************************************/
/*! This function frees the memory that has been allocated since the
last call to gk_malloc_init().
*/
/*************************************************************************/
void gk_malloc_cleanup(int showstats)
{
if (gkmcore != NULL) {
gk_gkmcorePop(gkmcore);
if (gkmcore->cmop == 0) {
gk_gkmcoreDestroy(&gkmcore, showstats);
gkmcore = NULL;
}
}
}
/*************************************************************************/
/*! This function is my wrapper around malloc that provides the following
enhancements over malloc:
* It always allocates one byte of memory, even if 0 bytes are requested.
This is to ensure that checks of returned values do not lead to NULL
due to 0 bytes requested.
* It zeros-out the memory that is allocated. This is for a quick init
of the underlying datastructures.
*/
/**************************************************************************/
void *gk_malloc(size_t nbytes, char *msg)
{
void *ptr=NULL;
if (nbytes == 0)
nbytes++; /* Force mallocs to actually allocate some memory */
ptr = (void *)malloc(nbytes);
if (ptr == NULL) {
fprintf(stderr, " Current memory used: %10zu bytes\n", gk_GetCurMemoryUsed());
fprintf(stderr, " Maximum memory used: %10zu bytes\n", gk_GetMaxMemoryUsed());
gk_errexit(SIGMEM, "***Memory allocation failed for %s. Requested size: %zu bytes",
msg, nbytes);
return NULL;
}
/* add this memory allocation */
if (gkmcore != NULL) gk_gkmcoreAdd(gkmcore, GK_MOPT_HEAP, nbytes, ptr);
return ptr;
}
/*************************************************************************
* This function is my wrapper around realloc
**************************************************************************/
void *gk_realloc(void *oldptr, size_t nbytes, char *msg)
{
void *ptr=NULL;
if (nbytes == 0)
nbytes++; /* Force mallocs to actually allocate some memory */
/* remove this memory de-allocation */
if (gkmcore != NULL && oldptr != NULL) gk_gkmcoreDel(gkmcore, oldptr);
ptr = (void *)realloc(oldptr, nbytes);
if (ptr == NULL) {
fprintf(stderr, " Maximum memory used: %10zu bytes\n", gk_GetMaxMemoryUsed());
fprintf(stderr, " Current memory used: %10zu bytes\n", gk_GetCurMemoryUsed());
gk_errexit(SIGMEM, "***Memory realloc failed for %s. " "Requested size: %zu bytes",
msg, nbytes);
return NULL;
}
/* add this memory allocation */
if (gkmcore != NULL) gk_gkmcoreAdd(gkmcore, GK_MOPT_HEAP, nbytes, ptr);
return ptr;
}
/*************************************************************************
* This function is my wrapper around free, allows multiple pointers
**************************************************************************/
void gk_free(void **ptr1,...)
{
va_list plist;
void **ptr;
if (*ptr1 != NULL) {
free(*ptr1);
/* remove this memory de-allocation */
if (gkmcore != NULL)
gk_gkmcoreDel(gkmcore, *ptr1);
}
*ptr1 = NULL;
va_start(plist, ptr1);
while ((ptr = va_arg(plist, void **)) != LTERM) {
if (*ptr != NULL) {
free(*ptr);
/* remove this memory de-allocation */
if (gkmcore != NULL)
gk_gkmcoreDel(gkmcore, *ptr);
}
*ptr = NULL;
}
va_end(plist);
}
/*************************************************************************
* This function returns the current ammount of dynamically allocated
* memory that is used by the system
**************************************************************************/
size_t gk_GetCurMemoryUsed()
{
if (gkmcore == NULL)
return 0;
else
return gkmcore->cur_hallocs;
}
/*************************************************************************
* This function returns the maximum ammount of dynamically allocated
* memory that was used by the system
**************************************************************************/
size_t gk_GetMaxMemoryUsed()
{
if (gkmcore == NULL)
return 0;
else
return gkmcore->max_hallocs;
}
/*************************************************************************/
/*! This function returns the VmSize and VmRSS of the calling process. */
/*************************************************************************/
void gk_GetVMInfo(size_t *vmsize, size_t *vmrss)
{
FILE *fp;
char fname[1024];
sprintf(fname, "/proc/%d/statm", getpid());
fp = gk_fopen(fname, "r", "proc/pid/statm");
if (fscanf(fp, "%zu %zu", vmsize, vmrss) != 2)
errexit("Failed to read to values from %s\n", fname);
gk_fclose(fp);
/*
*vmsize *= sysconf(_SC_PAGESIZE);
*vmrss *= sysconf(_SC_PAGESIZE);
*/
return;
}
/*************************************************************************/
/*! This function returns the peak virtual memory of the calling process
by reading the VmPeak field in /proc/self/status . */
/*************************************************************************/
size_t gk_GetProcVmPeak()
{
FILE *fp;
char line[128];
size_t vmpeak=0;
if (gk_fexists("/proc/self/status")) {
fp = gk_fopen("/proc/self/status", "r", "proc/self/status");
while (fgets(line, 128, fp) != NULL) {
if (strncmp(line, "VmPeak:", 7) == 0) {
vmpeak = atoll(line+8)*1024;
break;
}
}
gk_fclose(fp);
}
return vmpeak;
}