Skip to content

Commit

Permalink
0% tested fix compiler warnings with async zstd
Browse files Browse the repository at this point in the history
  • Loading branch information
PJB3005 committed Jun 11, 2024
1 parent 95d1903 commit 39be27e
Showing 1 changed file with 71 additions and 64 deletions.
135 changes: 71 additions & 64 deletions Robust.Cdn/Helpers/ZStd.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,30 +211,33 @@ public override async ValueTask<int> ReadAsync(
return 0;
}

unsafe
var outputPos = DecompressChunk();
if (outputPos > 0)
return outputPos;
} while (true);

unsafe int DecompressChunk()
{
fixed (byte* inputPtr = _buffer)
fixed (byte* outputPtr = buffer.Span)
{
fixed (byte* inputPtr = _buffer)
fixed (byte* outputPtr = buffer.Span)
{
ZSTD_outBuffer outputBuf = default;
outputBuf.dst = outputPtr;
outputBuf.pos = 0;
outputBuf.size = (nuint)buffer.Length;
ZSTD_inBuffer inputBuf = default;
inputBuf.src = inputPtr;
inputBuf.pos = (nuint)_bufferPos;
inputBuf.size = (nuint)_bufferSize;

var ret = ZSTD_decompressStream(_ctx, &outputBuf, &inputBuf);

_bufferPos = (int)inputBuf.pos;
ZStdException.ThrowIfError(ret);

if (outputBuf.pos > 0)
return (int)outputBuf.pos;
}
ZSTD_outBuffer outputBuf = default;
outputBuf.dst = outputPtr;
outputBuf.pos = 0;
outputBuf.size = (nuint)buffer.Length;
ZSTD_inBuffer inputBuf = default;
inputBuf.src = inputPtr;
inputBuf.pos = (nuint)_bufferPos;
inputBuf.size = (nuint)_bufferSize;

var ret = ZSTD_decompressStream(_ctx, &outputBuf, &inputBuf);

_bufferPos = (int)inputBuf.pos;
ZStdException.ThrowIfError(ret);

return (int)outputBuf.pos;
}
} while (true);
}
}

public override long Seek(long offset, SeekOrigin origin)
Expand Down Expand Up @@ -345,25 +348,7 @@ private async ValueTask FlushInternalAsync(ZSTD_EndDirective directive, Cancella

while (true)
{
nuint err;
unsafe
{
fixed (byte* outPtr = _buffer)
{
ZSTD_outBuffer outBuf = default;
outBuf.size = (nuint)_buffer.Length;
outBuf.pos = outBufPos;
outBuf.dst = outPtr;

ZSTD_inBuffer inBuf;


err = ZSTD_compressStream2(Context.Context, &outBuf, &inBuf, directive);
ZStdException.ThrowIfError(err);
outBufPos = outBuf.pos;
_bufferPos = (int)outBuf.pos;
}
}
var err = FlushChunk();

await _baseStream.WriteAsync(_buffer.AsMemory(0, (int)outBufPos), cancel);
_bufferPos = 0;
Expand All @@ -374,6 +359,26 @@ private async ValueTask FlushInternalAsync(ZSTD_EndDirective directive, Cancella
}

await _baseStream.FlushAsync(cancel);

unsafe nuint FlushChunk()
{
fixed (byte* outPtr = _buffer)
{
ZSTD_outBuffer outBuf = default;
outBuf.size = (nuint)_buffer.Length;
outBuf.pos = outBufPos;
outBuf.dst = outPtr;

ZSTD_inBuffer inBuf;

var err = ZSTD_compressStream2(Context.Context, &outBuf, &inBuf, directive);
ZStdException.ThrowIfError(err);
outBufPos = outBuf.pos;
_bufferPos = (int)outBuf.pos;

return err;
}
}
}

public override int Read(byte[] buffer, int offset, int count)
Expand Down Expand Up @@ -446,29 +451,7 @@ public override async ValueTask WriteAsync(

while (true)
{
unsafe
{
fixed (byte* outPtr = _buffer)
fixed (byte* inPtr = buffer.Span)
{
ZSTD_outBuffer outBuf = default;
outBuf.size = (nuint)_buffer.Length;
outBuf.pos = outBufPos;

ZSTD_inBuffer inBuf = default;
inBuf.pos = inBufPos;
inBuf.size = inBufSize;

outBuf.dst = outPtr;
inBuf.src = inPtr;

var err = ZSTD_compressStream2(Context.Context, &outBuf, &inBuf, ZSTD_EndDirective.ZSTD_e_continue);
ZStdException.ThrowIfError(err);
_bufferPos = (int)outBuf.pos;
outBufPos = outBuf.pos;
inBufPos = inBuf.pos;
}
}
CompressChunk();

if (inBufPos >= inBufSize)
break;
Expand All @@ -478,6 +461,30 @@ public override async ValueTask WriteAsync(
_bufferPos = 0;
outBufPos = 0;
}

unsafe void CompressChunk()
{
fixed (byte* outPtr = _buffer)
fixed (byte* inPtr = buffer.Span)
{
ZSTD_outBuffer outBuf = default;
outBuf.size = (nuint)_buffer.Length;
outBuf.pos = outBufPos;

ZSTD_inBuffer inBuf = default;
inBuf.pos = inBufPos;
inBuf.size = inBufSize;

outBuf.dst = outPtr;
inBuf.src = inPtr;

var err = ZSTD_compressStream2(Context.Context, &outBuf, &inBuf, ZSTD_EndDirective.ZSTD_e_continue);
ZStdException.ThrowIfError(err);
_bufferPos = (int)outBuf.pos;
outBufPos = outBuf.pos;
inBufPos = inBuf.pos;
}
}
}

public override bool CanRead => false;
Expand Down

0 comments on commit 39be27e

Please sign in to comment.