-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
metamodelrw.cpp
7695 lines (6840 loc) · 257 KB
/
metamodelrw.cpp
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.
//*****************************************************************************
// MetaModelRW.cpp
//
//
// Implementation for the Read/Write MiniMD code.
//
//*****************************************************************************
#include "stdafx.h"
#include <limits.h>
#include <posterror.h>
#include <metamodelrw.h>
#include <stgio.h>
#include <stgtiggerstorage.h>
#include "mdlog.h"
#include "rwutil.h"
#include "../compiler/importhelper.h"
#include "metadata.h"
#include "streamutil.h"
#ifdef _MSC_VER
#pragma intrinsic(memcpy)
#endif
//********** RidMap ***********************************************************
typedef CDynArray<RID> RIDMAP;
//********** Types. ***********************************************************
#define INDEX_ROW_COUNT_THRESHOLD 25
//********** Locals. **********************************************************
enum MetaDataSizeIndex
{
// Standard MetaData sizes (from VBA library).
MDSizeIndex_Standard = 0,
// Minimal MetaData sizes used mainly by Reflection.Emit for small assemblies (emitting 1 type per
// assembly).
// Motivated by the performance requirement in collectible types.
MDSizeIndex_Minimal = 1,
MDSizeIndex_Count
}; // enum MetaDataSizeIndex
// Gets index of MetaData sizes used to access code:g_PoolSizeInfo, code:g_HashSize and code:g_TblSizeInfo.
static
enum MetaDataSizeIndex
GetMetaDataSizeIndex(const OptionValue *pOptionValue)
{
if (pOptionValue->m_InitialSize == MDInitialSizeMinimal)
{
return MDSizeIndex_Minimal;
}
_ASSERTE(pOptionValue->m_InitialSize == MDInitialSizeDefault);
return MDSizeIndex_Standard;
} // GetSizeHint
#define IX_STRING_POOL 0
#define IX_US_BLOB_POOL 1
#define IX_GUID_POOL 2
#define IX_BLOB_POOL 3
static
const ULONG
g_PoolSizeInfo[MDSizeIndex_Count][4][2] =
{
{ // Standard pool sizes { Size in bytes, Number of buckets in hash } (code:MDSizeIndex_Standard).
{20000, 449}, // Strings
{5000, 150}, // User literal string blobs
{256, 16}, // Guids
{20000, 449} // Blobs
},
{ // Minimal pool sizes { Size in bytes, Number of buckets in hash } (code:MDSizeIndex_Minimal).
{300, 10}, // Strings
{50, 5}, // User literal string blobs
{16, 3}, // Guids
{200, 10} // Blobs
}
};
static
const ULONG
g_HashSize[MDSizeIndex_Count] =
{
257, // Standard MetaData size (code:MDSizeIndex_Standard).
50 // Minimal MetaData size (code:MDSizeIndex_Minimal).
};
static
const ULONG
g_TblSizeInfo[MDSizeIndex_Count][TBL_COUNT] =
{
// Standard table sizes (code:MDSizeIndex_Standard).
{
1, // Module
90, // TypeRef
65, // TypeDef
0, // FieldPtr
400, // Field
0, // MethodPtr
625, // Method
0, // ParamPtr
1200, // Param
6, // InterfaceImpl
500, // MemberRef
400, // Constant
650, // CustomAttribute
0, // FieldMarshal
0, // DeclSecurity
0, // ClassLayout
0, // FieldLayout
175, // StandAloneSig
0, // EventMap
0, // EventPtr
0, // Event
5, // PropertyMap
0, // PropertyPtr
25, // Property
45, // MethodSemantics
20, // MethodImpl
0, // ModuleRef
0, // TypeSpec
0, // ImplMap
0, // FieldRVA
0, // ENCLog
0, // ENCMap
0, // Assembly
0, // AssemblyProcessor
0, // AssemblyOS
0, // AssemblyRef
0, // AssemblyRefProcessor
0, // AssemblyRefOS
0, // File
0, // ExportedType
0, // ManifestResource
0, // NestedClass
0, // GenericParam
0, // MethodSpec
0, // GenericParamConstraint
#ifdef FEATURE_METADATA_EMIT_PORTABLE_PDB
/* Dummy tables to fill the gap to 0x30 */
0, // Dummy1
0, // Dummy2
0, // Dummy3
/* Actual portable PDB tables */
0, // Document
0, // MethodDebugInformation
0, // LocalScope
0, // LocalVariable
0, // LocalConstant
0, // ImportScope
// TODO:
// 0, // StateMachineMethod
// 0, // CustomDebugInformation
#endif // FEATURE_METADATA_EMIT_PORTABLE_PDB
},
// Minimal table sizes (code:MDSizeIndex_Minimal).
{
1, // Module
2, // TypeRef
2, // TypeDef
0, // FieldPtr
2, // Field
0, // MethodPtr
2, // Method
0, // ParamPtr
0, // Param
0, // InterfaceImpl
1, // MemberRef
0, // Constant
0, // CustomAttribute
0, // FieldMarshal
0, // DeclSecurity
0, // ClassLayout
0, // FieldLayout
0, // StandAloneSig
0, // EventMap
0, // EventPtr
0, // Event
0, // PropertyMap
0, // PropertyPtr
0, // Property
0, // MethodSemantics
0, // MethodImpl
0, // ModuleRef
0, // TypeSpec
0, // ImplMap
0, // FieldRVA
0, // ENCLog
0, // ENCMap
1, // Assembly
0, // AssemblyProcessor
0, // AssemblyOS
1, // AssemblyRef
0, // AssemblyRefProcessor
0, // AssemblyRefOS
0, // File
0, // ExportedType
0, // ManifestResource
0, // NestedClass
0, // GenericParam
0, // MethodSpec
0, // GenericParamConstraint
#ifdef FEATURE_METADATA_EMIT_PORTABLE_PDB
/* Dummy tables to fill the gap to 0x30 */
0, // Dummy1
0, // Dummy2
0, // Dummy3
/* Actual portable PDB tables */
0, // Document
0, // MethodDebugInformation
0, // LocalScope
0, // LocalVariable
0, // LocalConstant
0, // ImportScope
// TODO:
// 0, // StateMachineMethod
// 0, // CustomDebugInformation
#endif // FEATURE_METADATA_EMIT_PORTABLE_PDB
}
}; // g_TblSizeInfo
struct TblIndex
{
ULONG m_iName; // Name column.
ULONG m_iParent; // Parent column, if any.
ULONG m_Token; // Token of the table.
};
// Table to drive generic named-item indexing.
const TblIndex g_TblIndex[TBL_COUNT] =
{
{(ULONG) -1, (ULONG) -1, mdtModule}, // Module
{TypeRefRec::COL_Name, (ULONG) -1, mdtTypeRef}, // TypeRef
{TypeDefRec::COL_Name, (ULONG) -1, mdtTypeDef}, // TypeDef
{(ULONG) -1, (ULONG) -1, (ULONG) -1}, // FieldPtr
{(ULONG) -1, (ULONG) -1, mdtFieldDef}, // Field
{(ULONG) -1, (ULONG) -1, (ULONG) -1}, // MethodPtr
{(ULONG) -1, (ULONG) -1, mdtMethodDef}, // Method
{(ULONG) -1, (ULONG) -1, (ULONG) -1}, // ParamPtr
{(ULONG) -1, (ULONG) -1, mdtParamDef}, // Param
{(ULONG) -1, (ULONG) -1, mdtInterfaceImpl}, // InterfaceImpl
{MemberRefRec::COL_Name, MemberRefRec::COL_Class, mdtMemberRef}, // MemberRef
{(ULONG) -1, (ULONG) -1, (ULONG) -1}, // Constant
{(ULONG) -1, (ULONG) -1, mdtCustomAttribute},// CustomAttribute
{(ULONG) -1, (ULONG) -1, (ULONG) -1}, // FieldMarshal
{(ULONG) -1, (ULONG) -1, mdtPermission}, // DeclSecurity
{(ULONG) -1, (ULONG) -1, (ULONG) -1}, // ClassLayout
{(ULONG) -1, (ULONG) -1, (ULONG) -1}, // FieldLayout
{(ULONG) -1, (ULONG) -1, mdtSignature}, // StandAloneSig
{(ULONG) -1, (ULONG) -1, (ULONG) -1}, // EventMap
{(ULONG) -1, (ULONG) -1, (ULONG) -1}, // EventPtr
{(ULONG) -1, (ULONG) -1, mdtEvent}, // Event
{(ULONG) -1, (ULONG) -1, (ULONG) -1}, // PropertyMap
{(ULONG) -1, (ULONG) -1, (ULONG) -1}, // PropertyPtr
{(ULONG) -1, (ULONG) -1, mdtProperty}, // Property
{(ULONG) -1, (ULONG) -1, (ULONG) -1}, // MethodSemantics
{(ULONG) -1, (ULONG) -1, mdtMethodImpl}, // MethodImpl
{(ULONG) -1, (ULONG) -1, mdtModuleRef}, // ModuleRef
{(ULONG) -1, (ULONG) -1, mdtTypeSpec}, // TypeSpec
{(ULONG) -1, (ULONG) -1, (ULONG) -1}, // ImplMap <TODO>@FUTURE: Check that these are the right entries here.</TODO>
{(ULONG) -1, (ULONG) -1, (ULONG) -1}, // FieldRVA <TODO>@FUTURE: Check that these are the right entries here.</TODO>
{(ULONG) -1, (ULONG) -1, (ULONG) -1}, // ENCLog
{(ULONG) -1, (ULONG) -1, (ULONG) -1}, // ENCMap
{(ULONG) -1, (ULONG) -1, mdtAssembly}, // Assembly <TODO>@FUTURE: Update with the right number.</TODO>
{(ULONG) -1, (ULONG) -1, (ULONG) -1}, // AssemblyProcessor <TODO>@FUTURE: Update with the right number.</TODO>
{(ULONG) -1, (ULONG) -1, (ULONG) -1}, // AssemblyOS <TODO>@FUTURE: Update with the right number.</TODO>
{(ULONG) -1, (ULONG) -1, mdtAssemblyRef}, // AssemblyRef <TODO>@FUTURE: Update with the right number.</TODO>
{(ULONG) -1, (ULONG) -1, (ULONG) -1}, // AssemblyRefProcessor <TODO>@FUTURE: Update with the right number.</TODO>
{(ULONG) -1, (ULONG) -1, (ULONG) -1}, // AssemblyRefOS <TODO>@FUTURE: Update with the right number.</TODO>
{(ULONG) -1, (ULONG) -1, mdtFile}, // File <TODO>@FUTURE: Update with the right number.</TODO>
{(ULONG) -1, (ULONG) -1, mdtExportedType}, // ExportedType <TODO>@FUTURE: Update with the right number.</TODO>
{(ULONG) -1, (ULONG) -1, mdtManifestResource},// ManifestResource <TODO>@FUTURE: Update with the right number.</TODO>
{(ULONG) -1, (ULONG) -1, mdtNestedClass}, // NestedClass
{(ULONG) -1, (ULONG) -1, mdtGenericParam}, // GenericParam
{(ULONG) -1, (ULONG) -1, mdtMethodSpec}, // MethodSpec
{(ULONG) -1, (ULONG) -1, mdtGenericParamConstraint},// GenericParamConstraint
#ifdef FEATURE_METADATA_EMIT_PORTABLE_PDB
/* Dummy tables to fill the gap to 0x30 */
{(ULONG)-1, (ULONG)-1, (ULONG)-1}, // Dummy1
{(ULONG)-1, (ULONG)-1, (ULONG)-1}, // Dummy2
{(ULONG)-1, (ULONG)-1, (ULONG)-1}, // Dummy3
/* Actual portable PDB tables */
{(ULONG)-1, (ULONG)-1, mdtDocument}, // Document
{(ULONG)-1, (ULONG)-1, mdtMethodDebugInformation},// MethodDebugInformation
{(ULONG)-1, (ULONG)-1, mdtLocalScope}, // LocalScope
{(ULONG)-1, (ULONG)-1, mdtLocalVariable}, // LocalVariable
{(ULONG)-1, (ULONG)-1, mdtLocalConstant}, // LocalConstant
{(ULONG) -1, (ULONG) -1, mdtImportScope}, // ImportScope
// TODO:
// {(ULONG) -1, (ULONG) -1, mdtStateMachineMethod},// StateMachineMethod
// {(ULONG) -1, (ULONG) -1, mdtCustomDebugInformation},// CustomDebugInformation
#endif // FEATURE_METADATA_EMIT_PORTABLE_PDB
};
ULONG CMiniMdRW::m_TruncatedEncTables[] =
{
TBL_ENCLog,
TBL_ENCMap,
(ULONG) -1
};
//*****************************************************************************
// Given a token type, return the table index.
//*****************************************************************************
ULONG CMiniMdRW::GetTableForToken( // Table index, or -1.
mdToken tkn) // Token to find.
{
ULONG type = TypeFromToken(tkn);
// Get the type -- if a string, no associated table.
if (type >= mdtString)
return (ULONG) -1;
// Table number is same as high-byte of token.
ULONG ixTbl = type >> 24;
// Make sure.
_ASSERTE(ixTbl < TBL_COUNT);
return ixTbl;
} // CMiniMdRW::GetTableForToken
//*****************************************************************************
// Given a Table index, return the Token type.
//*****************************************************************************
mdToken CMiniMdRW::GetTokenForTable( // Token type, or -1.
ULONG ixTbl) // Table index.
{
_ASSERTE(g_TblIndex[ixTbl].m_Token == (ixTbl<<24) || g_TblIndex[ixTbl].m_Token == (ULONG) -1);
return g_TblIndex[ixTbl].m_Token;
} // CMiniMdRW::GetTokenForTable
//*****************************************************************************
// Helper classes for sorting MiniMdRW tables.
//*****************************************************************************
class CQuickSortMiniMdRW
{
protected:
CMiniMdRW &m_MiniMd; // The MiniMd with the data.
ULONG m_ixTbl; // The table.
ULONG m_ixCol; // The column.
int m_iCount; // How many items in array.
int m_iElemSize; // Size of one element.
RIDMAP *m_pRidMap; // Rid map that need to be swapped as we swap data
bool m_bMapToken; // MapToken handling desired.
BYTE m_buf[128]; // For swapping.
HRESULT getRow(UINT32 nIndex, void **ppRecord)
{
return m_MiniMd.m_Tables[m_ixTbl].GetRecord(nIndex, reinterpret_cast<BYTE **>(ppRecord));
}
void SetSorted() { m_MiniMd.SetSorted(m_ixTbl, true); }
HRESULT PrepMapTokens()
{
HRESULT hr = S_OK;
// If remap notifications are desired, prepare to collect the info in a RIDMAP.
if (m_bMapToken)
{
_ASSERTE(m_pRidMap == NULL); // Don't call twice.
IfNullGo(m_pRidMap = new (nothrow) RIDMAP);
if (!m_pRidMap->AllocateBlock(m_iCount + 1))
{
delete m_pRidMap;
m_pRidMap = NULL;
IfFailGo(E_OUTOFMEMORY);
}
for (int i=0; i<= m_iCount; ++i)
*(m_pRidMap->Get(i)) = i;
}
ErrExit:
return hr;
} // CQuickSortMiniMdRW::PrepMapTokens
__checkReturn
HRESULT DoMapTokens()
{
HRESULT hr;
if (m_bMapToken)
{
mdToken typ = m_MiniMd.GetTokenForTable(m_ixTbl);
for (int i=1; i<=m_iCount; ++i)
{
IfFailRet(m_MiniMd.MapToken(*(m_pRidMap->Get(i)), i, typ));
}
}
return S_OK;
} // CQuickSortMiniMdRW::DoMapTokens
public:
CQuickSortMiniMdRW(
CMiniMdRW &MiniMd, // MiniMd with the data.
ULONG ixTbl, // The table.
ULONG ixCol, // The column.
bool bMapToken) // If true, MapToken handling desired.
: m_MiniMd(MiniMd),
m_ixTbl(ixTbl),
m_ixCol(ixCol),
m_pRidMap(NULL),
m_bMapToken(bMapToken)
{
m_iElemSize = m_MiniMd.m_TableDefs[m_ixTbl].m_cbRec;
_ASSERTE(m_iElemSize <= (int) sizeof(m_buf));
}
~CQuickSortMiniMdRW()
{
if (m_bMapToken)
{
if (m_pRidMap)
{
m_pRidMap->Clear();
delete m_pRidMap;
m_pRidMap = NULL;
}
m_bMapToken = false;
}
} // CQuickSortMiniMdRW::~CQuickSortMiniMdRW
// set the RidMap
void SetRidMap(RIDMAP *pRidMap) { m_pRidMap = pRidMap; }
//*****************************************************************************
// Call to sort the array.
//*****************************************************************************
HRESULT Sort()
{
HRESULT hr = S_OK;
INDEBUG(m_MiniMd.Debug_CheckIsLockedForWrite();)
_ASSERTE(m_MiniMd.IsSortable(m_ixTbl));
m_iCount = m_MiniMd.GetCountRecs(m_ixTbl);
// If remap notifications are desired, prepare to collect the info in a RIDMAP.
IfFailGo(PrepMapTokens());
// We are going to sort tables. Invalidate the hash tables
if ( m_MiniMd.m_pLookUpHashes[m_ixTbl] != NULL )
{
delete m_MiniMd.m_pLookUpHashes[m_ixTbl];
m_MiniMd.m_pLookUpHashes[m_ixTbl] = NULL;
}
IfFailGo(SortRange(1, m_iCount));
// The table is sorted until its next change.
SetSorted();
// If remap notifications were desired, send them.
IfFailGo(DoMapTokens());
ErrExit:
return hr;
} // CQuickSortMiniMdRW::Sort
//*****************************************************************************
// Call to check whether the array is sorted without altering it.
//*****************************************************************************
HRESULT CheckSortedWithNoDuplicates()
{
HRESULT hr = S_OK;
int iCount = m_MiniMd.GetCountRecs(m_ixTbl);
int nResult;
m_MiniMd.SetSorted(m_ixTbl, false);
for (int i = 1; i < iCount; i++)
{
IfFailGo(Compare(i, i+1, &nResult));
if (nResult >= 0)
{
return S_OK;
}
}
// The table is sorted until its next change.
SetSorted();
ErrExit:
return hr;
} // CQuickSortMiniMdRW::CheckSortedWithNoDuplicates
//*****************************************************************************
// Override this function to do the comparison.
//*****************************************************************************
__checkReturn
HRESULT Compare(
int iLeft, // First item to compare.
int iRight, // Second item to compare.
int *pnResult) // -1, 0, or 1
{
HRESULT hr;
void *pLeft;
void *pRight;
IfFailRet(getRow(iLeft, &pLeft));
IfFailRet(getRow(iRight, &pRight));
ULONG ulLeft = m_MiniMd.GetCol(m_ixTbl, m_ixCol, pLeft);
ULONG ulRight = m_MiniMd.GetCol(m_ixTbl, m_ixCol, pRight);
if (ulLeft < ulRight)
{
*pnResult = -1;
return S_OK;
}
if (ulLeft == ulRight)
{
*pnResult = 0;
return S_OK;
}
*pnResult = 1;
return S_OK;
} // CQuickSortMiniMdRW::Compare
private:
__checkReturn
HRESULT SortRange(
int iLeft,
int iRight)
{
HRESULT hr;
int iLast;
int nResult;
while (true)
{
// if less than two elements you're done.
if (iLeft >= iRight)
{
return S_OK;
}
// The mid-element is the pivot, move it to the left.
IfFailRet(Compare(iLeft, (iLeft+iRight)/2, &nResult));
if (nResult != 0)
{
IfFailRet(Swap(iLeft, (iLeft+iRight)/2));
}
iLast = iLeft;
// move everything that is smaller than the pivot to the left.
for (int i = iLeft+1; i <= iRight; i++)
{
IfFailRet(Compare(i, iLeft, &nResult));
if (nResult < 0)
{
IfFailRet(Swap(i, ++iLast));
}
}
// Put the pivot to the point where it is in between smaller and larger elements.
IfFailRet(Compare(iLeft, iLast, &nResult));
if (nResult != 0)
{
IfFailRet(Swap(iLeft, iLast));
}
// Sort each partition.
int iLeftLast = iLast - 1;
int iRightFirst = iLast + 1;
if (iLeftLast - iLeft < iRight - iRightFirst)
{ // Left partition is smaller, sort it recursively
IfFailRet(SortRange(iLeft, iLeftLast));
// Tail call to sort the right (bigger) partition
iLeft = iRightFirst;
//iRight = iRight;
continue;
}
else
{ // Right partition is smaller, sort it recursively
IfFailRet(SortRange(iRightFirst, iRight));
// Tail call to sort the left (bigger) partition
//iLeft = iLeft;
iRight = iLeftLast;
continue;
}
}
} // CQuickSortMiniMdRW::SortRange
protected:
__checkReturn
inline HRESULT Swap(
int iFirst,
int iSecond)
{
HRESULT hr;
void *pFirst;
void *pSecond;
if (iFirst == iSecond)
{
return S_OK;
}
PREFAST_ASSUME_MSG(m_iElemSize <= (int) sizeof(m_buf), "The MetaData table row has to fit into buffer for swapping.");
IfFailRet(getRow(iFirst, &pFirst));
IfFailRet(getRow(iSecond, &pSecond));
memcpy(m_buf, pFirst, m_iElemSize);
memcpy(pFirst, pSecond, m_iElemSize);
memcpy(pSecond, m_buf, m_iElemSize);
if (m_pRidMap != NULL)
{
RID ridTemp;
ridTemp = *(m_pRidMap->Get(iFirst));
*(m_pRidMap->Get(iFirst)) = *(m_pRidMap->Get(iSecond));
*(m_pRidMap->Get(iSecond)) = ridTemp;
}
return S_OK;
} // CQuickSortMiniMdRW::Swap
}; // class CQuickSortMiniMdRW
class CStableSortMiniMdRW : public CQuickSortMiniMdRW
{
public:
CStableSortMiniMdRW(
CMiniMdRW &MiniMd, // MiniMd with the data.
ULONG ixTbl, // The table.
ULONG ixCol, // The column.
bool bMapToken) // Is MapToken handling desired.
: CQuickSortMiniMdRW(MiniMd, ixTbl, ixCol, bMapToken)
{}
//*****************************************************************************
// Call to sort the array.
//*****************************************************************************
__checkReturn
HRESULT Sort()
{
int i; // Outer loop counter.
int j; // Inner loop counter.
int bSwap; // Early out.
HRESULT hr = S_OK;
int nResult;
_ASSERTE(m_MiniMd.IsSortable(m_ixTbl));
m_iCount = m_MiniMd.GetCountRecs(m_ixTbl);
// If remap notifications are desired, prepare to collect the info in a RIDMAP.
IfFailGo(PrepMapTokens());
for (i=m_iCount; i>1; --i)
{
bSwap = 0;
for (j=1; j<i; ++j)
{
IfFailGo(Compare(j, j+1, &nResult));
if (nResult > 0)
{
IfFailGo(Swap(j, j+1));
bSwap = 1;
}
}
// If made a full pass w/o swaps, done.
if (!bSwap)
break;
}
// The table is sorted until its next change.
SetSorted();
// If remap notifications were desired, send them.
IfFailGo(DoMapTokens());
ErrExit:
return hr;
} // CStableSortMiniMdRW::Sort
}; // class CStableSortMiniMdRW
//-------------------------------------------------------------------------
#define SORTER(tbl,key) CQuickSortMiniMdRW sort##tbl(*this, TBL_##tbl, tbl##Rec::COL_##key, false);
#define SORTER_WITHREMAP(tbl,key) CQuickSortMiniMdRW sort##tbl(*this, TBL_##tbl, tbl##Rec::COL_##key, true);
#define STABLESORTER(tbl,key) CStableSortMiniMdRW sort##tbl(*this, TBL_##tbl, tbl##Rec::COL_##key, false);
#define STABLESORTER_WITHREMAP(tbl,key) CStableSortMiniMdRW sort##tbl(*this, TBL_##tbl, tbl##Rec::COL_##key, true);
//-------------------------------------------------------------------------
//********** Code. ************************************************************
//*****************************************************************************
// Ctor / dtor.
//*****************************************************************************
#ifdef _DEBUG
static bool bENCDeltaOnly = false;
#endif
CMiniMdRW::CMiniMdRW()
: m_pMemberRefHash(0),
m_pMemberDefHash(0),
m_pNamedItemHash(0),
m_pHandler(0),
m_cbSaveSize(0),
m_fIsReadOnly(false),
m_bPreSaveDone(false),
m_bPostGSSMod(false),
m_pMethodMap(0),
m_pFieldMap(0),
m_pPropertyMap(0),
m_pEventMap(0),
m_pParamMap(0),
m_pFilterTable(0),
m_pHostFilter(0),
m_pTokenRemapManager(0),
m_fMinimalDelta(FALSE),
m_rENCRecs(0)
{
#ifdef _DEBUG
if (CLRConfig::GetConfigValue(CLRConfig::INTERNAL_MD_EncDelta))
{
bENCDeltaOnly = true;
}
if (CLRConfig::GetConfigValue(CLRConfig::INTERNAL_MD_MiniMDBreak))
{
_ASSERTE(!"CMiniMdRW::CMiniMdRW()");
}
#endif // _DEBUG
ZeroMemory(&m_OptionValue, sizeof(OptionValue));
// initialize the embedded lookuptable struct. Further initialization, after constructor.
for (ULONG ixTbl=0; ixTbl<TBL_COUNT; ++ixTbl)
{
m_pVS[ixTbl] = 0;
m_pLookUpHashes[ixTbl] = 0;
}
// Assume that we can sort tables as needed.
memset(m_bSortable, 1, sizeof(m_bSortable));
// Initialize the global array of Ptr table indices.
g_PtrTableIxs[TBL_Field].m_ixtbl = TBL_FieldPtr;
g_PtrTableIxs[TBL_Field].m_ixcol = FieldPtrRec::COL_Field;
g_PtrTableIxs[TBL_Method].m_ixtbl = TBL_MethodPtr;
g_PtrTableIxs[TBL_Method].m_ixcol = MethodPtrRec::COL_Method;
g_PtrTableIxs[TBL_Param].m_ixtbl = TBL_ParamPtr;
g_PtrTableIxs[TBL_Param].m_ixcol = ParamPtrRec::COL_Param;
g_PtrTableIxs[TBL_Property].m_ixtbl = TBL_PropertyPtr;
g_PtrTableIxs[TBL_Property].m_ixcol = PropertyPtrRec::COL_Property;
g_PtrTableIxs[TBL_Event].m_ixtbl = TBL_EventPtr;
g_PtrTableIxs[TBL_Event].m_ixcol = EventPtrRec::COL_Event;
// AUTO_GROW initialization
m_maxRid = m_maxIx = 0;
m_limIx = USHRT_MAX >> 1;
m_limRid = USHRT_MAX >> AUTO_GROW_CODED_TOKEN_PADDING;
m_eGrow = eg_ok;
#ifdef _DEBUG
{
ULONG iMax, iCdTkn;
for (iMax=0, iCdTkn=0; iCdTkn<CDTKN_COUNT; ++iCdTkn)
{
CCodedTokenDef const *pCTD = &g_CodedTokens[iCdTkn];
if (pCTD->m_cTokens > iMax)
iMax = pCTD->m_cTokens;
}
// If assert fires, change define for AUTO_GROW_CODED_TOKEN_PADDING.
_ASSERTE(CMiniMdRW::m_cb[iMax] == AUTO_GROW_CODED_TOKEN_PADDING);
}
dbg_m_pLock = NULL;
#endif //_DEBUG
} // CMiniMdRW::CMiniMdRW
CMiniMdRW::~CMiniMdRW()
{
// Un-initialize the embedded lookuptable struct
for (ULONG ixTbl=0; ixTbl<TBL_COUNT; ++ixTbl)
{
if (m_pVS[ixTbl])
{
m_pVS[ixTbl]->Uninit();
delete m_pVS[ixTbl];
}
if ( m_pLookUpHashes[ixTbl] != NULL )
delete m_pLookUpHashes[ixTbl];
}
if (m_pFilterTable)
delete m_pFilterTable;
if (m_rENCRecs)
delete [] m_rENCRecs;
if (m_pHandler)
m_pHandler->Release(), m_pHandler = 0;
if (m_pHostFilter)
m_pHostFilter->Release();
if (m_pMemberRefHash)
delete m_pMemberRefHash;
if (m_pMemberDefHash)
delete m_pMemberDefHash;
if (m_pNamedItemHash)
delete m_pNamedItemHash;
if (m_pMethodMap)
delete m_pMethodMap;
if (m_pFieldMap)
delete m_pFieldMap;
if (m_pPropertyMap)
delete m_pPropertyMap;
if (m_pEventMap)
delete m_pEventMap;
if (m_pParamMap)
delete m_pParamMap;
if (m_pTokenRemapManager)
delete m_pTokenRemapManager;
} // CMiniMdRW::~CMiniMdRW
//*****************************************************************************
// return all found CAs in an enumerator
//*****************************************************************************
__checkReturn
HRESULT
CMiniMdRW::CommonEnumCustomAttributeByName(
mdToken tkObj, // [IN] Object with Custom Attribute.
LPCUTF8 szName, // [IN] Name of desired Custom Attribute.
bool fStopAtFirstFind, // [IN] just find the first one
HENUMInternal *phEnum) // enumerator to fill up
{
HRESULT hr = S_OK;
HRESULT hrRet = S_FALSE; // Assume that we won't find any
RID ridStart, ridEnd; // Loop start and endpoints.
CLookUpHash *pHashTable = m_pLookUpHashes[TBL_CustomAttribute];
_ASSERTE(phEnum != NULL);
HENUMInternal::ZeroEnum(phEnum);
HENUMInternal::InitDynamicArrayEnum(phEnum);
phEnum->m_tkKind = mdtCustomAttribute;
if (pHashTable)
{
// table is not sorted and hash is not built so we have to create a dynamic array
// create the dynamic enumerator.
TOKENHASHENTRY *p;
ULONG iHash;
int pos;
// Hash the data.
iHash = HashCustomAttribute(tkObj);
// Go through every entry in the hash chain looking for ours.
for (p = pHashTable->FindFirst(iHash, pos);
p;
p = pHashTable->FindNext(pos))
{
IfFailGo(CompareCustomAttribute( tkObj, szName, RidFromToken(p->tok)));
if (hr == S_OK)
{
hrRet = S_OK;
// If here, found a match.
IfFailGo( HENUMInternal::AddElementToEnum(
phEnum,
TokenFromRid(p->tok, mdtCustomAttribute)));
if (fStopAtFirstFind)
goto ErrExit;
}
}
}
else
{
// Get the list of custom values for the parent object.
if ( IsSorted(TBL_CustomAttribute) )
{
IfFailGo(getCustomAttributeForToken(tkObj, &ridEnd, &ridStart));
// If found none, done.
if (ridStart == 0)
goto ErrExit;
}
else
{
// linear scan of entire table.
ridStart = 1;
ridEnd = getCountCustomAttributes() + 1;
}
// Look for one with the given name.
for (; ridStart < ridEnd; ++ridStart)
{
IfFailGo(CompareCustomAttribute( tkObj, szName, ridStart));
if (hr == S_OK)
{
// If here, found a match.
hrRet = S_OK;
IfFailGo( HENUMInternal::AddElementToEnum(
phEnum,
TokenFromRid(ridStart, mdtCustomAttribute)));
if (fStopAtFirstFind)
goto ErrExit;
}
}
}
ErrExit:
if (FAILED(hr))
return hr;
return hrRet;
} // CMiniMdRW::CommonEnumCustomAttributeByName
//*****************************************************************************
// return just the blob value of the first found CA matching the query.
//*****************************************************************************
__checkReturn
HRESULT
CMiniMdRW::CommonGetCustomAttributeByNameEx( // S_OK or error.
mdToken tkObj, // [IN] Object with Custom Attribute.
LPCUTF8 szName, // [IN] Name of desired Custom Attribute.
mdCustomAttribute *ptkCA, // [OUT] put custom attribute token here
const void **ppData, // [OUT] Put pointer to data here.
ULONG *pcbData) // [OUT] Put size of data here.
{
HRESULT hr;
const void *pData;
ULONG cbData;
HENUMInternal hEnum;
mdCustomAttribute ca;
CustomAttributeRec *pRec;
hr = CommonEnumCustomAttributeByName(tkObj, szName, true, &hEnum);
if (hr != S_OK)
goto ErrExit;
if (ppData != NULL || ptkCA != NULL)
{
// now get the record out.
if (ppData == 0)
ppData = &pData;
if (pcbData == 0)
pcbData = &cbData;
if (HENUMInternal::EnumNext(&hEnum, &ca))
{
IfFailGo(GetCustomAttributeRecord(RidFromToken(ca), &pRec));
IfFailGo(getValueOfCustomAttribute(pRec, reinterpret_cast<const BYTE **>(ppData), pcbData));
if (ptkCA)
*ptkCA = ca;
}
else
{
_ASSERTE(!"Enum returned no items after EnumInit returned S_OK");
hr = S_FALSE;
}
}
ErrExit:
HENUMInternal::ClearEnum(&hEnum);
return hr;
} // CMiniMdRW::CommonGetCustomAttributeByName
//*****************************************************************************
// unmark everything in this module
//*****************************************************************************
__checkReturn
HRESULT
CMiniMdRW::UnmarkAll()
{
HRESULT hr = NOERROR;
ULONG ulSize = 0;
ULONG ixTbl;
FilterTable *pFilter;
// find the max rec count with all tables
for (ixTbl = 0; ixTbl < TBL_COUNT; ++ixTbl)
{
if (GetCountRecs(ixTbl) > ulSize)
ulSize = GetCountRecs(ixTbl);
}
IfNullGo(pFilter = GetFilterTable());
IfFailGo(pFilter->UnmarkAll(this, ulSize));
ErrExit:
return hr;
} // CMiniMdRW::UnmarkAll
//*****************************************************************************
// mark everything in this module
//*****************************************************************************
__checkReturn
HRESULT
CMiniMdRW::MarkAll()
{
HRESULT hr = NOERROR;
ULONG ulSize = 0;
ULONG ixTbl;
FilterTable *pFilter;