summaryrefslogtreecommitdiff
path: root/utils/tfstats/memdbg.cpp
blob: 670090c0e89691bae8b00f9353f67135382bbf9e (plain) (blame)
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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: code to track allocations via replacing the global new operator
//	some of this code was written by Paul Andre LeBlanc 
//	<[email protected]> I got it off of dejanews.com
//	usage: new TRACKED object-type
//
// $Workfile:     $
// $Date:         $
//
//------------------------------------------------------------------------------------------------------
// $Log: $
//
// $NoKeywords: $
//=============================================================================//
#include <new.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream.h>


#ifdef _DEBUG
#ifdef _MEMDEBUG
#define _MDEBUG
#endif
#endif

#ifdef _MDEBUG
static int numBytesAllocated=0;
//these were written by me, wes cumberland, not paul andre leblanc
void * operator new(size_t size)
{
	void *ptr = malloc(size);
	numBytesAllocated+=size;
	return ptr;
}

void * operator new[](size_t size)
{
	void *ptr = malloc(size);
	numBytesAllocated+=size;
	return ptr;
}

void operator delete(void* ptr)
{
	 free(ptr);
}

void operator delete[](void* ptr)
{
	free(ptr);
}


//this code will track allocations
//this code was written by Paul Andre LeBlanc <[email protected]>
//I got it off of dejanews.com
void *operator new(size_t size, const char *file, const int line) 
{
  void *ptr = new char[size];
  numBytesAllocated+=size;
  cout << "new: Allocating " << size << " bytes in file " << file << ", line " << line << ", address is " << ptr << " (" << numBytesAllocated<<" total allocated)"<< endl;
  return ptr;
}
 
void *operator new[](size_t size, const char *file, const int line) {
  void *ptr = new char[size];
  numBytesAllocated+=size;
  cout << "new[]: Allocating " << size << " bytes in file " << file << ", line " << line << ", address is " << ptr << " (" << numBytesAllocated<<" total allocated)" << endl;
  return ptr;
}
 
void operator delete(void *ptr, const char *file, const int line) {
  cout << "delete: Freeing memory allocated at file " << file << ", line " << line << ", address is " << ptr << endl;
  delete [] (char *) ptr;
}
 
void operator delete[](void *ptr, const char *file, const int line) 
{  
	cout << "delete[]: Freeing memory allocated at file " << file << ", line " << line << ", address is " << ptr << endl;
	delete [] (char *) ptr;
}

#endif
//------------------------------------------------------------------------------------------------------
// Function:	TFStats_win32_new_handler
// Purpose:	this function will be called if TFStats runs out of memory (unlikely)
//		this is a win32 specific version, the linux version does not pass an argument
// Input:	sz - the size of the allocation that failed
// Output:	int
//------------------------------------------------------------------------------------------------------
int TFStats_win32_new_handler(size_t sz)
{
	printf("TFStats ran out of memory trying to allocate %li bytes\n",sz);
	return 0;
}
//------------------------------------------------------------------------------------------------------
// Function:	TFStats_linux_new_handler
// Purpose:	this function will be called if TFStats runs out of memory (unlikely)
//		this is a linux specific version, the win32 version passes an argument
//------------------------------------------------------------------------------------------------------
void TFStats_linux_new_handler(void)
{
	printf("TFStats ran out of memory!\n");
}


//------------------------------------------------------------------------------------------------------
// Function:	TFStats_setNewHandler
// Purpose:	 sets the new handler to the TFStats new handler
//------------------------------------------------------------------------------------------------------
void TFStats_setNewHandler()
{
#ifdef WIN32
	_set_new_handler(TFStats_win32_new_handler);
	_set_new_mode(1);
#else
	std::set_new_handler(TFStats_linux_new_handler);
	//std::set_new_mode(1);
#endif
}