7
7
8
8
namespace BenchmarkDotNet . Disassemblers
9
9
{
10
- internal static class SourceCodeProvider
10
+ internal class SourceCodeProvider : IDisposable
11
11
{
12
- private static readonly Dictionary < SourceFile , string [ ] > SourceFileCache = new Dictionary < SourceFile , string [ ] > ( ) ;
13
- private static readonly Dictionary < SourceFile , string > SourceFilePathsCache = new Dictionary < SourceFile , string > ( ) ;
12
+ private readonly Dictionary < SourceFile , string [ ] > sourceFileCache = new Dictionary < SourceFile , string [ ] > ( ) ;
13
+ private readonly Dictionary < SourceFile , string > sourceFilePathsCache = new Dictionary < SourceFile , string > ( ) ;
14
+ private readonly Dictionary < PdbInfo , ManagedSymbolModule > pdbReaders = new Dictionary < PdbInfo , ManagedSymbolModule > ( ) ;
15
+ private readonly SymbolReader symbolReader = new SymbolReader ( TextWriter . Null ) { SymbolPath = SymbolPath . MicrosoftSymbolServerPath } ;
14
16
15
- internal static IEnumerable < Sharp > GetSource ( ClrMethod method , ILToNativeMap map )
17
+ public void Dispose ( )
16
18
{
17
- var sourceLocation = method . GetSourceLocation ( map . ILOffset ) ;
19
+ symbolReader . Dispose ( ) ;
20
+ }
21
+
22
+ internal IEnumerable < Sharp > GetSource ( ClrMethod method , ILToNativeMap map )
23
+ {
24
+ var sourceLocation = GetSourceLocation ( method , map . ILOffset ) ;
18
25
if ( sourceLocation == null )
19
26
yield break ;
20
27
@@ -39,12 +46,12 @@ internal static IEnumerable<Sharp> GetSource(ClrMethod method, ILToNativeMap map
39
46
}
40
47
}
41
48
42
- private static string GetFilePath ( SourceFile sourceFile )
43
- => SourceFilePathsCache . TryGetValue ( sourceFile , out string filePath ) ? filePath : sourceFile . Url ;
49
+ private string GetFilePath ( SourceFile sourceFile )
50
+ => sourceFilePathsCache . TryGetValue ( sourceFile , out string filePath ) ? filePath : sourceFile . Url ;
44
51
45
- private static string ReadSourceLine ( SourceFile file , int line )
52
+ private string ReadSourceLine ( SourceFile file , int line )
46
53
{
47
- if ( ! SourceFileCache . TryGetValue ( file , out string [ ] contents ) )
54
+ if ( ! sourceFileCache . TryGetValue ( file , out string [ ] contents ) )
48
55
{
49
56
// GetSourceFile method returns path when file is stored on the same machine
50
57
// otherwise it downloads it from the Symbol Server and returns the source code ;)
@@ -56,14 +63,14 @@ private static string ReadSourceLine(SourceFile file, int line)
56
63
if ( File . Exists ( wholeFileOrJustPath ) )
57
64
{
58
65
contents = File . ReadAllLines ( wholeFileOrJustPath ) ;
59
- SourceFilePathsCache . Add ( file , wholeFileOrJustPath ) ;
66
+ sourceFilePathsCache . Add ( file , wholeFileOrJustPath ) ;
60
67
}
61
68
else
62
69
{
63
70
contents = wholeFileOrJustPath . Split ( new string [ ] { Environment . NewLine } , StringSplitOptions . None ) ;
64
71
}
65
72
66
- SourceFileCache . Add ( file , contents ) ;
73
+ sourceFileCache . Add ( file , contents ) ;
67
74
}
68
75
69
76
return line - 1 < contents . Length
@@ -99,17 +106,8 @@ private static string GetSmartPointer(string sourceLine, int? start, int? end)
99
106
100
107
return new string ( prefix ) ;
101
108
}
102
- }
103
-
104
- internal static class ClrSourceExtensions
105
- {
106
- // TODO Not sure we want this to be a shared dictionary, especially without
107
- // any synchronization. Probably want to put this hanging off the Context
108
- // somewhere, or inside SymbolCache.
109
- private static readonly Dictionary < PdbInfo , ManagedSymbolModule > s_pdbReaders = new Dictionary < PdbInfo , ManagedSymbolModule > ( ) ;
110
- private static readonly SymbolReader symbolReader = new SymbolReader ( TextWriter . Null ) { SymbolPath = SymbolPath . MicrosoftSymbolServerPath } ;
111
109
112
- internal static SourceLocation GetSourceLocation ( this ClrMethod method , int ilOffset )
110
+ internal SourceLocation GetSourceLocation ( ClrMethod method , int ilOffset )
113
111
{
114
112
var reader = GetReaderForMethod ( method ) ;
115
113
if ( reader == null )
@@ -118,7 +116,7 @@ internal static SourceLocation GetSourceLocation(this ClrMethod method, int ilOf
118
116
return reader . SourceLocationForManagedCode ( ( uint ) method . MetadataToken , ilOffset ) ;
119
117
}
120
118
121
- internal static SourceLocation GetSourceLocation ( this ClrStackFrame frame )
119
+ internal SourceLocation GetSourceLocation ( ClrStackFrame frame )
122
120
{
123
121
var reader = GetReaderForMethod ( frame . Method ) ;
124
122
if ( reader == null )
@@ -145,15 +143,15 @@ private static int FindIlOffset(ClrStackFrame frame)
145
143
return last ;
146
144
}
147
145
148
- private static ManagedSymbolModule GetReaderForMethod ( ClrMethod method )
146
+ private ManagedSymbolModule GetReaderForMethod ( ClrMethod method )
149
147
{
150
148
ClrModule module = method ? . Type ? . Module ;
151
149
PdbInfo info = module ? . Pdb ;
152
150
153
151
ManagedSymbolModule reader = null ;
154
152
if ( info != null )
155
153
{
156
- if ( ! s_pdbReaders . TryGetValue ( info , out reader ) )
154
+ if ( ! pdbReaders . TryGetValue ( info , out reader ) )
157
155
{
158
156
string pdbPath = info . Path ;
159
157
if ( pdbPath != null )
@@ -173,7 +171,7 @@ private static ManagedSymbolModule GetReaderForMethod(ClrMethod method)
173
171
}
174
172
}
175
173
176
- s_pdbReaders [ info ] = reader ;
174
+ pdbReaders [ info ] = reader ;
177
175
}
178
176
}
179
177
0 commit comments