2019-03-13 03:09:24 +01:00
|
|
|
import { AL_MASK, MAX_SIZE_32 } from "./util/allocator";
|
2019-03-14 04:33:58 +01:00
|
|
|
import { HEAP_BASE, memory } from "./memory";
|
2019-03-10 21:38:15 +01:00
|
|
|
|
2019-03-15 09:26:31 +01:00
|
|
|
/** Whether the memory manager interface is implemented. */
|
|
|
|
// @ts-ignore: decorator, stub
|
|
|
|
@lazy export const MM_IMPLEMENTED: bool = isDefined(__memory_allocate);
|
|
|
|
|
|
|
|
/** Whether the garbage collector interface is implemented. */
|
|
|
|
// @ts-ignore: decorator, stub
|
|
|
|
@lazy export const GC_IMPLEMENTED: bool = isDefined(__gc_register);
|
|
|
|
|
|
|
|
/** Common runtime header. Each managed object has one. */
|
|
|
|
@unmanaged export class HEADER {
|
|
|
|
/** Unique id of the respective class or a magic value if not yet registered.*/
|
|
|
|
classId: u32;
|
|
|
|
/** Size of the allocated payload. */
|
|
|
|
payloadSize: u32;
|
|
|
|
/** Reserved field for use by GC. Only present if GC is. */
|
|
|
|
gc1: usize; // itcm: tagged next
|
|
|
|
/** Reserved field for use by GC. Only present if GC is. */
|
|
|
|
gc2: usize; // itcm: prev
|
|
|
|
}
|
2019-03-08 22:04:20 +01:00
|
|
|
|
2019-03-15 09:26:31 +01:00
|
|
|
/** Common runtime header size. */
|
|
|
|
export const HEADER_SIZE: usize = GC_IMPLEMENTED
|
|
|
|
? (offsetof<HEADER>( ) + AL_MASK) & ~AL_MASK // full header if GC is present
|
|
|
|
: (offsetof<HEADER>("gc1") + AL_MASK) & ~AL_MASK; // half header if GC is absent
|
|
|
|
|
|
|
|
/** Common runtime header magic. Used to assert registered/unregistered status. */
|
|
|
|
export const HEADER_MAGIC: u32 = 0xA55E4B17;
|
|
|
|
|
|
|
|
/** Gets the computed unique class id of a class type. */
|
|
|
|
// @ts-ignore: decorator
|
|
|
|
@unsafe @builtin
|
|
|
|
export declare function CLASSID<T>(): u32;
|
|
|
|
|
|
|
|
/** Iterates over all root objects of a reference type. */
|
|
|
|
// @ts-ignore: decorator
|
|
|
|
@unsafe @builtin
|
|
|
|
export declare function ITERATEROOTS(fn: (ref: usize) => void): void;
|
|
|
|
|
|
|
|
/** Adjusts an allocation to actual block size. Primarily targets TLSF. */
|
2019-03-17 00:11:16 +01:00
|
|
|
export function ADJUSTOBLOCK(payloadSize: usize): usize {
|
2019-03-15 09:26:31 +01:00
|
|
|
// round up to power of 2, e.g. 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>(payloadSize + HEADER_SIZE - 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Allocates a new object and returns a pointer to its payload. Does not fill. */
|
|
|
|
// @ts-ignore: decorator
|
2019-03-16 07:26:33 +01:00
|
|
|
@unsafe @inline
|
2019-03-15 09:26:31 +01:00
|
|
|
export function ALLOCATE(payloadSize: usize): usize {
|
2019-03-16 07:26:33 +01:00
|
|
|
return doAllocate(payloadSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
function doAllocate(payloadSize: usize): usize {
|
2019-03-17 00:11:16 +01:00
|
|
|
var header = changetype<HEADER>(memory.allocate(ADJUSTOBLOCK(payloadSize)));
|
2019-03-15 09:26:31 +01:00
|
|
|
header.classId = HEADER_MAGIC;
|
|
|
|
header.payloadSize = payloadSize;
|
|
|
|
if (GC_IMPLEMENTED) {
|
|
|
|
header.gc1 = 0;
|
|
|
|
header.gc2 = 0;
|
2019-03-08 22:50:30 +01:00
|
|
|
}
|
2019-03-15 09:26:31 +01:00
|
|
|
return changetype<usize>(header) + HEADER_SIZE;
|
|
|
|
}
|
2019-03-11 07:45:47 +01:00
|
|
|
|
2019-03-15 09:26:31 +01:00
|
|
|
/** Reallocates an object if necessary. Returns a pointer to its (moved) payload. */
|
|
|
|
// @ts-ignore: decorator
|
2019-03-16 07:26:33 +01:00
|
|
|
@unsafe @inline
|
2019-03-15 09:26:31 +01:00
|
|
|
export function REALLOCATE(ref: usize, newPayloadSize: usize): usize {
|
2019-03-16 07:26:33 +01:00
|
|
|
return doReallocate(ref, newPayloadSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
function doReallocate(ref: usize, newPayloadSize: usize): usize {
|
2019-03-15 09:26:31 +01:00
|
|
|
// Background: When managed objects are allocated these aren't immediately registered with GC
|
|
|
|
// but can be used as scratch objects while unregistered. This is useful in situations where
|
|
|
|
// the object must be reallocated multiple times because its final size isn't known beforehand,
|
|
|
|
// e.g. in Array#filter, with only the final object making it into GC'ed userland.
|
|
|
|
var header = changetype<HEADER>(ref - HEADER_SIZE);
|
|
|
|
var payloadSize = header.payloadSize;
|
|
|
|
if (payloadSize < newPayloadSize) {
|
2019-03-17 00:11:16 +01:00
|
|
|
let newAdjustedSize = ADJUSTOBLOCK(newPayloadSize);
|
|
|
|
if (select(ADJUSTOBLOCK(payloadSize), 0, ref > HEAP_BASE) < newAdjustedSize) {
|
2019-03-15 09:26:31 +01:00
|
|
|
// move if the allocation isn't large enough or not a heap object
|
|
|
|
let newHeader = changetype<HEADER>(memory.allocate(newAdjustedSize));
|
|
|
|
newHeader.classId = header.classId;
|
|
|
|
if (GC_IMPLEMENTED) {
|
|
|
|
newHeader.gc1 = 0;
|
|
|
|
newHeader.gc2 = 0;
|
|
|
|
}
|
|
|
|
let newRef = changetype<usize>(newHeader) + HEADER_SIZE;
|
|
|
|
memory.copy(newRef, ref, payloadSize);
|
|
|
|
memory.fill(newRef + payloadSize, 0, newPayloadSize - payloadSize);
|
|
|
|
if (header.classId == HEADER_MAGIC) {
|
|
|
|
// free right away if not registered yet
|
|
|
|
assert(ref > HEAP_BASE); // static objects aren't scratch objects
|
|
|
|
memory.free(changetype<usize>(header));
|
|
|
|
} else if (GC_IMPLEMENTED) {
|
|
|
|
// if previously registered, register again
|
|
|
|
// @ts-ignore: stub
|
|
|
|
__gc_register(ref);
|
2019-03-09 02:37:05 +01:00
|
|
|
}
|
2019-03-15 09:26:31 +01:00
|
|
|
header = newHeader;
|
|
|
|
ref = newRef;
|
2019-03-08 22:04:20 +01:00
|
|
|
} else {
|
2019-03-15 09:26:31 +01:00
|
|
|
// otherwise just clear additional memory within this block
|
|
|
|
memory.fill(ref + payloadSize, 0, newPayloadSize - payloadSize);
|
2019-03-08 22:04:20 +01:00
|
|
|
}
|
2019-03-15 09:26:31 +01:00
|
|
|
} else {
|
|
|
|
// if the size is the same or less, just update the header accordingly.
|
|
|
|
// unused space is cleared when grown, so no need to do this here.
|
2019-03-08 22:04:20 +01:00
|
|
|
}
|
2019-03-15 09:26:31 +01:00
|
|
|
header.payloadSize = newPayloadSize;
|
|
|
|
return ref;
|
|
|
|
}
|
2019-03-08 22:04:20 +01:00
|
|
|
|
2019-03-15 09:26:31 +01:00
|
|
|
/** Registers a managed object to be tracked by the garbage collector, if present. */
|
|
|
|
// @ts-ignore: decorator
|
|
|
|
@unsafe @inline
|
|
|
|
export function REGISTER<T>(ref: usize): T {
|
2019-03-17 08:46:26 +01:00
|
|
|
if (!isReference<T>()) ERROR("reference expected");
|
2019-03-16 07:26:33 +01:00
|
|
|
return changetype<T>(doRegister(ref, CLASSID<T>()));
|
|
|
|
}
|
|
|
|
|
|
|
|
function doRegister(ref: usize, classId: u32): usize {
|
|
|
|
if (!ASC_NO_ASSERT) assertUnregistered(ref);
|
|
|
|
changetype<HEADER>(ref - HEADER_SIZE).classId = classId;
|
2019-03-15 09:26:31 +01:00
|
|
|
// @ts-ignore: stub
|
|
|
|
if (GC_IMPLEMENTED) __gc_register(ref);
|
2019-03-16 07:26:33 +01:00
|
|
|
return ref;
|
2019-03-15 09:26:31 +01:00
|
|
|
}
|
2019-03-08 22:04:20 +01:00
|
|
|
|
2019-03-15 09:26:31 +01:00
|
|
|
/** Links a registered object with the (registered) object now referencing it. */
|
|
|
|
// @ts-ignore: decorator
|
|
|
|
@unsafe @inline
|
|
|
|
export function LINK<T,TParent>(ref: T, parentRef: TParent): void {
|
2019-03-17 08:46:26 +01:00
|
|
|
if (!isReference<T>()) ERROR("reference expected");
|
|
|
|
if (!isReference<TParent>()) ERROR("reference expected");
|
2019-03-16 07:26:33 +01:00
|
|
|
doLink(changetype<usize>(ref), changetype<usize>(parentRef));
|
|
|
|
}
|
|
|
|
|
|
|
|
function doLink(ref: usize, parentRef: usize): void {
|
|
|
|
if (!ASC_NO_ASSERT) {
|
|
|
|
assertRegistered(ref);
|
|
|
|
assertRegistered(parentRef);
|
|
|
|
}
|
2019-03-15 09:26:31 +01:00
|
|
|
// @ts-ignore: stub
|
|
|
|
if (GC_IMPLEMENTED) __gc_link(changetype<usize>(ref), changetype<usize>(parentRef));
|
2019-03-08 22:04:20 +01:00
|
|
|
}
|
|
|
|
|
2019-03-15 09:26:31 +01:00
|
|
|
/** Discards an unregistered object that turned out to be unnecessary. */
|
|
|
|
// @ts-ignore: decorator
|
2019-03-16 07:26:33 +01:00
|
|
|
@unsafe @inline
|
2019-03-15 09:26:31 +01:00
|
|
|
export function DISCARD(ref: usize): void {
|
2019-03-16 07:26:33 +01:00
|
|
|
doDiscard(ref);
|
|
|
|
}
|
|
|
|
|
|
|
|
function doDiscard(ref: usize): void {
|
|
|
|
if (!ASC_NO_ASSERT) assertUnregistered(ref);
|
2019-03-15 09:26:31 +01:00
|
|
|
memory.free(changetype<usize>(ref - HEADER_SIZE));
|
|
|
|
}
|
2019-03-14 04:33:58 +01:00
|
|
|
|
2019-03-15 13:13:48 +01:00
|
|
|
/** Wraps a static buffer within an array by copying its contents. */
|
|
|
|
// @ts-ignore: decorator
|
|
|
|
@unsafe @inline
|
|
|
|
export function WRAPARRAY<T>(buffer: ArrayBuffer): T[] {
|
2019-03-16 07:26:33 +01:00
|
|
|
return changetype<T[]>(doWrapArray(buffer, CLASSID<T[]>(), alignof<T>()));
|
|
|
|
}
|
|
|
|
|
|
|
|
function doWrapArray(buffer: ArrayBuffer, classId: u32, alignLog2: usize): usize {
|
|
|
|
var array = doRegister(doAllocate(offsetof<i32[]>()), classId);
|
2019-03-15 13:13:48 +01:00
|
|
|
var bufferSize = <usize>buffer.byteLength;
|
2019-03-16 07:26:33 +01:00
|
|
|
var newBuffer = doRegister(doAllocate(bufferSize), classId);
|
|
|
|
changetype<ArrayBufferView>(array).data = changetype<ArrayBuffer>(newBuffer); // links
|
2019-03-15 13:13:48 +01:00
|
|
|
changetype<ArrayBufferView>(array).dataStart = changetype<usize>(newBuffer);
|
2019-03-17 08:46:26 +01:00
|
|
|
changetype<ArrayBufferView>(array).dataLength = bufferSize;
|
2019-03-16 07:26:33 +01:00
|
|
|
store<i32>(changetype<usize>(array), <i32>(bufferSize >>> alignLog2), offsetof<i32[]>("length_"));
|
|
|
|
memory.copy(changetype<usize>(newBuffer), changetype<usize>(buffer), bufferSize);
|
|
|
|
return changetype<usize>(array);
|
2019-03-15 13:13:48 +01:00
|
|
|
}
|
|
|
|
|
2019-03-15 09:26:31 +01:00
|
|
|
// Helpers
|
2019-03-14 06:09:49 +01:00
|
|
|
|
2019-03-15 09:26:31 +01:00
|
|
|
/** Asserts that a managed object is still unregistered. */
|
2019-03-16 07:26:33 +01:00
|
|
|
// @ts-ignore: decorator
|
|
|
|
function assertUnregistered(ref: usize): void {
|
2019-03-15 09:26:31 +01:00
|
|
|
assert(ref > HEAP_BASE); // must be a heap object
|
|
|
|
assert(changetype<HEADER>(ref - HEADER_SIZE).classId == HEADER_MAGIC);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Asserts that a managed object has already been registered. */
|
2019-03-16 07:26:33 +01:00
|
|
|
// @ts-ignore: decorator
|
|
|
|
function assertRegistered(ref: usize): void {
|
2019-03-16 14:48:22 +01:00
|
|
|
// may be a static string or buffer (not a heap object)
|
2019-03-15 09:26:31 +01:00
|
|
|
assert(changetype<HEADER>(ref - HEADER_SIZE).classId != HEADER_MAGIC);
|
|
|
|
}
|
2019-03-12 02:14:30 +01:00
|
|
|
|
2019-03-15 09:26:31 +01:00
|
|
|
import { ArrayBuffer } from "./arraybuffer";
|
|
|
|
|
|
|
|
/** Maximum byte length of any buffer. */
|
|
|
|
// @ts-ignore: decorator
|
|
|
|
@lazy
|
|
|
|
export const MAX_BYTELENGTH: i32 = MAX_SIZE_32 - HEADER_SIZE;
|
|
|
|
|
|
|
|
/** Hard wired ArrayBufferView interface. */
|
|
|
|
export abstract class ArrayBufferView {
|
2019-03-11 07:45:47 +01:00
|
|
|
[key: number]: number;
|
2019-03-11 23:34:20 +01:00
|
|
|
|
2019-03-14 05:11:03 +01:00
|
|
|
// @ts-ignore: decorator
|
2019-03-14 06:09:49 +01:00
|
|
|
@unsafe
|
|
|
|
data: ArrayBuffer;
|
|
|
|
|
2019-03-14 05:11:03 +01:00
|
|
|
// @ts-ignore: decorator
|
2019-03-14 06:09:49 +01:00
|
|
|
@unsafe
|
|
|
|
dataStart: usize;
|
|
|
|
|
2019-03-14 05:11:03 +01:00
|
|
|
// @ts-ignore: decorator
|
2019-03-14 06:09:49 +01:00
|
|
|
@unsafe
|
2019-03-17 08:46:26 +01:00
|
|
|
dataLength: u32;
|
2019-03-11 07:45:47 +01:00
|
|
|
|
2019-03-16 11:24:13 +01:00
|
|
|
protected constructor(length: i32, alignLog2: i32) {
|
2019-03-15 09:26:31 +01:00
|
|
|
if (<u32>length > <u32>MAX_BYTELENGTH >>> alignLog2) throw new RangeError("Invalid length");
|
2019-03-16 11:24:13 +01:00
|
|
|
var buffer = new ArrayBuffer(length = length << alignLog2);
|
2019-03-12 02:14:30 +01:00
|
|
|
this.data = buffer;
|
2019-03-11 07:45:47 +01:00
|
|
|
this.dataStart = changetype<usize>(buffer);
|
2019-03-17 08:46:26 +01:00
|
|
|
this.dataLength = length;
|
2019-03-11 07:45:47 +01:00
|
|
|
}
|
|
|
|
|
2019-03-12 02:14:30 +01:00
|
|
|
get byteOffset(): i32 {
|
|
|
|
return <i32>(this.dataStart - changetype<usize>(this.data));
|
2019-03-11 07:45:47 +01:00
|
|
|
}
|
|
|
|
|
2019-03-12 02:14:30 +01:00
|
|
|
get byteLength(): i32 {
|
2019-03-17 08:46:26 +01:00
|
|
|
return this.dataLength;
|
2019-03-11 07:45:47 +01:00
|
|
|
}
|
2019-03-13 03:09:24 +01:00
|
|
|
|
|
|
|
get length(): i32 {
|
2019-03-15 09:26:31 +01:00
|
|
|
ERROR("missing implementation: subclasses must implement ArrayBufferView#length");
|
2019-03-13 03:09:24 +01:00
|
|
|
return unreachable();
|
|
|
|
}
|
2019-03-08 22:04:20 +01:00
|
|
|
}
|