-
Notifications
You must be signed in to change notification settings - Fork 131
Expand file tree
/
Copy pathsc_memmgr.cc
More file actions
405 lines (339 loc) · 11.7 KB
/
sc_memmgr.cc
File metadata and controls
405 lines (339 loc) · 11.7 KB
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
393
394
395
396
397
398
399
400
401
402
403
404
405
#define SC_MEMMGR_CC
#include <sc_cf.h>
#include "sc_memmgr.h"
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <set>
#ifdef SC_MEMMGR_ENABLE_CHECKS
/**
sc_memmgr_error definition
*/
class sc_memmgr_error {
private:
std::string _srcfile;
unsigned int _srcline;
unsigned int _occurences;
public:
sc_memmgr_error( const std::string & file, const unsigned int line );
sc_memmgr_error( const sc_memmgr_error & rhs );
~sc_memmgr_error( void );
bool operator<( const sc_memmgr_error & rhs ) const;
std::string getsrcfile( void ) const;
unsigned int getsrcline( void ) const;
unsigned int getoccurences( void ) const;
};
typedef std::set<sc_memmgr_error> sc_memmgr_errors;
typedef std::set<sc_memmgr_error>::iterator sc_memmgr_error_iterator;
/**
sc_memmgr_record definition
*/
class sc_memmgr_record {
private:
void * _addr;
size_t _size;
std::string _srcfile;
unsigned int _srcline;
public:
sc_memmgr_record( void * addr, size_t size, const char * file, const unsigned int line );
sc_memmgr_record( void * addr );
sc_memmgr_record( const sc_memmgr_record & rhs );
~sc_memmgr_record( void );
bool operator<( const sc_memmgr_record & rhs ) const;
void * getaddr( void ) const;
size_t getsize( void ) const;
std::string getsrcfile( void ) const;
unsigned int getsrcline( void ) const;
};
typedef std::set<sc_memmgr_record> sc_memmgr_records;
typedef std::set<sc_memmgr_record>::iterator sc_memmgr_record_iterator;
#endif /* SC_MEMMGR_ENABLE_CHECKS */
/**
sc_memmgr definition
*/
class sc_memmgr {
private:
#ifdef SC_MEMMGR_ENABLE_CHECKS
bool _record_insert_busy, _record_erase_busy;
// memory allocation/reallocation records, inserted at allocation, erased at deallocation.
sc_memmgr_records _records;
// memory stats
unsigned int _allocated; // amount of memory allocated simultaniously
unsigned int _maximum_allocated; // maximum amount of memory allocated simultaniously
unsigned int _allocated_total; // total amount of memory allocated in bytes
unsigned int _reallocated_total; // total amount of memory reallocated in bytes
unsigned int _deallocated_total; // total amount of memory deallocated in bytes
#endif /* SC_MEMMGR_ENABLE_CHECKS */
protected:
public:
sc_memmgr( void );
~sc_memmgr( void );
void * allocate( size_t size, const char * file, const int line );
void * reallocate( void * addr, size_t size, const char * file, const int line );
void deallocate( void * addr, const char * file, const int line );
};
/**
sc_memmgr instance.
There should be one static instance of memmgr.
This instance is automatically destroyed when application exits, so this allows us to add
memory leak detection in it's destructor.
*/
sc_memmgr memmgr;
/**
c memory functions implementation
*/
extern "C" {
void * sc_malloc_fn( unsigned int size, const char * file, const int line ) {
return memmgr.allocate( size, file, line );
}
void * sc_calloc_fn( unsigned int count, unsigned int size, const char * file, const int line ) {
return memmgr.allocate( count * size, file, line );
}
void * sc_realloc_fn( void * addr, unsigned int size, const char * file, const int line ) {
return memmgr.reallocate( addr, size, file, line );
}
void sc_free_fn( void * addr ) {
memmgr.deallocate( addr, "", 0 );
}
}
/**
c++ memory operators implementation
*/
void * sc_operator_new( size_t size, const char * file, const int line ) {
return memmgr.allocate( size, file, line );
}
void sc_operator_delete( void * addr, const char * file, const int line ) {
memmgr.deallocate( addr, file, line );
}
void sc_operator_delete( void * addr ) {
memmgr.deallocate( addr, "", 0 );
}
/**
sc_memmgr implementation
*/
sc_memmgr::sc_memmgr( void ) {
#ifdef SC_MEMMGR_ENABLE_CHECKS
_record_insert_busy = false;
_record_erase_busy = false;
_allocated = 0;
_maximum_allocated = 0;
_allocated_total = 0;
_reallocated_total = 0;
_deallocated_total = 0;
#endif /* SC_MEMMGR_ENABLE_CHECKS */
}
/**
Destructor:
sc_memmgr::~sc_memmgr(void)
Description:
The sc_memmgr destructor is used to check for memory leaks when enabled.
All records still present when sc_memmgr instance is destroyed can be considered as
memory leaks.
*/
sc_memmgr::~sc_memmgr( void ) {
#ifdef SC_MEMMGR_ENABLE_CHECKS
sc_memmgr_record_iterator irecord;
sc_memmgr_errors errors;
sc_memmgr_error_iterator ierror;
// Check if total allocated equals total deallocated
if( _allocated_total != _deallocated_total ) {
// todo: generate warning for possible memory leaks, enable full memory leak checking
fprintf( stderr, "sc_memmgr warning: Possible memory leaks detected (%d of %d bytes)\n", _allocated_total - _deallocated_total, _allocated_total );
}
// Compact leaks into an error list to prevent same leak being reported multiple times.
_record_insert_busy = true;
_record_erase_busy = true;
for( irecord = _records.begin();
irecord != _records.end();
irecord ++ ) {
sc_memmgr_error error( irecord->getsrcfile(), irecord->getsrcline() );
ierror = errors.find( error );
if( ierror == errors.end() ) {
errors.insert( error );
}
//else
// ierror->occurences ++;
}
_record_insert_busy = false;
_record_erase_busy = false;
// Loop through memory leaks to generate/buffer errors
for( ierror = errors.begin();
ierror != errors.end();
ierror ++ ) {
// todo: generate error for memory leak
fprintf( stderr, "sc_memmgr warning: Possible memory leak in %s line %d\n", ierror->getsrcfile().c_str(), ierror->getsrcline() );
}
// Clear remaining records
_record_erase_busy = true;
_records.clear();
errors.clear();
_record_erase_busy = false;
#endif /* SC_MEMMGR_ENABLE_CHECKS */
}
void * sc_memmgr::allocate( size_t size, const char * file, const int line ) {
void * addr;
// Allocate
addr = malloc( size );
if( addr == NULL ) {
// todo: error allocation failed
fprintf( stderr, "sc_memmgr error: Memory allocation failed in %s line %d\n", file, line );
}
// Some stl implementations (for example debian gcc) use the new operator to construct
// new elements when inserting sc_memmgr_record. When this our new operator gets used
// for this operation, this would result in an infinite loop. This is fixed by the
// _record_insert_busy flag.
#ifdef SC_MEMMGR_ENABLE_CHECKS
if( !_record_insert_busy ) {
// Store record for this allocation
_record_insert_busy = true;
_records.insert( sc_memmgr_record( addr, size, file, line ) );
_record_insert_busy = false;
// Update stats
_allocated += size;
if( _allocated > _maximum_allocated ) {
_maximum_allocated = _allocated;
}
_allocated_total += size;
}
#endif /* SC_MEMMGR_ENABLE_CHECKS */
return addr;
}
void * sc_memmgr::reallocate( void * addr, size_t size, const char * file, const int line ) {
#ifdef SC_MEMMGR_ENABLE_CHECKS
sc_memmgr_record_iterator record;
if( !_record_insert_busy ) {
// Find record of previous allocation/reallocation
record = _records.find( sc_memmgr_record( addr ) );
if( record == _records.end() ) {
// todo: error reallocating memory not allocated?
fprintf( stderr, "sc_memmgr warning: Reallocation of not allocated memory at %s line %d\n", file, line );
} else {
// Update stats
_allocated -= record->getsize();
_deallocated_total += record->getsize();
// Erase previous allocation/reallocation
_record_erase_busy = true;
_records.erase( record );
_record_erase_busy = false;
}
}
#endif /* SC_MEMMGR_ENABLE_CHECKS */
// Reallocate
addr = realloc( addr, size );
if( addr == NULL ) {
// todo: error reallocation failed
fprintf( stderr, "sc_memmgr error: Reallocation failed at %s line %d\n", file, line );
}
#ifdef SC_MEMMGR_ENABLE_CHECKS
if( !_record_insert_busy ) {
// Store record for this reallocation
_record_insert_busy = true;
_records.insert( sc_memmgr_record( addr, size, file, line ) );
_record_insert_busy = false;
// Update stats
_allocated += size;
if( _allocated > _maximum_allocated ) {
_maximum_allocated = _allocated;
}
_allocated_total += size;
_reallocated_total += size;
}
#endif /* SC_MEMMGR_ENABLE_CHECKS */
return addr;
}
void sc_memmgr::deallocate( void * addr, const char * file, const int line ) {
#ifdef SC_MEMMGR_ENABLE_CHECKS
sc_memmgr_record_iterator record;
if( !_record_erase_busy ) {
// Find record of previous allocation/reallocation
record = _records.find( sc_memmgr_record( addr ) );
if( record == _records.end() ) {
// todo: error free called for not allocated memory?
fprintf( stderr, "sc_memmgr warning: Deallocate of not allocated memory at %s line %d\n", file, line );
} else {
// Update stats
_allocated -= record->getsize();
_deallocated_total += record->getsize();
// Erase record
_record_erase_busy = true;
_records.erase( record );
_record_erase_busy = false;
}
}
#else
(void) file; // quell unused param warnings
(void) line;
#endif /* SC_MEMMGR_ENABLE_CHECKS */
// Deallocate
free( addr );
}
#ifdef SC_MEMMGR_ENABLE_CHECKS
/**
sc_memmgr_error implementation
*/
sc_memmgr_error::sc_memmgr_error( const std::string & file, const unsigned int line ) {
_srcfile = file;
_srcline = line;
_occurences = 1;
}
sc_memmgr_error::sc_memmgr_error( const sc_memmgr_error & rhs ) {
_srcfile = rhs._srcfile;
_srcline = rhs._srcline;
_occurences = rhs._occurences;
}
sc_memmgr_error::~sc_memmgr_error( void ) {
}
bool sc_memmgr_error::operator<( const sc_memmgr_error & rhs ) const {
if( _srcfile == rhs._srcfile ) {
return _srcline < rhs._srcline;
}
return _srcfile < rhs._srcfile;
}
std::string sc_memmgr_error::getsrcfile( void ) const {
return _srcfile;
}
unsigned int sc_memmgr_error::getsrcline( void ) const {
return _srcline;
}
unsigned int sc_memmgr_error::getoccurences( void ) const {
return _occurences;
}
/**
sc_memmgr_record implementation
*/
sc_memmgr_record::sc_memmgr_record( void * addr, size_t size, const char * file, const unsigned int line ) {
_addr = addr;
_size = size;
_srcfile = file;
_srcline = line;
}
sc_memmgr_record::sc_memmgr_record( void * addr ) {
_addr = addr;
_size = 0;
_srcfile = "";
_srcline = -1;
}
sc_memmgr_record::sc_memmgr_record( const sc_memmgr_record & rhs ) {
_addr = rhs._addr;
_size = rhs._size;
_srcfile = rhs._srcfile;
_srcline = rhs._srcline;
}
sc_memmgr_record::~sc_memmgr_record( void ) {
}
bool sc_memmgr_record::operator<( const sc_memmgr_record & rhs ) const {
return _addr < rhs._addr;
}
void * sc_memmgr_record::getaddr( void ) const {
return _addr;
}
size_t sc_memmgr_record::getsize( void ) const {
return _size;
}
std::string sc_memmgr_record::getsrcfile( void ) const {
return _srcfile;
}
unsigned int sc_memmgr_record::getsrcline( void ) const {
return _srcline;
}
#endif /* SC_MEMMGR_ENABLE_CHECKS */