zoukankan      html  css  js  c++  java
  • Marshal C#

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    using System.IO;

    using System.Runtime.InteropServices;

    using System.Diagnostics;

     

    namespace ConsoleApplication2

    {

        public class CompactHeap<T> : IDisposable where T : new()

        {

            #region Fields

     

            private IntPtr _ptr, _currPtr;

            private int _rawSize;

            private Type _type;

            private T _t;

           

            #endregion

     

            public CompactHeap(int num)

            {

                _t = new T();

                _type = _t.GetType();

     

                _rawSize = Marshal.SizeOf(_t);

                _ptr = Marshal.AllocHGlobal(_rawSize * num);

                _currPtr = _ptr;

     

            }

     

            public void Add(T t)

            {

                Marshal.StructureToPtr(t, _ptr, false);

                _ptr = new IntPtr((_ptr.ToInt32() + _rawSize));

                      

            }

            public T Get(int index)

            {

                IntPtr p = new IntPtr(_currPtr.ToInt32() + _rawSize * index);

                return (T)Marshal.PtrToStructure(p, _type);

     

     

            }

     

            public void Dispose()

            {

                Marshal.FreeHGlobal(_currPtr);

            }

     

     

        }

        class Program

        {

            static void Main(string[] args)

            {

                int totalNum = 1000 * 10000;

     

                using (CompactHeap<Modes> head = new CompactHeap<Modes>(totalNum))

                {

                    Stopwatch sw = new Stopwatch();

                    sw.Start();

                    for (int t = 0; t < totalNum; t++)

                    {

                        Modes m = new Modes(); m.UserHeap = t; m.UserID = t; head.Add(m);

                       

                    }

                    sw.Stop();

                    Console.WriteLine(sw.ElapsedMilliseconds.ToString());

     

     

                    Modes m500000 = head.Get(500000);

                    Console.WriteLine("m500000"+m500000.UserID);

     

                    Modes m8 = head.Get(8);

                    Console.WriteLine("m8"+m8.UserID);

     

     

                   

     

                }

     

     

                Console.ReadLine();

            }

        }

        public struct Modes

        {

            public int UserID;

            public int UserHeap;

        }

     

       

    }

     

     

    //照到抄的,联想是,本来是因为在Vb 里面要传个VarPtr过去。不过弄到后面发现GC.Handle 的那个 IntPtr 好像不行吧。觉得这类型的应该是在Marshal 里面操作吧。毕竟这个是操作非托管堆的。So 真正要做的时候该杂做呢。Marshal.allochgolbal()建实例。不过感觉性能有限吧。但是觉得肯定可以传IntPtr.

  • 相关阅读:
    对于glut和freeglut的一点比较和在VS2013上的配置问题
    应用程序无法启动(0*c000007b)
    无法定位程序输入点glPopAttrib于动态连结库OPENGL.dll上
    计算机中丢失OPENGL.dll
    Visual Studio "无法查找或打开PDB文件"解决方法
    VC包含目录、附加依赖项、库目录及具体设置
    无法解析的外部符号 _WinMain@16
    OpenGL入门学习
    linux-用户建立及权限分配
    linux下添加用户并赋予root权限
  • 原文地址:https://www.cnblogs.com/fat_li/p/2144912.html
Copyright © 2011-2022 走看看