int StreamCreateCallback(out IntPtr git_writestream_out, GitFilter self, IntPtr payload, IntPtr filterSourcePtr, IntPtr git_writestream_next) { int result = 0; var state = new StreamState(); try { Ensure.ArgumentNotZeroIntPtr(filterSourcePtr, "filterSourcePtr"); Ensure.ArgumentNotZeroIntPtr(git_writestream_next, "git_writestream_next"); state.thisStream = new GitWriteStream(); state.thisStream.close = StreamCloseCallback; state.thisStream.write = StreamWriteCallback; state.thisStream.free = StreamFreeCallback; state.thisPtr = Marshal.AllocHGlobal(Marshal.SizeOf(state.thisStream)); Marshal.StructureToPtr(state.thisStream, state.thisPtr, false); state.nextPtr = git_writestream_next; state.nextStream = new GitWriteStream(); Marshal.PtrToStructure(state.nextPtr, state.nextStream); state.filterSource = FilterSource.FromNativePtr(filterSourcePtr); state.output = new WriteStream(state.nextStream, state.nextPtr); Create(state.filterSource.Path, state.filterSource.Root, state.filterSource.SourceMode); if (!activeStreams.TryAdd(state.thisPtr, state)) { // AFAICT this is a theoretical error that could only happen if we manage // to free the stream pointer but fail to remove the dictionary entry. throw new InvalidOperationException("Overlapping stream pointers"); } } catch (Exception exception) { // unexpected failures means memory clean up required if (state.thisPtr != IntPtr.Zero) { Marshal.FreeHGlobal(state.thisPtr); state.thisPtr = IntPtr.Zero; } Log.Write(LogLevel.Error, "Filter.StreamCreateCallback exception"); Log.Write(LogLevel.Error, exception.ToString()); Proxy.giterr_set_str(GitErrorCategory.Filter, exception); result = (int)GitErrorCode.Error; } git_writestream_out = state.thisPtr; return(result); }
int StreamCreateCallback(out IntPtr git_writestream_out, GitFilter self, IntPtr payload, IntPtr filterSourcePtr, IntPtr git_writestream_next) { int result = 0; try { Ensure.ArgumentNotZeroIntPtr(filterSourcePtr, "filterSourcePtr"); Ensure.ArgumentNotZeroIntPtr(git_writestream_next, "git_writestream_next"); thisStream = new GitWriteStream(); thisStream.close = StreamCloseCallback; thisStream.write = StreamWriteCallback; thisStream.free = StreamFreeCallback; thisPtr = Marshal.AllocHGlobal(Marshal.SizeOf(thisStream)); Marshal.StructureToPtr(thisStream, thisPtr, false); nextPtr = git_writestream_next; nextStream = new GitWriteStream(); Marshal.PtrToStructure(nextPtr, nextStream); filterSource = FilterSource.FromNativePtr(filterSourcePtr); output = new WriteStream(nextStream, nextPtr); Create(filterSource.Path, filterSource.Root, filterSource.SourceMode); } catch (Exception exception) { // unexpected failures means memory clean up required if (thisPtr != IntPtr.Zero) { Marshal.FreeHGlobal(thisPtr); thisPtr = IntPtr.Zero; } Log.Write(LogLevel.Error, "Filter.StreamCreateCallback exception"); Log.Write(LogLevel.Error, exception.ToString()); Proxy.giterr_set_str(GitErrorCategory.Filter, exception); result = (int)GitErrorCode.Error; } git_writestream_out = thisPtr; return(result); }