aboutsummaryrefslogtreecommitdiff
path: root/APEX_1.4/shared/external/src/UserAllocator.cpp
blob: 9177fde7b7a9cd17ca3c9d646d509e877e923bcc (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
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
/*
 * Copyright (c) 2008-2017, NVIDIA CORPORATION.  All rights reserved.
 *
 * NVIDIA CORPORATION and its licensors retain all intellectual property
 * and proprietary rights in and to this software, related documentation
 * and any modifications thereto.  Any use, reproduction, disclosure or
 * distribution of this software and related documentation without an express
 * license agreement from NVIDIA CORPORATION is strictly prohibited.
 */

#include <stdio.h>
#include "UserAllocator.h"

#include "MemTracker.h"

#include "PxAssert.h"

#pragma warning(disable:4100 4152)

#ifdef USE_MEM_TRACKER

static const char* USERALLOCATOR = "UserAllocator";
#endif

/*============  UserPxAllocator  ============*/

#ifdef USE_MEM_TRACKER

#include <windows.h>

class MemMutex
{
public:
	MemMutex(void);
	~MemMutex(void);

public:
	// Blocking Lock.
	void Lock(void);

	// Unlock.
	void Unlock(void);

private:
	CRITICAL_SECTION m_Mutex;
};

//==================================================================================
MemMutex::MemMutex(void)
{
	InitializeCriticalSection(&m_Mutex);
}

//==================================================================================
MemMutex::~MemMutex(void)
{
	DeleteCriticalSection(&m_Mutex);
}

//==================================================================================
// Blocking Lock.
//==================================================================================
void MemMutex::Lock(void)
{
	EnterCriticalSection(&m_Mutex);
}

//==================================================================================
// Unlock.
//==================================================================================
void MemMutex::Unlock(void)
{
	LeaveCriticalSection(&m_Mutex);
}

static inline MemMutex& gMemMutex()
{
	static MemMutex sMemMutex;
	return sMemMutex;
}

static size_t getThreadId(void)
{
	return GetCurrentThreadId();
}

static bool memoryReport(MEM_TRACKER::MemTracker *memTracker,MEM_TRACKER::MemoryReportFormat format,const char *fname,bool reportAllLeaks) // detect memory leaks and, if any, write out a report to the filename specified.
{
	bool ret = false;

	size_t leakCount;
	size_t leaked = memTracker->detectLeaks(leakCount);
	if ( leaked )
	{
		uint32_t dataLen;
		void *mem = memTracker->generateReport(format,fname,dataLen,reportAllLeaks);
		if ( mem )
		{
			FILE *fph = fopen(fname,"wb");
			fwrite(mem,dataLen,1,fph);
			fclose(fph);
			memTracker->releaseReportMemory(mem);
		}
		ret = true; // it leaked memory!
	}
	return ret;
}

class ScopedLock
{
public:
	ScopedLock(MemMutex &mutex, bool enabled) : mMutex(mutex), mEnabled(enabled) 
	{
		if (mEnabled) mMutex.Lock(); 
	}
	~ScopedLock()
	{
		if (mEnabled) mMutex.Unlock(); 
	}

private:
	ScopedLock();
	ScopedLock(const ScopedLock&);
	ScopedLock& operator=(const ScopedLock&);

	MemMutex& mMutex;
	bool mEnabled;
};

#define SCOPED_TRACKER_LOCK_IF(USE_TRACKER) ScopedLock lock(gMemMutex(), (USE_TRACKER))
#define TRACKER_CALL_IF(USE_TRACKER, CALL)  if ((USE_TRACKER)) { (CALL); }

void releaseAndReset(MEM_TRACKER::MemTracker*& memTracker)
{
	MEM_TRACKER::releaseMemTracker(memTracker);
	memTracker = NULL;
}

#else

#define SCOPED_TRACKER_LOCK_IF(USE_TRACKER) 
#define TRACKER_CALL_IF(USE_TRACKER, CALL)  

#endif

int UserPxAllocator::gMemoryTrackerClients = 0;
MEM_TRACKER::MemTracker	*UserPxAllocator::mMemoryTracker=NULL;

unsigned int UserPxAllocator::mNumAllocations = 0;
unsigned int UserPxAllocator::mNumFrees = 0;

UserPxAllocator::UserPxAllocator(const char* context,
                                 const char* /*dllName*/,
                                 bool useTrackerIfSupported /* = true */)
	: mContext(context)
	, mMemoryAllocated(0)
	, mUseTracker(useTrackerIfSupported)
{
	SCOPED_TRACKER_LOCK_IF(mUseTracker);
	TRACKER_CALL_IF(mUseTracker && NULL == mMemoryTracker, mMemoryTracker = MEM_TRACKER::createMemTracker());
	TRACKER_CALL_IF(trackerEnabled(), ++gMemoryTrackerClients);
}

void* UserPxAllocator::allocate(size_t size, const char* typeName, const char* filename, int line)
{
	void* ret = 0;

	PX_UNUSED(typeName);
	PX_UNUSED(filename);
	PX_UNUSED(line);
	SCOPED_TRACKER_LOCK_IF(trackerEnabled());

#if PX_WINDOWS_FAMILY
	ret = ::_aligned_malloc(size, 16);
#elif PX_ANDROID || PX_LINUX_FAMILY || PX_SWITCH
	/* Allocate size + (15 + sizeof(void*)) bytes and shift pointer further, write original address in the beginning of block.*/
	/* Weirdly, memalign sometimes returns unaligned address */
	//	ret = ::memalign(size, 16);
	size_t alignment = 16;
	void* originalRet = ::malloc(size + 2*alignment + sizeof(void*));
	// find aligned location
	ret = ((char*)originalRet) + 2 * alignment - (((size_t)originalRet) & 0xF);
	// write block address prior to aligned position, so it could be possible to free it later
	::memcpy((char*)ret - sizeof(originalRet), &originalRet, sizeof(originalRet));
#else
	ret = ::malloc(size);
#endif

	TRACKER_CALL_IF(trackerEnabled(), mMemoryAllocated += size);
	TRACKER_CALL_IF(trackerEnabled(), mMemoryTracker->trackAlloc(getThreadId(),ret, size, MEM_TRACKER::MT_MALLOC, USERALLOCATOR, typeName, filename, (uint32_t)line));

	// this should probably be a atomic increment
	mNumAllocations++;

	return ret;
}

void UserPxAllocator::deallocate(void* memory)
{
	SCOPED_TRACKER_LOCK_IF(trackerEnabled());

	if (memory)
	{
#if PX_WINDOWS_FAMILY
		::_aligned_free(memory);
#elif PX_ANDROID || PX_LINUX_FAMILY || PX_SWITCH
		// Looks scary, but all it does is getting original unaligned block address back from 4/8 bytes (depends on pointer size) prior to <memory> pointer and frees this memory 
		void* originalPtr = (void*)(*(size_t*)((char*)memory - sizeof(void*)));
		::free(originalPtr);
#else
		::free(memory);
#endif

		// this should probably be a atomic decrement
		mNumFrees++;
	}

	TRACKER_CALL_IF(trackerEnabled(), mMemoryTracker->trackFree(getThreadId(), memory, MEM_TRACKER::MT_FREE, USERALLOCATOR, __FILE__, __LINE__));
}


UserPxAllocator::~UserPxAllocator()
{
	SCOPED_TRACKER_LOCK_IF(trackerEnabled());
	TRACKER_CALL_IF(trackerEnabled() && (0 == --gMemoryTrackerClients), releaseAndReset(mMemoryTracker));
}

bool UserPxAllocator::dumpMemoryLeaks(const char* filename)
{
	bool leaked = false;

	PX_UNUSED(filename);
	TRACKER_CALL_IF(mMemoryTracker, leaked = memoryReport(mMemoryTracker, MEM_TRACKER::MRF_SIMPLE_HTML, filename, true));

	return leaked;
}

#undef SCOPED_TRACKER_LOCK_IF
#undef TRACKER_CALL_IF