File: MemoryTrack.cpp

package info (click to toggle)
between 6%2Bdfsg1-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, jessie, jessie-kfreebsd, stretch
  • size: 3,532 kB
  • sloc: cpp: 28,110; php: 718; ansic: 638; objc: 245; sh: 236; makefile: 99; perl: 67
file content (277 lines) | stat: -rw-r--r-- 7,301 bytes parent folder | download | duplicates (30)
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
/*
 * Modification History
 *
 * 2002-October-17 Jason Rohrer
 * Created.
 *
 * 2002-October-18  Jason Rohrer
 * Changed to use custom list instead of SimpleVector because SimpleVector
 * uses debugMemory.
 * Added static initialization counting class.
 * Changed to use struct and malloc for AllocationList to avoid
 * circular new and delete calls.
 *
 * 2002-October-19  Jason Rohrer
 * Fixed a bug in adding to the alloc list.
 * Improved printing behavior.
 * Added support for clearing memory on allocation and deallocation.
 * Fixed to ignore deallocation of our own static lock.
 * Fixed bug in allocation count.
 * Added message for NULL pointer deallocation.
 * Put locks in place around print statements, which are not atomic on win32.
 * Changed to use hex notation when printing pointers.
 *
 * 2002-October-19  Jason Rohrer
 * Added ifdef for DEBUG_MEMORY.
 *
 * 2002-October-20  Jason Rohrer
 * Removed file and line arguments from deallocation calls.
 */



#ifdef DEBUG_MEMORY


#include "minorGems/util/development/memory/MemoryTrack.h"

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>



int MemoryTrackStaticInitCounter::mCount = 0;

MutexLock *MemoryTrack::mLock;

AllocationList *MemoryTrack::mListHead;

char MemoryTrack::mTracking = false;

int MemoryTrack::mTotalAllocationSize = 0;
int MemoryTrack::mTotalDeallocationSize = 0;
int MemoryTrack::mNumberOfAllocations = 0;



void MemoryTrack::addAllocation( void *inPointer,
                                 unsigned int inAllocationSize,
                                 int inAllocationType,
                                 const char *inFileName,
                                 int inLineNumber ) {

    mLock->lock();

    if( !mTracking ) {
        printf( "Tracking off on allocation (0x%x) [%d bytes] %s:%d.\n",
                (unsigned int)inPointer,
                inAllocationSize,
                inFileName, inLineNumber );

        mLock->unlock();
        return;
        }
    

    // insert after head of list
    AllocationList *element =
        (AllocationList *)malloc( sizeof( AllocationList ) );
    element->mPrevious = (void *)mListHead;
    element->mNext = mListHead->mNext;

    mListHead->mNext = (void *)element;


    AllocationList *nextElement = (AllocationList *)( element->mNext );
    if( nextElement != NULL ) {
        nextElement->mPrevious = (void *)element;
        }
    
    element->mPointer = inPointer;
    element->mAllocationSize = inAllocationSize;
    element->mAllocationType = inAllocationType;
    element->mFileName = inFileName;
    element->mLineNumber = inLineNumber;


    mTotalAllocationSize += inAllocationSize;

    mNumberOfAllocations ++;
    
    // wipe this block of memory
    clearMemory( inPointer, inAllocationSize );
    
    mLock->unlock();
    }



int MemoryTrack::addDeallocation( void *inPointer,
                                  int inDeallocationType ) {

    mLock->lock();

    if( inPointer == NULL ) {
        printf( "NULL pointer (0x%x) deallocated\n",
                (unsigned int)inPointer );
        }

    
    if( inPointer == (void *)mLock ) {
        // we're seeing the deallocation of our own static lock as
        // the system exits
        // ignore it
        mLock->unlock();
        return 0;
        }
    
    if( !mTracking ) {
        printf( "Tracking off on deallocation (0x%x)\n",
                (unsigned int)inPointer );
        mLock->unlock();
        return 0;
        }

    

    AllocationList *element = (AllocationList *)( mListHead->mNext );

    while( element != NULL ) {
        
        void *pointer = element->mPointer;
        
        if( pointer == inPointer ) {

            unsigned int allocationSize = element->mAllocationSize;
            int allocationType = element->mAllocationType;
            const char *allocFileName = element->mFileName;            
            int allocLineNumber = element->mLineNumber;
            
            // remove from list, whether or not types match
            AllocationList *previousElement =
                (AllocationList *)( element->mPrevious );
            AllocationList *nextElement =
                (AllocationList *)( element->mNext );
            
            // patch list
            previousElement->mNext = (void *)( nextElement );

            if( nextElement != NULL ) {
                nextElement->mPrevious = (void *)( previousElement );
                }
            
            free( element );
            
            mTotalDeallocationSize += allocationSize;
            
            
            if( allocationType == inDeallocationType ) {
                // found and types match
                mLock->unlock();

                // wipe this block of memory
                clearMemory( inPointer, allocationSize );
                return 0;
                }
            else {
                // allocation types don't match
                
                printf( "Attempt to deallocate (0x%x) [%d bytes] with wrong"
                        " delete form\n"
                        "    %s:%d (location of original allocation)\n",
                        (unsigned int)inPointer,
                        allocationSize,
                        allocFileName, allocLineNumber );

                mLock->unlock();
                
                return 2;
                }
            
            }

        element = (AllocationList *)( element->mNext );
        }

    // not found (delete of unallocated memory)
    printf( "Attempt to deallocate (0x%x) unallocated memory\n",
            (unsigned int)inPointer );

    mLock->unlock();
    
    return 1;
    }



void MemoryTrack::printLeaks() {
    mLock->lock();

    printf( "\n\n---- debugMemory report ----\n" );
    
    printf( "Number of Allocations:  %d\n", mNumberOfAllocations );
    printf( "Total allocations:      %d bytes\n", mTotalAllocationSize );
    printf( "Total deallocations:    %d bytes\n", mTotalDeallocationSize );
    
    int leakEstimate = mTotalAllocationSize - mTotalDeallocationSize;


    
    AllocationList *element = (AllocationList *)( mListHead->mNext );

    if( element == NULL ) {
        printf( "No leaks detected.\n" );
        }
    else {
        printf( "Leaks detected:\n" );
        }

    int leakSum = 0;
    while( element != NULL ) {
    
        printf( "Not deallocated (0x%x) [%d bytes]\n"
                "    %s:%d (location of original allocation)\n",
                (unsigned int)( element->mPointer ),
                element->mAllocationSize,
                element->mFileName,
                element->mLineNumber );

        leakSum += element->mAllocationSize;
        
        element = (AllocationList *)( element->mNext );
        }

    
    if( leakSum != leakEstimate ) {
        printf( "Warning:  Leak sum does not equal leak estimate.\n" );
        }

    
    printf( "Leaked memory:          %d bytes\n", leakSum );
    
    printf( "---- END debugMemory report ----\n\n" );

    
    mLock->unlock();
    }



void MemoryTrack::clearMemory( void *inPointer, unsigned int inSize ) {

    unsigned char *charArray = (unsigned char *)inPointer;

    for( unsigned int i=0; i<inSize; i++ ) {
        charArray[i] = (unsigned char)0xAA;
        }

    }



#endif