diff options
author | Felipe Magno de Almeida <felipe@expertisesolutions.com.br> | 2019-01-26 20:05:37 +0900 |
---|---|---|
committer | Felipe Magno de Almeida <felipe@expertisesolutions.com.br> | 2019-01-31 17:46:44 +0900 |
commit | ed3b4cd29f474ea386c7ac9e8489e3fa6b25ca00 (patch) | |
tree | 65d1ef3e8704e1424a8007005dbe13085be11d39 | |
parent | 81e1688fb6477dc10984b2189e4845bca6c44006 (diff) | |
download | efl-ed3b4cd29f474ea386c7ac9e8489e3fa6b25ca00.tar.gz |
efl-mono: Make workarounds for DotNet core 2.x
-rw-r--r-- | src/bindings/mono/eina_mono/eina_container_common.cs | 75 | ||||
-rw-r--r-- | src/bindings/mono/eina_mono/eina_hash.cs | 171 |
2 files changed, 169 insertions, 77 deletions
diff --git a/src/bindings/mono/eina_mono/eina_container_common.cs b/src/bindings/mono/eina_mono/eina_container_common.cs index f187149000..2f7b3dde1c 100644 --- a/src/bindings/mono/eina_mono/eina_container_common.cs +++ b/src/bindings/mono/eina_mono/eina_container_common.cs @@ -38,17 +38,14 @@ public struct InlistNode<T> public interface IBaseElementTraits<T> { IntPtr ManagedToNativeAlloc(T man); - IntPtr ManagedToNativeAllocRef(T man, bool refs); IntPtr ManagedToNativeAllocInlistNode(T man); void ManagedToNativeCopyTo(T man, IntPtr mem); void NativeFree(IntPtr nat); - void NativeFreeRef(IntPtr nat, bool unrefs); void NativeFreeInlistNodeElement(IntPtr nat); void NativeFreeInlistNode(IntPtr nat, bool freeElement); void NativeFreeInplace(IntPtr nat); void ResidueFreeInplace(IntPtr nat); T NativeToManaged(IntPtr nat); - T NativeToManagedRef(IntPtr nat); T NativeToManagedInlistNode(IntPtr nat); T NativeToManagedInplace(IntPtr nat); IntPtr EinaCompareCb(); @@ -70,12 +67,6 @@ public class StringElementTraits : IBaseElementTraits<string> return newstring; } - public IntPtr ManagedToNativeAllocRef(string man, bool refs) - { - // Keep alloc on C# ? - return ManagedToNativeAlloc(man); - } - public IntPtr ManagedToNativeAllocInlistNode(string man) { var node = new InlistNode<IntPtr>(); @@ -100,17 +91,13 @@ public class StringElementTraits : IBaseElementTraits<string> MemoryNative.Free(nat); } - public void NativeFreeRef(IntPtr nat, bool unrefs) - { - NativeFree(nat); - } - public void NativeFreeInlistNodeElement(IntPtr nat) { if (nat == IntPtr.Zero) return; - var node = Marshal.PtrToStructure< InlistNode<IntPtr> >(nat); - NativeFree(node.Val); + var val = Marshal.PtrToStructure<IntPtr> + (nat + Marshal.SizeOf<InlistMem>()); + NativeFree(val); } public void NativeFreeInlistNode(IntPtr nat, bool freeElement) @@ -124,7 +111,7 @@ public class StringElementTraits : IBaseElementTraits<string> public void NativeFreeInplace(IntPtr nat) { - MemoryNative.FreeRef(nat); + // MemoryNative.FreeRef(nat); } public void ResidueFreeInplace(IntPtr nat) @@ -139,11 +126,6 @@ public class StringElementTraits : IBaseElementTraits<string> return StringConversion.NativeUtf8ToManagedString(nat); } - public string NativeToManagedRef(IntPtr nat) - { - return NativeToManaged(nat); - } - public string NativeToManagedInlistNode(IntPtr nat) { if (nat == IntPtr.Zero) @@ -209,11 +191,11 @@ public class EflObjectElementTraits<T> : IBaseElementTraits<T> return Efl.Eo.Globals.efl_ref(h); } - public IntPtr ManagedToNativeAllocRef(T man, bool refs) - { - IntPtr h = refs ? ManagedToNativeAlloc(man) : ((Efl.Eo.IWrapper)man).NativeHandle; - return h; - } + // public IntPtr ManagedToNativeAllocRef(T man, bool refs) + // { + // IntPtr h = refs ? ManagedToNativeAlloc(man) : ((Efl.Eo.IWrapper)man).NativeHandle; + // return h; + // } public IntPtr ManagedToNativeAllocInlistNode(T man) { @@ -238,18 +220,19 @@ public class EflObjectElementTraits<T> : IBaseElementTraits<T> Efl.Eo.Globals.efl_unref(nat); } - public void NativeFreeRef(IntPtr nat, bool unrefs) - { - if (unrefs) - NativeFree(nat); - } + // public void NativeFreeRef(IntPtr nat, bool unrefs) + // { + // if (unrefs) + // NativeFree(nat); + // } public void NativeFreeInlistNodeElement(IntPtr nat) { if (nat == IntPtr.Zero) return; - var node = Marshal.PtrToStructure< InlistNode<IntPtr> >(nat); - NativeFree(node.Val); + var val = Marshal.PtrToStructure<IntPtr> + (nat + Marshal.SizeOf<InlistMem>()); + NativeFree(val); } public void NativeFreeInlistNode(IntPtr nat, bool freeElement) @@ -766,10 +749,10 @@ public static class TraitFunctions return GetTypeTraits<T>().ManagedToNativeAlloc(man); } - public static IntPtr ManagedToNativeAllocRef<T>(T man, bool refs = false) - { - return GetTypeTraits<T>().ManagedToNativeAllocRef(man, refs); - } + // public static IntPtr ManagedToNativeAllocRef<T>(T man, bool refs = false) + // { + // return GetTypeTraits<T>().ManagedToNativeAllocRef(man, refs); + // } public static void ManagedToNativeCopyTo<T>(T man, IntPtr mem) { @@ -786,10 +769,10 @@ public static class TraitFunctions GetTypeTraits<T>().NativeFree(nat); } - public static void NativeFreeRef<T>(IntPtr nat, bool unrefs = false) - { - GetTypeTraits<T>().NativeFreeRef(nat, unrefs); - } + // public static void NativeFreeRef<T>(IntPtr nat, bool unrefs = false) + // { + // GetTypeTraits<T>().NativeFreeRef(nat, unrefs); + // } public static void NativeFreeInlistNodeElement<T>(IntPtr nat) { @@ -816,10 +799,10 @@ public static class TraitFunctions return GetTypeTraits<T>().NativeToManaged(nat); } - public static T NativeToManagedRef<T>(IntPtr nat) - { - return GetTypeTraits<T>().NativeToManagedRef(nat); - } + // public static T NativeToManagedRef<T>(IntPtr nat) + // { + // return GetTypeTraits<T>().NativeToManagedRef(nat); + // } public static T NativeToManagedInlistNode<T>(IntPtr nat) { diff --git a/src/bindings/mono/eina_mono/eina_hash.cs b/src/bindings/mono/eina_mono/eina_hash.cs index 590033e7ab..c13f7d01d8 100644 --- a/src/bindings/mono/eina_mono/eina_hash.cs +++ b/src/bindings/mono/eina_mono/eina_hash.cs @@ -242,10 +242,13 @@ public class Hash<TKey, TValue> : IEnumerable<KeyValuePair<TKey, TValue>>, IDi public bool AddNew(TKey key, TValue val) { - var nk = ManagedToNativeAllocRef(key, true); - var nv = ManagedToNativeAlloc(val); + IntPtr gchnk = CopyNativeObject(key, ForceRefKey<TKey>()); + IntPtr nk = GetNativePtr<TKey>(gchnk, ForceRefKey<TKey>()); + IntPtr gchnv = CopyNativeObject(val, false); + IntPtr nv = GetNativePtr<TValue>(gchnv, false); var r = eina_hash_add(Handle, nk, nv); - NativeFreeRef<TKey>(nk); + FreeNativeIndirection<TKey>(gchnk, ForceRefKey<TKey>()); + FreeNativeIndirection<TValue>(gchnv, false); return r; } @@ -256,17 +259,20 @@ public class Hash<TKey, TValue> : IEnumerable<KeyValuePair<TKey, TValue>>, IDi public bool DelByKey(TKey key) { - var nk = ManagedToNativeAllocRef(key); + IntPtr gchnk = CopyNativeObject(key, ForceRefKey<TKey>()); + IntPtr nk = GetNativePtr<TKey>(gchnk, ForceRefKey<TKey>()); var r = eina_hash_del_by_key(Handle, nk); - NativeFreeRef<TKey>(nk, OwnKey && r); + FreeNativeIndirection<TKey>(gchnk, ForceRefKey<TKey>()); + // NativeFreeRef<TKey>(nk, OwnKey && r); return r; } public bool DelByValue(TValue val) { - var nv = ManagedToNativeAlloc(val); + IntPtr gchnv = CopyNativeObject(val, false); + IntPtr nv = GetNativePtr<TValue>(gchnv, false); var r = eina_hash_del_by_data(Handle, nv); - NativeFree<TValue>(nv); + FreeNativeIndirection<TValue>(gchnv, false); return r; } @@ -277,42 +283,52 @@ public class Hash<TKey, TValue> : IEnumerable<KeyValuePair<TKey, TValue>>, IDi public TValue Find(TKey key) { - var nk = ManagedToNativeAllocRef(key); + var gchnk = CopyNativeObject<TKey>(key, ForceRefKey<TKey>()); + var nk = GetNativePtr<TKey>(gchnk, ForceRefKey<TKey>()); var found = eina_hash_find(Handle, nk); - NativeFreeRef<TKey>(nk); + //NativeFreeRef<TKey>(nk); + FreeNativeIndirection<TKey>(gchnk, ForceRefKey<TKey>()); if (found == IntPtr.Zero) throw new KeyNotFoundException(); - return NativeToManaged<TValue>(found); + + return NativeToManaged<TValue>(IndirectNative<TValue>(found, false)); } public bool TryGetValue(TKey key, out TValue val) { - var nk = ManagedToNativeAllocRef(key); + var gchnk = CopyNativeObject<TKey>(key, ForceRefKey<TKey>()); + var nk = GetNativePtr<TKey>(gchnk, ForceRefKey<TKey>()); var found = eina_hash_find(Handle, nk); - NativeFreeRef<TKey>(nk); + FreeNativeIndirection<TKey>(gchnk, ForceRefKey<TKey>()); if (found == IntPtr.Zero) { val = default(TValue); return false; } - val = NativeToManaged<TValue>(found); + val = NativeToManaged<TValue>(IndirectNative<TValue>(found, false)); return true; } public bool ContainsKey(TKey key) { - var nk = ManagedToNativeAllocRef(key); + var gchnk = CopyNativeObject<TKey>(key, ForceRefKey<TKey>()); + var nk = GetNativePtr<TKey>(gchnk, ForceRefKey<TKey>()); + // var nk = ManagedToNativeAllocRef(key); var found = eina_hash_find(Handle, nk); - NativeFreeRef<TKey>(nk); + // NativeFreeRef<TKey>(nk); + FreeNativeIndirection<TKey>(gchnk, ForceRefKey<TKey>()); return found != IntPtr.Zero; } public bool Modify(TKey key, TValue val) { - var nk = ManagedToNativeAllocRef(key); - var nv = ManagedToNativeAlloc(val); + var gchnk = CopyNativeObject<TKey>(key, ForceRefKey<TKey>()); + var nk = GetNativePtr<TKey>(gchnk, ForceRefKey<TKey>()); + var gchnv = CopyNativeObject<TValue>(val, false); + var nv = GetNativePtr<TValue>(gchnv, false); var old = eina_hash_modify(Handle, nk, nv); - NativeFreeRef<TKey>(nk); + FreeNativeIndirection<TKey>(gchnk, ForceRefKey<TKey>()); + // NativeFreeRef<TKey>(nk); if (old == IntPtr.Zero) { NativeFree<TValue>(nv); @@ -323,14 +339,100 @@ public class Hash<TKey, TValue> : IEnumerable<KeyValuePair<TKey, TValue>>, IDi return true; } + private static bool ForceRefKey<T>() + { + return typeof(T) != typeof(string); + } + + private static IntPtr CopyNativeObject<T>(T value, bool forceRef) + { + if (!IsEflObject(typeof(T)) && forceRef) + { + GCHandle gch = GCHandle.Alloc(new byte[Marshal.SizeOf<T>()], GCHandleType.Pinned); + IntPtr pin = gch.AddrOfPinnedObject(); + + Console.WriteLine("address pinned: 0x{0:X}", pin.ToInt64()); + + ManagedToNativeCopyTo(value, pin); + + return GCHandle.ToIntPtr(gch); + } + else if(IsEflObject(typeof(T)) && forceRef) + { + GCHandle gch = GCHandle.Alloc(new byte[Marshal.SizeOf<IntPtr>()], GCHandleType.Pinned); + IntPtr pin = gch.AddrOfPinnedObject(); + + Console.WriteLine("address pinned: 0x{0:X}", pin.ToInt64()); + + ManagedToNativeCopyTo(value, pin); + + return GCHandle.ToIntPtr(gch); + } + else + { + return ManagedToNativeAlloc(value); + } + } + private static IntPtr GetNativePtr<T>(IntPtr gchptr, bool forceRef) + { + if (forceRef) + { + GCHandle gch = GCHandle.FromIntPtr(gchptr); + IntPtr pin = gch.AddrOfPinnedObject(); + + Console.WriteLine("address pinned: 0x{0:X}", pin.ToInt64()); + + return pin; + } + else + { + return gchptr; + } + } + private static void FreeNativeIndirection<T>(IntPtr gchptr, bool forceRef) + { + if (forceRef) + { + GCHandle gch = GCHandle.FromIntPtr(gchptr); + gch.Free(); + } + } + + private static IntPtr IndirectNative<T>(IntPtr ptr, bool forceRef) + { + if (forceRef) + { + IntPtr val = Marshal.ReadIntPtr(ptr); + return val; + } + else + { + return ptr; + } + } + public void Set(TKey key, TValue val) { - var nk = ManagedToNativeAllocRef(key, true); - var nv = ManagedToNativeAlloc(val); - var old = eina_hash_set(Handle, nk, nv); - NativeFreeRef<TKey>(nk, old != IntPtr.Zero); - if (old != IntPtr.Zero && OwnValue) - NativeFree<TValue>(old); + Console.WriteLine("Set"); + IntPtr gchnk = CopyNativeObject(key, ForceRefKey<TKey>()); + IntPtr nk = GetNativePtr<TKey>(gchnk, ForceRefKey<TKey>()); + // Console.WriteLine("key {0}", key); + // Console.WriteLine("nk: 0x{0:X}", nk.ToInt64()); + // if (ForceRefKey<TKey>()) + // { + // IntPtr ink = Marshal.ReadIntPtr(nk); + // Console.WriteLine("*nk 0x{0:X}", ink.ToInt32()); + // } + + IntPtr gchnv = CopyNativeObject(val, false); + IntPtr nv = GetNativePtr<TValue>(gchnv, false); + IntPtr old = eina_hash_set(Handle, nk, nv); + FreeNativeIndirection<TKey>(gchnk, ForceRefKey<TKey>()); + FreeNativeIndirection<TValue>(gchnv, false); + // NativeFreeRef<TKey>(nk, old != IntPtr.Zero); + // if (old != IntPtr.Zero && OwnValue) + // NativeFree<TValue>(old); + } public TValue this[TKey key] @@ -347,11 +449,17 @@ public class Hash<TKey, TValue> : IEnumerable<KeyValuePair<TKey, TValue>>, IDi public bool Move(TKey key_old, TKey key_new) { - var nk_old = ManagedToNativeAllocRef(key_old); - var nk_new = ManagedToNativeAllocRef(key_new, true); - var r = eina_hash_move(Handle, nk_old, nk_new); - NativeFreeRef<TKey>(nk_old, OwnKey && r); - NativeFreeRef<TKey>(nk_new, !r); + IntPtr gchnko = CopyNativeObject(key_old, ForceRefKey<TKey>()); + IntPtr nko = GetNativePtr<TKey>(gchnko, ForceRefKey<TKey>()); + IntPtr gchnk = CopyNativeObject(key_new, ForceRefKey<TKey>()); + IntPtr nk = GetNativePtr<TKey>(gchnk, ForceRefKey<TKey>()); + // var nk_old = ManagedToNativeAllocRef(key_old); + // var nk_new = ManagedToNativeAllocRef(key_new, true); + var r = eina_hash_move(Handle, nko, nk); + FreeNativeIndirection<TKey>(gchnko, ForceRefKey<TKey>()); + FreeNativeIndirection<TKey>(gchnk, ForceRefKey<TKey>()); + // NativeFreeRef<TKey>(nk_old, OwnKey && r); + // NativeFreeRef<TKey>(nk_new, !r); return r; } @@ -383,9 +491,10 @@ public class Hash<TKey, TValue> : IEnumerable<KeyValuePair<TKey, TValue>>, IDi for (IntPtr tuplePtr; eina_iterator_next(itr, out tuplePtr);) { var tuple = Marshal.PtrToStructure<Eina.HashTupleNative>(tuplePtr); - var key = NativeToManagedRef<TKey>(tuple.key); + IntPtr key = IndirectNative<TKey>(tuple.key, ForceRefKey<TKey>()); + var ikey = NativeToManaged<TKey>(tuple.key); var val = NativeToManaged<TValue>(tuple.data); - yield return new KeyValuePair<TKey, TValue>(key, val); + yield return new KeyValuePair<TKey, TValue>(ikey, val); } } finally |