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 public static IntPtr AddStringshare(string str)
80 IntPtr nativeStr = StringConversion.ManagedStringToNativeUtf8Alloc(str);
83 var strShare = NativeMethods.eina_stringshare_add(nativeStr);
88 Eina.MemoryNative.Free(nativeStr);
92 public static void DelStringshare(IntPtr str)
94 NativeMethods.eina_stringshare_del(str);
97 public static void DelStringshareRef(IntPtr ptr)
99 NativeMethods.efl_mono_native_stringshare_del_ref(ptr);
102 // IntPtr's for some native functions
103 public static IntPtr PtrCompareFuncPtrGet()
105 return NativeCustomExportFunctions.efl_mono_native_ptr_compare_addr_get();
108 public static IntPtr StrCompareFuncPtrGet()
110 return NativeCustomExportFunctions.efl_mono_native_str_compare_addr_get();
113 public static IntPtr FreeFuncPtrGet()
115 return NativeCustomExportFunctions.efl_mono_native_free_addr_get();
118 public static IntPtr StringshareDelFuncPtrGet()
120 return NativeMethods.efl_mono_native_stringshare_del_addr_get();
123 public static IntPtr EflUnrefFuncPtrGet()
125 return NativeCustomExportFunctions.efl_mono_native_efl_unref_addr_get();
129 public static class PrimitiveConversion
131 public static T PointerToManaged<T>(IntPtr nat)
133 if (nat == IntPtr.Zero)
135 Eina.Log.Error("Null pointer for primitive type.");
139 var w = Marshal.PtrToStructure<T>(nat);
143 public static IntPtr ManagedToPointerAlloc<T>(T man)
145 IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf<T>());
146 Marshal.StructureToPtr(man, ptr, false);
151 public static class StringConversion
153 public static IntPtr ManagedStringToNativeUtf8Alloc(string managedString)
155 if (managedString == null)
160 byte[] strbuf = Encoding.UTF8.GetBytes(managedString);
161 IntPtr native = MemoryNative.Alloc(strbuf.Length + 1);
164 Marshal.Copy(strbuf, 0, native, strbuf.Length);
165 Marshal.WriteByte(native + strbuf.Length, 0); // write the terminating null
170 MemoryNative.Free(native);
175 public static string NativeUtf8ToManagedString(IntPtr pNativeData)
177 if (pNativeData == IntPtr.Zero)
183 while (Marshal.ReadByte(pNativeData, len) != 0)
188 byte[] strbuf = new byte[len];
189 Marshal.Copy(pNativeData, strbuf, 0, strbuf.Length);
190 return Encoding.UTF8.GetString(strbuf);
194 /// <summary>Enum to handle resource ownership between managed and unmanaged code.</summary>
195 public enum Ownership
197 /// <summary> The resource is owned by the managed code. It should free the handle on disposal.</summary>
199 /// <summary> The resource is owned by the unmanaged code. It won't be freed on disposal.</summary>