summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFelipe Magno de Almeida <felipe@expertisesolutions.com.br>2019-01-26 20:05:37 +0900
committerFelipe Magno de Almeida <felipe@expertisesolutions.com.br>2019-01-31 17:46:44 +0900
commited3b4cd29f474ea386c7ac9e8489e3fa6b25ca00 (patch)
tree65d1ef3e8704e1424a8007005dbe13085be11d39
parent81e1688fb6477dc10984b2189e4845bca6c44006 (diff)
efl-mono: Make workarounds for DotNet core 2.x
-rw-r--r--src/bindings/mono/eina_mono/eina_container_common.cs75
-rw-r--r--src/bindings/mono/eina_mono/eina_hash.cs171
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>
38public interface IBaseElementTraits<T> 38public interface IBaseElementTraits<T>
39{ 39{
40 IntPtr ManagedToNativeAlloc(T man); 40 IntPtr ManagedToNativeAlloc(T man);
41 IntPtr ManagedToNativeAllocRef(T man, bool refs);
42 IntPtr ManagedToNativeAllocInlistNode(T man); 41 IntPtr ManagedToNativeAllocInlistNode(T man);
43 void ManagedToNativeCopyTo(T man, IntPtr mem); 42 void ManagedToNativeCopyTo(T man, IntPtr mem);
44 void NativeFree(IntPtr nat); 43 void NativeFree(IntPtr nat);
45 void NativeFreeRef(IntPtr nat, bool unrefs);
46 void NativeFreeInlistNodeElement(IntPtr nat); 44 void NativeFreeInlistNodeElement(IntPtr nat);
47 void NativeFreeInlistNode(IntPtr nat, bool freeElement); 45 void NativeFreeInlistNode(IntPtr nat, bool freeElement);
48 void NativeFreeInplace(IntPtr nat); 46 void NativeFreeInplace(IntPtr nat);
49 void ResidueFreeInplace(IntPtr nat); 47 void ResidueFreeInplace(IntPtr nat);
50 T NativeToManaged(IntPtr nat); 48 T NativeToManaged(IntPtr nat);
51 T NativeToManagedRef(IntPtr nat);
52 T NativeToManagedInlistNode(IntPtr nat); 49 T NativeToManagedInlistNode(IntPtr nat);
53 T NativeToManagedInplace(IntPtr nat); 50 T NativeToManagedInplace(IntPtr nat);
54 IntPtr EinaCompareCb(); 51 IntPtr EinaCompareCb();
@@ -70,12 +67,6 @@ public class StringElementTraits : IBaseElementTraits<string>
70 return newstring; 67 return newstring;
71 } 68 }
72 69
73 public IntPtr ManagedToNativeAllocRef(string man, bool refs)
74 {
75 // Keep alloc on C# ?
76 return ManagedToNativeAlloc(man);
77 }
78
79 public IntPtr ManagedToNativeAllocInlistNode(string man) 70 public IntPtr ManagedToNativeAllocInlistNode(string man)
80 { 71 {
81 var node = new InlistNode<IntPtr>(); 72 var node = new InlistNode<IntPtr>();
@@ -100,17 +91,13 @@ public class StringElementTraits : IBaseElementTraits<string>
100 MemoryNative.Free(nat); 91 MemoryNative.Free(nat);
101 } 92 }
102 93
103 public void NativeFreeRef(IntPtr nat, bool unrefs)
104 {
105 NativeFree(nat);
106 }
107
108 public void NativeFreeInlistNodeElement(IntPtr nat) 94 public void NativeFreeInlistNodeElement(IntPtr nat)
109 { 95 {
110 if (nat == IntPtr.Zero) 96 if (nat == IntPtr.Zero)
111 return; 97 return;
112 var node = Marshal.PtrToStructure< InlistNode<IntPtr> >(nat); 98 var val = Marshal.PtrToStructure<IntPtr>
113 NativeFree(node.Val); 99 (nat + Marshal.SizeOf<InlistMem>());
100 NativeFree(val);
114 } 101 }
115 102
116 public void NativeFreeInlistNode(IntPtr nat, bool freeElement) 103 public void NativeFreeInlistNode(IntPtr nat, bool freeElement)
@@ -124,7 +111,7 @@ public class StringElementTraits : IBaseElementTraits<string>
124 111
125 public void NativeFreeInplace(IntPtr nat) 112 public void NativeFreeInplace(IntPtr nat)
126 { 113 {
127 MemoryNative.FreeRef(nat); 114 // MemoryNative.FreeRef(nat);
128 } 115 }
129 116
130 public void ResidueFreeInplace(IntPtr nat) 117 public void ResidueFreeInplace(IntPtr nat)
@@ -139,11 +126,6 @@ public class StringElementTraits : IBaseElementTraits<string>
139 return StringConversion.NativeUtf8ToManagedString(nat); 126 return StringConversion.NativeUtf8ToManagedString(nat);
140 } 127 }
141 128
142 public string NativeToManagedRef(IntPtr nat)
143 {
144 return NativeToManaged(nat);
145 }
146
147 public string NativeToManagedInlistNode(IntPtr nat) 129 public string NativeToManagedInlistNode(IntPtr nat)
148 { 130 {
149 if (nat == IntPtr.Zero) 131 if (nat == IntPtr.Zero)
@@ -209,11 +191,11 @@ public class EflObjectElementTraits<T> : IBaseElementTraits<T>
209 return Efl.Eo.Globals.efl_ref(h); 191 return Efl.Eo.Globals.efl_ref(h);
210 } 192 }
211 193
212 public IntPtr ManagedToNativeAllocRef(T man, bool refs) 194 // public IntPtr ManagedToNativeAllocRef(T man, bool refs)
213 { 195 // {
214 IntPtr h = refs ? ManagedToNativeAlloc(man) : ((Efl.Eo.IWrapper)man).NativeHandle; 196 // IntPtr h = refs ? ManagedToNativeAlloc(man) : ((Efl.Eo.IWrapper)man).NativeHandle;
215 return h; 197 // return h;
216 } 198 // }
217 199
218 public IntPtr ManagedToNativeAllocInlistNode(T man) 200 public IntPtr ManagedToNativeAllocInlistNode(T man)
219 { 201 {
@@ -238,18 +220,19 @@ public class EflObjectElementTraits<T> : IBaseElementTraits<T>
238 Efl.Eo.Globals.efl_unref(nat); 220 Efl.Eo.Globals.efl_unref(nat);
239 } 221 }
240 222
241 public void NativeFreeRef(IntPtr nat, bool unrefs) 223 // public void NativeFreeRef(IntPtr nat, bool unrefs)
242 { 224 // {
243 if (unrefs) 225 // if (unrefs)
244 NativeFree(nat); 226 // NativeFree(nat);
245 } 227 // }
246 228
247 public void NativeFreeInlistNodeElement(IntPtr nat) 229 public void NativeFreeInlistNodeElement(IntPtr nat)
248 { 230 {
249 if (nat == IntPtr.Zero) 231 if (nat == IntPtr.Zero)
250 return; 232 return;
251 var node = Marshal.PtrToStructure< InlistNode<IntPtr> >(nat); 233 var val = Marshal.PtrToStructure<IntPtr>
252 NativeFree(node.Val); 234 (nat + Marshal.SizeOf<InlistMem>());
235 NativeFree(val);
253 } 236 }
254 237
255 public void NativeFreeInlistNode(IntPtr nat, bool freeElement) 238 public void NativeFreeInlistNode(IntPtr nat, bool freeElement)
@@ -766,10 +749,10 @@ public static class TraitFunctions
766 return GetTypeTraits<T>().ManagedToNativeAlloc(man); 749 return GetTypeTraits<T>().ManagedToNativeAlloc(man);
767 } 750 }
768 751
769 public static IntPtr ManagedToNativeAllocRef<T>(T man, bool refs = false) 752 // public static IntPtr ManagedToNativeAllocRef<T>(T man, bool refs = false)
770 { 753 // {
771 return GetTypeTraits<T>().ManagedToNativeAllocRef(man, refs); 754 // return GetTypeTraits<T>().ManagedToNativeAllocRef(man, refs);
772 } 755 // }
773 756
774 public static void ManagedToNativeCopyTo<T>(T man, IntPtr mem) 757 public static void ManagedToNativeCopyTo<T>(T man, IntPtr mem)
775 { 758 {
@@ -786,10 +769,10 @@ public static class TraitFunctions
786 GetTypeTraits<T>().NativeFree(nat); 769 GetTypeTraits<T>().NativeFree(nat);
787 } 770 }
788 771
789 public static void NativeFreeRef<T>(IntPtr nat, bool unrefs = false) 772 // public static void NativeFreeRef<T>(IntPtr nat, bool unrefs = false)
790 { 773 // {
791 GetTypeTraits<T>().NativeFreeRef(nat, unrefs); 774 // GetTypeTraits<T>().NativeFreeRef(nat, unrefs);
792 } 775 // }
793 776
794 public static void NativeFreeInlistNodeElement<T>(IntPtr nat) 777 public static void NativeFreeInlistNodeElement<T>(IntPtr nat)
795 { 778 {
@@ -816,10 +799,10 @@ public static class TraitFunctions
816 return GetTypeTraits<T>().NativeToManaged(nat); 799 return GetTypeTraits<T>().NativeToManaged(nat);
817 } 800 }
818 801
819 public static T NativeToManagedRef<T>(IntPtr nat) 802 // public static T NativeToManagedRef<T>(IntPtr nat)
820 { 803 // {
821 return GetTypeTraits<T>().NativeToManagedRef(nat); 804 // return GetTypeTraits<T>().NativeToManagedRef(nat);
822 } 805 // }
823 806
824 public static T NativeToManagedInlistNode<T>(IntPtr nat) 807 public static T NativeToManagedInlistNode<T>(IntPtr nat)
825 { 808 {
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
242 242
243 public bool AddNew(TKey key, TValue val) 243 public bool AddNew(TKey key, TValue val)
244 { 244 {
245 var nk = ManagedToNativeAllocRef(key, true); 245 IntPtr gchnk = CopyNativeObject(key, ForceRefKey<TKey>());
246 var nv = ManagedToNativeAlloc(val); 246 IntPtr nk = GetNativePtr<TKey>(gchnk, ForceRefKey<TKey>());
247 IntPtr gchnv = CopyNativeObject(val, false);
248 IntPtr nv = GetNativePtr<TValue>(gchnv, false);
247 var r = eina_hash_add(Handle, nk, nv); 249 var r = eina_hash_add(Handle, nk, nv);
248 NativeFreeRef<TKey>(nk); 250 FreeNativeIndirection<TKey>(gchnk, ForceRefKey<TKey>());
251 FreeNativeIndirection<TValue>(gchnv, false);
249 return r; 252 return r;
250 } 253 }
251 254
@@ -256,17 +259,20 @@ public class Hash<TKey, TValue> : IEnumerable<KeyValuePair<TKey, TValue>>, IDi
256 259
257 public bool DelByKey(TKey key) 260 public bool DelByKey(TKey key)
258 { 261 {
259 var nk = ManagedToNativeAllocRef(key); 262 IntPtr gchnk = CopyNativeObject(key, ForceRefKey<TKey>());
263 IntPtr nk = GetNativePtr<TKey>(gchnk, ForceRefKey<TKey>());
260 var r = eina_hash_del_by_key(Handle, nk); 264 var r = eina_hash_del_by_key(Handle, nk);
261 NativeFreeRef<TKey>(nk, OwnKey && r); 265 FreeNativeIndirection<TKey>(gchnk, ForceRefKey<TKey>());
266 // NativeFreeRef<TKey>(nk, OwnKey && r);
262 return r; 267 return r;
263 } 268 }
264 269
265 public bool DelByValue(TValue val) 270 public bool DelByValue(TValue val)
266 { 271 {
267 var nv = ManagedToNativeAlloc(val); 272 IntPtr gchnv = CopyNativeObject(val, false);
273 IntPtr nv = GetNativePtr<TValue>(gchnv, false);
268 var r = eina_hash_del_by_data(Handle, nv); 274 var r = eina_hash_del_by_data(Handle, nv);
269 NativeFree<TValue>(nv); 275 FreeNativeIndirection<TValue>(gchnv, false);
270 return r; 276 return r;
271 } 277 }
272 278
@@ -277,42 +283,52 @@ public class Hash<TKey, TValue> : IEnumerable<KeyValuePair<TKey, TValue>>, IDi
277 283
278 public TValue Find(TKey key) 284 public TValue Find(TKey key)
279 { 285 {
280 var nk = ManagedToNativeAllocRef(key); 286 var gchnk = CopyNativeObject<TKey>(key, ForceRefKey<TKey>());
287 var nk = GetNativePtr<TKey>(gchnk, ForceRefKey<TKey>());
281 var found = eina_hash_find(Handle, nk); 288 var found = eina_hash_find(Handle, nk);
282 NativeFreeRef<TKey>(nk); 289 //NativeFreeRef<TKey>(nk);
290 FreeNativeIndirection<TKey>(gchnk, ForceRefKey<TKey>());
283 if (found == IntPtr.Zero) 291 if (found == IntPtr.Zero)
284 throw new KeyNotFoundException(); 292 throw new KeyNotFoundException();
285 return NativeToManaged<TValue>(found); 293
294 return NativeToManaged<TValue>(IndirectNative<TValue>(found, false));
286 } 295 }
287 296
288 public bool TryGetValue(TKey key, out TValue val) 297 public bool TryGetValue(TKey key, out TValue val)
289 { 298 {
290 var nk = ManagedToNativeAllocRef(key); 299 var gchnk = CopyNativeObject<TKey>(key, ForceRefKey<TKey>());
300 var nk = GetNativePtr<TKey>(gchnk, ForceRefKey<TKey>());
291 var found = eina_hash_find(Handle, nk); 301 var found = eina_hash_find(Handle, nk);
292 NativeFreeRef<TKey>(nk); 302 FreeNativeIndirection<TKey>(gchnk, ForceRefKey<TKey>());
293 if (found == IntPtr.Zero) 303 if (found == IntPtr.Zero)
294 { 304 {
295 val = default(TValue); 305 val = default(TValue);
296 return false; 306 return false;
297 } 307 }
298 val = NativeToManaged<TValue>(found); 308 val = NativeToManaged<TValue>(IndirectNative<TValue>(found, false));
299 return true; 309 return true;
300 } 310 }
301 311
302 public bool ContainsKey(TKey key) 312 public bool ContainsKey(TKey key)
303 { 313 {
304 var nk = ManagedToNativeAllocRef(key); 314 var gchnk = CopyNativeObject<TKey>(key, ForceRefKey<TKey>());
315 var nk = GetNativePtr<TKey>(gchnk, ForceRefKey<TKey>());
316 // var nk = ManagedToNativeAllocRef(key);
305 var found = eina_hash_find(Handle, nk); 317 var found = eina_hash_find(Handle, nk);
306 NativeFreeRef<TKey>(nk); 318 // NativeFreeRef<TKey>(nk);
319 FreeNativeIndirection<TKey>(gchnk, ForceRefKey<TKey>());
307 return found != IntPtr.Zero; 320 return found != IntPtr.Zero;
308 } 321 }
309 322
310 public bool Modify(TKey key, TValue val) 323 public bool Modify(TKey key, TValue val)
311 { 324 {
312 var nk = ManagedToNativeAllocRef(key); 325 var gchnk = CopyNativeObject<TKey>(key, ForceRefKey<TKey>());
313 var nv = ManagedToNativeAlloc(val); 326 var nk = GetNativePtr<TKey>(gchnk, ForceRefKey<TKey>());
327 var gchnv = CopyNativeObject<TValue>(val, false);
328 var nv = GetNativePtr<TValue>(gchnv, false);
314 var old = eina_hash_modify(Handle, nk, nv); 329 var old = eina_hash_modify(Handle, nk, nv);
315 NativeFreeRef<TKey>(nk); 330 FreeNativeIndirection<TKey>(gchnk, ForceRefKey<TKey>());
331 // NativeFreeRef<TKey>(nk);
316 if (old == IntPtr.Zero) 332 if (old == IntPtr.Zero)
317 { 333 {
318 NativeFree<TValue>(nv); 334 NativeFree<TValue>(nv);
@@ -323,14 +339,100 @@ public class Hash<TKey, TValue> : IEnumerable<KeyValuePair<TKey, TValue>>, IDi
323 return true; 339 return true;
324 } 340 }
325 341
342 private static bool ForceRefKey<T>()
343 {
344 return typeof(T) != typeof(string);
345 }
346
347 private static IntPtr CopyNativeObject<T>(T value, bool forceRef)
348 {
349 if (!IsEflObject(typeof(T)) && forceRef)
350 {
351 GCHandle gch = GCHandle.Alloc(new byte[Marshal.SizeOf<T>()], GCHandleType.Pinned);
352 IntPtr pin = gch.AddrOfPinnedObject();
353
354 Console.WriteLine("address pinned: 0x{0:X}", pin.ToInt64());
355
356 ManagedToNativeCopyTo(value, pin);
357
358 return GCHandle.ToIntPtr(gch);
359 }
360 else if(IsEflObject(typeof(T)) && forceRef)
361 {
362 GCHandle gch = GCHandle.Alloc(new byte[Marshal.SizeOf<IntPtr>()], GCHandleType.Pinned);
363 IntPtr pin = gch.AddrOfPinnedObject();
364
365 Console.WriteLine("address pinned: 0x{0:X}", pin.ToInt64());
366
367 ManagedToNativeCopyTo(value, pin);
368
369 return GCHandle.ToIntPtr(gch);
370 }
371 else
372 {
373 return ManagedToNativeAlloc(value);
374 }
375 }
376 private static IntPtr GetNativePtr<T>(IntPtr gchptr, bool forceRef)
377 {
378 if (forceRef)
379 {
380 GCHandle gch = GCHandle.FromIntPtr(gchptr);
381 IntPtr pin = gch.AddrOfPinnedObject();
382
383 Console.WriteLine("address pinned: 0x{0:X}", pin.ToInt64());
384
385 return pin;
386 }
387 else
388 {
389 return gchptr;
390 }
391 }
392 private static void FreeNativeIndirection<T>(IntPtr gchptr, bool forceRef)
393 {
394 if (forceRef)
395 {
396 GCHandle gch = GCHandle.FromIntPtr(gchptr);
397 gch.Free();
398 }
399 }
400
401 private static IntPtr IndirectNative<T>(IntPtr ptr, bool forceRef)
402 {
403 if (forceRef)
404 {
405 IntPtr val = Marshal.ReadIntPtr(ptr);
406 return val;
407 }
408 else
409 {
410 return ptr;
411 }
412 }
413
326 public void Set(TKey key, TValue val) 414 public void Set(TKey key, TValue val)
327 { 415 {
328 var nk = ManagedToNativeAllocRef(key, true); 416 Console.WriteLine("Set");
329 var nv = ManagedToNativeAlloc(val); 417 IntPtr gchnk = CopyNativeObject(key, ForceRefKey<TKey>());
330 var old = eina_hash_set(Handle, nk, nv); 418 IntPtr nk = GetNativePtr<TKey>(gchnk, ForceRefKey<TKey>());
331 NativeFreeRef<TKey>(nk, old != IntPtr.Zero); 419 // Console.WriteLine("key {0}", key);
332 if (old != IntPtr.Zero && OwnValue) 420 // Console.WriteLine("nk: 0x{0:X}", nk.ToInt64());
333 NativeFree<TValue>(old); 421 // if (ForceRefKey<TKey>())
422 // {
423 // IntPtr ink = Marshal.ReadIntPtr(nk);
424 // Console.WriteLine("*nk 0x{0:X}", ink.ToInt32());
425 // }
426
427 IntPtr gchnv = CopyNativeObject(val, false);
428 IntPtr nv = GetNativePtr<TValue>(gchnv, false);
429 IntPtr old = eina_hash_set(Handle, nk, nv);
430 FreeNativeIndirection<TKey>(gchnk, ForceRefKey<TKey>());
431 FreeNativeIndirection<TValue>(gchnv, false);
432 // NativeFreeRef<TKey>(nk, old != IntPtr.Zero);
433 // if (old != IntPtr.Zero && OwnValue)
434 // NativeFree<TValue>(old);
435
334 } 436 }
335 437
336 public TValue this[TKey key] 438 public TValue this[TKey key]
@@ -347,11 +449,17 @@ public class Hash<TKey, TValue> : IEnumerable<KeyValuePair<TKey, TValue>>, IDi
347 449
348 public bool Move(TKey key_old, TKey key_new) 450 public bool Move(TKey key_old, TKey key_new)
349 { 451 {
350 var nk_old = ManagedToNativeAllocRef(key_old); 452 IntPtr gchnko = CopyNativeObject(key_old, ForceRefKey<TKey>());
351 var nk_new = ManagedToNativeAllocRef(key_new, true); 453 IntPtr nko = GetNativePtr<TKey>(gchnko, ForceRefKey<TKey>());
352 var r = eina_hash_move(Handle, nk_old, nk_new); 454 IntPtr gchnk = CopyNativeObject(key_new, ForceRefKey<TKey>());
353 NativeFreeRef<TKey>(nk_old, OwnKey && r); 455 IntPtr nk = GetNativePtr<TKey>(gchnk, ForceRefKey<TKey>());
354 NativeFreeRef<TKey>(nk_new, !r); 456 // var nk_old = ManagedToNativeAllocRef(key_old);
457 // var nk_new = ManagedToNativeAllocRef(key_new, true);
458 var r = eina_hash_move(Handle, nko, nk);
459 FreeNativeIndirection<TKey>(gchnko, ForceRefKey<TKey>());
460 FreeNativeIndirection<TKey>(gchnk, ForceRefKey<TKey>());
461 // NativeFreeRef<TKey>(nk_old, OwnKey && r);
462 // NativeFreeRef<TKey>(nk_new, !r);
355 return r; 463 return r;
356 } 464 }
357 465
@@ -383,9 +491,10 @@ public class Hash<TKey, TValue> : IEnumerable<KeyValuePair<TKey, TValue>>, IDi
383 for (IntPtr tuplePtr; eina_iterator_next(itr, out tuplePtr);) 491 for (IntPtr tuplePtr; eina_iterator_next(itr, out tuplePtr);)
384 { 492 {
385 var tuple = Marshal.PtrToStructure<Eina.HashTupleNative>(tuplePtr); 493 var tuple = Marshal.PtrToStructure<Eina.HashTupleNative>(tuplePtr);
386 var key = NativeToManagedRef<TKey>(tuple.key); 494 IntPtr key = IndirectNative<TKey>(tuple.key, ForceRefKey<TKey>());
495 var ikey = NativeToManaged<TKey>(tuple.key);
387 var val = NativeToManaged<TValue>(tuple.data); 496 var val = NativeToManaged<TValue>(tuple.data);
388 yield return new KeyValuePair<TKey, TValue>(key, val); 497 yield return new KeyValuePair<TKey, TValue>(ikey, val);
389 } 498 }
390 } 499 }
391 finally 500 finally