-
Notifications
You must be signed in to change notification settings - Fork 170
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #415 from iamcarbon/scoped-buffer
Introduce ScopedBuffer
- Loading branch information
Showing
7 changed files
with
78 additions
and
76 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
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
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,43 @@ | ||
// Copyright (c) Drew Noakes and contributors. All Rights Reserved. Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. | ||
|
||
using System.Buffers; | ||
|
||
namespace MetadataExtractor.IO; | ||
|
||
internal ref struct ScopedBuffer | ||
{ | ||
public const int MaxStackBufferSize = 256; | ||
|
||
private byte[]? _rentedBuffer; | ||
private readonly Span<byte> _span; | ||
|
||
public ScopedBuffer(int size) | ||
{ | ||
_rentedBuffer = ArrayPool<byte>.Shared.Rent(size); | ||
_span = _rentedBuffer.AsSpan(0, size); | ||
} | ||
|
||
public ScopedBuffer(Span<byte> span) | ||
{ | ||
_span = span; | ||
} | ||
|
||
public readonly Span<byte> Span => _span; | ||
|
||
public static implicit operator Span<byte>(ScopedBuffer bufferScope) | ||
{ | ||
return bufferScope._span; | ||
} | ||
|
||
public static implicit operator ReadOnlySpan<byte>(ScopedBuffer bufferScope) | ||
{ | ||
return bufferScope._span; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
if (_rentedBuffer is null) return; | ||
|
||
ArrayPool<byte>.Shared.Return(_rentedBuffer); | ||
} | ||
} |
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