示例#1
0
        internal static                CpuInfo[] NativeGet(Sigar sigar)
        {
            System.Type   typeFromHandle = typeof(CpuInfoList);
            System.IntPtr intPtr         = System.Runtime.InteropServices.Marshal.AllocHGlobal(System.Runtime.InteropServices.Marshal.SizeOf(typeFromHandle));
            int           num            = CpuInfoList.sigar_cpu_info_list_get(sigar.sigar.Handle, intPtr);

            if (num != 0)
            {
                System.Runtime.InteropServices.Marshal.FreeHGlobal(intPtr);
                throw Sigar.FindException(sigar, num);
            }
            CpuInfoList cpuInfoList = (CpuInfoList)System.Runtime.InteropServices.Marshal.PtrToStructure(intPtr, typeFromHandle);

            CpuInfo[]     array = new CpuInfo[cpuInfoList.Number];
            System.IntPtr ptr   = cpuInfoList.data;
            int           num2  = System.Runtime.InteropServices.Marshal.SizeOf(array[0]);
            int           num3  = 0;

            while ((long)num3 < (long)((ulong)cpuInfoList.Number))
            {
                array[num3] = (CpuInfo)System.Runtime.InteropServices.Marshal.PtrToStructure(ptr, typeof(CpuInfo));
                ptr         = Sigar.incrementIntPtr(ptr, num2);
                num3++;
            }
            CpuInfoList.sigar_cpu_info_list_destroy(sigar.sigar.Handle, intPtr);
            System.Runtime.InteropServices.Marshal.FreeHGlobal(intPtr);
            return(array);
        }
示例#2
0
        internal static         CpuInfo[] NativeGet(Sigar sigar)
        {
            Type   type = typeof(CpuInfoList);
            IntPtr ptr  = Marshal.AllocHGlobal(Marshal.SizeOf(type));

            int status = sigar_cpu_info_list_get(sigar.sigar.Handle, ptr);

            if (status != Sigar.OK)
            {
                Marshal.FreeHGlobal(ptr);
                throw Sigar.FindException(sigar, status);
            }

            CpuInfoList infosPtr =
                (CpuInfoList)Marshal.PtrToStructure(ptr, type);

            CpuInfo[] infos = new CpuInfo[infosPtr.Number];

            IntPtr eptr = infosPtr.data;
            int    size = Marshal.SizeOf(infos[0]);

            for (int i = 0; i < infosPtr.Number; i++)
            {
                infos[i] =
                    (CpuInfo)Marshal.PtrToStructure(eptr, typeof(CpuInfo));

                //eptr += sizeof(sigar_cpu_info_t);
                eptr = Sigar.incrementIntPtr(eptr, size);
            }

            sigar_cpu_info_list_destroy(sigar.sigar.Handle, ptr);

            Marshal.FreeHGlobal(ptr);

            return(infos);
        }