首页 > 解决方案 > 管理到本机值类转换:转换指针是否安全?

问题描述

我有一个 C# 项目,它使用库中的 C++ 类。C# 类实际上是 C++ 类的包装器,它们将 C++ 功能暴露给 C# 客户端代码。在许多地方,C++ 值类被转换为 C# 包装器并向后转换。在代码审查期间,我发现了两种转换类的方法:通过 reinterpret_cast(参见 operator *)和通过 pin_ptr(参见 MultiplyBy);如您所见,本机类和托管类都有三个“双”字段,这就是有人使用 reinterpret_cast 的原因;

在许多地方,使用 memcpy 将类从 C# 复制到 C++:memcpy(&NativePointInstance, &ManagedPointIntance, sizeof(double)*3);

我从一位开发人员那里听说,当我们使用 C# 值类时,reinterpret_cast 在某些情况下是安全的。

问题是:什么时候在 C# 值类上使用 reinterpret_cast 是安全的,什么时候不安全?在这种情况下,转换指针的最正确方法是什么——比如在运算符 * 中或在 MultiplyBy 中,或其他替代方法?

有人可以详细解释 MultiplyBy() 中发生了什么,这些技巧是如何工作的吗?

据我了解,该问题可能是由于优化器可能会更改字段顺序,GC 可能会重新组织堆,并且托管代码和本机代码之间的字段对齐方式可能不同。

// this is C++ native class
class NativePoint
{
public:
  double x;
  double y;
  double z;
  NativePoint(double x, double y, double z)
  {
    this->x = x;
    this->y = y;
    this->z = z;
  }
  NativePoint operator * (int value)
  {
    return NativePoint(x * value, y * value, z * value);
  }
};

// this class managed C++ class
[StructLayout(LayoutKind::Sequential)]
public value class ManagedPoint
{
internal:
  double x;
  double y;
  double z;
  ManagedPoint(const NativePoint& p)
  {
    x = p.x;
    y = p.y;
    z = p.z;
  }
public:
  static ManagedPoint operator * (ManagedPoint a, double value)
  {
    return ManagedPoint((*reinterpret_cast<NativePoint*>(&(a))) * value);
  }
  ManagedPoint MultiplyBy(double value)
  {
    pin_ptr<ManagedPoint> pThisTmp = &*this;
    NativePoint* pThis = reinterpret_cast<NativePoint*>(&*pThisTmp);
    return ManagedPoint(*pThis * value);
  }
};

// this should be called from C# code, or another .NET app
int main(array<System::String ^> ^args)
{
  NativePoint p_native = NativePoint(1, 1, 1);
  ManagedPoint p = ManagedPoint(p_native);
  Console::WriteLine("p is {" + p.x + ", " + p.y + ", " + p.z + "}");
  ManagedPoint p1 = p * 5;
  Console::WriteLine("p1 is {" + p1.x + ", " + p1.y + ", " + p1.z + "}");
  ManagedPoint p2 = p.MultiplyBy(5);
  Console::WriteLine("p2 is {" + p2.x + ", " + p2.y + ", " + p2.z + "}");
  Console::ReadLine();
  return 0;
}

标签: c++.netreinterpret-castpin-ptr

解决方案


好吧,我最终使用了本机类的常用构造函数。它看起来对我来说绝对安全,并且从剩余的变体中最快。来自 Marshal::PtrToStructure() 评论的想法很好,但是对于我的测试示例,执行速度比使用构造函数的速度慢。指针转换是最快的解决方案,但是在评论中非常可怕的例子之后,我不会再冒险使用它了(除非我们真的需要优化它,那么 LayoutKind::Explicit 应该做的事情)。

这是我用于测试的代码:

// this is C++ native class
class NativePoint
{
public:
  double x;
  double y;
  double z;
  NativePoint()
  {

  }
  NativePoint(double x, double y, double z)
  {
    this->x = x;
    this->y = y;
    this->z = z;
  }
  NativePoint operator * (int value)
  {
    return NativePoint(x * value, y * value, z * value);
  }
};

// this class managed C++ class
[StructLayout(LayoutKind::Sequential)]
public value class ManagedPoint
{
internal:
  double x;
  double y;
  double z;
  ManagedPoint(const NativePoint& p)
  {
    x = p.x;
    y = p.y;
    z = p.z;
  }
  ManagedPoint(double x, double y, double z)
  {
    this->x = x;
    this->y = y;
    this->z = z;
  }
public:
  static ManagedPoint operator * (ManagedPoint a, double value)
  {
    return ManagedPoint((*reinterpret_cast<NativePoint*>(&(a))) * value);
  }
  ManagedPoint MultiplyBy(double value)
  {
    pin_ptr<ManagedPoint> pThisTmp = &*this;
    NativePoint* pThis = reinterpret_cast<NativePoint*>(&*pThisTmp);
    return ManagedPoint(*pThis * value);
  }
};

// this class managed C++ class
[StructLayout(LayoutKind::Sequential)]
public value class ManagedPoint2
{
internal:
  double x;
  double y;
  double z;
  ManagedPoint2(const NativePoint& p)
  {
    x = p.x;
    y = p.y;
    z = p.z;
  }
  ManagedPoint2(double x, double y, double z)
  {
    this->x = x;
    this->y = y;
    this->z = z;
  }
public:
  static ManagedPoint2 operator * (ManagedPoint2 a, double value)
  {
    return ManagedPoint2((NativePoint(a.x, a.y, a.z)) * value);
  }
  ManagedPoint2 MultiplyBy(double value)
  {
    return ManagedPoint2((NativePoint(this->x, this->y, this->z)) * value);
  }
};

// this class managed C++ class
[StructLayout(LayoutKind::Sequential)]
public value class ManagedPoint3
{
internal:
  double x;
  double y;
  double z;
  ManagedPoint3(const NativePoint& p)
  {
    x = p.x;
    y = p.y;
    z = p.z;
  }
  ManagedPoint3(double x, double y, double z)
  {
    this->x = x;
    this->y = y;
    this->z = z;
  }
public:
  static ManagedPoint3 operator * (ManagedPoint3 a, double value)
  {
    NativePoint p;
    Marshal::StructureToPtr(a, IntPtr(&p), false);
    return ManagedPoint3(p * value);
  }
  ManagedPoint3 MultiplyBy(double value)
  {
    NativePoint p;
    Marshal::StructureToPtr(*this, IntPtr(&p), false);
    return ManagedPoint3(p * value);
  }
};

// this class managed C++ class
[StructLayout(LayoutKind::Sequential)]
public value class ManagedPoint4
{
internal:
  double x;
  double y;
  double z;
  ManagedPoint4(const NativePoint& p)
  {
    x = p.x;
    y = p.y;
    z = p.z;
  }
  ManagedPoint4(double x, double y, double z)
  {
    this->x = x;
    this->y = y;
    this->z = z;
  }
public:
  static ManagedPoint4 operator * (ManagedPoint4 a, double value)
  {
    return ManagedPoint4(ManagedPoint4::ToNative(a) * value);
  }
  ManagedPoint4 MultiplyBy(double value)
  {
    return ManagedPoint4(ManagedPoint4::ToNative(*this) * value);
  }
  static NativePoint ToNative(const ManagedPoint4& pp)
  {
    NativePoint p;
    Marshal::StructureToPtr(pp, IntPtr(&p), false);
    return p;
  }
};

// this should be called from C# code, or another .NET app
int main(array<System::String ^> ^args)
{
  Stopwatch time;
  time.Start();
  for (int i = 0; i < 10000000; i++)
  {
    ManagedPoint a = ManagedPoint(1, 2, 3) * 4;
  }
  time.Stop();
  Console::WriteLine("time: " + time.ElapsedMilliseconds);

  Stopwatch time2;
  time2.Start();
  for (int i = 0; i < 10000000; i++)
  {
    ManagedPoint2 a2 = ManagedPoint2(1, 2, 3) * 4;
  }
  time2.Stop();
  Console::WriteLine("time2: " + time2.ElapsedMilliseconds);

  Stopwatch time3;
  time3.Start();
  for (int i = 0; i < 10000000; i++)
  {
    ManagedPoint3 a3 = ManagedPoint3(1, 2, 3) * 4;
  }
  time3.Stop();
  Console::WriteLine("time3: " + time3.ElapsedMilliseconds);

  Stopwatch time4;
  time4.Start();
  for (int i = 0; i < 10000000; i++)
  {
    ManagedPoint4 a3 = ManagedPoint4(1, 2, 3) * 4;
  }
  time4.Stop();
  Console::WriteLine("time4: " + time4.ElapsedMilliseconds);

  Console::ReadLine();
  Console::WriteLine("======================================================");
  Console::WriteLine();

  return 0;
}

这是输出:

time: 374
time2: 382
time3: 857
time4: 961

time: 395
time2: 413
time3: 900
time4: 968

time: 376
time2: 378
time3: 840
time4: 909

推荐阅读