/// <summary> /// /// </summary> /// <param name="name"></param> /// <param name="comparison"></param> public Comparator(String name, Comparison <NativeArray> comparison) { GCHandle selfHandle = default; try { Byte[] utf = Encoding.UTF8.GetBytes(name); this._NativeName = Marshal.AllocHGlobal(utf.Length + 1); Marshal.Copy(utf, 0, this._NativeName, utf.Length); this._Comparison = comparison; selfHandle = GCHandle.Alloc(this); this.Handle = LevelDBInterop.leveldb_comparator_create( GCHandle.ToIntPtr(selfHandle), Marshal.GetFunctionPointerForDelegate(_Destructor), Marshal.GetFunctionPointerForDelegate(Compare), Marshal.GetFunctionPointerForDelegate(_GetName)); if (this.Handle == IntPtr.Zero) { throw new ApplicationException("Failed to initialize LevelDB comparator"); } } catch { if (selfHandle != default) { selfHandle.Free(); } if (this._NativeName != IntPtr.Zero) { Marshal.FreeHGlobal(this._NativeName); } throw; } }
public IntPtr Init(string name, Func <NativeArray, NativeArray, int> cmp) { // TODO: Complete member initialization this.cmp = cmp; this.namePinned = GCHandle.Alloc( Encoding.ASCII.GetBytes(name), GCHandleType.Pinned); var thisHandle = GCHandle.Alloc(this); var chandle = LevelDBInterop.leveldb_comparator_create( GCHandle.ToIntPtr(thisHandle), Marshal.GetFunctionPointerForDelegate(destructor), Marshal.GetFunctionPointerForDelegate(compare), Marshal.GetFunctionPointerForDelegate(nameAccessor) ); if (chandle == default(IntPtr)) { thisHandle.Free(); } return(chandle); }