• 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.

  • 相关阅读:
    Linux(Debian、Ubuntu、Deepin等)安装最新版Chrome Unstable
    JavaScript根据经纬度获取距离信息
    CSS滚动条样式定制
    Linux下 Apache Vhost 配置 防止403
    Unity减小安装包的体积(210MB减小到7MB)
    Ubuntu17.04配置LNMP(Nginx+PHP7+MySQL)简单教程 快速 易学 简单易懂
    Yii2项目实现Markdown功能 在线Markdown编辑器
    Gradle全局代理配置
    Angular4.0从入门到实战打造在线竞拍网站学习笔记之三依赖注入
    Python使用PyMysql操作数据库
  • 原文地址:https://www.cnblogs.com/fat_li/p/2144912.html
Copyright © 2020-2023  润新知