1 #pragma warning disable 1591
5 using System.Runtime.InteropServices;
13 public delegate int EinaCompareCb(IntPtr data1, IntPtr data2);
14 public delegate void EinaFreeCb(IntPtr data);
18 internal static class NativeCustomExportFunctions
20 [DllImport(efl.Libs.CustomExports)] public static extern void
21 efl_mono_native_free(IntPtr ptr);
22 [DllImport(efl.Libs.CustomExports)] public static extern void
23 efl_mono_native_free_ref(IntPtr ptr);
24 [DllImport(efl.Libs.CustomExports)] public static extern IntPtr
25 efl_mono_native_alloc(uint count);
26 [DllImport(efl.Libs.CustomExports)] public static extern IntPtr
27 efl_mono_native_memset(IntPtr ptr, uint fill, uint count);
28 [DllImport(efl.Libs.CustomExports)] public static extern IntPtr
29 efl_mono_native_alloc_copy(IntPtr val, uint size);
30 [DllImport(efl.Libs.CustomExports)] public static extern IntPtr
31 efl_mono_native_strdup(string str);
33 [DllImport(efl.Libs.CustomExports)] public static extern IntPtr
34 efl_mono_native_ptr_compare_addr_get();
35 [DllImport(efl.Libs.CustomExports)] public static extern IntPtr
36 efl_mono_native_str_compare_addr_get();
38 [DllImport(efl.Libs.CustomExports)] public static extern IntPtr
39 efl_mono_native_free_addr_get();
40 [DllImport(efl.Libs.CustomExports)] public static extern IntPtr
41 efl_mono_native_efl_unref_addr_get();
44 /// <summary>Wrapper around native memory DllImport'd functions</summary>
45 public static class MemoryNative
47 public static void Free(IntPtr ptr)
49 NativeCustomExportFunctions.efl_mono_native_free(ptr);
52 public static void FreeRef(IntPtr ptr)
54 NativeCustomExportFunctions.efl_mono_native_free_ref(ptr);
57 // This public api uses int as Marshal.SizeOf return an int instead of uint.
58 public static IntPtr Alloc(int count)
60 return NativeCustomExportFunctions.efl_mono_native_alloc(Convert.ToUInt32(count));
63 public static void Memset(IntPtr ptr, int fill, int count)
65 NativeCustomExportFunctions.efl_mono_native_memset(ptr, Convert.ToUInt32(fill), Convert.ToUInt32(count));
68 public static IntPtr AllocCopy(IntPtr ptr, int count)
70 return NativeCustomExportFunctions.efl_mono_native_alloc_copy(ptr, Convert.ToUInt32(count));
73 public static IntPtr StrDup(string str)
75 return NativeCustomExportFunctions.efl_mono_native_strdup(str);
78 // IntPtr's for some native functions
79 public static IntPtr PtrCompareFuncPtrGet()
81 return NativeCustomExportFunctions.efl_mono_native_ptr_compare_addr_get();
84 public static IntPtr StrCompareFuncPtrGet()
86 return NativeCustomExportFunctions.efl_mono_native_str_compare_addr_get();
89 public static IntPtr FreeFuncPtrGet()
91 return NativeCustomExportFunctions.efl_mono_native_free_addr_get();
94 public static IntPtr EflUnrefFuncPtrGet()
96 return NativeCustomExportFunctions.efl_mono_native_efl_unref_addr_get();
100 public static class PrimitiveConversion
102 public static T PointerToManaged<T>(IntPtr nat)
104 if (nat == IntPtr.Zero)
106 Eina.Log.Error("Null pointer for primitive type.");
110 var w = Marshal.PtrToStructure<T>(nat);
114 public static IntPtr ManagedToPointerAlloc<T>(T man)
116 IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf<T>());
117 Marshal.StructureToPtr(man, ptr, false);
122 public static class StringConversion
124 public static IntPtr ManagedStringToNativeUtf8Alloc(string managedString)
126 if (managedString == null)
131 byte[] strbuf = Encoding.UTF8.GetBytes(managedString);
132 IntPtr native = MemoryNative.Alloc(strbuf.Length + 1);
133 Marshal.Copy(strbuf, 0, native, strbuf.Length);
134 Marshal.WriteByte(native + strbuf.Length, 0); // write the terminating null
138 public static string NativeUtf8ToManagedString(IntPtr pNativeData)
140 if (pNativeData == IntPtr.Zero)
146 while (Marshal.ReadByte(pNativeData, len) != 0)
151 byte[] strbuf = new byte[len];
152 Marshal.Copy(pNativeData, strbuf, 0, strbuf.Length);
153 return Encoding.UTF8.GetString(strbuf);
157 /// <summary>Enum to handle resource ownership between managed and unmanaged code.</summary>
158 public enum Ownership
160 /// <summary> The resource is owned by the managed code. It should free the handle on disposal.</summary>
162 /// <summary> The resource is owned by the unmanaged code. It won't be freed on disposal.</summary>