From 1c193be863e61dcc27abebc7b3d354d34254b2d1 Mon Sep 17 00:00:00 2001 From: Rolf Bjarne Kvinge Date: Tue, 19 Oct 2021 15:51:34 +0200 Subject: [PATCH] [MidiThruConnection] Numerous other code updates (#13008) * Enable nullability and fix code accordingly. * Use 'is' and 'is not' instead of '==' and '!=' for object identity. * Use CFString.CreateNative/ReleaseNative instead of other means to create native strings (the fastest and least memory hungry option). --- src/CoreMidi/MidiThruConnection.cs | 47 +++++++++++++++++++----------- 1 file changed, 30 insertions(+), 17 deletions(-) diff --git a/src/CoreMidi/MidiThruConnection.cs b/src/CoreMidi/MidiThruConnection.cs index ca50f8b024..7b6b1bf88d 100644 --- a/src/CoreMidi/MidiThruConnection.cs +++ b/src/CoreMidi/MidiThruConnection.cs @@ -6,6 +6,8 @@ // Copyright 2016 Xamarin Inc. // +#nullable enable + using System; using System.Runtime.InteropServices; @@ -59,14 +61,17 @@ namespace CoreMidi { /* CFDataRef */ IntPtr inConnectionParams, /* MIDIThruConnectionRef* */ out MidiThruConnectionRef outConnection); - public static MidiThruConnection Create (string persistentOwnerID, MidiThruConnectionParams connectionParams, out MidiError error) + public static MidiThruConnection? Create (string persistentOwnerID, MidiThruConnectionParams connectionParams, out MidiError error) { MidiThruConnectionRef ret; using (var data = connectionParams.WriteStruct ()) { - var retStr = NSString.CreateNative (persistentOwnerID); - error = MIDIThruConnectionCreate (retStr, data.Handle, out ret); - NSString.ReleaseNative (retStr); + var retStr = CFString.CreateNative (persistentOwnerID); + try { + error = MIDIThruConnectionCreate (retStr, data.Handle, out ret); + } finally { + CFString.ReleaseNative (retStr); + } } if (error != MidiError.Ok) @@ -75,7 +80,7 @@ namespace CoreMidi { return new MidiThruConnection (ret); } - public static MidiThruConnection Create (string persistentOwnerID, MidiThruConnectionParams connectionParams) + public static MidiThruConnection? Create (string persistentOwnerID, MidiThruConnectionParams connectionParams) { MidiError error; return Create (persistentOwnerID, connectionParams, out error); @@ -86,7 +91,7 @@ namespace CoreMidi { /* MIDIThruConnectionRef* */ MidiThruConnectionRef connection, /* CFDataRef */ out IntPtr outConnectionParams); - public MidiThruConnectionParams GetParams (out MidiError error) + public MidiThruConnectionParams? GetParams (out MidiError error) { if (Handle == InvalidRef) throw new ObjectDisposedException ("MidiThruConnection"); @@ -96,7 +101,7 @@ namespace CoreMidi { if (error != MidiError.Ok || ret == IntPtr.Zero) return null; using (var data = Runtime.GetNSObject (ret, true)) { - if (data == null) + if (data is null) return null; var cnnParams = new MidiThruConnectionParams (); cnnParams.ReadStruct (data); @@ -104,7 +109,7 @@ namespace CoreMidi { } } - public MidiThruConnectionParams GetParams () + public MidiThruConnectionParams? GetParams () { MidiError error; return GetParams (out error); @@ -119,7 +124,7 @@ namespace CoreMidi { { if (Handle == InvalidRef) throw new ObjectDisposedException ("MidiThruConnection"); - if (connectionParams == null) + if (connectionParams is null) throw new ArgumentNullException (nameof (connectionParams)); using (var data = connectionParams.WriteStruct ()) { @@ -133,31 +138,39 @@ namespace CoreMidi { /* CFStringRef* */ IntPtr inPersistentOwnerID, /* CFDataRef */ out IntPtr outConnectionList); - public static MidiThruConnection[] Find (string persistentOwnerID, out MidiError error) + public static MidiThruConnection[]? Find (string persistentOwnerID, out MidiError error) { - if (persistentOwnerID == null) + if (persistentOwnerID is null) throw new ArgumentNullException (nameof (persistentOwnerID)); IntPtr ret; - using (var nssstr = new NSString (persistentOwnerID)) { - error = MIDIThruConnectionFind (nssstr.Handle, out ret); + var persistentOwnerIDHandle = CFString.CreateNative (persistentOwnerID); + try { + error = MIDIThruConnectionFind (persistentOwnerIDHandle, out ret); + } finally { + CFString.ReleaseNative (persistentOwnerIDHandle); } using (var data = Runtime.GetNSObject (ret)) { + if (data is null) + return null; var typeSize = Marshal.SizeOf (typeof (MidiThruConnectionRef)); var totalObjs = (int) data.Length / typeSize; if (totalObjs == 0) return null; - var basePtr = (int) data.Bytes; var connections = new MidiThruConnection[totalObjs]; - for (int i = 0; i < totalObjs; i++) - connections[i] = new MidiThruConnection ((uint)(basePtr + i * typeSize)); + unsafe { + uint* handles = (uint*) (IntPtr) data.Bytes; + for (int i = 0; i < totalObjs; i++) { + connections [i] = new MidiThruConnection (handles [i]); + } + } return connections; } } - public static MidiThruConnection[] Find (string persistentOwnerID) + public static MidiThruConnection[]? Find (string persistentOwnerID) { MidiError error; return Find (persistentOwnerID, out error);