-
Notifications
You must be signed in to change notification settings - Fork 357
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Added
GroupDisposable
to dispose a collection of items all together
- Renamed `LLamaModelKvOverride` to `LLamaModelMetadataOverride`
- Loading branch information
1 parent
c21da98
commit bc095e6
Showing
4 changed files
with
99 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using System; | ||
using System.Buffers; | ||
using System.Collections.Generic; | ||
|
||
namespace LLama.Native; | ||
|
||
/// <summary> | ||
/// Disposes all contained disposables when this class is disposed | ||
/// </summary> | ||
internal sealed class GroupDisposable | ||
: IDisposable | ||
{ | ||
private bool _disposed; | ||
|
||
private readonly List<MemoryHandle> _handles = new(); | ||
private readonly List<IDisposable> _disposables = new(); | ||
|
||
/// <inheritdoc /> | ||
~GroupDisposable() | ||
{ | ||
Dispose(); | ||
} | ||
|
||
public MemoryHandle Add(MemoryHandle handle) | ||
{ | ||
if (_disposed) | ||
throw new ObjectDisposedException("Cannot add new handle, already disposed"); | ||
_handles.Add(handle); | ||
|
||
return handle; | ||
} | ||
|
||
public T Add<T>(T disposable) | ||
where T : class, IDisposable | ||
{ | ||
if (_disposed) | ||
throw new ObjectDisposedException("Cannot add new IDisposable, already disposed"); | ||
_disposables.Add(disposable); | ||
|
||
return disposable; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public void Dispose() | ||
{ | ||
if (_disposed) | ||
return; | ||
|
||
foreach (var memoryHandle in _handles) | ||
memoryHandle.Dispose(); | ||
foreach (var disposable in _disposables) | ||
disposable.Dispose(); | ||
|
||
_disposed = true; | ||
GC.SuppressFinalize(this); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters