mirror of
https://github.com/fluencelabs/assemblyscript
synced 2025-06-17 00:41:32 +00:00
Initial GC integration (#196)
This commit is contained in:
@ -2,12 +2,10 @@ 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 {
|
||||
function computeSize(byteLength: i32): usize {
|
||||
// round up to power of 2, with HEADER_SIZE=8:
|
||||
// 0 -> 2^3 = 8
|
||||
// 1..8 -> 2^4 = 16
|
||||
@ -17,16 +15,23 @@ export function computeSize(byteLength: i32): usize {
|
||||
return <usize>1 << <usize>(<u32>32 - clz<u32>(byteLength + HEADER_SIZE - 1));
|
||||
}
|
||||
|
||||
/** Allocates a raw ArrayBuffer. Contents remain uninitialized. */
|
||||
export function allocUnsafe(byteLength: i32): ArrayBuffer {
|
||||
// Low-level utility
|
||||
|
||||
function __gc(ref: usize): void {}
|
||||
|
||||
export function allocateUnsafe(byteLength: i32): ArrayBuffer {
|
||||
assert(<u32>byteLength <= <u32>MAX_BLENGTH);
|
||||
var buffer = memory.allocate(computeSize(byteLength));
|
||||
var buffer: usize;
|
||||
if (isManaged<ArrayBuffer>()) {
|
||||
buffer = __gc_allocate(computeSize(byteLength), __gc); // tslint:disable-line
|
||||
} else {
|
||||
buffer = memory.allocate(computeSize(byteLength));
|
||||
}
|
||||
store<i32>(buffer, byteLength, offsetof<ArrayBuffer>("byteLength"));
|
||||
return changetype<ArrayBuffer>(buffer);
|
||||
}
|
||||
|
||||
/** Reallocates an ArrayBuffer, resizing it as requested. Tries to modify the buffer in place. */
|
||||
export function reallocUnsafe(buffer: ArrayBuffer, newByteLength: i32): ArrayBuffer {
|
||||
export function reallocateUnsafe(buffer: ArrayBuffer, newByteLength: i32): ArrayBuffer {
|
||||
var oldByteLength = buffer.byteLength;
|
||||
if (newByteLength > oldByteLength) {
|
||||
assert(newByteLength <= MAX_BLENGTH);
|
||||
@ -38,7 +43,7 @@ export function reallocUnsafe(buffer: ArrayBuffer, newByteLength: i32): ArrayBuf
|
||||
<usize>(newByteLength - oldByteLength)
|
||||
);
|
||||
} else { // slow path: copy to new buffer
|
||||
let newBuffer = allocUnsafe(newByteLength);
|
||||
let newBuffer = allocateUnsafe(newByteLength);
|
||||
memory.copy(
|
||||
changetype<usize>(newBuffer) + HEADER_SIZE,
|
||||
changetype<usize>(buffer) + HEADER_SIZE,
|
||||
@ -59,22 +64,18 @@ export function reallocUnsafe(buffer: ArrayBuffer, newByteLength: i32): ArrayBuf
|
||||
return buffer;
|
||||
}
|
||||
|
||||
@inline
|
||||
export function loadUnsafe<T,V>(buffer: ArrayBuffer, index: i32): V {
|
||||
@inline export function loadUnsafe<T,V>(buffer: ArrayBuffer, index: i32): V {
|
||||
return <V>load<T>(changetype<usize>(buffer) + (<usize>index << alignof<T>()), HEADER_SIZE);
|
||||
}
|
||||
|
||||
@inline
|
||||
export function storeUnsafe<T,V>(buffer: ArrayBuffer, index: i32, value: V): void {
|
||||
@inline export function storeUnsafe<T,V>(buffer: ArrayBuffer, index: i32, value: V): void {
|
||||
store<T>(changetype<usize>(buffer) + (<usize>index << alignof<T>()), value, HEADER_SIZE);
|
||||
}
|
||||
|
||||
@inline
|
||||
export function loadUnsafeWithOffset<T,V>(buffer: ArrayBuffer, index: i32, byteOffset: i32): V {
|
||||
@inline 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);
|
||||
}
|
||||
|
||||
@inline
|
||||
export function storeUnsafeWithOffset<T,V>(buffer: ArrayBuffer, index: i32, value: V, byteOffset: i32): void {
|
||||
@inline export function storeUnsafeWithOffset<T,V>(buffer: ArrayBuffer, index: i32, value: V, byteOffset: i32): void {
|
||||
store<T>(changetype<usize>(buffer) + <usize>byteOffset + (<usize>index << alignof<T>()), value, HEADER_SIZE);
|
||||
}
|
||||
|
Reference in New Issue
Block a user