From 1c085a090c3911575219a5644254bd495c540f80 Mon Sep 17 00:00:00 2001 From: Jordan Russell Date: Sat, 9 Nov 2024 23:56:08 -0600 Subject: [PATCH] LZMACompressor: Replace Integer64 usage. --- Projects/Src/Compression.LZMACompressor.pas | 47 +++++++++---------- .../islzma/islzma_exe.c | 7 +-- 2 files changed, 25 insertions(+), 29 deletions(-) diff --git a/Projects/Src/Compression.LZMACompressor.pas b/Projects/Src/Compression.LZMACompressor.pas index 46e62f7f0..c040159d5 100644 --- a/Projects/Src/Compression.LZMACompressor.pas +++ b/Projects/Src/Compression.LZMACompressor.pas @@ -14,7 +14,7 @@ interface uses Windows, SysUtils, - Compression.Base, Shared.Int64Em; + Compression.Base; function LZMAInitCompressFunctions(Module: HMODULE): Boolean; function LZMAGetLevel(const Value: String; var Level: Integer): Boolean; @@ -76,7 +76,7 @@ TLZMACompressorSharedEvents = record PLZMACompressorSharedData = ^TLZMACompressorSharedData; TLZMACompressorSharedData = record NoMoreInput: BOOL; - ProgressKB: LongWord; + ProgressKB: UInt32; EncodeResult: TLZMASRes; InputBuffer: TLZMACompressorRingBuffer; OutputBuffer: TLZMACompressorRingBuffer; @@ -101,7 +101,7 @@ TLZMACompressor = class(TCustomCompressor) FEncodeStarted: Boolean; FEncodeFinished: Boolean; FLastInputWriteCount: LongWord; - FLastProgressKB: LongWord; + FLastProgressKB: UInt32; procedure FlushOutputBuffer(const OnlyOptimalSize: Boolean); procedure InitializeProps(const CompressionLevel: Integer; const ACompressorProps: TCompressorProps); @@ -158,7 +158,7 @@ TLZMAWorkerThread = class(TLZMACompressorCustomWorker) FLastProgressTick: DWORD; function FillBuffer(const AWrite: Boolean; const Data: Pointer; Size: Cardinal; var ProcessedSize: Cardinal): HRESULT; - function ProgressMade(const TotalBytesProcessed: Integer64): HRESULT; + function ProgressMade(const TotalBytesProcessed: UInt64): HRESULT; function Read(var Data; Size: Cardinal; var ProcessedSize: Cardinal): HRESULT; function WakeMainAndWaitUntil(const AWakeEvent, AWaitEvent: THandle): HRESULT; procedure WorkerThreadProc; @@ -199,7 +199,7 @@ TLZMASeqOutStream = record end; PLZMACompressProgress = ^TLZMACompressProgress; TLZMACompressProgress = record - Progress: function(p: PLZMACompressProgress; inSize, outSize: Integer64): TLZMASRes; stdcall; + Progress: function(p: PLZMACompressProgress; inSize, outSize: UInt64): TLZMASRes; stdcall; Instance: TLZMAWorkerThread; end; @@ -312,7 +312,7 @@ function LZMASeqOutStreamWriteWrapper(p: PLZMASeqOutStream; const buf; end; function LZMACompressProgressProgressWrapper(p: PLZMACompressProgress; - inSize, outSize: Integer64): TLZMASRes; stdcall; + inSize, outSize: UInt64): TLZMASRes; stdcall; begin if p.Instance.ProgressMade(inSize) = S_OK then Result := SZ_OK @@ -629,11 +629,10 @@ function TLZMAWorkerThread.Write(const Data; Size: Cardinal; InterlockedExchange(FWriteLock, 0); end; -function TLZMAWorkerThread.ProgressMade(const TotalBytesProcessed: Integer64): HRESULT; +function TLZMAWorkerThread.ProgressMade(const TotalBytesProcessed: UInt64): HRESULT; { Called from worker thread (or a thread spawned by the worker thread) } var T: DWORD; - KBProcessed: Integer64; begin T := GetTickCount; if Cardinal(T - FLastProgressTick) >= Cardinal(100) then begin @@ -646,11 +645,8 @@ function TLZMAWorkerThread.ProgressMade(const TotalBytesProcessed: Integer64): H { Make sure TotalBytesProcessed isn't negative. LZMA's Types.h says "-1 for size means unknown value", though I don't see any place where LzmaEnc actually does call Progress with inSize = -1. } - if Longint(TotalBytesProcessed.Hi) >= 0 then begin - KBProcessed := TotalBytesProcessed; - Div64(KBProcessed, 1024); - FShared.ProgressKB := KBProcessed.Lo; - end; + if Int64(TotalBytesProcessed) >= 0 then + FShared.ProgressKB := UInt32(TotalBytesProcessed shr 10); Result := WakeMainAndWaitUntil(FEvents.WorkerHasProgressEvent, FEvents.EndWaitOnProgressEvent); InterlockedExchange(FProgressLock, 0); @@ -940,23 +936,26 @@ procedure TLZMACompressor.SatisfyWorkerWaitOnOutput; end; procedure TLZMACompressor.UpdateProgress; +const + MaxBytesPerProgressProcCall = 1 shl 30; { 1 GB } var - NewProgressKB: LongWord; - Bytes: Integer64; + NewProgressKB: UInt32; + Bytes: UInt64; + LimitedBytes: Cardinal; begin if IsEventSet(FEvents.WorkerHasProgressEvent) then begin if Assigned(ProgressProc) then begin NewProgressKB := FShared.ProgressKB; - Bytes.Hi := 0; - Bytes.Lo := NewProgressKB - FLastProgressKB; { wraparound is OK } - Mul64(Bytes, 1024); + Bytes := UInt64(UInt32(NewProgressKB - FLastProgressKB)) shl 10; { wraparound is OK } FLastProgressKB := NewProgressKB; - while Bytes.Hi <> 0 do begin - ProgressProc(Cardinal($80000000)); - ProgressProc(Cardinal($80000000)); - Dec(Bytes.Hi); - end; - ProgressProc(Bytes.Lo); + repeat + if Bytes >= MaxBytesPerProgressProcCall then + LimitedBytes := MaxBytesPerProgressProcCall + else + LimitedBytes := Cardinal(Bytes); + ProgressProc(LimitedBytes); + Dec(Bytes, LimitedBytes); + until Bytes = 0; end; if not ResetEvent(FEvents.WorkerHasProgressEvent) then LZMAWin32Error('UpdateProgress: ResetEvent'); diff --git a/Projects/Src/Compression.LZMACompressor/islzma/islzma_exe.c b/Projects/Src/Compression.LZMACompressor/islzma/islzma_exe.c index e780a928a..6be8937d2 100644 --- a/Projects/Src/Compression.LZMACompressor/islzma/islzma_exe.c +++ b/Projects/Src/Compression.LZMACompressor/islzma/islzma_exe.c @@ -50,7 +50,7 @@ struct TLZMACompressorSharedEvents { struct TLZMACompressorSharedData { volatile BOOL NoMoreInput; - volatile LongWord ProgressKB; + volatile UInt32 ProgressKB; volatile SRes EncodeResult; struct TLZMACompressorRingBuffer InputBuffer; struct TLZMACompressorRingBuffer OutputBuffer; @@ -245,7 +245,6 @@ static HRESULT ProgressMade(const UInt64 TotalBytesProcessed) /* Called from worker thread (or a thread spawned by the worker thread) */ { DWORD T; - UInt64 KBProcessed; HRESULT Result; T = GetTickCount(); @@ -259,9 +258,7 @@ static HRESULT ProgressMade(const UInt64 TotalBytesProcessed) "-1 for size means unknown value", though I don't see any place where LzmaEnc actually does call Progress with inSize = -1. */ if ((Int64)TotalBytesProcessed >= 0) { - KBProcessed = TotalBytesProcessed; - KBProcessed /= 1024; - FShared->ProgressKB = (LongWord)KBProcessed; + FShared->ProgressKB = (UInt32)(TotalBytesProcessed >> 10); } Result = WakeMainAndWaitUntil( THandle32ToHandle(FEvents->WorkerHasProgressEvent),