未使用のMFインターロップファイルを削除しました。
MfProxyEncoderのトランスコード進捗報告処理をTranscodeProgressReporterクラスに抽出しました。
キャンセル登録の型をIAsyncOperationWithProgressからIAsyncActionWithProgressに修正しました。
MfProxyEncoderFactory・MemoryBudget・PooledMemoryStream・CacheEntryViewModel等をプライマリコンストラクタ構文に変換しました。
ProxyCacheManagerのCacheKeyをrecord structに変換し、Equalsのオーバーライドを整理しました。
PooledBufferをmutableなstructに変更し、Interlocked.Exchangeによるスレッドセーフなディスポーズを実装しました。
FallbackStreamにRead(Span<byte>)・Write(ReadOnlySpan<byte>)・WriteAsyncの各オーバーロードを追加しました。
MemoryBudgetのTickCountを32ビットuintにキャストし、ラッピング演算の整合性を修正しました。
GetAllSnapshotsをList<T>ベースに変更し、配列の事前確保とArray.Resizeを除去しました。
ZeroDiskProxyVideoSourceのUpgrade適用ロジックをTryApplyUpgradeメソッドとして抽出しました。
ProxyGenerationItem・ByteFormatter・CacheEntryViewModelの文字列生成をstring.Createと補間文字列に統一しました。
PluginHostからResourceRegistryへの依存を取り除き、関連するDispose呼び出しを削除しました。
CheckExportWindowにDebug.Assertを追加し、スレッドプールの非同期継続から呼ばれた場合の前提条件違反を検出できるようにしました。
ClearAllのforeachでデコンストラクション構文をkeyプロパティの明示的な参照に変更しました。
CreateCoreメソッドをインスタンスメンバーからstaticメソッドに変更しました。
パターンマッチングをis { IsLoaded: true }・is { IsValid: true }形式に統一しました。
83 行
2.3 KiB
C#
83 行
2.3 KiB
C#
using System.Collections.Concurrent;
|
|
using System.Diagnostics;
|
|
using System.Runtime.InteropServices;
|
|
|
|
namespace ZeroDiskProxy.Resource;
|
|
|
|
internal sealed class ResourceRegistry : IDisposable
|
|
{
|
|
private readonly ConcurrentDictionary<long, RegistryEntry> _entries = new();
|
|
private long _nextId;
|
|
private int _disposed;
|
|
|
|
internal int Count => _entries.Count;
|
|
|
|
internal ResourceHandle TrackComPtr(nint comPtr, string? debugName = null)
|
|
{
|
|
ObjectDisposedException.ThrowIf(Volatile.Read(ref _disposed) != 0, this);
|
|
if (comPtr == nint.Zero)
|
|
return ResourceHandle.Invalid;
|
|
|
|
var id = Interlocked.Increment(ref _nextId);
|
|
_entries[id] = new RegistryEntry(id, comPtr, null, debugName);
|
|
return new ResourceHandle(id, this);
|
|
}
|
|
|
|
internal ResourceHandle TrackDisposable(IDisposable disposable, string? debugName = null)
|
|
{
|
|
ObjectDisposedException.ThrowIf(Volatile.Read(ref _disposed) != 0, this);
|
|
|
|
var id = Interlocked.Increment(ref _nextId);
|
|
_entries[id] = new RegistryEntry(id, nint.Zero, disposable, debugName);
|
|
return new ResourceHandle(id, this);
|
|
}
|
|
|
|
internal void Release(long id)
|
|
{
|
|
if (!_entries.TryRemove(id, out var entry))
|
|
return;
|
|
|
|
ReleaseEntry(entry);
|
|
}
|
|
|
|
internal void ReleaseAll()
|
|
{
|
|
foreach (var kvp in _entries)
|
|
{
|
|
if (_entries.TryRemove(kvp.Key, out var entry))
|
|
ReleaseEntry(entry);
|
|
}
|
|
}
|
|
|
|
private static void ReleaseEntry(RegistryEntry entry)
|
|
{
|
|
try
|
|
{
|
|
if (entry.ComPtr != nint.Zero)
|
|
Marshal.Release(entry.ComPtr);
|
|
|
|
entry.Disposable?.Dispose();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Debug.WriteLine(string.Concat("[ResourceRegistry] Release failed: ", ex.Message));
|
|
}
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
if (Interlocked.Exchange(ref _disposed, 1) != 0)
|
|
return;
|
|
|
|
ReleaseAll();
|
|
}
|
|
|
|
private sealed record RegistryEntry(long Id, nint ComPtr, IDisposable? Disposable, string? DebugName);
|
|
}
|
|
|
|
internal readonly struct ResourceHandle(long id, ResourceRegistry registry) : IDisposable
|
|
{
|
|
internal static ResourceHandle Invalid => default;
|
|
|
|
public void Dispose() => registry?.Release(id);
|
|
} |