summaryrefslogtreecommitdiffstats
path: root/apps/ktorrent/leaktrace.cpp
blob: 76439cecbfe2eab5e063c974d1fd5307313c0fe3 (plain)
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
#ifdef KT_LEAKTRACE


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <util/constants.h>

/*
 * prime number for the address lookup hash table.
 * if you have _really_ many memory allocations, use a
 * higher value, like 343051 for instance.
 */
#define SOME_PRIME 35323
#define ADDR_HASH(addr) ((unsigned long) addr % SOME_PRIME)

using namespace bt;

struct MemAlloc
{
	void* ptr;
	size_t size;
	void* alloc_addr; // addr of function which did the allocation
	MemAlloc* left;
	MemAlloc* right;
};

struct MemAllocTree
{
	Uint32 num_buckets;
	Uint32 count;
	Uint64 bytes;
	pthread_mutex_t mutex;
	MemAlloc* buckets[SOME_PRIME];
};

static MemAllocTree mtree = {0,0,0,PTHREAD_MUTEX_INITIALIZER};
static bool print_status_done = false;

static void InsertIntoTree(MemAlloc* cnode,MemAlloc* target)
{
	if (target->ptr < cnode->ptr)
	{
		if (!cnode->left)
			cnode->left = target;
		else
			InsertIntoTree(cnode->left,target);
	}
	else
	{
		if (!cnode->right)
			cnode->right = target;
		else
			InsertIntoTree(cnode->right,target);
	}
}

static void PrintTree(MemAlloc* ma)
{
	if (!ma)
		return;
	
	printf("\t0x%p 0x%p %i bytes\n",ma->alloc_addr,ma->ptr,ma->size);
	PrintTree(ma->left);
	PrintTree(ma->right);
}

static void PrintTreeToFile(FILE* fptr,MemAlloc* ma)
{
	if (!ma)
		return;
	
	fprintf(fptr, "L %10p   %9ld  # %p\n",ma->alloc_addr,ma->size,ma->ptr);
	PrintTreeToFile(fptr,ma->left);
	PrintTreeToFile(fptr,ma->right);
}

static void WriteLeakReport()
{
	FILE* report = fopen("leak.out","wt");
	if (!report)
	{
		printf("Cannot write report !!!!!!!!\n");
		return;
	}
		
	
	for (Uint32 b = 0;b < SOME_PRIME;b++)
		PrintTreeToFile(report,mtree.buckets[b]);
	fclose(report);
}

static void PrintStatus()
{
	if (mtree.count == 0)
	{
		printf("No memory leaks detected !!!\n");
	}
	else
	{
		printf("LeakTrace results : \n");
		for (Uint32 b = 0;b < SOME_PRIME;b++)
			PrintTree(mtree.buckets[b]);
		printf("====================\n");
		printf("Total : %i leaks, %li bytes leaked\n",mtree.count,mtree.bytes);
	}
	WriteLeakReport();
	print_status_done = true;
}



static void RegisterAlloc(void* ptr,Uint32 size)
{
	MemAlloc* ma = (MemAlloc*)malloc(sizeof(MemAlloc));
	ma->left = ma->right = 0;
	ma->size = size;
	ma->alloc_addr = __builtin_return_address(1);
	ma->ptr = ptr;
	if (mtree.num_buckets == 0)
	{
		// init buckets
		for (Uint32 b = 0;b < SOME_PRIME;b++)
			mtree.buckets[b] = 0;
		mtree.num_buckets = SOME_PRIME;
		atexit(PrintStatus);
	}
	
	// hash the address
	Uint32 b = ADDR_HASH(ptr);
	if (!mtree.buckets[b])
	{
		mtree.count++;
		mtree.bytes += size;
		mtree.buckets[b] = ma;
	}
	else
	{
		// walk the tree and insert it
		InsertIntoTree(mtree.buckets[b],ma);
		mtree.count++;
		mtree.bytes += size;
	}
}

static void DeregisterAlloc(void* ptr)
{
	if (print_status_done)
		printf("PrintStatus already happened !!!!!!!!!!\n");
	Uint32 b = ADDR_HASH(ptr);
	
	MemAlloc* p = mtree.buckets[b];
	MemAlloc* prev = 0;
	while (p && p->ptr != ptr)
	{
		prev = p;
		if (ptr < p->ptr)
			p = p->left;
		else if (ptr > p->ptr)
			p = p->right;
	}
	
	if (!p)
		return;
	
	if (!prev)
	{
		// it's the root
		mtree.count--;
		mtree.bytes -= p->size;
		free(p);
		mtree.buckets[b] = 0;
	}
	else
	{
		// first update some additional info
		mtree.count--;
		mtree.bytes -= p->size;
				
		if (!p->left && !p->right)
		{
			// no children so just free p
			if (prev->left == p)
			{
				free(prev->left);
				prev->left = 0;
			}
			else
			{
				free(prev->right);
				prev->right = 0;
			}
		}
		else if (p->left && !p->right)
		{
			// one child of p is zero, so just attach 
			// the child of p to prev
			if (prev->left == p)
				prev->left = p->left;
			else
				prev->right = p->left;
			free(p);
		}
		else if (!p->left && p->right)
		{
			// one child of p is zero, so just attach 
			// the child of p to prev
			if (prev->left == p)
				prev->left = p->right;
			else
				prev->right = p->right;
			free(p);
		}
		else
		{
			// both children exist
			if (prev->left == p)
			{
				// attach the left child of p
				prev->left = p->left;
				InsertIntoTree(prev,p->right);
			}
			else
			{
				// attach the right child of p
				prev->right = p->right;
				InsertIntoTree(prev,p->left);
			}
			
			free(p);
		}
	}
		
}


void* operator new(size_t size) 
{
	void* ptr = malloc(size);
	if (!ptr)
	{
		printf("PANIC : memory allocation failed !\n");
		exit(-1);
	}
	
	
	pthread_mutex_lock(&mtree.mutex);
	RegisterAlloc(ptr,size);
	pthread_mutex_unlock(&mtree.mutex);
	return ptr;
}


void* operator new[] (size_t size) 
{
	void* ptr = malloc(size);
	if (!ptr)
	{
		printf("PANIC : memory allocation failed !\n");
		exit(-1);
	}
	
	pthread_mutex_lock(&mtree.mutex);
	RegisterAlloc(ptr,size);
	pthread_mutex_unlock(&mtree.mutex);
	return ptr;
}


void operator delete (void *ptr) 
{
	if (!ptr)
		return;
	
	
	pthread_mutex_lock(&mtree.mutex);
	DeregisterAlloc(ptr);
	pthread_mutex_unlock(&mtree.mutex);
	free(ptr);
}


void operator delete[] (void *ptr) 
{
	if (!ptr)
		return;
	
	pthread_mutex_lock(&mtree.mutex);
	DeregisterAlloc(ptr);
	pthread_mutex_unlock(&mtree.mutex);
	free(ptr);
}

#endif // KT_LEAKTRACE