@@ -16,14 +16,35 @@ export namespace bs {
1616 /** Proposed size of output */
1717 export let stackSize : usize = 0 ;
1818
19+ /**
20+ * Proposes that the buffer size is should be greater than or equal to the proposed size.
21+ * If necessary, reallocates the buffer to the exact new size.
22+ * @param size - The size to propose.
23+ */
24+ // @ts -ignore: decorator
25+ @inline export function ensureSize ( size : u32 ) : void {
26+ // console.log("Ensure " + (stackSize).toString() + " -> " + (stackSize + size).toString() + " (" + size.toString() + ") " + (((stackSize + size) > bufferSize) ? "+" : ""));
27+ if ( offset + size > bufferSize + changetype < usize > ( buffer ) ) {
28+ const deltaBytes = nextPowerOf2 ( size + 64 ) ;
29+ bufferSize += deltaBytes ;
30+ // @ts -ignore: exists
31+ const newPtr = changetype < ArrayBuffer > ( __renew (
32+ changetype < usize > ( buffer ) ,
33+ bufferSize
34+ ) ) ;
35+ offset = offset + changetype < usize > ( newPtr ) - changetype < usize > ( buffer ) ;
36+ buffer = newPtr ;
37+ }
38+ }
39+
1940 /**
2041 * Proposes that the buffer size is should be greater than or equal to the proposed size.
2142 * If necessary, reallocates the buffer to the exact new size.
2243 * @param size - The size to propose.
2344 */
2445 // @ts -ignore: decorator
2546 @inline export function proposeSize ( size : u32 ) : void {
26- console . log ( "Propose " + ( stackSize ) . toString ( ) + " -> " + ( stackSize + size ) . toString ( ) + " (" + size . toString ( ) + ") " + ( ( ( stackSize + size ) > bufferSize ) ? "+" : "" ) ) ;
47+ // console.log("Propose " + (stackSize).toString() + " -> " + (stackSize + size).toString() + " (" + size.toString() + ") " + (((stackSize + size) > bufferSize) ? "+" : ""));
2748 if ( ( stackSize += size ) > bufferSize ) {
2849 const deltaBytes = nextPowerOf2 ( size ) ;
2950 bufferSize += deltaBytes ;
@@ -44,7 +65,7 @@ export namespace bs {
4465 */
4566 // @ts -ignore: decorator
4667 @inline export function growSize ( size : u32 ) : void {
47- console . log ( "Grow " + ( stackSize ) . toString ( ) + " -> " + ( stackSize + size ) . toString ( ) + " (" + size . toString ( ) + ") " + ( ( ( stackSize + size ) > bufferSize ) ? "+" : "" ) ) ;
68+ // console.log("Grow " + (stackSize).toString() + " -> " + (stackSize + size).toString() + " (" + size.toString() + ") " + (((stackSize + size) > bufferSize) ? "+" : ""));
4869 if ( ( stackSize += size ) > bufferSize ) {
4970 const deltaBytes = nextPowerOf2 ( size + 64 ) ;
5071 bufferSize += deltaBytes ;
@@ -53,7 +74,7 @@ export namespace bs {
5374 changetype < usize > ( buffer ) ,
5475 bufferSize
5576 ) ) ;
56- if ( buffer != newPtr ) console . log ( " Old: " + changetype < usize > ( buffer ) . toString ( ) + "\n New: " + changetype < usize > ( newPtr ) . toString ( ) ) ;
77+ // if (buffer != newPtr) console.log(" Old: " + changetype<usize>(buffer).toString() + "\n New: " + changetype<usize>(newPtr).toString());
5778 offset = offset + changetype < usize > ( newPtr ) - changetype < usize > ( buffer ) ;
5879 buffer = newPtr ;
5980 }
0 commit comments