Skip to content

Commit fa5ad54

Browse files
committed
ref types in shared memory done (incl. VMT)
1 parent a18a675 commit fa5ad54

File tree

19 files changed

+269
-54
lines changed

19 files changed

+269
-54
lines changed

Basic/01_allocOnStack/Program.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
namespace AllocOnStackSample
22
{
33
using System;
4+
using System.Collections.Generic;
45
using System.Runtime.CLR;
56

67
class Program
@@ -31,10 +32,9 @@ public override string ToString()
3132

3233
static void Main(string[] args)
3334
{
34-
var x = new SubCustomer();
35-
var addr = EntityPtr.ToPointer(x).ToInt32();
36-
x.M1();
37-
Console.WriteLine(x);
35+
var x = new Customer();
36+
x.SetType<SubCustomer>();
37+
Console.WriteLine(x.M1());
3838
}
3939
}
4040
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{63A1E5C2-289E-4A2E-80E0-5AAAB9DE2E89}</ProjectGuid>
8+
<OutputType>Exe</OutputType>
9+
<AppDesignerFolder>Properties</AppDesignerFolder>
10+
<RootNamespace>_10_directCastFeatures</RootNamespace>
11+
<AssemblyName>10_directCastFeatures</AssemblyName>
12+
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
13+
<FileAlignment>512</FileAlignment>
14+
</PropertyGroup>
15+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
16+
<PlatformTarget>AnyCPU</PlatformTarget>
17+
<DebugSymbols>true</DebugSymbols>
18+
<DebugType>full</DebugType>
19+
<Optimize>false</Optimize>
20+
<OutputPath>bin\Debug\</OutputPath>
21+
<DefineConstants>DEBUG;TRACE</DefineConstants>
22+
<ErrorReport>prompt</ErrorReport>
23+
<WarningLevel>4</WarningLevel>
24+
</PropertyGroup>
25+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
26+
<PlatformTarget>AnyCPU</PlatformTarget>
27+
<DebugType>pdbonly</DebugType>
28+
<Optimize>true</Optimize>
29+
<OutputPath>bin\Release\</OutputPath>
30+
<DefineConstants>TRACE</DefineConstants>
31+
<ErrorReport>prompt</ErrorReport>
32+
<WarningLevel>4</WarningLevel>
33+
</PropertyGroup>
34+
<ItemGroup>
35+
<Reference Include="EntityPtr, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
36+
<SpecificVersion>False</SpecificVersion>
37+
<HintPath>..\..\libs\EntityPtr.dll</HintPath>
38+
</Reference>
39+
<Reference Include="System" />
40+
<Reference Include="System.Core" />
41+
<Reference Include="System.Xml.Linq" />
42+
<Reference Include="System.Data.DataSetExtensions" />
43+
<Reference Include="Microsoft.CSharp" />
44+
<Reference Include="System.Data" />
45+
<Reference Include="System.Xml" />
46+
</ItemGroup>
47+
<ItemGroup>
48+
<Compile Include="Program.cs" />
49+
<Compile Include="Properties\AssemblyInfo.cs" />
50+
</ItemGroup>
51+
<ItemGroup>
52+
<None Include="App.config" />
53+
</ItemGroup>
54+
<ItemGroup>
55+
<ProjectReference Include="..\..\System.Runtime.CLR\System.Runtime.CLR.csproj">
56+
<Project>{E3BC3AE5-C2AB-442E-B649-C37E7982207E}</Project>
57+
<Name>System.Runtime.CLR</Name>
58+
</ProjectReference>
59+
</ItemGroup>
60+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
61+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
62+
Other similar extension points exist, see Microsoft.Common.targets.
63+
<Target Name="BeforeBuild">
64+
</Target>
65+
<Target Name="AfterBuild">
66+
</Target>
67+
-->
68+
</Project>
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
5+
</startup>
6+
</configuration>
+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
namespace _10_directCastFeatures
2+
{
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Runtime.CLR;
6+
7+
class A
8+
{
9+
public virtual string SomeMethod()
10+
{
11+
return "A::M1";
12+
}
13+
}
14+
15+
class B : A
16+
{
17+
public override string SomeMethod()
18+
{
19+
return "B::M1";
20+
}
21+
}
22+
23+
class Program
24+
{
25+
static void Main(string[] args)
26+
{
27+
ShowSimpleCastExample();
28+
29+
CastingBetweenIncompatibleTypes();
30+
31+
Console.ReadKey();
32+
}
33+
34+
private static void ShowSimpleCastExample()
35+
{
36+
// for example, this object comes from external library
37+
var obj = new A();
38+
39+
// rewrite VMT address
40+
obj.SetType<B>();
41+
42+
// Call SomeMethod with changed logic
43+
Console.WriteLine("Calling SomeMethod from object with rewritten VMT address:\r\n{0} (initially was A::M1)\r\n\n", obj.SomeMethod());
44+
}
45+
46+
private static void CastingBetweenIncompatibleTypes()
47+
{
48+
// Create List of derived class instances
49+
var obj = new List<B>();
50+
51+
// Interpret it as List of base class instances
52+
var changed = EntityPtr.CastRef<List<A>>(obj);
53+
54+
// call method
55+
changed.Add(new B());
56+
57+
// call another method
58+
Console.WriteLine("We cannot cast from List<B> to List<A> using C#, but can using CastRef<List<A>>(...):\r\n\"{0}\" is added using List<A> which is actually List<B>", changed[0]);
59+
}
60+
}
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("10_directCastFeatures")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("Microsoft")]
12+
[assembly: AssemblyProduct("10_directCastFeatures")]
13+
[assembly: AssemblyCopyright("Copyright © Microsoft 2015")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("db577fd2-d6c5-4a24-adb2-93d906cc3092")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]

CLRTests.sln

+16
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "04_sharedMemory", "04_share
6262
EndProject
6363
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "09_directCastPerf", "Basic\09_directCastPerf\09_directCastPerf.csproj", "{890270A3-C3CB-49B6-BC90-277F7C17600F}"
6464
EndProject
65+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "10_directCastFeatures", "Basic\10_directCastFeatures\10_directCastFeatures.csproj", "{63A1E5C2-289E-4A2E-80E0-5AAAB9DE2E89}"
66+
EndProject
6567
Global
6668
GlobalSection(SolutionConfigurationPlatforms) = preSolution
6769
Debug|Any CPU = Debug|Any CPU
@@ -370,6 +372,19 @@ Global
370372
{890270A3-C3CB-49B6-BC90-277F7C17600F}.Release|Mixed Platforms.Build.0 = Release|Any CPU
371373
{890270A3-C3CB-49B6-BC90-277F7C17600F}.Release|Win32.ActiveCfg = Release|Any CPU
372374
{890270A3-C3CB-49B6-BC90-277F7C17600F}.Release|x86.ActiveCfg = Release|Any CPU
375+
{63A1E5C2-289E-4A2E-80E0-5AAAB9DE2E89}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
376+
{63A1E5C2-289E-4A2E-80E0-5AAAB9DE2E89}.Debug|Any CPU.Build.0 = Debug|Any CPU
377+
{63A1E5C2-289E-4A2E-80E0-5AAAB9DE2E89}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
378+
{63A1E5C2-289E-4A2E-80E0-5AAAB9DE2E89}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
379+
{63A1E5C2-289E-4A2E-80E0-5AAAB9DE2E89}.Debug|Win32.ActiveCfg = Debug|Any CPU
380+
{63A1E5C2-289E-4A2E-80E0-5AAAB9DE2E89}.Debug|x86.ActiveCfg = Debug|Any CPU
381+
{63A1E5C2-289E-4A2E-80E0-5AAAB9DE2E89}.Debug|x86.Build.0 = Debug|Any CPU
382+
{63A1E5C2-289E-4A2E-80E0-5AAAB9DE2E89}.Release|Any CPU.ActiveCfg = Release|Any CPU
383+
{63A1E5C2-289E-4A2E-80E0-5AAAB9DE2E89}.Release|Any CPU.Build.0 = Release|Any CPU
384+
{63A1E5C2-289E-4A2E-80E0-5AAAB9DE2E89}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
385+
{63A1E5C2-289E-4A2E-80E0-5AAAB9DE2E89}.Release|Mixed Platforms.Build.0 = Release|Any CPU
386+
{63A1E5C2-289E-4A2E-80E0-5AAAB9DE2E89}.Release|Win32.ActiveCfg = Release|Any CPU
387+
{63A1E5C2-289E-4A2E-80E0-5AAAB9DE2E89}.Release|x86.ActiveCfg = Release|Any CPU
373388
EndGlobalSection
374389
GlobalSection(SolutionProperties) = preSolution
375390
HideSolutionNode = FALSE
@@ -387,6 +402,7 @@ Global
387402
{50654A4C-D50A-49A3-9461-AF9E9074E271} = {9538927F-476D-484D-B45B-A8929681C375}
388403
{3AA6FBCD-1D76-4CDC-9845-8FC86710EAD5} = {9538927F-476D-484D-B45B-A8929681C375}
389404
{890270A3-C3CB-49B6-BC90-277F7C17600F} = {9538927F-476D-484D-B45B-A8929681C375}
405+
{63A1E5C2-289E-4A2E-80E0-5AAAB9DE2E89} = {9538927F-476D-484D-B45B-A8929681C375}
390406
{6453CD0A-9EBC-4B1F-A200-0EAEAD50091C} = {4B82994E-3A0D-41A8-A280-571497189D26}
391407
{438296AD-5A5A-45B6-BF30-BE283B589E33} = {4B82994E-3A0D-41A8-A280-571497189D26}
392408
{0208D865-800F-4C3D-A25F-146E3E50BC2A} = {4B82994E-3A0D-41A8-A280-571497189D26}

RocketScience/04_sharedMemory/04_sharedMemory.csproj

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@
99
<AppDesignerFolder>Properties</AppDesignerFolder>
1010
<RootNamespace>_04_sharedMemory</RootNamespace>
1111
<AssemblyName>04_sharedMemory</AssemblyName>
12-
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
12+
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
1313
<FileAlignment>512</FileAlignment>
14+
<TargetFrameworkProfile />
1415
</PropertyGroup>
1516
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
16-
<PlatformTarget>AnyCPU</PlatformTarget>
17+
<PlatformTarget>x86</PlatformTarget>
1718
<DebugSymbols>true</DebugSymbols>
1819
<DebugType>full</DebugType>
1920
<Optimize>false</Optimize>
@@ -44,7 +45,6 @@
4445
<Reference Include="System.Core" />
4546
<Reference Include="System.Xml.Linq" />
4647
<Reference Include="System.Data.DataSetExtensions" />
47-
<Reference Include="Microsoft.CSharp" />
4848
<Reference Include="System.Data" />
4949
<Reference Include="System.Xml" />
5050
</ItemGroup>
+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
<?xml version="1.0" encoding="utf-8" ?>
1+
<?xml version="1.0"?>
22
<configuration>
33
<startup>
4-
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
5-
</startup>
6-
</configuration>
4+
5+
<supportedRuntime version="v2.0.50727"/></startup>
6+
</configuration>
+6-11
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,32 @@
11
namespace _04_sharedMemory
22
{
33
using System;
4-
using System.Diagnostics.Contracts;
5-
using System.Runtime.CLR;
64
using _04_sharedMemoryLib;
75

86
class Program
97
{
10-
static unsafe void Main(string[] args)
8+
static void Main(string[] args)
119
{
1210
var fixedmap = new FixedAddressTypesMap(0x0017f000);
13-
14-
var mt = fixedmap.AddType<SharedType>();
11+
var holder = fixedmap.GetOrAddType<SharedType>();
1512

1613
var testobject = new SharedType();
17-
var aa = EntityPtr.ToPointer(testobject);
18-
*(int*) EntityPtr.ToPointerWithOffset(testobject) = (int)mt; // rewrite mt to new
1914
Console.WriteLine(testobject.ToString());
2015

2116
using (var sender = new SharedMemoryManager<SharedType>(typeof(SharedType).FullName, 1024))
2217
using (var reciever = new SharedMemoryManager<string>(typeof(string).FullName, 1024))
2318
{
24-
var tosend = new SharedType();
25-
tosend = new SharedType();
19+
var tosend = new SharedType();
2620
tosend.SetX(100);
2721

28-
sender.SendObject(tosend);
22+
holder.AsSharedType(tosend);
23+
sender.ShareObject(tosend);
24+
2925
var obj = reciever.ReceiveObject();
3026

3127
Console.WriteLine("{0}", obj);
3228
Console.ReadKey();
3329
}
34-
Console.WriteLine(aa);
3530
}
3631
}
3732
}
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
3-
<package id="SharedMemory" version="1.1.4" targetFramework="net45" />
3+
<package id="SharedMemory" version="1.1.4" targetFramework="net45" requireReinstallation="True" />
44
</packages>

RocketScience/04_sharedMemoryLib/04_sharedMemoryLib.csproj

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
<AppDesignerFolder>Properties</AppDesignerFolder>
1010
<RootNamespace>_04_sharedMemoryLib</RootNamespace>
1111
<AssemblyName>04_sharedMemoryLib</AssemblyName>
12-
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
12+
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
1313
<FileAlignment>512</FileAlignment>
14+
<TargetFrameworkProfile />
1415
</PropertyGroup>
1516
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
1617
<DebugSymbols>true</DebugSymbols>
@@ -21,6 +22,7 @@
2122
<ErrorReport>prompt</ErrorReport>
2223
<WarningLevel>4</WarningLevel>
2324
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
25+
<PlatformTarget>x86</PlatformTarget>
2426
</PropertyGroup>
2527
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
2628
<DebugType>pdbonly</DebugType>

0 commit comments

Comments
 (0)