summaryrefslogtreecommitdiff
path: root/indra/llcommon/lltrace.h
blob: 7694f65441f4032d33249cc8bd85b84b927b645c (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
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
/** 
 * @file lltrace.h
 * @brief Runtime statistics accumulation.
 *
 * $LicenseInfo:firstyear=2001&license=viewerlgpl$
 * Second Life Viewer Source Code
 * Copyright (C) 2012, Linden Research, Inc.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation;
 * version 2.1 of the License only.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 * 
 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 * $/LicenseInfo$
 */

#ifndef LL_LLTRACE_H
#define LL_LLTRACE_H

#include "stdtypes.h"
#include "llpreprocessor.h"

#include "llmemory.h"
#include "llrefcount.h"
#include "llunit.h"
#include "llapr.h"
#include "llthreadlocalstorage.h"

#include <list>

#define LL_RECORD_BLOCK_TIME(block_timer) LLTrace::TimeBlock::Recorder LL_GLUE_TOKENS(block_time_recorder, __COUNTER__)(block_timer);

namespace LLTrace
{
	class Recording;

	typedef LLUnit<LLUnits::Bytes, F64>			Bytes;
	typedef LLUnit<LLUnits::Kilobytes, F64>		Kilobytes;
	typedef LLUnit<LLUnits::Megabytes, F64>		Megabytes;
	typedef LLUnit<LLUnits::Gigabytes, F64>		Gigabytes;
	typedef LLUnit<LLUnits::Bits, F64>			Bits;
	typedef LLUnit<LLUnits::Kilobits, F64>		Kilobits;
	typedef LLUnit<LLUnits::Megabits, F64>		Megabits;
	typedef LLUnit<LLUnits::Gigabits, F64>		Gigabits;

	typedef LLUnit<LLUnits::Seconds, F64>		Seconds;
	typedef LLUnit<LLUnits::Milliseconds, F64>	Milliseconds;
	typedef LLUnit<LLUnits::Minutes, F64>		Minutes;
	typedef LLUnit<LLUnits::Hours, F64>			Hours;
	typedef LLUnit<LLUnits::Milliseconds, F64>	Milliseconds;
	typedef LLUnit<LLUnits::Microseconds, F64>	Microseconds;
	typedef LLUnit<LLUnits::Nanoseconds, F64>	Nanoseconds;

	typedef LLUnit<LLUnits::Meters, F64>		Meters;
	typedef LLUnit<LLUnits::Kilometers, F64>	Kilometers;
	typedef LLUnit<LLUnits::Centimeters, F64>	Centimeters;
	typedef LLUnit<LLUnits::Millimeters, F64>	Millimeters;

	void init();
	void cleanup();
	bool isInitialized();

	const LLThreadLocalPointer<class ThreadRecorder>& get_thread_recorder();
	void set_thread_recorder(class ThreadRecorder*);

	class MasterThreadRecorder& getMasterThreadRecorder();

	// one per thread per type
	template<typename ACCUMULATOR>
	class AccumulatorBuffer : public LLRefCount
	{
		typedef AccumulatorBuffer<ACCUMULATOR> self_t;
		static const U32 DEFAULT_ACCUMULATOR_BUFFER_SIZE = 64;
	private:
		struct StaticAllocationMarker { };

		AccumulatorBuffer(StaticAllocationMarker m)
		:	mStorageSize(0),
			mStorage(NULL),
			mNextStorageSlot(0)
		{
		}

	public:

		AccumulatorBuffer(const AccumulatorBuffer& other = *getDefaultBuffer())
		:	mStorageSize(0),
			mStorage(NULL),
			mNextStorageSlot(other.mNextStorageSlot)
		{
			resize(other.mStorageSize);
			for (S32 i = 0; i < mNextStorageSlot; i++)
			{
				mStorage[i] = other.mStorage[i];
			}
		}

		~AccumulatorBuffer()
		{
			if (LLThreadLocalSingletonPointer<ACCUMULATOR>::getInstance() == mStorage)
			{
				LLThreadLocalSingletonPointer<ACCUMULATOR>::setInstance(getDefaultBuffer()->mStorage);
			}
			delete[] mStorage;
		}

		LL_FORCE_INLINE ACCUMULATOR& operator[](size_t index) 
		{ 
			return mStorage[index]; 
		}

		LL_FORCE_INLINE const ACCUMULATOR& operator[](size_t index) const
		{ 
			return mStorage[index]; 
		}

		void addSamples(const AccumulatorBuffer<ACCUMULATOR>& other)
		{
			llassert(mNextStorageSlot == other.mNextStorageSlot);

			for (size_t i = 0; i < mNextStorageSlot; i++)
			{
				mStorage[i].addSamples(other.mStorage[i]);
			}
		}

		void copyFrom(const AccumulatorBuffer<ACCUMULATOR>& other)
		{
			for (size_t i = 0; i < mNextStorageSlot; i++)
			{
				mStorage[i] = other.mStorage[i];
			}
		}

		void reset(const AccumulatorBuffer<ACCUMULATOR>* other = NULL)
		{
			for (size_t i = 0; i < mNextStorageSlot; i++)
			{
				mStorage[i].reset(other ? &other->mStorage[i] : NULL);
			}
		}

		void makePrimary()
		{
			LLThreadLocalSingletonPointer<ACCUMULATOR>::setInstance(mStorage);
		}

		bool isPrimary() const
		{
			return LLThreadLocalSingletonPointer<ACCUMULATOR>::getInstance() == mStorage;
		}

		LL_FORCE_INLINE static ACCUMULATOR* getPrimaryStorage() 
		{ 
			return LLThreadLocalSingletonPointer<ACCUMULATOR>::getInstance(); 
		}

		// NOTE: this is not thread-safe.  We assume that slots are reserved in the main thread before any child threads are spawned
		size_t reserveSlot()
		{
			if (LLTrace::isInitialized())
			{
				llerrs << "Attempting to declare trace object after program initialization.  Trace objects should be statically initialized." << llendl;
			}
			size_t next_slot = mNextStorageSlot++;
			if (next_slot >= mStorageSize)
			{
				resize(mStorageSize + (mStorageSize >> 2));
			}
			llassert(mStorage && next_slot < mStorageSize);
			return next_slot;
		}

		void resize(size_t new_size)
		{
			if (new_size <= mStorageSize) return;

			ACCUMULATOR* old_storage = mStorage;
			mStorage = new ACCUMULATOR[new_size];
			if (old_storage)
			{
				for (S32 i = 0; i < mStorageSize; i++)
				{
					mStorage[i] = old_storage[i];
				}
			}
			mStorageSize = new_size;
			delete[] old_storage;

			self_t* default_buffer = getDefaultBuffer();
			if (this != default_buffer
				&& new_size > default_buffer->size())
			{
				//NB: this is not thread safe, but we assume that all resizing occurs during static initialization
				default_buffer->resize(new_size);
			}
		}

		size_t size() const
		{
			return mNextStorageSlot;
		}

		static self_t* getDefaultBuffer()
		{
			// this buffer is allowed to leak so that trace calls from global destructors have somewhere to put their data
			// so as not to trigger an access violation
			static self_t* sBuffer = new AccumulatorBuffer(StaticAllocationMarker());
			static bool sInitialized = false;
			if (!sInitialized)
			{
				sBuffer->resize(DEFAULT_ACCUMULATOR_BUFFER_SIZE);
				sInitialized = true;
			}
			return sBuffer;
		}

	private:
		ACCUMULATOR*								mStorage;
		size_t										mStorageSize;
		size_t										mNextStorageSlot;
	};

	//TODO: replace with decltype when C++11 is enabled
	template<typename T>
	struct MeanValueType
	{
		typedef F64 type;
	};

	template<typename ACCUMULATOR>
	class TraceType 
	:	 public LLInstanceTracker<TraceType<ACCUMULATOR>, std::string>
	{
	public:
		typedef typename MeanValueType<TraceType<ACCUMULATOR> >::type  mean_t;

		TraceType(const char* name, const char* description = NULL)
		:	LLInstanceTracker<TraceType<ACCUMULATOR>, std::string>(name),
			mName(name),
			mDescription(description ? description : ""),
			mAccumulatorIndex(AccumulatorBuffer<ACCUMULATOR>::getDefaultBuffer()->reserveSlot())
		{}

		LL_FORCE_INLINE ACCUMULATOR* getPrimaryAccumulator() const
		{
			ACCUMULATOR* accumulator_storage = AccumulatorBuffer<ACCUMULATOR>::getPrimaryStorage();
			return &accumulator_storage[mAccumulatorIndex];
		}

		size_t getIndex() const { return mAccumulatorIndex; }

		const std::string& getName() const { return mName; }

	protected:
		const std::string	mName;
		const std::string	mDescription;
		const size_t		mAccumulatorIndex;
	};

	template<typename T>
	class MeasurementAccumulator
	{
	public:
		typedef T value_t;
		typedef MeasurementAccumulator<T> self_t;

		MeasurementAccumulator()
		:	mSum(0),
			mMin((std::numeric_limits<T>::max)()),
			mMax((std::numeric_limits<T>::min)()),
			mMean(0),
			mVarianceSum(0),
			mNumSamples(0),
			mLastValue(0)
		{}

		LL_FORCE_INLINE void sample(T value)
		{
			T storage_value(value);
			mNumSamples++;
			mSum += storage_value;
			if (storage_value < mMin)
			{
				mMin = storage_value;
			}
			if (storage_value > mMax)
			{
				mMax = storage_value;
			}
			F64 old_mean = mMean;
			mMean += ((F64)storage_value - old_mean) / (F64)mNumSamples;
			mVarianceSum += ((F64)storage_value - old_mean) * ((F64)storage_value - mMean);
			mLastValue = storage_value;
		}

		void addSamples(const self_t& other)
		{
			if (other.mNumSamples)
			{
				mSum += other.mSum;
				if (other.mMin < mMin)
				{
					mMin = other.mMin;
				}
				if (other.mMax > mMax)
				{
					mMax = other.mMax;
				}
				F64 weight = (F64)mNumSamples / (F64)(mNumSamples + other.mNumSamples);
				mNumSamples += other.mNumSamples;
				mMean = mMean * weight + other.mMean * (1.f - weight);

				// combine variance (and hence standard deviation) of 2 different sized sample groups using
				// the following formula: http://www.mrc-bsu.cam.ac.uk/cochrane/handbook/chapter_7/7_7_3_8_combining_groups.htm
				F64 n_1 = (F64)mNumSamples,
					n_2 = (F64)other.mNumSamples;
				F64 m_1 = mMean,
					m_2 = other.mMean;
				F64 sd_1 = getStandardDeviation(),
					sd_2 = other.getStandardDeviation();
				if (n_1 == 0)
				{
					mVarianceSum = other.mVarianceSum;
				}
				else if (n_2 == 0)
				{
					// don't touch variance
					// mVarianceSum = mVarianceSum;
				}
				else
				{
					mVarianceSum =  (F64)mNumSamples
								* ((((n_1 - 1.f) * sd_1 * sd_1)
									+ ((n_2 - 1.f) * sd_2 * sd_2)
									+ (((n_1 * n_2) / (n_1 + n_2))
										* ((m_1 * m_1) + (m_2 * m_2) - (2.f * m_1 * m_2))))
								/ (n_1 + n_2 - 1.f));
				}
				mLastValue = other.mLastValue;
			}
		}

		void reset(const self_t* other)
		{
			mNumSamples = 0;
			mSum = 0;
			mMin = 0;
			mMax = 0;
			mMean = 0;
			mVarianceSum = 0;
			mLastValue = other ? other->mLastValue : 0;
		}

		T	getSum() const { return (T)mSum; }
		T	getMin() const { return (T)mMin; }
		T	getMax() const { return (T)mMax; }
		T	getLastValue() const { return (T)mLastValue; }
		F64	getMean() const { return mMean; }
		F64 getStandardDeviation() const { return sqrtf(mVarianceSum / mNumSamples); }
		U32 getSampleCount() const { return mNumSamples; }

	private:
		T	mSum,
			mMin,
			mMax,
			mLastValue;

		F64	mMean,
			mVarianceSum;

		U32	mNumSamples;
	};

	template<typename T>
	class CountAccumulator
	{
	public:
		typedef CountAccumulator<T> self_t;
		typedef T value_t;

		CountAccumulator()
		:	mSum(0),
			mNumSamples(0)
		{}

		LL_FORCE_INLINE void add(T value)
		{
			mNumSamples++;
			mSum += value;
		}

		void addSamples(const CountAccumulator<T>& other)
		{
			mSum += other.mSum;
			mNumSamples += other.mNumSamples;
		}

		void reset(const self_t* other)
		{
			mNumSamples = 0;
			mSum = 0;
		}

		T	getSum() const { return (T)mSum; }

		U32 getSampleCount() const { return mNumSamples; }

	private:
		T	mSum;

		U32	mNumSamples;
	};

	class TimeBlockAccumulator
	{
	public:
		typedef LLUnit<LLUnits::Seconds, F64> value_t;
		typedef TimeBlockAccumulator self_t;

		// fake class that allows us to view call count aspect of timeblock accumulator
		struct CallCountAspect 
		{
			typedef U32 value_t;
		};

		struct SelfTimeAspect
		{
			typedef LLUnit<LLUnits::Seconds, F64> value_t;
		};

		TimeBlockAccumulator();
		void addSamples(const self_t& other);
		void reset(const self_t* other);

		//
		// members
		//
		U64							mSelfTimeCounter,
									mTotalTimeCounter;
		U32							mCalls;
		class TimeBlock*			mParent;		// last acknowledged parent of this time block
		class TimeBlock*			mLastCaller;	// used to bootstrap tree construction
		U16							mActiveCount;	// number of timers with this ID active on stack
		bool						mMoveUpTree;	// needs to be moved up the tree of timers at the end of frame

	};


	template<>
	struct MeanValueType<TraceType<TimeBlockAccumulator> >
	{
		typedef LLUnit<LLUnits::Seconds, F64> type;
	};

	template<>
	class TraceType<TimeBlockAccumulator::CallCountAspect>
	:	public TraceType<TimeBlockAccumulator>
	{
	public:
		typedef F32 mean_t;

		TraceType(const char* name, const char* description = "")
		:	TraceType<TimeBlockAccumulator>(name, description)
		{}
	};

	template<>
	class TraceType<TimeBlockAccumulator::SelfTimeAspect>
		:	public TraceType<TimeBlockAccumulator>
	{
	public:
		typedef F32 mean_t;

		TraceType(const char* name, const char* description = "")
			:	TraceType<TimeBlockAccumulator>(name, description)
		{}
	};

	class TimeBlock;
	class TimeBlockTreeNode
	{
	public:
		TimeBlockTreeNode();

		void setParent(TimeBlock* parent);
		TimeBlock* getParent() { return mParent; }

		TimeBlock*					mBlock;
		TimeBlock*					mParent;	
		std::vector<TimeBlock*>		mChildren;
		bool						mNeedsSorting;
	};


	template <typename T = F64>
	class Measurement
	:	public TraceType<MeasurementAccumulator<typename LLUnits::HighestPrecisionType<T>::type_t> >
	{
	public:
		typedef typename LLUnits::HighestPrecisionType<T>::type_t storage_t;
		typedef TraceType<MeasurementAccumulator<typename LLUnits::HighestPrecisionType<T>::type_t> > trace_t;

		Measurement(const char* name, const char* description = NULL) 
		:	trace_t(name, description)
		{}

		template<typename UNIT_T>
		void sample(UNIT_T value)
		{
			T converted_value(value);
			trace_t::getPrimaryAccumulator()->sample(LLUnits::rawValue(converted_value));
		}
	};

	template <typename T = F64>
	class Count 
	:	public TraceType<CountAccumulator<typename LLUnits::HighestPrecisionType<T>::type_t> >
	{
	public:
		typedef typename LLUnits::HighestPrecisionType<T>::type_t storage_t;
		typedef TraceType<CountAccumulator<typename LLUnits::HighestPrecisionType<T>::type_t> > trace_t;

		Count(const char* name, const char* description = NULL) 
		:	trace_t(name)
		{}

		template<typename UNIT_T>
		void add(UNIT_T value)
		{
			T converted_value(value);
			trace_t::getPrimaryAccumulator()->add(LLUnits::rawValue(converted_value));
		}
	};

struct MemStatAccumulator
{
	MemStatAccumulator()
	:	mSize(0),
		mChildSize(0),
		mAllocatedCount(0),
		mDeallocatedCount(0)
	{}

	void addSamples(const MemStatAccumulator& other)
	{
		mSize += other.mSize;
		mChildSize += other.mChildSize;
		mAllocatedCount += other.mAllocatedCount;
		mDeallocatedCount += other.mDeallocatedCount;
	}

	void reset(const MemStatAccumulator* other)
	{
		mSize = 0;
		mChildSize = 0;
		mAllocatedCount = 0;
		mDeallocatedCount = 0;
	}

	size_t		mSize,
				mChildSize;
	int			mAllocatedCount,
				mDeallocatedCount;
};

class MemStat : public TraceType<MemStatAccumulator>
{
public:
	typedef TraceType<MemStatAccumulator> trace_t;
	MemStat(const char* name)
	:	trace_t(name)
	{}
};

// measures effective memory footprint of specified type
// specialize to cover different types

template<typename T>
struct MemFootprint
{
	static size_t measure(const T& value)
	{
		return sizeof(T);
	}

	static size_t measure()
	{
		return sizeof(T);
	}
};

template<typename T>
struct MemFootprint<T*>
{
	static size_t measure(const T* value)
	{
		if (!value)
		{
			return 0;
		}
		return MemFootprint<T>::measure(*value);
	}

	static size_t measure()
	{
		return MemFootprint<T>::measure();
	}
};

template<typename T>
struct MemFootprint<std::basic_string<T> >
{
	static size_t measure(const std::basic_string<T>& value)
	{
		return value.capacity() * sizeof(T);
	}

	static size_t measure()
	{
		return sizeof(std::basic_string<T>);
	}
};

template<typename T>
struct MemFootprint<std::vector<T> >
{
	static size_t measure(const std::vector<T>& value)
	{
		return value.capacity() * MemFootprint<T>::measure();
	}

	static size_t measure()
	{
		return sizeof(std::vector<T>);
	}
};

template<typename T>
struct MemFootprint<std::list<T> >
{
	static size_t measure(const std::list<T>& value)
	{
		return value.size() * (MemFootprint<T>::measure() + sizeof(void*) * 2);
	}

	static size_t measure()
	{
		return sizeof(std::list<T>);
	}
};

template <size_t ALIGNMENT, size_t RESERVE>
void* allocAligned(size_t size)
{
	llstatic_assert((ALIGNMENT > 0) && (ALIGNMENT & (ALIGNMENT - 1)) == 0, "Alignment must be a power of 2");

	void* padded_allocation;
	const size_t aligned_reserve = (RESERVE / ALIGNMENT) 
		+ ((RESERVE % ALIGNMENT) ? ALIGNMENT : 0);
	const size_t size_with_reserve = size + aligned_reserve;
	if (ALIGNMENT <= LL_DEFAULT_HEAP_ALIGN)
	{
		padded_allocation = malloc(size_with_reserve);
	}
	else
	{
#if LL_WINDOWS
		padded_allocation = _aligned_malloc(size_with_reserve, ALIGNMENT);
#elif LL_DARWIN
		padded_allocation = ll_aligned_malloc(size_with_reserve, ALIGNMENT);
#else
		posix_memalign(&padded_allocation, ALIGNMENT, size_with_reserve);
#endif
	}
	return (char*)padded_allocation + aligned_reserve;
}

template<size_t ALIGNMENT, size_t RESERVE>
void deallocAligned(void* ptr)
{
	const size_t aligned_reserve = (RESERVE / ALIGNMENT) 
		+ ((RESERVE % ALIGNMENT) ? ALIGNMENT : 0);

	void* original_allocation = (char*)ptr - aligned_reserve;

	if (ALIGNMENT <= LL_DEFAULT_HEAP_ALIGN)
	{
		free(original_allocation);
	}
	else
	{
#if LL_WINDOWS
		_aligned_free(original_allocation);
#elif LL_DARWIN
		ll_aligned_free(original_allocation);
#else
		free(original_allocation);		
#endif
	}	
}

template<typename DERIVED, size_t ALIGNMENT = LL_DEFAULT_HEAP_ALIGN>
class MemTrackable
{
	template<typename TRACKED, typename TRACKED_IS_TRACKER>
	struct TrackMemImpl;

	typedef MemTrackable<DERIVED> mem_trackable_t;

public:
	typedef void mem_trackable_tag_t;

	~MemTrackable()
	{
		memDisclaim(mMemFootprint);
	}

	void* operator new(size_t size) 
	{
		MemStatAccumulator* accumulator = DERIVED::sMemStat.getPrimaryAccumulator();
		if (accumulator)
		{
			accumulator->mSize += size;
			accumulator->mAllocatedCount++;
		}

		// reserve 4 bytes for allocation size (and preserving requested alignment)
		void* allocation = allocAligned<ALIGNMENT, sizeof(size_t)>(size);
		((size_t*)allocation)[-1] = size;

		return allocation;
	}

	void operator delete(void* ptr)
	{
		size_t allocation_size = ((size_t*)ptr)[-1];
		MemStatAccumulator* accumulator = DERIVED::sMemStat.getPrimaryAccumulator();
		if (accumulator)
		{
			accumulator->mSize -= allocation_size;
			accumulator->mAllocatedCount--;
			accumulator->mDeallocatedCount++;
		}
		deallocAligned<ALIGNMENT, sizeof(size_t)>(ptr);
	}

	void *operator new [](size_t size)
	{
		MemStatAccumulator* accumulator = DERIVED::sMemStat.getPrimaryAccumulator();
		if (accumulator)
		{
			accumulator->mSize += size;
			accumulator->mAllocatedCount++;
		}

		// reserve 4 bytes for allocation size (and preserving requested alignment)
		void* allocation = allocAligned<ALIGNMENT, sizeof(size_t)>(size);
		((size_t*)allocation)[-1] = size;

		return allocation;
	}

	void operator delete[](void* ptr)
	{
		size_t* allocation_size = (size_t*)((char*)ptr - 8);
		MemStatAccumulator* accumulator = DERIVED::sMemStat.getPrimaryAccumulator();
		if (accumulator)
		{
			accumulator->mSize -= *allocation_size;
			accumulator->mAllocatedCount--;
			accumulator->mDeallocatedCount++;
		}
		deallocAligned<ALIGNMENT, sizeof(size_t)>(ptr);
	}

	// claim memory associated with other objects/data as our own, adding to our calculated footprint
	template<typename CLAIM_T>
	CLAIM_T& memClaim(CLAIM_T& value)
	{
		TrackMemImpl<CLAIM_T>::claim(*this, value);
		return value;
	}

	template<typename CLAIM_T>
	const CLAIM_T& memClaim(const CLAIM_T& value)
	{
		TrackMemImpl<CLAIM_T>::claim(*this, value);
		return value;
	}


	void memClaim(size_t size)
	{
		MemStatAccumulator* accumulator = DERIVED::sMemStat.getPrimaryAccumulator();
		mMemFootprint += size;
		if (accumulator)
		{
			accumulator->mSize += size;
		}
	}

	// remove memory we had claimed from our calculated footprint
	template<typename CLAIM_T>
	CLAIM_T& memDisclaim(CLAIM_T& value)
	{
		TrackMemImpl<CLAIM_T>::disclaim(*this, value);
		return value;
	}

	template<typename CLAIM_T>
	const CLAIM_T& memDisclaim(const CLAIM_T& value)
	{
		TrackMemImpl<CLAIM_T>::disclaim(*this, value);
		return value;
	}

	void memDisclaim(size_t size)
	{
		MemStatAccumulator* accumulator = DERIVED::sMemStat.getPrimaryAccumulator();
		if (accumulator)
		{
			accumulator->mSize -= size;
		}
		mMemFootprint -= size;
	}

private:
	size_t mMemFootprint;



	template<typename TRACKED, typename TRACKED_IS_TRACKER = void>
	struct TrackMemImpl
	{
		static void claim(mem_trackable_t& tracker, const TRACKED& tracked)
		{
			MemStatAccumulator* accumulator = DERIVED::sMemStat.getPrimaryAccumulator();
			if (accumulator)
			{
				size_t footprint = MemFootprint<TRACKED>::measure(tracked);
				accumulator->mSize += footprint;
				tracker.mMemFootprint += footprint;
			}
		}

		static void disclaim(mem_trackable_t& tracker, const TRACKED& tracked)
		{
			MemStatAccumulator* accumulator = DERIVED::sMemStat.getPrimaryAccumulator();
			if (accumulator)
			{
				size_t footprint = MemFootprint<TRACKED>::measure(tracked);
				accumulator->mSize -= footprint;
				tracker.mMemFootprint -= footprint;
			}
		}
	};

	template<typename TRACKED>
	struct TrackMemImpl<TRACKED, typename TRACKED::mem_trackable_tag_t>
	{
		static void claim(mem_trackable_t& tracker, TRACKED& tracked)
		{
			MemStatAccumulator* accumulator = DERIVED::sMemStat.getPrimaryAccumulator();
			if (accumulator)
			{
				accumulator->mChildSize += MemFootprint<TRACKED>::measure(tracked);
			}
		}

		static void disclaim(mem_trackable_t& tracker, TRACKED& tracked)
		{
			MemStatAccumulator* accumulator = DERIVED::sMemStat.getPrimaryAccumulator();
			if (accumulator)
			{
				accumulator->mChildSize -= MemFootprint<TRACKED>::measure(tracked);
			}
		}
	};
};

}
#endif // LL_LLTRACE_H