Direct Memory Access Help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • O.B.

    Direct Memory Access Help

    Given the two following structures:

    [StructLayout(La youtKind.Explic it, Size=16)]
    public struct Articulation {
    [FieldOffset(0)] public byte typeDesignator; // 1 byte
    [FieldOffset(1)] public byte changeIndicator ; // 1 byte
    [FieldOffset(2)] public Unsigned16 attachmentID; // 2 bytes
    [FieldOffset(4)] public Unsigned32 type; // 4 bytes
    [FieldOffset(8)] public Bits64 value; // 8 bytes
    }

    [StructLayout(La youtKind.Explic it, Size=1392)]
    public struct EntityState {
    public static byte TYPE_ENTITY_STA TE = 1;
    [FieldOffset(0)] public Header header; // 12 bytes
    [FieldOffset(12)] public EntityIdentifie r entityID; // 6 bytes
    [FieldOffset(18)] public ForceIdentifier forceID; // 1 byte
    [FieldOffset(19)] public byte numArticulation s; // 1 byte
    [FieldOffset(20)] public EntityType type; // 8 bytes
    [FieldOffset(28)] public EntityType altType; // 8 bytes
    [FieldOffset(36)] public Vector linearVelocity; // 12 bytes
    [FieldOffset(48)] public WorldCoordinate location; // 24 bytes
    [FieldOffset(72)] public EulerAngles orientation; // 12 bytes
    [FieldOffset(84)] public Unsigned32 appearance; // 4 bytes
    [FieldOffset(88)] public DeadReckoning deadReckoning; // 40 bytes
    [FieldOffset(128 )] public EntityMarking marking; // 12 bytes
    [FieldOffset(140 )] public Unsigned32 capabilities; // 4 bytes
    // In C#, you cannot have "public fixed Articulation
    // articulations[78]" because "fixed" does not support
    // reference types. Use memory pointers?
    [FieldOffset(144 )] public Articulation articulations;
    }

    As you can see, I hit a small problem where C# does not support "fixed"
    with a custom type. So, I'd like to add an operation to the EntityState
    structure that allows a user to load an articulation given an index.

    // A workaround for this.articulati ons[index]
    public Articulation getArticulation At(int index);

    In C#, how does one go about directly accessing memory to perform this
    behavior? Thanks in advance.
  • chanmm

    #2
    Re: Direct Memory Access Help

    There is always this bible for me. If you cannot find in the C# 2.0
    specification then try to forget about it.


    chanmm


    "O.B." <funkjunk@bells outh.netwrote in message
    news:12ketofglm rfa90@corp.supe rnews.com...
    Given the two following structures:
    >
    [StructLayout(La youtKind.Explic it, Size=16)]
    public struct Articulation {
    [FieldOffset(0)] public byte typeDesignator; // 1 byte
    [FieldOffset(1)] public byte changeIndicator ; // 1 byte
    [FieldOffset(2)] public Unsigned16 attachmentID; // 2 bytes
    [FieldOffset(4)] public Unsigned32 type; // 4 bytes
    [FieldOffset(8)] public Bits64 value; // 8 bytes
    }
    >
    [StructLayout(La youtKind.Explic it, Size=1392)]
    public struct EntityState {
    public static byte TYPE_ENTITY_STA TE = 1;
    [FieldOffset(0)] public Header header; // 12 bytes
    [FieldOffset(12)] public EntityIdentifie r entityID; // 6 bytes
    [FieldOffset(18)] public ForceIdentifier forceID; // 1 byte
    [FieldOffset(19)] public byte numArticulation s; // 1 byte
    [FieldOffset(20)] public EntityType type; // 8 bytes
    [FieldOffset(28)] public EntityType altType; // 8 bytes
    [FieldOffset(36)] public Vector linearVelocity; // 12 bytes
    [FieldOffset(48)] public WorldCoordinate location; // 24 bytes
    [FieldOffset(72)] public EulerAngles orientation; // 12 bytes
    [FieldOffset(84)] public Unsigned32 appearance; // 4 bytes
    [FieldOffset(88)] public DeadReckoning deadReckoning; // 40 bytes
    [FieldOffset(128 )] public EntityMarking marking; // 12 bytes
    [FieldOffset(140 )] public Unsigned32 capabilities; // 4 bytes
    // In C#, you cannot have "public fixed Articulation
    // articulations[78]" because "fixed" does not support
    // reference types. Use memory pointers?
    [FieldOffset(144 )] public Articulation articulations;
    }
    >
    As you can see, I hit a small problem where C# does not support "fixed"
    with a custom type. So, I'd like to add an operation to the EntityState
    structure that allows a user to load an articulation given an index.
    >
    // A workaround for this.articulati ons[index]
    public Articulation getArticulation At(int index);
    >
    In C#, how does one go about directly accessing memory to perform this
    behavior? Thanks in advance.

    Comment

    • Willy Denoyette [MVP]

      #3
      Re: Direct Memory Access Help


      "O.B." <funkjunk@bells outh.netwrote in message
      news:12ketofglm rfa90@corp.supe rnews.com...
      | Given the two following structures:
      |
      | [StructLayout(La youtKind.Explic it, Size=16)]
      | public struct Articulation {
      | [FieldOffset(0)] public byte typeDesignator; // 1 byte
      | [FieldOffset(1)] public byte changeIndicator ; // 1 byte
      | [FieldOffset(2)] public Unsigned16 attachmentID; // 2 bytes
      | [FieldOffset(4)] public Unsigned32 type; // 4 bytes
      | [FieldOffset(8)] public Bits64 value; // 8 bytes
      | }
      |
      | [StructLayout(La youtKind.Explic it, Size=1392)]
      | public struct EntityState {
      | public static byte TYPE_ENTITY_STA TE = 1;
      | [FieldOffset(0)] public Header header; // 12 bytes
      | [FieldOffset(12)] public EntityIdentifie r entityID; // 6 bytes
      | [FieldOffset(18)] public ForceIdentifier forceID; // 1 byte
      | [FieldOffset(19)] public byte numArticulation s; // 1 byte
      | [FieldOffset(20)] public EntityType type; // 8 bytes
      | [FieldOffset(28)] public EntityType altType; // 8 bytes
      | [FieldOffset(36)] public Vector linearVelocity; // 12 bytes
      | [FieldOffset(48)] public WorldCoordinate location; // 24 bytes
      | [FieldOffset(72)] public EulerAngles orientation; // 12 bytes
      | [FieldOffset(84)] public Unsigned32 appearance; // 4 bytes
      | [FieldOffset(88)] public DeadReckoning deadReckoning; // 40 bytes
      | [FieldOffset(128 )] public EntityMarking marking; // 12 bytes
      | [FieldOffset(140 )] public Unsigned32 capabilities; // 4 bytes
      | // In C#, you cannot have "public fixed Articulation
      | // articulations[78]" because "fixed" does not support
      | // reference types. Use memory pointers?
      | [FieldOffset(144 )] public Articulation articulations;
      | }
      |
      | As you can see, I hit a small problem where C# does not support "fixed"
      | with a custom type. So, I'd like to add an operation to the EntityState
      | structure that allows a user to load an articulation given an index.
      |
      | // A workaround for this.articulati ons[index]
      | public Articulation getArticulation At(int index);
      |
      | In C#, how does one go about directly accessing memory to perform this
      | behavior? Thanks in advance.

      What you should do, is define the array of struct as a ByValArray of struct
      Articulation with a size equal to the size of the array of structs, that is
      the number of elements.


      public struct EntityState {
      ..
      [MarshalAs(Unman agedType.ByValA rray, SizeConst = 78)]
      public Articulation [] articulations;
      }

      Beware that the struct array must be aligned on machine word boundary!

      Willy.


      Comment

      Working...