@@ -6,6 +6,7 @@ import { PendingThrow } from "./thread.ts";
66const ALIGN = 16 ;
77// Like a failed malloc in HotSpot, going past this throws OutOfMemoryError.
88const LIMIT = 512 << 20 ;
9+ const MIN_SHRINK = 2 << 20 ;
910
1011export class NativeMemory {
1112 buf : ArrayBuffer ;
@@ -15,40 +16,79 @@ export class NativeMemory {
1516 private sizes = new Map < number , number > ( ) ;
1617 // free blocks by size
1718 private free = new Map < number , number [ ] > ( ) ;
19+ // end address to size, and start address to list index, for the free blocks
20+ private freeEnds = new Map < number , number > ( ) ;
21+ private freeAt = new Map < number , number > ( ) ;
22+
23+ private initial : number ;
1824
1925 constructor ( initial = 1 << 20 ) {
26+ this . initial = initial ;
2027 this . buf = new ArrayBuffer ( initial ) ;
2128 this . u8 = new Uint8Array ( this . buf ) ;
2229 this . dv = new DataView ( this . buf ) ;
2330 }
2431
32+ private resize ( size : number ) {
33+ const next = new ArrayBuffer ( size ) ;
34+ new Uint8Array ( next ) . set ( this . u8 . subarray ( 0 , Math . min ( this . top , size ) ) ) ;
35+ this . buf = next ;
36+ this . u8 = new Uint8Array ( next ) ;
37+ this . dv = new DataView ( next ) ;
38+ }
39+
2540 private grow ( need : number ) {
2641 if ( need > LIMIT ) throw new PendingThrow ( "java/lang/OutOfMemoryError" , `Unable to allocate ${ need - this . buf . byteLength } bytes` ) ;
2742 let size = this . buf . byteLength ;
2843 while ( size < need ) size *= 2 ;
2944 size = Math . min ( size , LIMIT ) ;
30- const next = new ArrayBuffer ( size ) ;
31- new Uint8Array ( next ) . set ( this . u8 ) ;
32- this . buf = next ;
33- this . u8 = new Uint8Array ( next ) ;
34- this . dv = new DataView ( next ) ;
45+ try { this . resize ( size ) ; }
46+ catch ( e ) {
47+ if ( e instanceof RangeError ) throw new PendingThrow ( "java/lang/OutOfMemoryError" , `Unable to allocate ${ size } bytes` ) ;
48+ throw e ;
49+ }
50+ }
51+
52+ // The saved state of a snapshot: the bytes up to top and the block tables.
53+ restore ( bytes : Uint8Array , top : number , sizes : Map < number , number > , free : Map < number , number [ ] > ) {
54+ this . buf = new ArrayBuffer ( Math . max ( this . buf . byteLength , bytes . length ) ) ;
55+ this . u8 = new Uint8Array ( this . buf ) ;
56+ this . dv = new DataView ( this . buf ) ;
57+ this . u8 . set ( bytes ) ;
58+ this . top = top ;
59+ this . sizes = sizes ;
60+ this . free = free ;
61+ this . freeEnds . clear ( ) ;
62+ this . freeAt . clear ( ) ;
63+ for ( const [ size , blocks ] of free ) {
64+ blocks . forEach ( ( start , i ) => { this . freeEnds . set ( start + size , size ) ; this . freeAt . set ( start , i ) ; } ) ;
65+ }
66+ }
67+
68+ // The order of a free list does not matter, so the last block fills the gap.
69+ private takeFree ( size : number , list : number [ ] , at : number ) : number {
70+ const addr = list [ at ] ;
71+ const last = list . pop ( ) ! ;
72+ if ( at < list . length ) { list [ at ] = last ; this . freeAt . set ( last , at ) ; }
73+ if ( ! list . length ) this . free . delete ( size ) ;
74+ this . freeAt . delete ( addr ) ;
75+ this . freeEnds . delete ( addr + size ) ;
76+ return addr ;
3577 }
3678
3779 malloc ( n : number , alignment = ALIGN ) : number {
3880 const size = Math . max ( ALIGN , Math . ceil ( n / ALIGN ) * ALIGN ) ;
3981 const list = this . free . get ( size ) ;
4082 const reuse = list ?. findIndex ( ( addr ) => addr % alignment === 0 ) ?? - 1 ;
4183 if ( list && reuse >= 0 ) {
42- // the order of a free list does not matter, so the last block fills the gap
43- const addr = list [ reuse ] ;
44- list [ reuse ] = list [ list . length - 1 ] ;
45- list . pop ( ) ;
84+ const addr = this . takeFree ( size , list , reuse ) ;
4685 this . sizes . set ( addr , size ) ;
4786 return addr ;
4887 }
4988 const addr = Math . ceil ( this . top / alignment ) * alignment ;
50- this . top = addr + size ;
51- if ( this . top > this . buf . byteLength ) this . grow ( this . top ) ;
89+ const top = addr + size ;
90+ if ( top > this . buf . byteLength ) this . grow ( top ) ;
91+ this . top = top ;
5292 this . sizes . set ( addr , size ) ;
5393 return addr ;
5494 }
@@ -57,9 +97,28 @@ export class NativeMemory {
5797 const size = this . sizes . get ( addr ) ;
5898 if ( size === undefined ) return ;
5999 this . sizes . delete ( addr ) ;
60- let list = this . free . get ( size ) ;
61- if ( ! list ) this . free . set ( size , ( list = [ ] ) ) ;
62- list . push ( addr ) ;
100+ if ( addr + size < this . top ) {
101+ let list = this . free . get ( size ) ;
102+ if ( ! list ) this . free . set ( size , ( list = [ ] ) ) ;
103+ this . freeAt . set ( addr , list . push ( addr ) - 1 ) ;
104+ this . freeEnds . set ( addr + size , size ) ;
105+ return ;
106+ }
107+ // Lower top over this block and any free blocks that end where it starts.
108+ let top = addr ;
109+ for ( let below = this . freeEnds . get ( top ) ; below !== undefined ; below = this . freeEnds . get ( top ) ) {
110+ top -= below ;
111+ this . takeFree ( below , this . free . get ( below ) ! , this . freeAt . get ( top ) ! ) ;
112+ }
113+ this . top = Math . max ( top , ALIGN ) ;
114+ if ( this . buf . byteLength >= MIN_SHRINK && this . top <= this . buf . byteLength / 4 ) {
115+ const size = Math . max ( this . initial , 2 ** Math . ceil ( Math . log2 ( this . top ) ) ) ;
116+ // best effort: a free must not fail
117+ if ( size < this . buf . byteLength ) {
118+ try { this . resize ( size ) ; }
119+ catch ( e ) { if ( ! ( e instanceof RangeError ) ) throw e ; }
120+ }
121+ }
63122 }
64123
65124 realloc ( addr : number , n : number ) : number {
0 commit comments