[Mac] Moved GetAddress implementation to its own class
authorthefiddler <stapostol@gmail.com>
Sat, 28 Dec 2013 00:06:33 +0000 (01:06 +0100)
committerthefiddler <stapostol@gmail.com>
Sat, 28 Dec 2013 00:06:33 +0000 (01:06 +0100)
The functions defined in libdl.dylib are useful in more places than
just AglContext. Moving them to their own class ensures we can access
these from wherever we might need them.

Source/OpenTK/Platform/MacOS/AglContext.cs
Source/OpenTK/Platform/MacOS/NS.cs [new file with mode: 0644]

index 65bdd0a..d1de666 100644 (file)
@@ -464,64 +464,16 @@ namespace OpenTK.Platform.MacOS
 
         #region IGraphicsContextInternal Members
 
-        private const string Library = "libdl.dylib";
-
-        [DllImport(Library, EntryPoint = "NSIsSymbolNameDefined")]
-        private static extern bool NSIsSymbolNameDefined(string s);
-        [DllImport(Library, EntryPoint = "NSIsSymbolNameDefined")]
-        private static extern bool NSIsSymbolNameDefined(IntPtr s);
-        [DllImport(Library, EntryPoint = "NSLookupAndBindSymbol")]
-        private static extern IntPtr NSLookupAndBindSymbol(string s);
-        [DllImport(Library, EntryPoint = "NSLookupAndBindSymbol")]
-        private static extern IntPtr NSLookupAndBindSymbol(IntPtr s);
-        [DllImport(Library, EntryPoint = "NSAddressOfSymbol")]
-        private static extern IntPtr NSAddressOfSymbol(IntPtr symbol);
-
         public override IntPtr GetAddress(string function)
         {
-            // Instead of allocating and combining strings in managed memory
-            // we do that directly in unmanaged memory. This way, we avoid
-            // 2 string allocations every time this function is called.
-
-            // must add a '_' prefix and null-terminate the function name,
-            // hence we allocate +2 bytes
-            IntPtr ptr = Marshal.AllocHGlobal(function.Length + 2);
-            try
-            {
-                Marshal.WriteByte(ptr, (byte)'_');
-                for (int i = 0; i < function.Length; i++)
-                {
-                    Marshal.WriteByte(ptr, i + 1, (byte)function[i]);
-                }
-                Marshal.WriteByte(ptr, function.Length + 1, 0); // null-terminate
-
-                IntPtr symbol = IntPtr.Zero;
-                if (NSIsSymbolNameDefined(ptr))
-                {
-                    symbol = NSLookupAndBindSymbol(ptr);
-                    if (symbol != IntPtr.Zero)
-                        symbol = NSAddressOfSymbol(symbol);
-                }
-                return symbol;
-            }
-            finally
-            {
-                Marshal.FreeHGlobal(ptr);
-            }
+            return NS.GetAddress(function);
         }
 
         public override IntPtr GetAddress(IntPtr function)
         {
-            if (!NSIsSymbolNameDefined(function))
-                return IntPtr.Zero;
-
-            IntPtr symbol = NSLookupAndBindSymbol(function);
-            if (symbol != IntPtr.Zero)
-                symbol = NSAddressOfSymbol(symbol);
-
-            return symbol;
+            return NS.GetAddress(function);
         }
-        
+
         #endregion
     }
 }
diff --git a/Source/OpenTK/Platform/MacOS/NS.cs b/Source/OpenTK/Platform/MacOS/NS.cs
new file mode 100644 (file)
index 0000000..945a7e2
--- /dev/null
@@ -0,0 +1,92 @@
+#region License
+//
+// NS.cs
+//
+// Author:
+//       Stefanos A. <stapostol@gmail.com>
+//
+// Copyright (c) 2006-2013 Stefanos Apostolopoulos
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+//
+
+#endregion
+
+using System;
+using System.Runtime.InteropServices;
+
+namespace OpenTK.Platform.MacOS
+{
+
+    internal class NS
+    {
+        const string Library = "libdl.dylib";
+
+        [DllImport(Library, EntryPoint = "NSIsSymbolNameDefined")]
+        static extern bool NSIsSymbolNameDefined(string s);
+        [DllImport(Library, EntryPoint = "NSIsSymbolNameDefined")]
+        static extern bool NSIsSymbolNameDefined(IntPtr s);
+        [DllImport(Library, EntryPoint = "NSLookupAndBindSymbol")]
+        static extern IntPtr NSLookupAndBindSymbol(string s);
+        [DllImport(Library, EntryPoint = "NSLookupAndBindSymbol")]
+        static extern IntPtr NSLookupAndBindSymbol(IntPtr s);
+        [DllImport(Library, EntryPoint = "NSAddressOfSymbol")]
+        static extern IntPtr NSAddressOfSymbol(IntPtr symbol);
+
+        public static IntPtr GetAddress(string function)
+        {
+            // Instead of allocating and combining strings in managed memory
+            // we do that directly in unmanaged memory. This way, we avoid
+            // 2 string allocations every time this function is called.
+
+            // must add a '_' prefix and null-terminate the function name,
+            // hence we allocate +2 bytes
+            IntPtr ptr = Marshal.AllocHGlobal(function.Length + 2);
+            try
+            {
+                Marshal.WriteByte(ptr, (byte)'_');
+                for (int i = 0; i < function.Length; i++)
+                {
+                    Marshal.WriteByte(ptr, i + 1, (byte)function[i]);
+                }
+                Marshal.WriteByte(ptr, function.Length + 1, 0); // null-terminate
+
+                IntPtr symbol = GetAddress(ptr);
+                return symbol;
+            }
+            finally
+            {
+                Marshal.FreeHGlobal(ptr);
+            }
+        }
+
+        public static IntPtr GetAddress(IntPtr function)
+        {
+            IntPtr symbol = IntPtr.Zero;
+            if (NSIsSymbolNameDefined(function))
+            {
+                symbol = NSLookupAndBindSymbol(function);
+                if (symbol != IntPtr.Zero)
+                    symbol = NSAddressOfSymbol(symbol);
+            }
+            return symbol;
+        }
+    }
+}
+