Skip to content

Instantly share code, notes, and snippets.

@MichalStrehovsky
Created June 28, 2019 08:16
Show Gist options
  • Save MichalStrehovsky/df2c7736cc4d42d3093f2d02fab30361 to your computer and use it in GitHub Desktop.
Save MichalStrehovsky/df2c7736cc4d42d3093f2d02fab30361 to your computer and use it in GitHub Desktop.
MarshalHelpers.cs
else if (MarshalUtils.IsBlittableType(type) && !isField)
{
if (nativeType == NativeTypeKind.Invalid || nativeType == NativeTypeKind.LPStruct)
return MarshallerKind.BlittablePtr;
else
return MarshallerKind.Invalid;
}
Marshaller.cs
class BlittablePtrMarshaller : Marshaller
{
protected override void AllocManagedToNative(ILCodeStream codeStream)
{
if (IsManagedByRef)
{
// For byrefs, we might need to allocate something
throw new NotSupportedException();
}
}
protected override void TransformManagedToNative(ILCodeStream codeStream)
{
ILEmitter emitter = _ilCodeStreams.Emitter;
ILCodeLabel lNullValue = emitter.NewCodeLabel();
ILToken tokGetRawData = emitter.NewToken(Context.GetWellKnownType(WellKnownType.Object).GetKnownMethod("GetRawData", null));
LoadManagedValue(codeStream);
codeStream.Emit(ILOpcode.brfalse, lNullValue);
ILLocalVariable vPinnedLocal = emitter.NewLocal(ManagedType, isPinned: true);
LoadManagedValue(codeStream);
codeStream.EmitStLoc(vPinnedLocal);
codeStream.EmitLdLoc(vPinnedLocal);
codeStream.Emit(ILOpcode.call, tokGetRawData);
codeStream.Emit(ILOpcode.conv_u);
StoreNativeValue(codeStream);
codeStream.EmitLabel(lNullValue);
}
protected override void AllocNativeToManaged(ILCodeStream codeStream)
{
throw new NotSupportedException();
}
protected override void TransformNativeToManaged(ILCodeStream codeStream)
{
// Nothing to do since right now we just pin
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment