mirror of
https://github.com/fluencelabs/assemblyscript
synced 2025-05-01 01:42:14 +00:00
dummy gc
This commit is contained in:
parent
d4d5814fc2
commit
d9463c5484
@ -1,4 +1,4 @@
|
|||||||
import { ALLOCATE, REALLOCATE, DISCARD, RETAIN, RELEASE, REGISTER, MAX_BYTELENGTH, ArrayBufferView } from "./runtime";
|
import { ALLOCATE, REALLOCATE, DISCARD, RETAIN, RELEASE, REGISTER, MAX_BYTELENGTH, ArrayBufferView, MOVE } from "./runtime";
|
||||||
import { ArrayBuffer } from "./arraybuffer";
|
import { ArrayBuffer } from "./arraybuffer";
|
||||||
import { COMPARATOR, SORT } from "./util/sort";
|
import { COMPARATOR, SORT } from "./util/sort";
|
||||||
import { itoa, dtoa, itoa_stream, dtoa_stream, MAX_DOUBLE_LENGTH } from "./util/number";
|
import { itoa, dtoa, itoa_stream, dtoa_stream, MAX_DOUBLE_LENGTH } from "./util/number";
|
||||||
@ -76,8 +76,10 @@ export class Array<T> extends ArrayBufferView {
|
|||||||
if (isManaged<T>()) {
|
if (isManaged<T>()) {
|
||||||
let offset = this.dataStart + (<usize>index << alignof<T>());
|
let offset = this.dataStart + (<usize>index << alignof<T>());
|
||||||
let oldValue = load<T>(offset);
|
let oldValue = load<T>(offset);
|
||||||
store<T>(offset, RETAIN<T,this>(value, this));
|
if (value !== oldValue) {
|
||||||
RELEASE<T,this>(oldValue, this); // order is important
|
RELEASE<T,this>(oldValue, this);
|
||||||
|
store<T>(offset, RETAIN<T,this>(value, this));
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
store<T>(this.dataStart + (<usize>index << alignof<T>()), value);
|
store<T>(this.dataStart + (<usize>index << alignof<T>()), value);
|
||||||
}
|
}
|
||||||
@ -155,8 +157,7 @@ export class Array<T> extends ArrayBufferView {
|
|||||||
if (isManaged<T>()) {
|
if (isManaged<T>()) {
|
||||||
let thisStart = this.dataStart;
|
let thisStart = this.dataStart;
|
||||||
for (let offset: usize = 0; offset < thisSize; offset += sizeof<T>()) {
|
for (let offset: usize = 0; offset < thisSize; offset += sizeof<T>()) {
|
||||||
let element = load<T>(thisStart + offset);
|
store<T>(outStart + offset, RETAIN<T,Array<T>>(load<T>(thisStart + offset), out));
|
||||||
store<T>(outStart + offset, RETAIN<T,Array<T>>(element, out));
|
|
||||||
}
|
}
|
||||||
let otherStart = other.dataStart;
|
let otherStart = other.dataStart;
|
||||||
let otherSize = <usize>otherLen << alignof<T>();
|
let otherSize = <usize>otherLen << alignof<T>();
|
||||||
@ -332,13 +333,11 @@ export class Array<T> extends ArrayBufferView {
|
|||||||
var thisStart = this.dataStart;
|
var thisStart = this.dataStart;
|
||||||
var thisBase = thisStart + (<usize>start << alignof<T>());
|
var thisBase = thisStart + (<usize>start << alignof<T>());
|
||||||
for (let i = 0; i < deleteCount; ++i) {
|
for (let i = 0; i < deleteCount; ++i) {
|
||||||
let deleted = load<T>(thisBase + (<usize>i << alignof<T>()));
|
store<T>(resultStart + (<usize>i << alignof<T>()),
|
||||||
if (isManaged<T>()) {
|
isManaged<T>()
|
||||||
store<T>(resultStart + (<usize>i << alignof<T>()), RETAIN<T,Array<T>>(deleted, result));
|
? MOVE<T,this,Array<T>>(load<T>(thisBase + (<usize>i << alignof<T>())), this, result)
|
||||||
RELEASE<T,this>(deleted, this); // order is important
|
: load<T>(thisBase + (<usize>i << alignof<T>()))
|
||||||
} else {
|
);
|
||||||
store<T>(resultStart + (<usize>i << alignof<T>()), deleted);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
memory.copy(
|
memory.copy(
|
||||||
result.dataStart,
|
result.dataStart,
|
||||||
|
35
std/assembly/collector/dummy.ts
Normal file
35
std/assembly/collector/dummy.ts
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
// A dummy GC for looking at generated GC code without actually implementing it.
|
||||||
|
|
||||||
|
// @ts-ignore: decorator
|
||||||
|
@inline
|
||||||
|
const TRACE = false;
|
||||||
|
|
||||||
|
// @ts-ignore: decorator
|
||||||
|
@global @unsafe
|
||||||
|
function __gc_register(ref: usize): void {
|
||||||
|
if (TRACE) trace("gc.register", 1, ref);
|
||||||
|
}
|
||||||
|
|
||||||
|
// @ts-ignore: decorator
|
||||||
|
@global @unsafe
|
||||||
|
function __gc_retain(ref: usize, parentRef: usize): void {
|
||||||
|
if (TRACE) trace("gc.retain", 2, ref, parentRef);
|
||||||
|
}
|
||||||
|
|
||||||
|
// @ts-ignore: decorator
|
||||||
|
@global @unsafe
|
||||||
|
function __gc_release(ref: usize, parentRef: usize): void {
|
||||||
|
if (TRACE) trace("gc.release", 2, ref, parentRef);
|
||||||
|
}
|
||||||
|
|
||||||
|
// @ts-ignore: decorator
|
||||||
|
@global @unsafe
|
||||||
|
function __gc_move(ref: usize, oldParentRef: usize, newParentRef: usize): void {
|
||||||
|
if (TRACE) trace("gc.move", 3, ref, oldParentRef, newParentRef);
|
||||||
|
}
|
||||||
|
|
||||||
|
// @ts-ignore: decorator
|
||||||
|
@global @unsafe
|
||||||
|
function __gc_collect(): void {
|
||||||
|
if (TRACE) trace("gc.collect");
|
||||||
|
}
|
@ -214,7 +214,7 @@ function objToRef(obj: ManagedObject): usize {
|
|||||||
// @ts-ignore: decorator
|
// @ts-ignore: decorator
|
||||||
@global @unsafe
|
@global @unsafe
|
||||||
export function __gc_register(ref: usize): void {
|
export function __gc_register(ref: usize): void {
|
||||||
if (TRACE) trace("gc.register", 2, ref);
|
if (TRACE) trace("gc.register", 1, ref);
|
||||||
step(); // also makes sure it's initialized
|
step(); // also makes sure it's initialized
|
||||||
var obj = refToObj(ref);
|
var obj = refToObj(ref);
|
||||||
obj.color = white;
|
obj.color = white;
|
||||||
|
@ -38,8 +38,10 @@ export class FixedArray<T> {
|
|||||||
if (isManaged<T>()) {
|
if (isManaged<T>()) {
|
||||||
let offset = changetype<usize>(this) + (<usize>index << alignof<T>());
|
let offset = changetype<usize>(this) + (<usize>index << alignof<T>());
|
||||||
let oldValue = load<T>(offset);
|
let oldValue = load<T>(offset);
|
||||||
store<T>(offset, RETAIN<T,this>(value, this));
|
if (value !== oldValue) {
|
||||||
RELEASE<T,this>(oldValue, this); // order is important
|
RELEASE<T,this>(oldValue, this);
|
||||||
|
store<T>(offset, RETAIN<T,this>(value, this));
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
store<T>(changetype<usize>(this) + (<usize>index << alignof<T>()), value);
|
store<T>(changetype<usize>(this) + (<usize>index << alignof<T>()), value);
|
||||||
}
|
}
|
||||||
|
@ -106,8 +106,10 @@ export class Map<K,V> {
|
|||||||
if (entry) {
|
if (entry) {
|
||||||
if (isManaged<V>()) {
|
if (isManaged<V>()) {
|
||||||
let oldValue = entry.value;
|
let oldValue = entry.value;
|
||||||
entry.value = RETAIN<V,this>(value, this);
|
if (value !== oldValue) {
|
||||||
RELEASE<V,this>(oldValue, this); // order is important
|
RELEASE<V,this>(oldValue, this);
|
||||||
|
entry.value = RETAIN<V,this>(value, this);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
entry.value = value;
|
entry.value = value;
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
// The runtime provides a set of macros for dealing with common AssemblyScript internals, like
|
// The runtime provides a set of macros for dealing with common AssemblyScript internals, like
|
||||||
// allocation, memory management in general, integration with a (potenial) garbage collector
|
// allocation, memory management in general, integration with a (potential) garbage collector
|
||||||
// and interfaces to hard-wired data types like buffers and their views. Doing so ensures that
|
// and interfaces to hard-wired data types like buffers and their views. Doing so ensures that
|
||||||
// no matter which underlying implementation of a memory allocator or garbage collector is used,
|
// no matter which underlying implementation of a memory allocator or garbage collector is used,
|
||||||
// as long as all runtime/managed objects adhere to the runtime conventions, it'll all play well
|
// as long as all runtime/managed objects adhere to the runtime conventions, it'll all play well
|
||||||
@ -19,7 +19,7 @@ import { HEAP_BASE, memory } from "./memory";
|
|||||||
// Changes the size of a previously allocated, but not yet registered, runtime object, for
|
// Changes the size of a previously allocated, but not yet registered, runtime object, for
|
||||||
// example when a pre-allocated buffer turned out to be too small or too large. This works by
|
// example when a pre-allocated buffer turned out to be too small or too large. This works by
|
||||||
// aligning dynamic allocations to actual block size internally so in the best case REALLOCATE
|
// aligning dynamic allocations to actual block size internally so in the best case REALLOCATE
|
||||||
// only changes a size while in the worst case moves the object to larger block.
|
// only updates payload size while in the worst case moves the object to larger a block.
|
||||||
//
|
//
|
||||||
// DISCARD(ref)
|
// DISCARD(ref)
|
||||||
// ------------
|
// ------------
|
||||||
@ -45,6 +45,14 @@ import { HEAP_BASE, memory } from "./memory";
|
|||||||
// ignore this by design, while a reference counting collector decrements the reference count
|
// ignore this by design, while a reference counting collector decrements the reference count
|
||||||
// and potentially frees the runtime object.
|
// and potentially frees the runtime object.
|
||||||
//
|
//
|
||||||
|
// MOVE<T,TOldParent,TNewParent>(ref, oldParentRef, newParentRef)
|
||||||
|
// --------------------------------------------------------------
|
||||||
|
// Moves a reference to ref hold by oldParentRef to be now hold by newParentRef. This is a
|
||||||
|
// special case of first RELEASE'ing a reference on one and instantly RETAIN'ing the reference
|
||||||
|
// on another parent. A tracing garbage collector will most likely link the runtime object as if
|
||||||
|
// RETAIN'ed on the new parent only, while a reference counting collector can skip increment and
|
||||||
|
// decrement, as decrementing might otherwise involve a costly check for cyclic garbage.
|
||||||
|
//
|
||||||
// ALLOCATE_UNMANAGED(size)
|
// ALLOCATE_UNMANAGED(size)
|
||||||
// ------------------------
|
// ------------------------
|
||||||
// Allocates an unmanaged struct-like object. This is used by the compiler as an abstraction
|
// Allocates an unmanaged struct-like object. This is used by the compiler as an abstraction
|
||||||
@ -209,7 +217,11 @@ function doRegister(ref: usize, classId: u32): usize {
|
|||||||
export function RETAIN<T,TParent>(ref: T, parentRef: TParent): T {
|
export function RETAIN<T,TParent>(ref: T, parentRef: TParent): T {
|
||||||
if (!isManaged<T>()) ERROR("managed reference expected");
|
if (!isManaged<T>()) ERROR("managed reference expected");
|
||||||
if (!isManaged<TParent>()) ERROR("managed reference expected");
|
if (!isManaged<TParent>()) ERROR("managed reference expected");
|
||||||
doRetain(changetype<usize>(ref), changetype<usize>(parentRef));
|
if (isNullable<T>()) {
|
||||||
|
if (ref !== null) doRetain(changetype<usize>(ref), changetype<usize>(parentRef));
|
||||||
|
} else {
|
||||||
|
doRetain(changetype<usize>(ref), changetype<usize>(parentRef));
|
||||||
|
}
|
||||||
return ref;
|
return ref;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -228,7 +240,13 @@ function doRetain(ref: usize, parentRef: usize): void {
|
|||||||
export function RELEASE<T,TParent>(ref: T, parentRef: TParent): void {
|
export function RELEASE<T,TParent>(ref: T, parentRef: TParent): void {
|
||||||
if (!isManaged<T>()) ERROR("managed reference expected");
|
if (!isManaged<T>()) ERROR("managed reference expected");
|
||||||
if (!isManaged<TParent>()) ERROR("managed reference expected");
|
if (!isManaged<TParent>()) ERROR("managed reference expected");
|
||||||
doRelease(changetype<usize>(ref), changetype<usize>(parentRef));
|
// FIXME: new Array<Ref>(10) has non-nullable elements but still contains `null`s.
|
||||||
|
// In the future, something like this should probably initialize with `new Ref()`s.
|
||||||
|
// if (isNullable<T>()) {
|
||||||
|
if (ref !== null) doRelease(changetype<usize>(ref), changetype<usize>(parentRef));
|
||||||
|
// } else {
|
||||||
|
// doRelease(changetype<usize>(ref), changetype<usize>(parentRef));
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
function doRelease(ref: usize, parentRef: usize): void {
|
function doRelease(ref: usize, parentRef: usize): void {
|
||||||
@ -240,6 +258,41 @@ function doRelease(ref: usize, parentRef: usize): void {
|
|||||||
if (GC_IMPLEMENTED) __gc_release(changetype<usize>(ref), changetype<usize>(parentRef));
|
if (GC_IMPLEMENTED) __gc_release(changetype<usize>(ref), changetype<usize>(parentRef));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Moves a registered object from one parent to another. */
|
||||||
|
// @ts-ignore: decorator
|
||||||
|
@unsafe @inline
|
||||||
|
export function MOVE<T,TOldParent,TNewParent>(ref: T, oldParentRef: TOldParent, newParentRef: TNewParent): T {
|
||||||
|
if (!isManaged<T>()) ERROR("managed reference expected");
|
||||||
|
if (!isManaged<TOldParent>()) ERROR("managed reference expected");
|
||||||
|
if (!isManaged<TNewParent>()) ERROR("managed reference expected");
|
||||||
|
if (isNullable<T>()) {
|
||||||
|
if (ref !== null) doMove(changetype<usize>(ref), changetype<usize>(oldParentRef), changetype<usize>(newParentRef));
|
||||||
|
} else {
|
||||||
|
doMove(changetype<usize>(ref), changetype<usize>(oldParentRef), changetype<usize>(newParentRef));
|
||||||
|
}
|
||||||
|
return ref;
|
||||||
|
}
|
||||||
|
|
||||||
|
function doMove(ref: usize, oldParentRef: usize, newParentRef: usize): void {
|
||||||
|
if (!ASC_NO_ASSERT) {
|
||||||
|
assertRegistered(ref);
|
||||||
|
assertRegistered(oldParentRef);
|
||||||
|
assertRegistered(newParentRef);
|
||||||
|
}
|
||||||
|
if (GC_IMPLEMENTED) {
|
||||||
|
// @ts-ignore: stub
|
||||||
|
if (isDefined(__gc_move)) {
|
||||||
|
// @ts-ignore: stub
|
||||||
|
__gc_move(changetype<usize>(ref), changetype<usize>(oldParentRef), changetype<usize>(newParentRef));
|
||||||
|
} else {
|
||||||
|
// @ts-ignore: stub
|
||||||
|
__gc_retain(changetype<usize>(ref), changetype<usize>(newParentRef));
|
||||||
|
// @ts-ignore: stub
|
||||||
|
__gc_release(changetype<usize>(ref), changetype<usize>(oldParentRef));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/** Discards an unregistered object that turned out to be unnecessary. */
|
/** Discards an unregistered object that turned out to be unnecessary. */
|
||||||
// @ts-ignore: decorator
|
// @ts-ignore: decorator
|
||||||
@unsafe @inline
|
@unsafe @inline
|
||||||
@ -283,7 +336,7 @@ function assertUnregistered(ref: usize): void {
|
|||||||
/** Asserts that a managed object has already been registered. */
|
/** Asserts that a managed object has already been registered. */
|
||||||
// @ts-ignore: decorator
|
// @ts-ignore: decorator
|
||||||
function assertRegistered(ref: usize): void {
|
function assertRegistered(ref: usize): void {
|
||||||
// may be a static string or buffer (not a heap object)
|
assert(ref !== null); // may be a static string or buffer (not a heap object)
|
||||||
assert(changetype<HEADER>(ref - HEADER_SIZE).classId != HEADER_MAGIC);
|
assert(changetype<HEADER>(ref - HEADER_SIZE).classId != HEADER_MAGIC);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -357,16 +357,16 @@ import { compareImpl, parse, CharCode, isWhiteSpaceOrLineTerminator } from "./ut
|
|||||||
let result = new Array<String>(length);
|
let result = new Array<String>(length);
|
||||||
let resultStart = changetype<ArrayBufferView>(result).dataStart;
|
let resultStart = changetype<ArrayBufferView>(result).dataStart;
|
||||||
for (let i: isize = 0; i < length; ++i) {
|
for (let i: isize = 0; i < length; ++i) {
|
||||||
let charStr = ALLOCATE(2);
|
let charStr = REGISTER<String>(ALLOCATE(2));
|
||||||
store<u16>(
|
store<u16>(
|
||||||
charStr,
|
changetype<usize>(charStr),
|
||||||
load<u16>(changetype<usize>(this) + (<usize>i << 1))
|
load<u16>(changetype<usize>(this) + (<usize>i << 1))
|
||||||
);
|
);
|
||||||
// result[i] = charStr
|
// result[i] = charStr
|
||||||
store<String>(resultStart + (<usize>i << alignof<usize>()),
|
store<String>(resultStart + (<usize>i << alignof<usize>()),
|
||||||
isManaged<String>()
|
isManaged<String>()
|
||||||
? RETAIN<String,Array<String>>(REGISTER<String>(charStr), result)
|
? RETAIN<String,Array<String>>(charStr, result)
|
||||||
: REGISTER<String>(charStr)
|
: charStr
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
|
@ -231,9 +231,8 @@ tests.forEach(filename => {
|
|||||||
let memory = new WebAssembly.Memory({ initial: 10 });
|
let memory = new WebAssembly.Memory({ initial: 10 });
|
||||||
let exports = {};
|
let exports = {};
|
||||||
|
|
||||||
const RUNTIME_HEADER_SIZE = 8; // 16 if GC is present
|
|
||||||
|
|
||||||
function getString(ptr) {
|
function getString(ptr) {
|
||||||
|
const RUNTIME_HEADER_SIZE = exports[".capabilities"] & 2 ? 16 : 8;
|
||||||
if (!ptr) return "null";
|
if (!ptr) return "null";
|
||||||
var U32 = new Uint32Array(exports.memory ? exports.memory.buffer : memory.buffer);
|
var U32 = new Uint32Array(exports.memory ? exports.memory.buffer : memory.buffer);
|
||||||
var U16 = new Uint16Array(exports.memory ? exports.memory.buffer : memory.buffer);
|
var U16 = new Uint16Array(exports.memory ? exports.memory.buffer : memory.buffer);
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -1,7 +1,10 @@
|
|||||||
import "allocator/arena";
|
import "allocator/arena";
|
||||||
|
import "collector/dummy";
|
||||||
import { Array } from "array";
|
import { Array } from "array";
|
||||||
import { COMPARATOR } from "util/sort";
|
import { COMPARATOR } from "util/sort";
|
||||||
|
|
||||||
|
@start export function main(): void {}
|
||||||
|
|
||||||
// Obtains the internal capacity of an array from its backing buffer.
|
// Obtains the internal capacity of an array from its backing buffer.
|
||||||
function internalCapacity<T>(array: Array<T>): i32 {
|
function internalCapacity<T>(array: Array<T>): i32 {
|
||||||
// the memory region used by the backing buffer might still be larger in that the ArrayBuffer
|
// the memory region used by the backing buffer might still be larger in that the ArrayBuffer
|
||||||
|
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user