File tree Expand file tree Collapse file tree 1 file changed +14
-8
lines changed Expand file tree Collapse file tree 1 file changed +14
-8
lines changed Original file line number Diff line number Diff line change @@ -80,25 +80,31 @@ public static RentedBuffer RentBuffer(int size)
8080 throw new ArgumentOutOfRangeException ( nameof ( size ) , size , $ "Valid buffer size range is [1..{ MaxAllocationSize } ] bytes.") ;
8181 }
8282
83- __isBufferRented = true ;
84-
8583 if ( __threadId == default )
8684 {
8785 __threadId = Thread . CurrentThread . ManagedThreadId ;
8886 }
8987
88+ // Allocate space
89+ RentedBuffer rentedBuffer ;
9090 if ( size > MaxSize )
9191 {
92- return new RentedBuffer ( __threadId , new byte [ size ] ) ;
92+ rentedBuffer = new RentedBuffer ( __threadId , new byte [ size ] ) ;
9393 }
94-
95- if ( __buffer == null || __buffer . Length < size )
94+ else
9695 {
97- var newSize = size <= MinSize ? MinSize : PowerOf2 . RoundUpToPowerOf2 ( size ) ;
98- __buffer = new byte [ newSize ] ;
96+ if ( __buffer == null || __buffer . Length < size )
97+ {
98+ var newSize = size <= MinSize ? MinSize : PowerOf2 . RoundUpToPowerOf2 ( size ) ;
99+ __buffer = new byte [ newSize ] ;
100+ }
101+
102+ rentedBuffer = new RentedBuffer ( __threadId , __buffer ) ;
99103 }
100104
101- return new RentedBuffer ( __threadId , __buffer ) ;
105+ // Set this last (in case of an exception in this method we want this to stay as-is.)
106+ __isBufferRented = true ;
107+ return rentedBuffer ;
102108 }
103109 }
104110}
You can’t perform that action at this time.
0 commit comments