|
3 | 3 | using System; |
4 | 4 | using System.Net; |
5 | 5 | using System.Net.Sockets; |
| 6 | +using System.Runtime.CompilerServices; |
6 | 7 | using System.Runtime.InteropServices; |
7 | 8 |
|
8 | 9 | namespace Hi3Helper.Plugin.Core.Utility; |
@@ -88,29 +89,38 @@ public static IPAddress[] GetIPAddressArray(DnsARecordResult* dnsARecordP) |
88 | 89 | DnsARecordResult* next = endResultP->NextResult; |
89 | 90 |
|
90 | 91 | // Convert the struct into IPAddress instance. |
91 | | - returnIpAddresses[offset++] = GetIPAddressFromResultAndDispose(endResultP); |
| 92 | + returnIpAddresses[offset++] = new IPAddress(new ReadOnlySpan<byte>(endResultP->AddressData, endResultP->AddressDataLength)); |
92 | 93 | endResultP = next; |
93 | 94 | } while (endResultP != null); // Do the loop if the next entry is not null. |
94 | 95 |
|
95 | 96 | // If done, return the array. |
96 | 97 | return returnIpAddresses; |
97 | 98 | } |
98 | 99 |
|
99 | | - // ReSharper disable once InconsistentNaming |
100 | | - private static IPAddress GetIPAddressFromResultAndDispose(DnsARecordResult* resultP) |
| 100 | + /// <summary> |
| 101 | + /// Free the instance of <see cref="DnsARecordResult"/> struct and its entries. |
| 102 | + /// </summary> |
| 103 | + /// <param name="resultP">The pointer of the current <see cref="DnsARecordResult"/> entry.</param> |
| 104 | + /// <param name="isFreeAll">If set to true, the current entry and all its next entries will be freed. Otherwise, just free the current one.</param> |
| 105 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 106 | + public static void Free(nint resultP, bool isFreeAll = true) |
| 107 | + => Free((DnsARecordResult*)resultP, isFreeAll); |
| 108 | + |
| 109 | + /// <summary> |
| 110 | + /// Free the instance of <see cref="DnsARecordResult"/> struct and its entries. |
| 111 | + /// </summary> |
| 112 | + /// <param name="resultP">The pointer of the current <see cref="DnsARecordResult"/> entry.</param> |
| 113 | + /// <param name="isFreeAll">If set to true, the current entry and all its next entries will be freed. Otherwise, just free the current one.</param> |
| 114 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 115 | + public static void Free(DnsARecordResult* resultP, bool isFreeAll = true) |
101 | 116 | { |
102 | | - try |
103 | | - { |
104 | | - return new IPAddress(new ReadOnlySpan<byte>(resultP->AddressData, resultP->AddressDataLength)); |
105 | | - } |
106 | | - finally |
| 117 | + do |
107 | 118 | { |
108 | | - // If the result is not null, free the string and the struct. |
109 | | - if (resultP != null) |
110 | | - { |
111 | | - Mem.Free(resultP); |
112 | | - } |
113 | | - } |
| 119 | + DnsARecordResult* nextP = resultP->NextResult; |
| 120 | + Mem.Free(resultP); |
| 121 | + |
| 122 | + resultP = nextP; |
| 123 | + } while (resultP != null && isFreeAll); |
114 | 124 | } |
115 | 125 |
|
116 | 126 | /// <summary> |
|
0 commit comments