-
Notifications
You must be signed in to change notification settings - Fork 5k
/
Copy pathholder.h
1400 lines (1213 loc) · 39.8 KB
/
holder.h
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
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
#ifndef __HOLDER_H_
#define __HOLDER_H_
#include <wincrypt.h>
#include "cor.h"
#include "staticcontract.h"
#include "volatile.h"
#include "palclr.h"
#include <minipal/com/memory.h>
#include <utility>
#include <type_traits>
#if defined(FEATURE_COMINTEROP) && !defined(STRIKE)
#include <Activation.h>
#include <Inspectable.h>
#endif
// Note: you can't use CONTRACT's in this file. You can't use dynamic contracts because the impl of dynamic
// contracts depends on holders. You can't use static contracts because they include the function name as a string,
// and the template names in this file are just too long, so you get a compiler error.
//
// All the functions in this file are pretty basic, so the lack of CONTRACT's isn't a big loss.
#ifdef _MSC_VER
// Make sure we can recurse deep enough for FORCEINLINE
#pragma inline_recursion(on)
#pragma inline_depth(16)
#pragma warning(disable:4714)
#endif // _MSC_VER
//------------------------------------------------------------------------------------------------
// Declare but do not define methods that would normally be automatically generated by the
// compiler. This will produce a link-time error if they are used. This is involved in object
// initialization and operator eliding; see
// http://groups.google.com/group/comp.lang.c++/msg/3cd673ab749bed83?dmode=source
// for the intricate details.
#ifdef __GNUC__
// GCC checks accessibility of the copy ctor before performing elision optimization, so
// it must be declared as public. VC does not, so we can declare it private to give an
// earlier error message.
#define HIDE_GENERATED_METHODS(_NAME) \
public: \
_NAME(_NAME const &); \
private: \
_NAME & operator=(_NAME const &);
#else
#define HIDE_GENERATED_METHODS(_NAME) \
private: \
_NAME(_NAME const &); \
_NAME & operator=(_NAME const &);
#endif
#ifdef _DEBUG
//------------------------------------------------------------------------------------------------
// This is used to make Visual Studio autoexp.dat work sensibly with holders again.
// The problem is that certain codebases (particulary Fusion) implement key data structures
// using a class but refer to it using a holder to an interface type. This combination prevents
// autoexp rules from working as desired.
//
// Example: Take this useful autoexp rule for CAssemblyName.
//
// CAssemblyName=<_rProp._rProp[3].asStr>
//
// To get the same rule to fire when your assemblyname is wrapped in a ReleaseHolder,
// add these companion rules.
//
// HolderBase<CAssemblyName *>=<m_value->_rProp._rProp[3].asStr>
// HolderBase<IAssemblyName *>=<m_pAutoExpVisibleValue->_asCAssemblyName->_rProp._rProp[3].asStr>
//
//------------------------------------------------------------------------------------------------
struct AutoExpVisibleValue
{
private:
union
{
const void *_pPreventEmptyUnion;
};
};
#endif //_DEBUG
//-----------------------------------------------------------------------------
// Holder is the base class of all holder objects. Any backout object should derive from it.
// (Eventually some additional bookkeeping and exception handling code will be placed in this
// base class.)
//
// There are several ways to use this class:
// 1. Derive from HolderBase, and instantiate a Holder or Wrapper around your base. This is necessary
// if you need to add more state to your holder.
// 2. Instantiate the Holder template with your type and functions.
// 3. Instantiate the Wrapper template with your type and functions. The Wrapper adds some additional
// operator overloads to provide "smart pointer" like behavior
// 4. Use a prebaked Holder. This is ALWAYS the preferable strategy. It is expected that
// the general design patter is that Holders will be provided as part of a typical data abstraction.
// (See Crst for an example of this.)
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// HolderBase defines the base holder functionality. You can subtype and plug in
// a different base if you need to add more members for access during
// acquire & release
//-----------------------------------------------------------------------------
template <typename TYPE>
class HolderBase
{
friend class ClrDataAccess;
protected:
TYPE m_value;
HolderBase(TYPE value)
: m_value(value)
{
#ifdef _DEBUG
m_pAutoExpVisibleValue = (const AutoExpVisibleValue *)(&m_value);
#endif //_DEBUG
}
void DoAcquire()
{
// Insert any global or thread bookkeeping here
}
void DoRelease()
{
// Insert any global or thread bookkeeping here
}
#ifdef _DEBUG
private:
// NOT DEAD CODE: This field is not referenced in the source code but it is not a dead field. See comments above "class AutoExpVisibleValue" for why this field exists.
// Note: What we really want is for m_value and m_pAutoExpVisibleValue to be members of a union. But the compiler won't let you build that
// since it can't prove that "TYPE" doesn't have a non-trivial constructor.
const AutoExpVisibleValue *m_pAutoExpVisibleValue; // This field is to be referenced from individual autoexp.dat files, NOT from the CLR source code.
#endif //_DEBUG
}; // class HolderBase<>
#ifndef _PREFAST_ // Work around an ICE error in EspX.dll
template <typename TYPE>
BOOL CompareDefault(TYPE value, TYPE defaultValue)
{
STATIC_CONTRACT_SUPPORTS_DAC;
return value == defaultValue;
}
#else
template <typename TYPE>
BOOL CompareDefault(TYPE value, TYPE defaultValue)
{
return FALSE;
}
#endif
template <typename TYPE>
BOOL NoNull(TYPE value, TYPE defaultValue)
{
return FALSE;
}
// Used e.g. for retail version of code:SyncAccessHolder
template <typename TYPE>
class NoOpBaseHolder
{
public:
FORCEINLINE NoOpBaseHolder()
{
}
FORCEINLINE NoOpBaseHolder(TYPE value, BOOL take = TRUE)
{
}
FORCEINLINE ~NoOpBaseHolder()
{
}
FORCEINLINE void Assign(TYPE value, BOOL fTake = TRUE)
{
}
FORCEINLINE void Acquire()
{
}
FORCEINLINE void Release()
{
}
FORCEINLINE void Clear()
{
}
FORCEINLINE void SuppressRelease()
{
}
FORCEINLINE TYPE Extract()
{
}
FORCEINLINE TYPE GetValue()
{
}
FORCEINLINE BOOL IsNull() const
{
return FALSE;
}
private:
NoOpBaseHolder& operator=(NoOpBaseHolder const &);
NoOpBaseHolder(NoOpBaseHolder const &);
}; // NoOpBaseHolder<>
template
<
typename TYPE,
typename BASE,
UINT_PTR DEFAULTVALUE = 0,
BOOL IS_NULL(TYPE, TYPE) = CompareDefault<TYPE>
>
class BaseHolder : protected BASE
{
friend class ClrDataAccess;
protected:
BOOL m_acquired; // Have we acquired the resource?
static_assert(!std::is_pointer<TYPE>::value || DEFAULTVALUE == 0 || DEFAULTVALUE == UINT_PTR(-1 /*INVALID_HANDLE_VALUE*/),
"DEFAULTVALUE must be NULL for pointer holders and wrappers.");
public:
FORCEINLINE BaseHolder()
: BASE(TYPE(DEFAULTVALUE)),
m_acquired(FALSE)
{
}
FORCEINLINE BaseHolder(TYPE value)
: BASE(value),
m_acquired(FALSE)
{
if (!IsNull())
Acquire();
}
FORCEINLINE BaseHolder(TYPE value, BOOL takeOwnership)
: BASE(value),
m_acquired(FALSE)
{
if (takeOwnership)
Acquire();
}
FORCEINLINE ~BaseHolder()
{
Release();
}
// Sets the value to 'value'. Doesn't call Acquire if value is DEFAULTVALUE.
FORCEINLINE void Assign(TYPE value)
{
Assign(value, !IS_NULL(value, TYPE(DEFAULTVALUE)));
}
// Sets the value to 'value'. Doesn't call Acquire if fTake is FALSE.
FORCEINLINE void Assign(TYPE value, BOOL takeOwnership)
{
Release();
this->m_value = value;
if (takeOwnership)
Acquire();
}
FORCEINLINE void Acquire()
{
STATIC_CONTRACT_WRAPPER;
_ASSERTE(!m_acquired);
if (!IsNull())
{
this->DoAcquire();
m_acquired = TRUE;
}
}
FORCEINLINE void Release()
{
STATIC_CONTRACT_WRAPPER;
if (m_acquired)
{
_ASSERTE(!IsNull());
this->DoRelease();
m_acquired = FALSE;
}
}
FORCEINLINE void Clear()
{
STATIC_CONTRACT_WRAPPER;
Assign(TYPE(DEFAULTVALUE), FALSE);
}
FORCEINLINE void SuppressRelease()
{
STATIC_CONTRACT_LEAF;
m_acquired = FALSE;
}
FORCEINLINE TYPE Extract()
{
STATIC_CONTRACT_WRAPPER;
SuppressRelease();
return GetValue();
}
FORCEINLINE TYPE GetValue()
{
STATIC_CONTRACT_LEAF;
return this->m_value;
}
FORCEINLINE BOOL IsNull() const
{
STATIC_CONTRACT_WRAPPER;
return IS_NULL(this->m_value, TYPE(DEFAULTVALUE));
}
HIDE_GENERATED_METHODS(BaseHolder)
}; // BaseHolder<>
template <void (*ACQUIRE)(), void (*RELEASEF)()>
class StateHolder
{
private:
BOOL m_acquired; // Have we acquired the state?
public:
FORCEINLINE StateHolder(BOOL take = TRUE)
: m_acquired(FALSE)
{
STATIC_CONTRACT_WRAPPER;
if (take)
Acquire();
}
FORCEINLINE ~StateHolder()
{
STATIC_CONTRACT_WRAPPER;
Release();
}
FORCEINLINE void Acquire()
{
STATIC_CONTRACT_WRAPPER;
// Insert any global or thread bookkeeping here
_ASSERTE(!m_acquired);
ACQUIRE();
m_acquired = TRUE;
}
FORCEINLINE void Release()
{
STATIC_CONTRACT_WRAPPER;
// Insert any global or thread bookkeeping here
if (m_acquired)
{
RELEASEF();
m_acquired = FALSE;
}
}
FORCEINLINE void Clear()
{
STATIC_CONTRACT_WRAPPER;
if (m_acquired)
{
RELEASEF();
}
m_acquired = FALSE;
}
FORCEINLINE void SuppressRelease()
{
STATIC_CONTRACT_LEAF;
m_acquired = FALSE;
}
HIDE_GENERATED_METHODS(StateHolder)
}; // class StateHolder<>
// Holder for the case where the acquire function can fail.
template <typename VALUE, BOOL (*ACQUIRE)(VALUE value), void (*RELEASEF)(VALUE value)>
class ConditionalStateHolder
{
private:
VALUE m_value;
BOOL m_acquired; // Have we acquired the state?
public:
FORCEINLINE ConditionalStateHolder(VALUE value, BOOL take = TRUE)
: m_value(value), m_acquired(FALSE)
{
STATIC_CONTRACT_WRAPPER;
if (take)
Acquire();
}
FORCEINLINE ~ConditionalStateHolder()
{
STATIC_CONTRACT_WRAPPER;
Release();
}
FORCEINLINE BOOL Acquire()
{
STATIC_CONTRACT_WRAPPER;
// Insert any global or thread bookkeeping here
_ASSERTE(!m_acquired);
m_acquired = ACQUIRE(m_value);
return m_acquired;
}
FORCEINLINE void Release()
{
STATIC_CONTRACT_WRAPPER;
// Insert any global or thread bookkeeping here
if (m_acquired)
{
RELEASEF(m_value);
m_acquired = FALSE;
}
}
FORCEINLINE void Clear()
{
STATIC_CONTRACT_WRAPPER;
if (m_acquired)
{
RELEASEF(m_value);
}
m_acquired = FALSE;
}
FORCEINLINE void SuppressRelease()
{
STATIC_CONTRACT_LEAF;
m_acquired = FALSE;
}
FORCEINLINE BOOL Acquired()
{
STATIC_CONTRACT_LEAF;
return m_acquired;
}
HIDE_GENERATED_METHODS(ConditionalStateHolder)
}; // class ConditionalStateHolder<>
// Making the copy constructor private produces a warning about "can't generate copy
// constructor" on all holders (duh, that's the point.)
#ifdef _MSC_VER
#pragma warning(disable:4511)
#endif // _MSC_VER
//-----------------------------------------------------------------------------
// BaseWrapper is just Base like a Holder, but it "transparently" proxies the type it contains,
// using operator overloads. Use this when you want a holder to expose the functionality of
// the value it contains.
//-----------------------------------------------------------------------------
template <typename TYPE, typename BASE,
UINT_PTR DEFAULTVALUE = 0, BOOL IS_NULL(TYPE, TYPE) = CompareDefault<TYPE>>
class BaseWrapper : public BaseHolder<TYPE, BASE, DEFAULTVALUE, IS_NULL>
{
typedef BaseHolder<TYPE, BASE, DEFAULTVALUE, IS_NULL> BaseT;
#ifdef __GNUC__
//#pragma GCC visibility push(hidden)
#endif // __GNUC__
// This temporary object takes care of the case where we are initializing
// a holder's contents by passing it as an out parameter. The object is
// guaranteed to live longer than the call it is passed to, hence we should
// properly acquire the object on return
friend class AddressInitHolder;
class AddressInitHolder
{
protected:
BaseWrapper<TYPE,BASE,DEFAULTVALUE,IS_NULL> &m_holder;
public:
FORCEINLINE AddressInitHolder(BaseWrapper<TYPE,BASE,DEFAULTVALUE,IS_NULL> &holder)
: m_holder(holder)
{
//
// We must clear the value, to avoid the following scenario:
//
// ReleaseHolder<MyType> pMyType;
// hr = MyFunction(&pMyType, ...);
// <do something with pMyType>
// hr = MyFunction(&pMyType, ...); <- calls Release before call and Acquire on return.
//
// If the second call to MyFunction were to fail and return without updating the
// out parameter, then ~AddressInitHolder will all Acquire, which is a no-op on
// the underlying pointer but sets m_acquired to TRUE. Thus, when pMyType goes
// out of scope, ReleaseHolder will cause a double-release of pMyType.
//
// By calling Clear, then the call to Acquire in the dtor will not set m_acquired
// to true since IsNull(m_holder.m_value) will return true.
//
m_holder.Clear();
}
FORCEINLINE ~AddressInitHolder()
{
m_holder.Acquire();
}
// It's not optimal to have to declare these casting operators. But if we don't,
// people cannot cast the result of &holder to these values. (The C++ compiler won't
// automatically use the TYPE * cast as an intermediate value.) So we put them here,
// rather than forcing callers to double cast in these common cases.
FORCEINLINE operator IUnknown **()
{
IUnknown *unknown;
// Typesafe check. This will fail at compile time if
// m_holder.m_value can't be converted to an IUnknown *.
unknown = static_cast<IUnknown*>(m_holder.m_value);
// do the cast with an unsafe cast
return (IUnknown **)(&m_holder.m_value);
}
#if defined(FEATURE_COMINTEROP) && !defined(STRIKE)
FORCEINLINE operator IInspectable **()
{
IInspectable *inspectable;
// Typesafe check. This will fail at compile time if
// m_holder.m_value can't be converted to an IInspectable *.
inspectable = static_cast<IInspectable *>(m_holder.m_value);
return (IInspectable **)(&m_holder.m_value);
}
#endif // FEATURE_COMINTEROP
FORCEINLINE operator void **()
{
return (void **)(&m_holder.m_value);
}
FORCEINLINE operator void *()
{
return (void *)(&m_holder.m_value);
}
};
// Separate out method with TYPE * cast operator, since it may clash with IUnknown ** or
// void ** cast operator.
friend class TypedAddressInitHolder;
class TypedAddressInitHolder : public AddressInitHolder
{
public:
FORCEINLINE TypedAddressInitHolder(BaseWrapper & holder)
: AddressInitHolder(holder)
{
}
FORCEINLINE operator TYPE *()
{
return static_cast<TYPE *>(&this->m_holder.m_value);
}
};
#ifdef __GNUC__
//#pragma GCC visibility pop
#endif // __GNUC__
public:
FORCEINLINE BaseWrapper()
: BaseT(TYPE(DEFAULTVALUE), FALSE)
{
}
FORCEINLINE BaseWrapper(TYPE value)
: BaseT(value)
{
}
FORCEINLINE BaseWrapper(TYPE value, BOOL take)
: BaseT(value, take)
{
}
FORCEINLINE BaseWrapper& operator=(TYPE value)
{
BaseT::Assign(value);
return *this;
}
FORCEINLINE operator TYPE() const
{
return this->m_value;
}
FORCEINLINE TypedAddressInitHolder operator&()
{
return TypedAddressInitHolder(*this);
}
template <typename T>
FORCEINLINE bool operator==(T const & value) const
{
return !!(this->m_value == TYPE(value));
}
template <typename T>
FORCEINLINE bool operator!=(T const & value) const
{
return !!(this->m_value != TYPE(value));
}
// This handles the NULL value that is an int and the
// compiler doesn't want to convert int to a pointer.
FORCEINLINE bool operator==(int value) const
{
return !!(this->m_value == TYPE((void*)(SIZE_T)value));
}
FORCEINLINE bool operator!=(int value) const
{
return !!(this->m_value != TYPE((void*)(SIZE_T)value));
}
FORCEINLINE const TYPE &operator->() const
{
return this->m_value;
}
FORCEINLINE int operator!() const
{
return this->IsNull();
}
private:
HIDE_GENERATED_METHODS(BaseWrapper)
}; // class BaseWrapper<>
//-----------------------------------------------------------------------------
// Generic templates to use to wrap up acquire/release functionality for Holder
//-----------------------------------------------------------------------------
template <typename TYPE>
FORCEINLINE void DoNothing(TYPE value)
{
// @TODO: Due to prefast template problems, implementations of the DoNothing macro have been changed
// Search by prefast, and remove them when prefast is ready
}
FORCEINLINE void DoNothing()
{
}
// Prefast stuff.We should have DoNothing<type*> in the holder declaration, but currently
// prefast doesnt support, it, so im stuffing all these here so if we need to change the template you can change
// everything here. When prefast works, remove the following functions
struct ConnectionCookie;
FORCEINLINE void ConnectionCookieDoNothing(ConnectionCookie* p)
{
}
class ComCallWrapper;
FORCEINLINE void CCWHolderDoNothing(ComCallWrapper* p)
{
}
FORCEINLINE void DispParamHolderDoNothing(VARIANT* p)
{
}
FORCEINLINE void VariantPtrDoNothing(VARIANT* p)
{
}
FORCEINLINE void VariantDoNothing(VARIANT)
{
}
FORCEINLINE void ZeroDoNothing(VOID* p)
{
}
class CtxEntry;
FORCEINLINE void CtxEntryDoNothing(CtxEntry* p)
{
}
struct RCW;
FORCEINLINE void NewRCWHolderDoNothing(RCW*)
{
}
// Prefast stuff.We should have DoNothing<SafeArray*> in the holder declaration
FORCEINLINE void SafeArrayDoNothing(SAFEARRAY* p)
{
}
//-----------------------------------------------------------------------------
// Holder/Wrapper are the simplest way to define holders - they synthesizes a base class out of
// function pointers
//-----------------------------------------------------------------------------
template <typename TYPE, void (*ACQUIREF)(TYPE), void (*RELEASEF)(TYPE)>
class FunctionBase : protected HolderBase<TYPE>
{
friend class ClrDataAccess;
protected:
FORCEINLINE FunctionBase(TYPE value)
: HolderBase<TYPE>(value)
{
}
FORCEINLINE void DoAcquire()
{
ACQUIREF(this->m_value);
}
void DoRelease()
{
RELEASEF(this->m_value);
}
}; // class Function<>
template
<
typename TYPE,
void (*ACQUIREF)(TYPE),
void (*RELEASEF)(TYPE),
UINT_PTR DEFAULTVALUE = 0,
BOOL IS_NULL(TYPE, TYPE) = CompareDefault<TYPE>,
// For legacy compat (see EEJitManager::WriterLockHolder), where default ctor
// causes ACQUIREF(DEFAULTVALUE), but ACQUIREF ignores the argument and
// operates on static or global value instead.
bool DEFAULT_CTOR_ACQUIRE = true
>
class Holder : public BaseHolder<TYPE, FunctionBase<TYPE, ACQUIREF, RELEASEF>,
DEFAULTVALUE, IS_NULL>
{
typedef BaseHolder<TYPE, FunctionBase<TYPE, ACQUIREF, RELEASEF>,
DEFAULTVALUE, IS_NULL> BaseT;
public:
FORCEINLINE Holder()
: BaseT(TYPE(DEFAULTVALUE), DEFAULT_CTOR_ACQUIRE)
{
STATIC_CONTRACT_WRAPPER;
}
FORCEINLINE Holder(TYPE value)
: BaseT(value)
{
STATIC_CONTRACT_WRAPPER;
}
FORCEINLINE Holder(TYPE value, BOOL takeOwnership)
: BaseT(value, takeOwnership)
{
STATIC_CONTRACT_WRAPPER;
}
FORCEINLINE Holder& operator=(TYPE p)
{
STATIC_CONTRACT_WRAPPER;
BaseT::operator=(p);
return *this;
}
HIDE_GENERATED_METHODS(Holder)
};
//---------------------------------------------------------------------------------------
//
template
<
typename TYPE,
void (*ACQUIREF)(TYPE),
void (*RELEASEF)(TYPE),
UINT_PTR DEFAULTVALUE = 0,
BOOL IS_NULL(TYPE, TYPE) = CompareDefault<TYPE>,
// For legacy compat (see EEJitManager::WriterLockHolder), where default ctor
// causes ACQUIREF(DEFAULTVALUE), but ACQUIREF ignores the argument and
// operates on static or global value instead.
bool DEFAULT_CTOR_ACQUIRE = true
>
class Wrapper : public BaseWrapper<TYPE, FunctionBase<TYPE, ACQUIREF, RELEASEF>,
DEFAULTVALUE, IS_NULL>
{
typedef BaseWrapper<TYPE, FunctionBase<TYPE, ACQUIREF, RELEASEF>,
DEFAULTVALUE, IS_NULL> BaseT;
public:
FORCEINLINE Wrapper()
: BaseT(TYPE(DEFAULTVALUE), DEFAULT_CTOR_ACQUIRE)
{
STATIC_CONTRACT_WRAPPER;
}
FORCEINLINE Wrapper(TYPE value)
: BaseT(value)
{
STATIC_CONTRACT_WRAPPER;
}
FORCEINLINE Wrapper(TYPE value, BOOL takeOwnership)
: BaseT(value, takeOwnership)
{
STATIC_CONTRACT_WRAPPER;
}
FORCEINLINE Wrapper& operator=(TYPE const & value)
{
STATIC_CONTRACT_WRAPPER;
BaseT::operator=(value);
return *this;
}
HIDE_GENERATED_METHODS(Wrapper)
}; // Wrapper<>
//---------------------------------------------------------------------------------------
// - Cannot use the standard INDEBUG macro: holder.h is used in places where INDEBUG is defined in the nonstandard way
#if defined(_DEBUG)
#define INDEBUG_AND_WINDOWS_FOR_HOLDERS(x) x
#else
#define INDEBUG_AND_WINDOWS_FOR_HOLDERS(x)
#endif
template <typename _TYPE, void (*_RELEASEF)(_TYPE*)>
class SpecializedWrapper : public Wrapper<_TYPE*, DoNothing<_TYPE*>, _RELEASEF, 0>
{
using BaseT = Wrapper<_TYPE*, DoNothing<_TYPE*>, _RELEASEF, 0>;
public:
FORCEINLINE SpecializedWrapper() : BaseT(NULL, FALSE)
{
STATIC_CONTRACT_WRAPPER;
INDEBUG_AND_WINDOWS_FOR_HOLDERS(m_pvalue = &this->m_value;)
}
FORCEINLINE SpecializedWrapper(_TYPE* value) : BaseT(value)
{
STATIC_CONTRACT_WRAPPER;
INDEBUG_AND_WINDOWS_FOR_HOLDERS(m_pvalue = &this->m_value;)
}
FORCEINLINE SpecializedWrapper(_TYPE* value, BOOL takeOwnership) : BaseT(value, takeOwnership)
{
STATIC_CONTRACT_WRAPPER;
INDEBUG_AND_WINDOWS_FOR_HOLDERS(m_pvalue = &this->m_value;)
}
FORCEINLINE ~SpecializedWrapper()
{
}
SpecializedWrapper(SpecializedWrapper const &) = delete;
SpecializedWrapper & operator=(SpecializedWrapper const &) = delete;
FORCEINLINE SpecializedWrapper& operator=(_TYPE * value)
{
STATIC_CONTRACT_WRAPPER;
BaseT::operator=(value);
return *this;
}
FORCEINLINE SpecializedWrapper(SpecializedWrapper && other)
: BaseT(NULL, FALSE)
{
STATIC_CONTRACT_WRAPPER;
INDEBUG_AND_WINDOWS_FOR_HOLDERS(m_pvalue = &this->m_value;)
*this = std::move(other);
}
FORCEINLINE SpecializedWrapper& operator=(SpecializedWrapper && other)
{
BaseT::m_value = std::move(other.BaseT::m_value);
BaseT::m_acquired = std::move(other.BaseT::m_acquired);
other.BaseT::m_value = nullptr;
other.BaseT::m_acquired = FALSE;
return *this;
}
/* Since operator& is overloaded we need a way to get a type safe this pointer. */
FORCEINLINE SpecializedWrapper* GetAddr()
{
STATIC_CONTRACT_LEAF;
return this;
}
private:
/* m_ppValue: Do not use from source code: Only for convenient use from debugger */
/* watch windows - saves five mouseclicks when inspecting holders. */
INDEBUG_AND_WINDOWS_FOR_HOLDERS(_TYPE ** m_pvalue;)
};
//-----------------------------------------------------------------------------
// NOTE: THIS IS UNSAFE TO USE IN THE VM for interop COM objects!!
// WE DO NOT CORRECTLY CHANGE TO PREEMPTIVE MODE BEFORE CALLING RELEASE!!
// USE SafeComHolder
//
// ReleaseHolder : COM Interface holder for use outside the VM (or on well known instances
// which do not need preemptive Relesae)
//
// Usage example:
//
// {
// ReleaseHolder<IFoo> foo;
// hr = FunctionToGetRefOfFoo(&foo);
// // Note ComHolder doesn't call AddRef - it assumes you already have a ref (if non-0).
// } // foo->Release() on out of scope (WITHOUT RESPECT FOR GC MODE!!)
//
//-----------------------------------------------------------------------------
template <typename TYPE>
FORCEINLINE void DoTheRelease(TYPE *value)
{
if (value)
{
value->Release();
}
}
template<typename _TYPE>
using DoNothingHolder = SpecializedWrapper<_TYPE, DoNothing<_TYPE*>>;
#ifndef SOS_INCLUDE
template<typename _TYPE>
using ReleaseHolder = SpecializedWrapper<_TYPE, DoTheRelease<_TYPE>>;
#endif // SOS_INCLUDE
template<typename _TYPE>
using NonVMComHolder = SpecializedWrapper<_TYPE, DoTheRelease<_TYPE>>;
//-----------------------------------------------------------------------------
// StubHolder : holder for stubs
//
// Usage example:
//
// {
// StubHolder<Stub> foo;
// foo = new Stub();
// foo->AddRef();
// // Note StubHolder doesn't call AddRef for you.
// } // foo->DecRef() on out of scope
//
//-----------------------------------------------------------------------------
template<typename _TYPE>
class ExecutableWriterHolderNoLog;
class ExecutableAllocator;
template <typename TYPE, typename LOGGER=ExecutableAllocator>
FORCEINLINE void StubRelease(TYPE* value)
{
if (value)
{
#ifdef LOG_EXECUTABLE_ALLOCATOR_STATISTICS
#ifdef HOST_UNIX
LOGGER::LogUsage(__FILE__, __LINE__, __PRETTY_FUNCTION__);
#else
LOGGER::LogUsage(__FILE__, __LINE__, __FUNCTION__);
#endif
#endif // LOG_EXECUTABLE_ALLOCATOR_STATISTICS
ExecutableWriterHolderNoLog<TYPE> stubWriterHolder(value, sizeof(TYPE));
stubWriterHolder.GetRW()->DecRef();
}
}
template<typename _TYPE>
using StubHolder = SpecializedWrapper<_TYPE, StubRelease<_TYPE>>;
//-----------------------------------------------------------------------------
// CoTaskMemHolder : CoTaskMemAlloc allocated memory holder
//
// {
// CoTaskMemHolder<Foo> foo = (Foo*) CoTaskMemAlloc(sizeof(Foo));
// } // delete foo on out of scope
//-----------------------------------------------------------------------------
template <typename TYPE>
FORCEINLINE void DeleteCoTaskMem(TYPE *value)
{
if (value)
CoTaskMemFree(value);
}
template<typename _TYPE>
using CoTaskMemHolder = SpecializedWrapper<_TYPE, DeleteCoTaskMem<_TYPE>>;
//-----------------------------------------------------------------------------
// NewHolder : New'ed memory holder
//
// {
// NewHolder<Foo> foo = new Foo ();
// } // delete foo on out of scope
//-----------------------------------------------------------------------------
template <typename TYPE>
FORCEINLINE void Delete(TYPE *value)
{
STATIC_CONTRACT_LEAF;
static_assert(!std::is_same<typename std::remove_cv<TYPE>::type, WCHAR>::value,
"Must use NewArrayHolder (not NewHolder) for strings.");
static_assert(!std::is_same<typename std::remove_cv<TYPE>::type, CHAR>::value,
"Must use NewArrayHolder (not NewHolder) for strings.");
delete value;
}
template<typename _TYPE>