summaryrefslogblamecommitdiffstats
path: root/external/optick/optick_memory.h
blob: 45249c694c9de0d711cab1545ae9b7035d24c888 (plain) (tree)
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419


































































































































































































































































































































































































































                                                                                                                                                                            
#pragma once

#include "optick_common.h"

#if USE_OPTICK

#include <cstring>
#include <new>
#include <stdlib.h>
#include <atomic>

#include <array>
#include <list>
#include <string>
#include <sstream>
#include <unordered_set>
#include <unordered_map>
#include <vector>

namespace Optick
{
	class Memory
	{
		struct Header
		{
			uint64_t size;
		};

		static std::atomic<uint64_t> memAllocated;

		static void* (*allocate)(size_t);
		static void  (*deallocate)(void* p);
	public:
		static OPTICK_INLINE void* Alloc(size_t size)
		{
			size_t totalSize = size + sizeof(Header);
			void *ptr = allocate(totalSize);
			OPTICK_VERIFY(ptr, "Can't allocate memory", return nullptr);

			Header* header = (Header*)ptr;
			header->size = totalSize;
			memAllocated += totalSize;

			return (uint8_t*)ptr + sizeof(Header);
		}

		static OPTICK_INLINE void Free(void* p)
		{
			if (p != nullptr)
			{
				uint8_t* basePtr = (uint8_t*)p - sizeof(Header);
				Header* header = (Header*)basePtr;
				memAllocated -= header->size;
				deallocate(basePtr);
			}
		}

		static OPTICK_INLINE size_t GetAllocatedSize()
		{
			return (size_t)memAllocated;
		}

		template<class T>
		static T* New()
		{
			return new (Memory::Alloc(sizeof(T))) T();
		}

		template<class T, class P1>
		static T* New(P1 p1)
		{
			return new (Memory::Alloc(sizeof(T))) T(p1);
		}

		template<class T, class P1, class P2>
		static T* New(P1 p1, P2 p2)
		{
			return new (Memory::Alloc(sizeof(T))) T(p1, p2);
		}

		template<class T>
		static void Delete(T* p)
		{
			if (p)
			{
				p->~T();
				Free(p);
			}
		}

		static void SetAllocator(void* (*allocateFn)(size_t), void(*deallocateFn)(void*))
		{
			allocate = allocateFn;
			deallocate = deallocateFn;
		}

		template<typename T> 
		struct Allocator : public std::allocator<T> 
		{
			Allocator() {}
			template<class U> 
			Allocator(const Allocator<U>&) {}
			template<typename U> struct rebind { typedef Allocator<U> other; };

			typename std::allocator<T>::pointer allocate(typename std::allocator<T>::size_type n, typename std::allocator<void>::const_pointer = 0)
			{
				return reinterpret_cast<typename std::allocator<T>::pointer>(Memory::Alloc(n * sizeof(T)));
			}

			void deallocate(typename std::allocator<T>::pointer p, typename std::allocator<T>::size_type)
			{
				Memory::Free(p);
			}
		};
	};

	// std::* section
	template <typename T, size_t _Size> class array  : public std::array<T, _Size>{};
	template <typename T> class vector : public std::vector<T, Memory::Allocator<T>>{};
	template <typename T> class list : public std::list<T, Memory::Allocator<T>>{};
	template <typename T> class unordered_set : public std::unordered_set<T, std::hash<T>, std::equal_to<T>, Memory::Allocator<T>>{};
	template <typename T, typename V> class unordered_map : public std::unordered_map<T, V, std::hash<T>, std::equal_to<T>, Memory::Allocator<std::pair<const T, V>>>{};
	
	using string = std::basic_string<char, std::char_traits<char>, Memory::Allocator<char>>;
	using wstring = std::basic_string<wchar_t, std::char_traits<wchar_t>, Memory::Allocator<wchar_t>>;
	
	using istringstream = std::basic_istringstream<char, std::char_traits<char>, Memory::Allocator<char>>;
	using ostringstream = std::basic_ostringstream<char, std::char_traits<char>, Memory::Allocator<char>>;
	using stringstream = std::basic_stringstream<char, std::char_traits<char>, Memory::Allocator<char>>;

	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	template<class T, uint32 SIZE>
	struct MemoryChunk
	{
		OPTICK_ALIGN_CACHE T data[SIZE];
		MemoryChunk* next;
		MemoryChunk* prev;

		MemoryChunk() : next(0), prev(0) {}

		~MemoryChunk()
		{
			MemoryChunk* chunk = this;
			while (chunk->next)
				chunk = chunk->next;

			while (chunk != this)
			{
				MemoryChunk* toDelete = chunk;
				chunk = toDelete->prev;
				Memory::Delete(toDelete);
			}

			if (prev != nullptr)
			{
				prev->next = nullptr;
				prev = nullptr;
			}
		}
	};
	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	template<class T, uint32 SIZE = 16>
	class MemoryPool
	{
		typedef MemoryChunk<T, SIZE> Chunk;
		Chunk* root;
		Chunk* chunk;
		uint32 index;

		OPTICK_INLINE void AddChunk()
		{
			index = 0;
			if (!chunk || !chunk->next)
			{
				Chunk* newChunk = Memory::New<Chunk>();
				if (chunk)
				{
					chunk->next = newChunk;
					newChunk->prev = chunk;
					chunk = newChunk;
				}
				else
				{
					root = chunk = newChunk;
				}
			}
			else
			{
				chunk = chunk->next;
			}
		}
	public:
		MemoryPool() : root(nullptr), chunk(nullptr), index(SIZE) {}

		OPTICK_INLINE T& Add()
		{
			if (index >= SIZE)
				AddChunk();

			return chunk->data[index++];
		}

		OPTICK_INLINE T& Add(const T& item)
		{
			return Add() = item;
		}

		OPTICK_INLINE T* AddRange(const T* items, size_t count, bool allowOverlap = true)
		{
			if (count == 0 || (count > SIZE && !allowOverlap))
				return nullptr;

			if (count >= (SIZE - index) && !allowOverlap)
			{
				AddChunk();
			}

			T* result = &chunk->data[index];

			while (count)
			{
				size_t numLeft = SIZE - index;
				size_t numCopy = numLeft < count ? numLeft : count;
				std::memcpy(&chunk->data[index], items, sizeof(T) * numCopy);

				count -= numCopy;
				items += numCopy;
				index += (uint32_t)numCopy;

				if (count)
					AddChunk();
			}

			return result;
		}


		OPTICK_INLINE T* TryAdd(int count)
		{
			if (index + count <= SIZE)
			{
				T* res = &chunk->data[index];
				index += count;
				return res;
			}

			return nullptr;
		}

		OPTICK_INLINE T* Back()
		{
			if (chunk && index > 0)
				return &chunk->data[index - 1];

			if (chunk && chunk->prev != nullptr)
				return &chunk->prev->data[SIZE - 1];

			return nullptr;
		}

		OPTICK_INLINE size_t Size() const
		{
			if (root == nullptr)
				return 0;

			size_t count = 0;

			for (const Chunk* it = root; it != chunk; it = it->next)
				count += SIZE;

			return count + index;
		}

		OPTICK_INLINE bool IsEmpty() const
		{
			return (chunk == nullptr) || (chunk == root && index == 0);
		}

		OPTICK_INLINE void Clear(bool preserveMemory = true)
		{
			if (!preserveMemory)
			{
				if (root)
				{
					Memory::Delete(root);
					root = nullptr;
					chunk = nullptr;
					index = SIZE;
				}
			}
			else if (root)
			{
				index = 0;
				chunk = root;
			}
		}

		class const_iterator
		{
			void advance()
			{
				if (chunkIndex < SIZE - 1)
				{
					++chunkIndex;
				}
				else
				{
					chunkPtr = chunkPtr->next;
					chunkIndex = 0;
				}
			}
		public:
			typedef const_iterator self_type;
			typedef T value_type;
			typedef T& reference;
			typedef T* pointer;
			typedef int difference_type;
			const_iterator(const Chunk* ptr, size_t index) : chunkPtr(ptr), chunkIndex(index) { }
			self_type operator++()
			{
				self_type i = *this;
				advance();
				return i;
			}
			self_type operator++(int /*junk*/)
			{
				advance();
				return *this;
			}
			reference operator*() { return (reference)chunkPtr->data[chunkIndex]; }
			const pointer operator->() { return &chunkPtr->data[chunkIndex]; }
			bool operator==(const self_type& rhs) { return (chunkPtr == rhs.chunkPtr) && (chunkIndex == rhs.chunkIndex); }
			bool operator!=(const self_type& rhs) { return (chunkPtr != rhs.chunkPtr) || (chunkIndex != rhs.chunkIndex); }
		private:
			const Chunk* chunkPtr;
			size_t chunkIndex;
		};

		const_iterator begin() const
		{
			return const_iterator(root, 0);
		}

		const_iterator end() const
		{
			return const_iterator(chunk, index);
		}

		template<class Func>
		void ForEach(Func func) const
		{
			for (const Chunk* it = root; it != chunk; it = it->next)
				for (uint32 i = 0; i < SIZE; ++i)
					func(it->data[i]);

			if (chunk)
				for (uint32 i = 0; i < index; ++i)
					func(chunk->data[i]);
		}

		template<class Func>
		void ForEach(Func func)
		{
			for (Chunk* it = root; it != chunk; it = it->next)
				for (uint32 i = 0; i < SIZE; ++i)
					func(it->data[i]);

			if (chunk)
				for (uint32 i = 0; i < index; ++i)
					func(chunk->data[i]);
		}

		template<class Func>
		void ForEachChunk(Func func) const
		{
			for (const Chunk* it = root; it != chunk; it = it->next)
				func(it->data, SIZE);

			if (chunk)
				func(chunk->data, index);
		}

		void ToArray(T* destination) const
		{
			uint32 curIndex = 0;

			for (const Chunk* it = root; it != chunk; it = it->next)
			{
				memcpy(&destination[curIndex], it->data, sizeof(T) * SIZE);
				curIndex += SIZE;
			}

			if (chunk && index > 0)
			{
				memcpy(&destination[curIndex], chunk->data, sizeof(T) * index);
			}
		}
	};
	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	template<uint32 CHUNK_SIZE>
	class MemoryBuffer : private MemoryPool<uint8, CHUNK_SIZE>
	{
	public:
		template<class U>
		U* Add(U* data, size_t size, bool allowOverlap = true)
		{
			return (U*)(MemoryPool<uint8, CHUNK_SIZE>::AddRange((uint8*)data, size, allowOverlap));
		}

		template<class T>
		T* Add(const T& val, bool allowOverlap = true)
		{
			return static_cast<T*>(Add(&val, sizeof(T), allowOverlap));
		}
	};
	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
}

#endif //USE_OPTICK