@@ -307,6 +307,15 @@ function removeBlock(root: Root, block: Block): void {
307
307
// must perform those updates.
308
308
}
309
309
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
+
310
319
/** Searches for a free block of at least the specified size. */
311
320
function searchBlock ( root : Root , size : usize ) : Block | null {
312
321
// size was already asserted by caller
@@ -317,13 +326,8 @@ function searchBlock(root: Root, size: usize): Block | null {
317
326
fl = 0 ;
318
327
sl = < u32 > ( size >> AL_BITS ) ;
319
328
} 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 ) ;
327
331
sl = < u32 > ( ( requestSize >> ( fl - SL_BITS ) ) ^ ( 1 << SL_BITS ) ) ;
328
332
fl -= SB_BITS - 1 ;
329
333
}
@@ -428,10 +432,8 @@ function growMemory(root: Root, size: usize): void {
428
432
return ;
429
433
}
430
434
// 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 ) ;
435
437
}
436
438
// and additional BLOCK_OVERHEAD must be taken into account. If we are going
437
439
// to merge with the tail block, that's one time, otherwise it's two times.
0 commit comments