@@ -307,6 +307,15 @@ function removeBlock(root: Root, block: Block): void {
307307 // must perform those updates.
308308}
309309
310+ function roundSize ( size : usize ) : usize {
311+ const halfMaxSize = BLOCK_MAXSIZE >> 1 ; // don't round last fl
312+ const inv : usize = sizeof < usize > ( ) * 8 - 1 ;
313+ const invRound = inv - SL_BITS ;
314+ return size < halfMaxSize
315+ ? size + ( 1 << ( invRound - clz < usize > ( size ) ) ) - 1
316+ : size ;
317+ }
318+
310319/** Searches for a free block of at least the specified size. */
311320function searchBlock ( root : Root , size : usize ) : Block | null {
312321 // size was already asserted by caller
@@ -317,13 +326,8 @@ function searchBlock(root: Root, size: usize): Block | null {
317326 fl = 0 ;
318327 sl = < u32 > ( size >> AL_BITS ) ;
319328 } else {
320- const halfMaxSize = BLOCK_MAXSIZE >> 1 ; // don't round last fl
321- const inv : usize = sizeof < usize > ( ) * 8 - 1 ;
322- const invRound = inv - SL_BITS ;
323- let requestSize = size < halfMaxSize
324- ? size + ( 1 << ( invRound - clz < usize > ( size ) ) ) - 1
325- : size ;
326- fl = inv - clz < usize > ( requestSize ) ;
329+ const requestSize = roundSize ( size ) ;
330+ fl = sizeof < usize > ( ) * 8 - 1 - clz < usize > ( requestSize ) ;
327331 sl = < u32 > ( ( requestSize >> ( fl - SL_BITS ) ) ^ ( 1 << SL_BITS ) ) ;
328332 fl -= SB_BITS - 1 ;
329333 }
@@ -428,10 +432,8 @@ function growMemory(root: Root, size: usize): void {
428432 return ;
429433 }
430434 // Here, both rounding performed in searchBlock ...
431- const halfMaxSize = BLOCK_MAXSIZE >> 1 ;
432- if ( size < halfMaxSize ) { // don't round last fl
433- const invRound = ( sizeof < usize > ( ) * 8 - 1 ) - SL_BITS ;
434- size += ( 1 << ( invRound - clz < usize > ( size ) ) ) - 1 ;
435+ if ( size > SB_SIZE ) { // don't round last fl
436+ size = roundSize ( size ) ;
435437 }
436438 // and additional BLOCK_OVERHEAD must be taken into account. If we are going
437439 // to merge with the tail block, that's one time, otherwise it's two times.
0 commit comments