2018-04-07 03:27:22 +02:00
|
|
|
import { AL_MASK, MAX_SIZE_32 } from "./allocator";
|
|
|
|
|
|
|
|
/** Size of an ArrayBuffer header. */
|
|
|
|
export const HEADER_SIZE: usize = (offsetof<ArrayBuffer>() + AL_MASK) & ~AL_MASK;
|
|
|
|
|
|
|
|
/** Maximum byte length of an ArrayBuffer. */
|
|
|
|
export const MAX_BLENGTH: i32 = <i32>MAX_SIZE_32 - HEADER_SIZE;
|
|
|
|
|
|
|
|
/** Computes an ArrayBuffer's size in memory. */
|
|
|
|
export function computeSize(byteLength: i32): usize {
|
|
|
|
// round up to power of 2, with HEADER_SIZE=8:
|
|
|
|
// 0 -> 2^3 = 8
|
|
|
|
// 1..8 -> 2^4 = 16
|
|
|
|
// 9..24 -> 2^5 = 32
|
|
|
|
// ...
|
|
|
|
// MAX_LENGTH -> 2^30 = 0x40000000 (MAX_SIZE_32)
|
|
|
|
return <usize>1 << <usize>(<u32>32 - clz<u32>(byteLength + HEADER_SIZE - 1));
|
|
|
|
}
|
|
|
|
|
2018-04-08 00:43:38 +02:00
|
|
|
/** Allocates a raw ArrayBuffer. Contents remain uninitialized. */
|
|
|
|
export function allocUnsafe(byteLength: i32): ArrayBuffer {
|
2018-04-07 03:27:22 +02:00
|
|
|
assert(<u32>byteLength <= <u32>MAX_BLENGTH);
|
2018-07-18 23:49:32 +02:00
|
|
|
var buffer = memory.allocate(computeSize(byteLength));
|
2018-04-07 03:27:22 +02:00
|
|
|
store<i32>(buffer, byteLength, offsetof<ArrayBuffer>("byteLength"));
|
|
|
|
return changetype<ArrayBuffer>(buffer);
|
|
|
|
}
|
|
|
|
|
2018-04-08 00:43:38 +02:00
|
|
|
/** Reallocates an ArrayBuffer, resizing it as requested. Tries to modify the buffer in place. */
|
|
|
|
export function reallocUnsafe(buffer: ArrayBuffer, newByteLength: i32): ArrayBuffer {
|
|
|
|
var oldByteLength = buffer.byteLength;
|
|
|
|
if (newByteLength > oldByteLength) {
|
|
|
|
assert(newByteLength <= MAX_BLENGTH);
|
2018-04-11 23:35:19 +02:00
|
|
|
if (newByteLength <= <i32>(computeSize(oldByteLength) - HEADER_SIZE)) { // fast path: zero out additional space
|
2018-04-08 00:43:38 +02:00
|
|
|
store<i32>(changetype<usize>(buffer), newByteLength, offsetof<ArrayBuffer>("byteLength"));
|
2018-07-18 23:49:32 +02:00
|
|
|
memory.fill(
|
2018-04-11 23:35:19 +02:00
|
|
|
changetype<usize>(buffer) + HEADER_SIZE + <usize>oldByteLength,
|
2018-04-08 00:43:38 +02:00
|
|
|
0,
|
|
|
|
<usize>(newByteLength - oldByteLength)
|
|
|
|
);
|
|
|
|
} else { // slow path: copy to new buffer
|
|
|
|
let newBuffer = allocUnsafe(newByteLength);
|
2018-07-18 23:49:32 +02:00
|
|
|
memory.copy(
|
2018-04-08 00:43:38 +02:00
|
|
|
changetype<usize>(newBuffer) + HEADER_SIZE,
|
|
|
|
changetype<usize>(buffer) + HEADER_SIZE,
|
2018-04-11 23:35:19 +02:00
|
|
|
<usize>oldByteLength
|
|
|
|
);
|
2018-07-18 23:49:32 +02:00
|
|
|
memory.fill(
|
2018-04-11 23:35:19 +02:00
|
|
|
changetype<usize>(newBuffer) + HEADER_SIZE + <usize>oldByteLength,
|
|
|
|
0,
|
|
|
|
<usize>(newByteLength - oldByteLength)
|
2018-04-08 00:43:38 +02:00
|
|
|
);
|
|
|
|
return newBuffer;
|
|
|
|
}
|
|
|
|
} else if (newByteLength < oldByteLength) { // fast path: override size
|
|
|
|
// TBD: worth to copy and release if size is significantly less than before?
|
|
|
|
assert(newByteLength >= 0);
|
|
|
|
store<i32>(changetype<usize>(buffer), newByteLength, offsetof<ArrayBuffer>("byteLength"));
|
|
|
|
}
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2018-04-11 23:35:19 +02:00
|
|
|
@inline
|
2018-04-25 05:04:35 +02:00
|
|
|
export function loadUnsafe<T,V>(buffer: ArrayBuffer, index: i32): V {
|
|
|
|
return <V>load<T>(changetype<usize>(buffer) + (<usize>index << alignof<T>()), HEADER_SIZE);
|
2018-04-11 23:35:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@inline
|
2018-04-25 05:04:35 +02:00
|
|
|
export function storeUnsafe<T,V>(buffer: ArrayBuffer, index: i32, value: V): void {
|
2018-04-11 23:35:19 +02:00
|
|
|
store<T>(changetype<usize>(buffer) + (<usize>index << alignof<T>()), value, HEADER_SIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
@inline
|
2018-04-25 05:04:35 +02:00
|
|
|
export function loadUnsafeWithOffset<T,V>(buffer: ArrayBuffer, index: i32, byteOffset: i32): V {
|
|
|
|
return <V>load<T>(changetype<usize>(buffer) + <usize>byteOffset + (<usize>index << alignof<T>()), HEADER_SIZE);
|
2018-04-11 23:35:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@inline
|
2018-04-25 05:04:35 +02:00
|
|
|
export function storeUnsafeWithOffset<T,V>(buffer: ArrayBuffer, index: i32, value: V, byteOffset: i32): void {
|
2018-04-11 23:35:19 +02:00
|
|
|
store<T>(changetype<usize>(buffer) + <usize>byteOffset + (<usize>index << alignof<T>()), value, HEADER_SIZE);
|
|
|
|
}
|