@@ -130,9 +130,9 @@ internal sealed unsafe class DeflaterEngine : IDisposable
130130 /// This array contains the part of the uncompressed stream that
131131 /// is of relevance. The current character is indexed by strstart.
132132 /// </summary>
133- private IManagedByteBuffer windowMemoryOwner ;
133+ private IMemoryOwner < byte > windowMemoryOwner ;
134134 private MemoryHandle windowMemoryHandle ;
135- private readonly byte [ ] window ;
135+ private readonly Memory < byte > window ;
136136 private readonly byte * pinnedWindowPointer ;
137137
138138 private int maxChain ;
@@ -153,19 +153,19 @@ public DeflaterEngine(MemoryAllocator memoryAllocator, DeflateStrategy strategy)
153153
154154 // Create pinned pointers to the various buffers to allow indexing
155155 // without bounds checks.
156- this . windowMemoryOwner = memoryAllocator . AllocateManagedByteBuffer ( 2 * DeflaterConstants . WSIZE ) ;
157- this . window = this . windowMemoryOwner . Array ;
158- this . windowMemoryHandle = this . windowMemoryOwner . Memory . Pin ( ) ;
156+ this . windowMemoryOwner = memoryAllocator . Allocate < byte > ( 2 * DeflaterConstants . WSIZE ) ;
157+ this . window = this . windowMemoryOwner . Memory ;
158+ this . windowMemoryHandle = this . window . Pin ( ) ;
159159 this . pinnedWindowPointer = ( byte * ) this . windowMemoryHandle . Pointer ;
160160
161161 this . headMemoryOwner = memoryAllocator . Allocate < short > ( DeflaterConstants . HASH_SIZE ) ;
162162 this . head = this . headMemoryOwner . Memory ;
163- this . headMemoryHandle = this . headMemoryOwner . Memory . Pin ( ) ;
163+ this . headMemoryHandle = this . head . Pin ( ) ;
164164 this . pinnedHeadPointer = ( short * ) this . headMemoryHandle . Pointer ;
165165
166166 this . prevMemoryOwner = memoryAllocator . Allocate < short > ( DeflaterConstants . WSIZE ) ;
167167 this . prev = this . prevMemoryOwner . Memory ;
168- this . prevMemoryHandle = this . prevMemoryOwner . Memory . Pin ( ) ;
168+ this . prevMemoryHandle = this . prev . Pin ( ) ;
169169 this . pinnedPrevPointer = ( short * ) this . prevMemoryHandle . Pointer ;
170170
171171 // We start at index 1, to avoid an implementation deficiency, that
@@ -303,7 +303,7 @@ public void SetLevel(int level)
303303 case DeflaterConstants . DEFLATE_STORED :
304304 if ( this . strstart > this . blockStart )
305305 {
306- this . huffman . FlushStoredBlock ( this . window , this . blockStart , this . strstart - this . blockStart , false ) ;
306+ this . huffman . FlushStoredBlock ( this . window . Span , this . blockStart , this . strstart - this . blockStart , false ) ;
307307 this . blockStart = this . strstart ;
308308 }
309309
@@ -313,7 +313,7 @@ public void SetLevel(int level)
313313 case DeflaterConstants . DEFLATE_FAST :
314314 if ( this . strstart > this . blockStart )
315315 {
316- this . huffman . FlushBlock ( this . window , this . blockStart , this . strstart - this . blockStart , false ) ;
316+ this . huffman . FlushBlock ( this . window . Span , this . blockStart , this . strstart - this . blockStart , false ) ;
317317 this . blockStart = this . strstart ;
318318 }
319319
@@ -327,7 +327,7 @@ public void SetLevel(int level)
327327
328328 if ( this . strstart > this . blockStart )
329329 {
330- this . huffman . FlushBlock ( this . window , this . blockStart , this . strstart - this . blockStart , false ) ;
330+ this . huffman . FlushBlock ( this . window . Span , this . blockStart , this . strstart - this . blockStart , false ) ;
331331 this . blockStart = this . strstart ;
332332 }
333333
@@ -362,7 +362,10 @@ public void FillWindow()
362362 more = this . inputEnd - this . inputOff ;
363363 }
364364
365- Buffer . BlockCopy ( this . inputBuf , this . inputOff , this . window , this . strstart + this . lookahead , more ) ;
365+ Unsafe . CopyBlockUnaligned (
366+ ref this . window . Span [ this . strstart + this . lookahead ] ,
367+ ref this . inputBuf [ this . inputOff ] ,
368+ unchecked ( ( uint ) more ) ) ;
366369
367370 this . inputOff += more ;
368371 this . lookahead += more ;
@@ -426,7 +429,11 @@ private int InsertString()
426429
427430 private void SlideWindow ( )
428431 {
429- Unsafe . CopyBlockUnaligned ( ref this . window [ 0 ] , ref this . window [ DeflaterConstants . WSIZE ] , DeflaterConstants . WSIZE ) ;
432+ Unsafe . CopyBlockUnaligned (
433+ ref this . window . Span [ 0 ] ,
434+ ref this . window . Span [ DeflaterConstants . WSIZE ] ,
435+ DeflaterConstants . WSIZE ) ;
436+
430437 this . matchStart -= DeflaterConstants . WSIZE ;
431438 this . strstart -= DeflaterConstants . WSIZE ;
432439 this . blockStart -= DeflaterConstants . WSIZE ;
@@ -663,7 +670,7 @@ private bool DeflateStored(bool flush, bool finish)
663670 lastBlock = false ;
664671 }
665672
666- this . huffman . FlushStoredBlock ( this . window , this . blockStart , storedLength , lastBlock ) ;
673+ this . huffman . FlushStoredBlock ( this . window . Span , this . blockStart , storedLength , lastBlock ) ;
667674 this . blockStart += storedLength ;
668675 return ! ( lastBlock || storedLength == 0 ) ;
669676 }
@@ -683,7 +690,7 @@ private bool DeflateFast(bool flush, bool finish)
683690 if ( this . lookahead == 0 )
684691 {
685692 // We are flushing everything
686- this . huffman . FlushBlock ( this . window , this . blockStart , this . strstart - this . blockStart , finish ) ;
693+ this . huffman . FlushBlock ( this . window . Span , this . blockStart , this . strstart - this . blockStart , finish ) ;
687694 this . blockStart = this . strstart ;
688695 return false ;
689696 }
@@ -743,7 +750,7 @@ private bool DeflateFast(bool flush, bool finish)
743750 if ( this . huffman . IsFull ( ) )
744751 {
745752 bool lastBlock = finish && ( this . lookahead == 0 ) ;
746- this . huffman . FlushBlock ( this . window , this . blockStart , this . strstart - this . blockStart , lastBlock ) ;
753+ this . huffman . FlushBlock ( this . window . Span , this . blockStart , this . strstart - this . blockStart , lastBlock ) ;
747754 this . blockStart = this . strstart ;
748755 return ! lastBlock ;
749756 }
@@ -771,7 +778,7 @@ private bool DeflateSlow(bool flush, bool finish)
771778 this . prevAvailable = false ;
772779
773780 // We are flushing everything
774- this . huffman . FlushBlock ( this . window , this . blockStart , this . strstart - this . blockStart , finish ) ;
781+ this . huffman . FlushBlock ( this . window . Span , this . blockStart , this . strstart - this . blockStart , finish ) ;
775782 this . blockStart = this . strstart ;
776783 return false ;
777784 }
@@ -846,7 +853,7 @@ private bool DeflateSlow(bool flush, bool finish)
846853 }
847854
848855 bool lastBlock = finish && ( this . lookahead == 0 ) && ! this . prevAvailable ;
849- this . huffman . FlushBlock ( this . window , this . blockStart , len , lastBlock ) ;
856+ this . huffman . FlushBlock ( this . window . Span , this . blockStart , len , lastBlock ) ;
850857 this . blockStart += len ;
851858 return ! lastBlock ;
852859 }
0 commit comments