@@ -459,22 +459,26 @@ function prepareSize(size: usize): usize {
459459}
460460
461461/** Initilizes the root structure. */
462- export function initializeRoot ( ) : void {
463- var rootOffset = ( __heap_base + AL_MASK ) & ~ AL_MASK ;
464- var pagesBefore = memory . size ( ) ;
465- var pagesNeeded = < i32 > ( ( ( ( rootOffset + ROOT_SIZE ) + 0xffff ) & ~ 0xffff ) >>> 16 ) ;
466- if ( pagesNeeded > pagesBefore && memory . grow ( pagesNeeded - pagesBefore ) < 0 ) unreachable ( ) ;
467- var root = changetype < Root > ( rootOffset ) ;
468- root . flMap = 0 ;
469- SETTAIL ( root , changetype < Block > ( 0 ) ) ;
470- for ( let fl : usize = 0 ; fl < FL_BITS ; ++ fl ) {
471- SETSL ( root , fl , 0 ) ;
472- for ( let sl : u32 = 0 ; sl < SL_SIZE ; ++ sl ) {
473- SETHEAD ( root , fl , sl , null ) ;
462+ export function maybeInitialize ( ) : Root {
463+ var root = ROOT ;
464+ if ( ! root ) {
465+ const rootOffset = ( __heap_base + AL_MASK ) & ~ AL_MASK ;
466+ let pagesBefore = memory . size ( ) ;
467+ let pagesNeeded = < i32 > ( ( ( ( rootOffset + ROOT_SIZE ) + 0xffff ) & ~ 0xffff ) >>> 16 ) ;
468+ if ( pagesNeeded > pagesBefore && memory . grow ( pagesNeeded - pagesBefore ) < 0 ) unreachable ( ) ;
469+ root = changetype < Root > ( rootOffset ) ;
470+ root . flMap = 0 ;
471+ SETTAIL ( root , changetype < Block > ( 0 ) ) ;
472+ for ( let fl : usize = 0 ; fl < FL_BITS ; ++ fl ) {
473+ SETSL ( root , fl , 0 ) ;
474+ for ( let sl : u32 = 0 ; sl < SL_SIZE ; ++ sl ) {
475+ SETHEAD ( root , fl , sl , null ) ;
476+ }
474477 }
478+ addMemory ( root , ( rootOffset + ROOT_SIZE + AL_MASK ) & ~ AL_MASK , memory . size ( ) << 16 ) ;
479+ ROOT = root ;
475480 }
476- addMemory ( root , ( rootOffset + ROOT_SIZE + AL_MASK ) & ~ AL_MASK , memory . size ( ) << 16 ) ;
477- ROOT = root ;
481+ return root ;
478482}
479483
480484// @ts -ignore: decorator
@@ -551,9 +555,11 @@ export function reallocateBlock(root: Root, block: Block, size: usize): Block {
551555 var newBlock = allocateBlock ( root , size ) ;
552556 newBlock . rtId = block . rtId ;
553557 memory . copy ( changetype < usize > ( newBlock ) + BLOCK_OVERHEAD , changetype < usize > ( block ) + BLOCK_OVERHEAD , size ) ;
554- block . mmInfo = blockInfo | FREE ;
555- insertBlock ( root , block ) ;
556- if ( isDefined ( ASC_RTRACE ) ) onfree ( block ) ;
558+ if ( changetype < usize > ( block ) >= __heap_base ) {
559+ block . mmInfo = blockInfo | FREE ;
560+ insertBlock ( root , block ) ;
561+ if ( isDefined ( ASC_RTRACE ) ) onfree ( block ) ;
562+ }
557563 return newBlock ;
558564}
559565
@@ -569,28 +575,21 @@ export function freeBlock(root: Root, block: Block): void {
569575// @ts -ignore: decorator
570576@global @unsafe
571577export function __alloc ( size : usize , id : u32 ) : usize {
572- var root = ROOT ;
573- if ( ! root ) {
574- initializeRoot ( ) ;
575- root = ROOT ;
576- }
577- var block = allocateBlock ( root , size ) ;
578+ var block = allocateBlock ( maybeInitialize ( ) , size ) ;
578579 block . rtId = id ;
579580 return changetype < usize > ( block ) + BLOCK_OVERHEAD ;
580581}
581582
582583// @ts -ignore: decorator
583584@global @unsafe
584585export function __realloc ( ref : usize , size : usize ) : usize {
585- if ( DEBUG ) assert ( ROOT ) ; // must be initialized
586586 assert ( ref != 0 && ! ( ref & AL_MASK ) ) ; // must exist and be aligned
587- return changetype < usize > ( reallocateBlock ( ROOT , changetype < Block > ( ref - BLOCK_OVERHEAD ) , size ) ) + BLOCK_OVERHEAD ;
587+ return changetype < usize > ( reallocateBlock ( maybeInitialize ( ) , changetype < Block > ( ref - BLOCK_OVERHEAD ) , size ) ) + BLOCK_OVERHEAD ;
588588}
589589
590590// @ts -ignore: decorator
591591@global @unsafe
592592export function __free ( ref : usize ) : void {
593- if ( DEBUG ) assert ( ROOT ) ; // must be initialized
594593 assert ( ref != 0 && ! ( ref & AL_MASK ) ) ; // must exist and be aligned
595- freeBlock ( ROOT , changetype < Block > ( ref - BLOCK_OVERHEAD ) ) ;
594+ freeBlock ( maybeInitialize ( ) , changetype < Block > ( ref - BLOCK_OVERHEAD ) ) ;
596595}
0 commit comments