aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.Client/PS3/PS3Extras/HeapInspector/Samples/ReplaceNewDelete/ReplaceNewDeleteSample.cpp
blob: 305d589b5d1398f9dafed6c6fb1c3c68e04012f5 (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
// =================================================================================================================================
// This sample demonstrates how to replace operator new and delete and how to send these allocations to the HeapInspector client.
// Please note that the Hook sample captures allocations on a lower level and will also trace all new/delete allocations. 
//
// WARNING: Make sure that you replace both the array and non-array operators. If there are any pairing issues in your code 
// (allocating with new[] and deleting with delete), HeapInspector will miss those deallocations and that will be problematic 
// for a subsequent allocation on that address: it will then warn that it found a double allocation. That will actually be a 
// sign that the operators aren't properly matched.
// =================================================================================================================================

#include "../../Server/HeapInspectorServer.h"
#include <stdlib.h>
#include <new>

using namespace HeapInspectorServer;

void Wait(int a_MilliSeconds);

void* operator new(size_t a_Size)
{
	Mutation mutation = BeginAlloc();
	void* mem = malloc(a_Size);
	EndAlloc(mutation, 0, mem, a_Size, a_Size);
	return mem;
}

void operator delete(void* a_Pointer)
{
	Mutation mutation = BeginFree();
	free(a_Pointer);
	EndFree(mutation, 0, a_Pointer);
}

void* operator new[](size_t a_Size)
{
	Mutation mutation = BeginAlloc();
	void* mem = malloc(a_Size);
	EndAlloc(mutation, 0, mem, a_Size, a_Size);
	return mem;
}

void operator delete[](void* a_Pointer)
{
	Mutation mutation = BeginFree();
	free(a_Pointer);
	EndFree(mutation, 0, a_Pointer);
}

void RunHeapInspectorServer()
{
	Initialise(GetDefaultHeapInfo(), 3000, WaitForConnection_Enabled);

	while (1)
	{
		int* xArray = new int[100];
		float* y = new float;

		Wait(100);

		delete[] xArray;
		delete y;

		Wait(100);
	}

	Shutdown();
}