Skip to content

Commit e4caf1e

Browse files
author
Paulo Janotti
committed
Adding missing new source file
1 parent 69f67db commit e4caf1e

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
using System;
2+
using System.IO;
3+
using System.Threading;
4+
5+
namespace Coverlet.Core.Instrumentation
6+
{
7+
/// <summary>
8+
/// This static class will be injected on a module being instrumented in order to direct on module hits
9+
/// 'to a single location.
10+
/// </summary>
11+
/// <remarks>
12+
/// As this type is going to be customized on each module it doesn't follow typical practices regarding
13+
/// visibility of members, etc.
14+
/// </remarks>
15+
public static class ModuleTrackerTemplate
16+
{
17+
public static string HitsFilePath;
18+
19+
public readonly static int[] HitsArray;
20+
21+
static ModuleTrackerTemplate()
22+
{
23+
AppDomain.CurrentDomain.ProcessExit += new EventHandler(UnloadModule);
24+
AppDomain.CurrentDomain.DomainUnload += new EventHandler(UnloadModule);
25+
// At the end of the instrumentation of a module, the instrumenter needs to add code here
26+
// to initialize the static fields according to the values derived from the instrumentation of
27+
// the module.
28+
}
29+
30+
public static void UnloadModule(object sender, EventArgs e)
31+
{
32+
// TODO: same module can be unloaded multiple times in the same process. Need to check and handle this case.
33+
// TODO: perhaps some kind of global mutex based on the name of the modules hits file, and after the first
34+
// TODO: they update the hit count from the file already created. Something like this, (minus the read stuff):
35+
using (var mutex = new Mutex(true, Path.GetFileNameWithoutExtension(HitsFilePath) + "_Mutex", out bool createdNew))
36+
{
37+
if (!createdNew)
38+
mutex.WaitOne();
39+
40+
if (!File.Exists(HitsFilePath))
41+
{
42+
// File not created yet, just write it
43+
using (var fs = new FileStream(HitsFilePath, FileMode.Create))
44+
using (var bw = new BinaryWriter(fs))
45+
{
46+
bw.Write(HitsArray.Length);
47+
foreach (int hitCount in HitsArray)
48+
{
49+
bw.Write(hitCount);
50+
}
51+
}
52+
}
53+
else
54+
{
55+
using (var fs = File.Open(HitsFilePath, FileMode.Open))
56+
using (var br = new BinaryReader(fs))
57+
using (var bw = new BinaryWriter(fs))
58+
{
59+
int hitsLength = br.ReadInt32();
60+
// TODO: check hits length match
61+
62+
for (int i = 0; i < hitsLength; ++i)
63+
{
64+
int oldHitCount = br.ReadInt32();
65+
bw.Seek(-sizeof(int), SeekOrigin.Current);
66+
bw.Write(HitsArray[i] + oldHitCount);
67+
}
68+
}
69+
}
70+
}
71+
}
72+
}
73+
}

0 commit comments

Comments
 (0)