Ejemplo n.º 1
0
        /// <summary>
        /// Ensure <see cref="MemoryCopy"/> functionality.
        /// </summary>
        private static void EnsureMemoryCopy()
        {
            if (MemoryCopyPointer == null)
            {
                IntPtr memoryCopyPtr;

                switch (Environment.OSVersion.Platform)
                {
                case PlatformID.Win32Windows:
                case PlatformID.Win32NT:
                case PlatformID.Win32S:
                    memoryCopyPtr = GetProcAddress.GetAddress("msvcrt.dll", "memcpy");
                    if (memoryCopyPtr != IntPtr.Zero)
                    {
                        MemoryCopyPointer = (MemoryCopyDelegate)Marshal.GetDelegateForFunctionPointer(memoryCopyPtr, typeof(MemoryCopyDelegate));
                        return;
                    }

                    throw new NotSupportedException("no suitable memcpy support");

                case PlatformID.Unix:
                    memoryCopyPtr = GetProcAddress.GetAddress("libc.so.6", "memcpy");
                    if (memoryCopyPtr != IntPtr.Zero)
                    {
                        MemoryCopyPointer = (MemoryCopyDelegate)Marshal.GetDelegateForFunctionPointer(memoryCopyPtr, typeof(MemoryCopyDelegate));
                        return;
                    }

                    throw new NotSupportedException("no suitable memcpy support");

                default:
                    throw new NotSupportedException("no suitable memcpy support");
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Link delegates fields using import declarations.
        /// </summary>
        /// <param name="path">
        /// A <see cref="System.String"/> that specifies the assembly file path containing the import functions.
        /// </param>
        /// <param name="type">
        /// A <see cref="System.Type"/> that specifies the type used for detecting import declarations and delegates fields.
        /// </param>
        protected static void LinkLibraryProcImports(string path, Type type)
        {
            SortedList <string, MethodInfo> sImportMap;
            List <FieldInfo> sDelegates;

            LinkProcAddressImports(path, type, delegate(string libpath, string function) {
                return(GetProcAddress.GetAddress(libpath, function));
            }, out sImportMap, out sDelegates);
        }
Ejemplo n.º 3
0
        protected Plugin(string assemblyPath, string factoryFunction)
        {
            if (assemblyPath == null)
            {
                throw new ArgumentNullException("assemblyPath");
            }

            if ((_CreatePluginFactoryPtr = GetProcAddress.GetAddress(assemblyPath, factoryFunction)) == IntPtr.Zero)
            {
                throw new ArgumentException("not a valid factory function", "factoryFunction");
            }

            // Marshal.GetDelegateForFunctionPointer(createFactoryPtr, factoryFunction);
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Link delegates fields using import declarations, using platform standard method for determining procesures addresses.
 /// </summary>
 /// <param name="path">
 /// A <see cref="String"/> that specifies the assembly file path containing the import functions.
 /// </param>
 /// <param name="imports">
 /// A <see cref="ImportMap"/> mapping a <see cref="MethodInfo"/> with the relative function name.
 /// </param>
 /// <param name="delegates">
 /// A <see cref="DelegateList"/> listing <see cref="FieldInfo"/> related to function delegates.
 /// </param>
 /// <exception cref="ArgumentNullException">
 /// Exception thrown if <paramref name="path"/>, <paramref name="imports"/> or <paramref name="delegates"/> is null.
 /// </exception>
 protected static void LoadProcDelegates(string path, ImportMap imports, DelegateList delegates)
 {
     LoadProcDelegates(path, imports, delegates, delegate(string libpath, string function) {
         return(GetProcAddress.GetAddress(libpath, function));
     });
 }
Ejemplo n.º 5
0
        /// <summary>
        /// Load available SIMD extensions.
        /// </summary>
        public static void LoadSimdExtensions()
        {
            const string AssemblyPath = "OpenGL.Simd.dll";

            IntPtr getCpuInformationPtr = IntPtr.Zero;

            if (File.Exists(AssemblyPath) == false)
            {
                return;
            }

            try {
                getCpuInformationPtr = GetProcAddress.GetAddress(AssemblyPath, "GetCpuInformation");
            } catch { /* Ignore exception, leave 'getCpuInformationPtr' equals to 'IntPtr.Zero' */ }

            // No CPU information? Ahi ahi ahi
            if (getCpuInformationPtr == IntPtr.Zero)
            {
                return;
            }

            GetCpuInformation getCpuInformation = (GetCpuInformation)Marshal.GetDelegateForFunctionPointer(getCpuInformationPtr, typeof(GetCpuInformation));
            CpuInformation    cpuInfo           = new CpuInformation();

            getCpuInformation(ref cpuInfo);

            if (cpuInfo.SimdSupport != SimdTechnology.None)
            {
                FieldInfo[] fields = typeof(Memory).GetFields(BindingFlags.Static | BindingFlags.NonPublic);

                foreach (FieldInfo fieldInfo in fields)
                {
                    // Test for SSSE3 support
                    if ((cpuInfo.SimdSupport & SimdTechnology.SSSE3) != 0)
                    {
                        string entryPoint = String.Format("{0}_{1}", fieldInfo.Name, "SSSE3");
                        IntPtr address    = GetProcAddress.GetAddress(AssemblyPath, entryPoint);

                        if (address != IntPtr.Zero)
                        {
                            fieldInfo.SetValue(null, Marshal.GetDelegateForFunctionPointer(address, fieldInfo.FieldType));
                            continue;
                        }
                    }

                    // Test for SSE3 support
                    if ((cpuInfo.SimdSupport & SimdTechnology.SSE3) != 0)
                    {
                        string entryPoint = String.Format("{0}_{1}", fieldInfo.Name, "SSE3");
                        IntPtr address    = GetProcAddress.GetAddress(AssemblyPath, entryPoint);

                        if (address != IntPtr.Zero)
                        {
                            fieldInfo.SetValue(null, Marshal.GetDelegateForFunctionPointer(address, fieldInfo.FieldType));
                            continue;
                        }
                    }

                    // Test for SSE2 support
                    if ((cpuInfo.SimdSupport & SimdTechnology.SSE2) != 0)
                    {
                        string entryPoint = String.Format("{0}_{1}", fieldInfo.Name, "SSE2");
                        IntPtr address    = GetProcAddress.GetAddress(AssemblyPath, entryPoint);

                        if (address != IntPtr.Zero)
                        {
                            fieldInfo.SetValue(null, Marshal.GetDelegateForFunctionPointer(address, fieldInfo.FieldType));
                            continue;
                        }
                    }

                    // Test for SSE support
                    if ((cpuInfo.SimdSupport & SimdTechnology.SSE) != 0)
                    {
                        string entryPoint = String.Format("{0}_{1}", fieldInfo.Name, "SSE");
                        IntPtr address    = GetProcAddress.GetAddress(AssemblyPath, entryPoint);

                        if (address != IntPtr.Zero)
                        {
                            fieldInfo.SetValue(null, Marshal.GetDelegateForFunctionPointer(address, fieldInfo.FieldType));
                            continue;
                        }
                    }

                    // Test for MMX support
                    if ((cpuInfo.SimdSupport & SimdTechnology.MMX) != 0)
                    {
                        string entryPoint = String.Format("{0}_{1}", fieldInfo.Name, "MMX");
                        IntPtr address    = GetProcAddress.GetAddress(AssemblyPath, entryPoint);

                        if (address != IntPtr.Zero)
                        {
                            fieldInfo.SetValue(null, Marshal.GetDelegateForFunctionPointer(address, fieldInfo.FieldType));
                            continue;
                        }
                    }

                    // Reset field
                    fieldInfo.SetValue(null, null);
                }
            }
        }