Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
201 changes: 150 additions & 51 deletions CHDSharpLib/CHDBlockRead.cs
Original file line number Diff line number Diff line change
Expand Up @@ -365,47 +365,90 @@ internal static ChdError ReadBlock(
case CompressionType.Compressiontype2:
case CompressionType.Compressiontype3:
{
lock (mapEntry)
if (buffInOverride is null)
{
if (mapEntry.BuffOutCache == null)
lock (mapEntry)
{
var buffIn = buffInOverride ?? mapEntry.BuffIn;
if (buffIn is null)
if (mapEntry.BuffOutCache == null)
{
ChdDiagnostics.SetDetail(
"compressed input buffer was not loaded for this hunk"
);
return ChdError.Chderrcodecerror;
}
var ret = Decompress(mapEntry, mapEntry.BuffIn, buffOut, buffOutLength, compression[(int)mapEntry.Comptype], codec);

var ret = compression[(int)mapEntry.Comptype]
.Invoke(buffIn, (int)mapEntry.Length, buffOut, buffOutLength, codec);
if (ret != ChdError.Chderrnone)
return ret;

if (ret != ChdError.Chderrnone)
return ret;
// if this block is re-used keep a copy of it.
if (mapEntry.UseCount > 0)
{
mapEntry.BuffOutCache = arrPool.Rent();
Array.Copy(buffOut, 0, mapEntry.BuffOutCache, 0, buffOutLength);
}

// if this block is re-used keep a copy of it.
if (mapEntry.UseCount > 0)
break;
}

Array.Copy(mapEntry.BuffOutCache, 0, buffOut, 0, buffOutLength);

if (Interlocked.Decrement(ref mapEntry.UseCount) == 0)
{
mapEntry.BuffOutCache = arrPool.Rent();
Array.Copy(buffOut, 0, mapEntry.BuffOutCache, 0, buffOutLength);
ReleaseCache(mapEntry, arrPool);
}

break;
checkCrc = false;
}

Array.Copy(mapEntry.BuffOutCache, 0, buffOut, 0, buffOutLength);
break; // Returns
}

Interlocked.Decrement(ref mapEntry.UseCount);
if (mapEntry.UseCount == 0)
var cache = Volatile.Read(ref mapEntry.BuffOutCache);
if (cache == null)
{
var ret = Decompress(mapEntry, buffInOverride, buffOut, buffOutLength, compression[(int)mapEntry.Comptype], codec);

if (ret != ChdError.Chderrnone)
return ret;

// if this block is re-used keep a copy of it.
if (mapEntry.UseCount > 0)
{
arrPool.Return(mapEntry.BuffOutCache);
mapEntry.BuffOutCache = null!;
lock (mapEntry)
{
if (mapEntry.BuffOutCache == null && mapEntry.UseCount > 0)
{
var pooled = arrPool.Rent();
Array.Copy(buffOut, 0, pooled, 0, buffOutLength);
Volatile.Write(ref mapEntry.BuffOutCache, pooled);
}
else
{
// Lost the race: another thread already published the cache.
// This read still counts as one of the N consumers.
if (Interlocked.Decrement(ref mapEntry.UseCount) == 0)
{
ReleaseCache(mapEntry, arrPool);
}
}
}
}

checkCrc = false;
break;
}

Array.Copy(cache, 0, buffOut, 0, buffOutLength);

var newUseCount = Interlocked.Decrement(ref mapEntry.UseCount);
if (newUseCount == 0)
{
lock (mapEntry)
{
if (mapEntry.BuffOutCache != null)
{
ReleaseCache(mapEntry, arrPool);
}
}
}

checkCrc = false;

break;
}
case CompressionType.Compressionnone:
Expand Down Expand Up @@ -499,53 +542,91 @@ internal static ChdError ReadBlock(
if (mapEntry.SecondaryReader == null)
return ChdError.Chderrcodecerror;

lock (mapEntry)
{
if (mapEntry.BuffOutCache == null)
if (buffInOverride is null)
{
var buffIn = buffInOverride ?? mapEntry.BuffIn;
if (buffIn is null)
lock (mapEntry)
{
ChdDiagnostics.SetDetail(
"compressed input buffer was not loaded for this hunk"
);
return ChdError.Chderrcodecerror;
if (mapEntry.BuffOutCache == null)
{
var ret = Decompress(mapEntry, mapEntry.BuffIn, buffOut, buffOutLength, mapEntry.SecondaryReader, codec);
if (ret != ChdError.Chderrnone)
return ret;

if (mapEntry.UseCount > 0)
{
mapEntry.BuffOutCache = arrPool.Rent();
Array.Copy(buffOut, 0, mapEntry.BuffOutCache, 0, buffOutLength);
}

break;
}

Array.Copy(mapEntry.BuffOutCache, 0, buffOut, 0, buffOutLength);

if (Interlocked.Decrement(ref mapEntry.UseCount) == 0)
{
ReleaseCache(mapEntry, arrPool);
}

checkCrc = false;
}

var ret = mapEntry.SecondaryReader.Invoke(
buffIn,
(int)mapEntry.Length,
buffOut,
buffOutLength,
codec
);
break;
}


var cache = Volatile.Read(ref mapEntry.BuffOutCache);
if (cache == null)
{
var ret = Decompress(mapEntry, buffInOverride, buffOut, buffOutLength, mapEntry.SecondaryReader, codec);

if (ret != ChdError.Chderrnone)
return ret;

// if this block is re-used keep a copy of it.
if (mapEntry.UseCount > 0)
{
mapEntry.BuffOutCache = arrPool.Rent();
Array.Copy(buffOut, 0, mapEntry.BuffOutCache, 0, buffOutLength);
lock (mapEntry)
{
if (mapEntry.BuffOutCache == null && mapEntry.UseCount > 0)
{
var pooled = arrPool.Rent();
Array.Copy(buffOut, 0, pooled, 0, buffOutLength);
Volatile.Write(ref mapEntry.BuffOutCache, pooled);
}
else
{
// Lost the race: another thread already published the cache.
// This read still counts as one of the N consumers.
if (Interlocked.Decrement(ref mapEntry.UseCount) == 0)
{
ReleaseCache(mapEntry, arrPool);
}
}
}
}

break;
}

Array.Copy(mapEntry.BuffOutCache, 0, buffOut, 0, buffOutLength);
Array.Copy(cache, 0, buffOut, 0, buffOutLength);

Interlocked.Decrement(ref mapEntry.UseCount);
if (mapEntry.UseCount == 0)
var newUseCount = Interlocked.Decrement(ref mapEntry.UseCount);
if (newUseCount == 0)
{
arrPool.Return(mapEntry.BuffOutCache);
mapEntry.BuffOutCache = null!;
lock (mapEntry)
{
if (mapEntry.BuffOutCache != null)
{
ReleaseCache(mapEntry, arrPool);
}
}
}

checkCrc = false;
}

break;
}
break;
}

default:
return ChdError.Chderrdecompressionerror;
Expand Down Expand Up @@ -580,4 +661,22 @@ internal static ChdError ReadBlock(

return ChdError.Chderrnone;
}
}

private static ChdError Decompress(MapEntry mapEntry, byte[]? buffIn, byte[] buffOut, int buffOutLength, ChdReader compression, ChdCodecState codec)
{
if (buffIn is null)
{
ChdDiagnostics.SetDetail("compressed input buffer was not loaded for this hunk");
return ChdError.Chderrcodecerror;
}

return compression
.Invoke(buffIn, (int)mapEntry.Length, buffOut, buffOutLength, codec);
}

private static void ReleaseCache(MapEntry mapEntry, ArrayPool arrPool)
{
arrPool.Return(mapEntry.BuffOutCache!);
mapEntry.BuffOutCache = null!;
}
}