This repository has been archived by the owner on Jul 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemleaks.c
188 lines (154 loc) · 6.47 KB
/
memleaks.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
/*
* Memory Leaks Finder
*
* Copyright (C) 2011, Michał Dobaczewski / mdobak@(Google mail)
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
typedef struct {
void* memory;
void* original;
char* allocInFile;
int allocInLine;
double allocOnTime;
char** reallocsInFile;
int* reallocsInLine;
size_t* reallocsSize;
double* reallocsOnTime;
void** realloc;
int numOfReallocs;
int memorySize;
int released;
} _allocations;
_allocations* _memoryAllocs;
int _numOfMemAllocs;
size_t _totalAalloc = 0;
size_t _maxAalloc = 0;
size_t _totalUunreleased = 0;
size_t _totalReleased = 0;
void MEMORY_STATUS()
{
FILE* out = fopen("memory.txt", "w");
if (out == NULL)
out = stdout;
fprintf(out, "-------------------------------------------------\n");
fprintf(out, "Total allocated memory: %i bytes\n", _totalAalloc);
fprintf(out, "Total released memory: %i bytes\n", _totalReleased);
fprintf(out, "Unreleased memory: %i bytes\n", _totalAalloc - _totalReleased);
fprintf(out, "Maximum allocated memory: %i bytes\n", _maxAalloc);
fprintf(out, "-------------------------------------------------\n");
fprintf(out, "Unreleased memory\n");
fprintf(out, "-------------------------------------------------\n");
for (int i = 0; i < _numOfMemAllocs; ++i) {
if (_memoryAllocs[i].released == 0) {
fprintf(out, " * %s:%i (allocated %i bytes, run time: %.2f)\n",
_memoryAllocs[i].allocInFile,
_memoryAllocs[i].allocInLine,
_memoryAllocs[i].memorySize,
_memoryAllocs[i].allocOnTime);
char* READ_TEST = _memoryAllocs[i].memory;
READ_TEST += 1;
if (_memoryAllocs[i].numOfReallocs > 0)
fprintf(out, " * Reallocations\n");
for (int j = 0; j < _memoryAllocs[i].numOfReallocs; ++j)
fprintf(out, " %s:%i (resize to %u bytes, run time: %.2f)\n",
_memoryAllocs[i].reallocsInFile[j],
_memoryAllocs[i].reallocsInLine[j],
_memoryAllocs[i].reallocsSize[j],
_memoryAllocs[i].reallocsOnTime[j]);
}
}
fclose(out);
}
void* __wrapper_malloc(size_t size, int line, char* file)
{
_memoryAllocs = (_allocations*)realloc(_memoryAllocs, sizeof(_allocations) * (_numOfMemAllocs + 1));
_memoryAllocs[_numOfMemAllocs].memory = malloc(size);
_memoryAllocs[_numOfMemAllocs].original = _memoryAllocs[_numOfMemAllocs].memory;
_memoryAllocs[_numOfMemAllocs].allocInFile = file;
_memoryAllocs[_numOfMemAllocs].allocInLine = line;
_memoryAllocs[_numOfMemAllocs].allocOnTime = (double)clock() / CLOCKS_PER_SEC;
_memoryAllocs[_numOfMemAllocs].reallocsInFile = NULL;
_memoryAllocs[_numOfMemAllocs].reallocsInLine = NULL;
_memoryAllocs[_numOfMemAllocs].reallocsSize = NULL;
_memoryAllocs[_numOfMemAllocs].reallocsOnTime = NULL;
_memoryAllocs[_numOfMemAllocs].realloc = NULL;
_memoryAllocs[_numOfMemAllocs].numOfReallocs = 0;
_memoryAllocs[_numOfMemAllocs].memorySize = size;
_memoryAllocs[_numOfMemAllocs].released = 0;
_numOfMemAllocs += 1;
_totalAalloc += size;
return _memoryAllocs[_numOfMemAllocs - 1].memory;
}
void __wrapper_free(void *ptr, int line, char* file)
{
if (ptr == NULL)
return;
for (int i = 0; i < _numOfMemAllocs; ++i) {
if (_memoryAllocs[i].memory == ptr && !_memoryAllocs[i].released) {
_memoryAllocs[i].released = 1;
_totalReleased += _memoryAllocs[i].memorySize;
size_t totalUunreleased = 0;
for (int i = 0; i < _numOfMemAllocs; ++i)
if (_memoryAllocs[i].released == 0)
totalUunreleased += _memoryAllocs[i].memorySize;
if (totalUunreleased > _maxAalloc)
_maxAalloc = totalUunreleased;
free(ptr);
return;
}
}
printf("\nWARNING! FREE USING UNDEF PTR\n%s:%i\n", file, line);
free(ptr);
}
void* __wrapper_realloc(void *ptr, size_t size, int line, char* file)
{
if (ptr == NULL)
return __wrapper_malloc(size, line, file);
for (int i = 0; i < _numOfMemAllocs; ++i) {
if (_memoryAllocs[i].memory == ptr && !_memoryAllocs[i].released) {
_memoryAllocs[i].memory = realloc(ptr, size);
_memoryAllocs[i].reallocsInFile =
(char**)realloc(_memoryAllocs[i].reallocsInFile,
(_memoryAllocs[i].numOfReallocs + 1) * sizeof(char*));
_memoryAllocs[i].reallocsInLine =
(int*)realloc(_memoryAllocs[i].reallocsInLine,
(_memoryAllocs[i].numOfReallocs + 1) * sizeof(int));
_memoryAllocs[i].reallocsSize =
(size_t*)realloc(_memoryAllocs[i].reallocsSize,
(_memoryAllocs[i].numOfReallocs + 1) * sizeof(size_t));
_memoryAllocs[i].reallocsOnTime =
(double*)realloc(_memoryAllocs[i].reallocsOnTime,
(_memoryAllocs[i].numOfReallocs + 1) * sizeof(double));
_memoryAllocs[i].realloc =
(void**)realloc(_memoryAllocs[i].realloc,
(_memoryAllocs[i].numOfReallocs + 1) * sizeof(void**));
_memoryAllocs[i].reallocsInFile[_memoryAllocs[i].numOfReallocs] = file;
_memoryAllocs[i].reallocsInLine[_memoryAllocs[i].numOfReallocs] = line;
_memoryAllocs[i].reallocsSize[_memoryAllocs[i].numOfReallocs] = size;
_memoryAllocs[i].reallocsOnTime[_memoryAllocs[i].numOfReallocs] = (double)clock() / CLOCKS_PER_SEC;
_memoryAllocs[i].realloc[_memoryAllocs[i].numOfReallocs] = _memoryAllocs[i].memory;
_memoryAllocs[i].numOfReallocs += 1;
_totalAalloc += size - _memoryAllocs[i].memorySize;
_memoryAllocs[i].memorySize = size;
return _memoryAllocs[i].memory;
}
}
printf("\nWARNING! REALLOC USING UNDEF PTR\n%s:%i\n", file, line);
return NULL;
}