NativeCore.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using System.Collections.Generic;
  4. namespace AtomicEngine
  5. {
  6. static class NativeCore
  7. {
  8. [DllImport (Constants.LIBNAME, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
  9. private static extern void csb_AtomicEngine_ReleaseRef (IntPtr refCounted);
  10. // given an existing instance classID, construct the managed instance, with downcast support (ask for Component, get StaticModel for example)
  11. public static Dictionary<IntPtr, Func<IntPtr, RefCounted>> nativeClassIDToManagedConstructor = new Dictionary<IntPtr, Func<IntPtr, RefCounted>>();
  12. // weak references here, hold a ref native side
  13. public static Dictionary<IntPtr, WeakReference> nativeLookup = new Dictionary<IntPtr, WeakReference> ();
  14. // native engine types, instances of these types can be trivially recreated managed side
  15. private static Dictionary<Type, Type> nativeTypes = new Dictionary<Type, Type> ();
  16. public static bool GetNativeType (Type type)
  17. {
  18. return nativeTypes.ContainsKey (type);
  19. }
  20. public static void RegisterNativeType (Type type)
  21. {
  22. nativeTypes.Add (type, type);
  23. }
  24. public static void ReleaseExpiredNativeReferences()
  25. {
  26. List<IntPtr> released = null;
  27. foreach(KeyValuePair<IntPtr, WeakReference> entry in nativeLookup)
  28. {
  29. if (entry.Value.Target == null || !entry.Value.IsAlive)
  30. {
  31. if (released == null)
  32. released = new List<IntPtr> ();
  33. released.Add (entry.Key);
  34. }
  35. }
  36. if (released == null)
  37. return;
  38. foreach (IntPtr native in released) {
  39. nativeLookup.Remove (native);
  40. csb_AtomicEngine_ReleaseRef(native);
  41. //Console.WriteLine("Released: " + test);
  42. }
  43. }
  44. static IntPtr test = IntPtr.Zero;
  45. // called by native code
  46. public static void NETUpdate (float timeStep)
  47. {
  48. GC.Collect();
  49. GC.WaitForPendingFinalizers();
  50. GC.Collect();
  51. ReleaseExpiredNativeReferences();
  52. ComponentCore.Update(timeStep);
  53. }
  54. // called by native code for every refcounted deleted
  55. public static void RefCountedDeleted (IntPtr native)
  56. {
  57. // native side deleted, immediate remove, if we have a script reference still this is an error
  58. WeakReference w;
  59. if (nativeLookup.TryGetValue (native, out w)) {
  60. if ( w != null && w.IsAlive) {
  61. throw new System.InvalidOperationException("Native Refcounted was deleted with live managed instance");
  62. }
  63. }
  64. nativeLookup.Remove(native);
  65. }
  66. // register a newly created native
  67. public static IntPtr RegisterNative (IntPtr native, RefCounted r)
  68. {
  69. r.nativeInstance = native;
  70. var w = new WeakReference (r);
  71. NativeCore.nativeLookup [native] = w;
  72. // keep native side alive
  73. r.AddRef();
  74. return native;
  75. }
  76. // wraps an existing native instance, with downcast support
  77. public static T WrapNative<T> (IntPtr native) where T:RefCounted
  78. {
  79. if (native == IntPtr.Zero)
  80. return null;
  81. WeakReference w;
  82. // first see if we're already available
  83. if (nativeLookup.TryGetValue (native, out w)) {
  84. if (w.IsAlive) {
  85. // we're alive!
  86. return (T)w.Target;
  87. } else {
  88. // we were seen before, but have since been GC'd, remove!
  89. nativeLookup.Remove (native);
  90. csb_AtomicEngine_ReleaseRef(native);
  91. }
  92. }
  93. IntPtr classID = RefCounted.csb_Atomic_RefCounted_GetClassID (native);
  94. // and store, with downcast support for instance Component -> StaticModel
  95. // we never want to hit this path for script inherited natives
  96. RefCounted r = nativeClassIDToManagedConstructor[classID](native);
  97. w = new WeakReference (r);
  98. NativeCore.nativeLookup [native] = w;
  99. // store a ref, so native side will not be released while we still have a reference in managed code
  100. r.AddRef();
  101. return (T) r;
  102. }
  103. }
  104. }