mirror of
https://github.com/fluencelabs/assemblyscript
synced 2025-06-24 20:21:48 +00:00
take a step back
This commit is contained in:
@ -1,41 +1,30 @@
|
||||
/**
|
||||
* Arena Memory Allocator
|
||||
*
|
||||
* Provides a `memory.reset` function to reset the heap to its initial state. A user has to make
|
||||
* sure that there are no more references to cleared memory afterwards. Always aligns to 8 bytes.
|
||||
*
|
||||
* @module std/assembly/allocator/arena
|
||||
*//***/
|
||||
|
||||
import { HEAP_BASE, memory } from "../memory";
|
||||
import { AL_MASK, MAX_SIZE_32 } from "../util/allocator";
|
||||
|
||||
var startOffset: usize = (HEAP_BASE + AL_MASK) & ~AL_MASK;
|
||||
var offset: usize = startOffset;
|
||||
@lazy var startOffset: usize = (HEAP_BASE + AL_MASK) & ~AL_MASK;
|
||||
@lazy var offset: usize = startOffset;
|
||||
|
||||
// Memory allocator implementation
|
||||
@global namespace memory {
|
||||
|
||||
export function allocate(size: usize): usize {
|
||||
if (size > MAX_SIZE_32) unreachable();
|
||||
var ptr = offset;
|
||||
var newPtr = (ptr + max<usize>(size, 1) + AL_MASK) & ~AL_MASK;
|
||||
var pagesBefore = memory.size();
|
||||
if (newPtr > <usize>pagesBefore << 16) {
|
||||
let pagesNeeded = ((newPtr - ptr + 0xffff) & ~0xffff) >>> 16;
|
||||
let pagesWanted = max(pagesBefore, pagesNeeded); // double memory
|
||||
if (memory.grow(pagesWanted) < 0) {
|
||||
if (memory.grow(pagesNeeded) < 0) {
|
||||
unreachable(); // out of memory
|
||||
}
|
||||
@unsafe @global function __memory_allocate(size: usize): usize {
|
||||
if (size > MAX_SIZE_32) unreachable();
|
||||
var ptr = offset;
|
||||
var newPtr = (ptr + max<usize>(size, 1) + AL_MASK) & ~AL_MASK;
|
||||
var pagesBefore = memory.size();
|
||||
if (newPtr > <usize>pagesBefore << 16) {
|
||||
let pagesNeeded = ((newPtr - ptr + 0xffff) & ~0xffff) >>> 16;
|
||||
let pagesWanted = max(pagesBefore, pagesNeeded); // double memory
|
||||
if (memory.grow(pagesWanted) < 0) {
|
||||
if (memory.grow(pagesNeeded) < 0) {
|
||||
unreachable(); // out of memory
|
||||
}
|
||||
}
|
||||
offset = newPtr;
|
||||
return ptr;
|
||||
}
|
||||
|
||||
export function free(ptr: usize): void { /* nop */ }
|
||||
|
||||
export function reset(): void {
|
||||
offset = startOffset;
|
||||
}
|
||||
offset = newPtr;
|
||||
return ptr;
|
||||
}
|
||||
|
||||
@unsafe @global function __memory_free(ptr: usize): void {
|
||||
}
|
||||
|
||||
@unsafe @global function __memory_reset(): void {
|
||||
offset = startOffset;
|
||||
}
|
||||
|
@ -1,542 +0,0 @@
|
||||
/**
|
||||
* Buddy Memory Allocator.
|
||||
* @module std/assembly/allocator/buddy
|
||||
*//***/
|
||||
|
||||
/*
|
||||
Copyright 2018 Evan Wallace
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
*/// see: https://github.com/evanw/buddy-malloc
|
||||
|
||||
/*
|
||||
* This file implements a buddy memory allocator, which is an allocator that
|
||||
* allocates memory within a fixed linear address range. It spans the address
|
||||
* range with a binary tree that tracks free space. Both "malloc" and "free"
|
||||
* are O(log N) time where N is the maximum possible number of allocations.
|
||||
*
|
||||
* The "buddy" term comes from how the tree is used. When memory is allocated,
|
||||
* nodes in the tree are split recursively until a node of the appropriate size
|
||||
* is reached. Every split results in two child nodes, each of which is the
|
||||
* buddy of the other. When a node is freed, the node and its buddy can be
|
||||
* merged again if the buddy is also free. This makes the memory available
|
||||
* for larger allocations again.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Every allocation needs an 8-byte header to store the allocation size while
|
||||
* staying 8-byte aligned. The address returned by "malloc" is the address
|
||||
* right after this header (i.e. the size occupies the 8 bytes before the
|
||||
* returned address).
|
||||
*/
|
||||
const HEADER_SIZE: usize = 8;
|
||||
|
||||
/*
|
||||
* The minimum allocation size is 16 bytes because we have an 8-byte header and
|
||||
* we need to stay 8-byte aligned.
|
||||
*/
|
||||
const MIN_ALLOC_LOG2: usize = 4;
|
||||
const MIN_ALLOC: usize = 1 << MIN_ALLOC_LOG2;
|
||||
|
||||
/*
|
||||
* The maximum allocation size is currently set to 2gb. This is the total size
|
||||
* of the heap. It's technically also the maximum allocation size because the
|
||||
* heap could consist of a single allocation of this size. But of course real
|
||||
* heaps will have multiple allocations, so the real maximum allocation limit
|
||||
* is at most 1gb.
|
||||
*/
|
||||
const MAX_ALLOC_LOG2: usize = 30; // 31;
|
||||
const MAX_ALLOC: usize = 1 << MAX_ALLOC_LOG2;
|
||||
|
||||
/*
|
||||
* Allocations are done in powers of two starting from MIN_ALLOC and ending at
|
||||
* MAX_ALLOC inclusive. Each allocation size has a bucket that stores the free
|
||||
* list for that allocation size.
|
||||
*
|
||||
* Given a bucket index, the size of the allocations in that bucket can be
|
||||
* found with "(size_t)1 << (MAX_ALLOC_LOG2 - bucket)".
|
||||
*/
|
||||
const BUCKET_COUNT: usize = MAX_ALLOC_LOG2 - MIN_ALLOC_LOG2 + 1;
|
||||
|
||||
/*
|
||||
* Free lists are stored as circular doubly-linked lists. Every possible
|
||||
* allocation size has an associated free list that is threaded through all
|
||||
* currently free blocks of that size. That means MIN_ALLOC must be at least
|
||||
* "sizeof(list_t)". MIN_ALLOC is currently 16 bytes, so this will be true for
|
||||
* both 32-bit and 64-bit.
|
||||
*/
|
||||
@unmanaged
|
||||
class List {
|
||||
prev: List;
|
||||
next: List;
|
||||
static readonly SIZE: usize = 2 * sizeof<usize>();
|
||||
}
|
||||
|
||||
/*
|
||||
* Each bucket corresponds to a certain allocation size and stores a free list
|
||||
* for that size. The bucket at index 0 corresponds to an allocation size of
|
||||
* MAX_ALLOC (i.e. the whole address space).
|
||||
*/
|
||||
var BUCKETS_START: usize = HEAP_BASE;
|
||||
var BUCKETS_END: usize = BUCKETS_START + BUCKET_COUNT * List.SIZE;
|
||||
|
||||
function buckets$get(index: usize): List {
|
||||
assert(index < BUCKET_COUNT);
|
||||
return changetype<List>(BUCKETS_START + index * List.SIZE);
|
||||
}
|
||||
|
||||
/*
|
||||
* We could initialize the allocator by giving it one free block the size of
|
||||
* the entire address space. However, this would cause us to instantly reserve
|
||||
* half of the entire address space on the first allocation, since the first
|
||||
* split would store a free list entry at the start of the right child of the
|
||||
* root. Instead, we have the tree start out small and grow the size of the
|
||||
* tree as we use more memory. The size of the tree is tracked by this value.
|
||||
*/
|
||||
var bucket_limit: usize;
|
||||
|
||||
/*
|
||||
* This array represents a linearized binary tree of bits. Every possible
|
||||
* allocation larger than MIN_ALLOC has a node in this tree (and therefore a
|
||||
* bit in this array).
|
||||
*
|
||||
* Given the index for a node, lineraized binary trees allow you to traverse to
|
||||
* the parent node or the child nodes just by doing simple arithmetic on the
|
||||
* index:
|
||||
*
|
||||
* - Move to parent: index = (index - 1) / 2;
|
||||
* - Move to left child: index = index * 2 + 1;
|
||||
* - Move to right child: index = index * 2 + 2;
|
||||
* - Move to sibling: index = ((index - 1) ^ 1) + 1;
|
||||
*
|
||||
* Each node in this tree can be in one of several states:
|
||||
*
|
||||
* - UNUSED (both children are UNUSED)
|
||||
* - SPLIT (one child is UNUSED and the other child isn't)
|
||||
* - USED (neither children are UNUSED)
|
||||
*
|
||||
* These states take two bits to store. However, it turns out we have enough
|
||||
* information to distinguish between UNUSED and USED from context, so we only
|
||||
* need to store SPLIT or not, which only takes a single bit.
|
||||
*
|
||||
* Note that we don't need to store any nodes for allocations of size MIN_ALLOC
|
||||
* since we only ever care about parent nodes.
|
||||
*/
|
||||
const SPLIT_COUNT: usize = (1 << (BUCKET_COUNT - 1)) / 8;
|
||||
var NODE_IS_SPLIT_START: usize = BUCKETS_END;
|
||||
var NODE_IS_SPLIT_END: usize = NODE_IS_SPLIT_START + SPLIT_COUNT * sizeof<u8>();
|
||||
|
||||
function node_is_split$get(index: usize): i32 {
|
||||
assert(index < SPLIT_COUNT);
|
||||
return load<u8>(NODE_IS_SPLIT_START + index);
|
||||
}
|
||||
|
||||
function node_is_split$set(index: usize, state: i32): void {
|
||||
assert(index < SPLIT_COUNT);
|
||||
store<u8>(NODE_IS_SPLIT_START + index, state);
|
||||
}
|
||||
|
||||
/*
|
||||
* This is the starting address of the address range for this allocator. Every
|
||||
* returned allocation will be an offset of this pointer from 0 to MAX_ALLOC.
|
||||
*/
|
||||
var base_ptr: usize;
|
||||
|
||||
/*
|
||||
* This is the maximum address that has ever been used by the allocator. It's
|
||||
* used to know when to call "brk" to request more memory from the kernel.
|
||||
*/
|
||||
var max_ptr: usize;
|
||||
|
||||
/*
|
||||
* Make sure all addresses before "new_value" are valid and can be used. Memory
|
||||
* is allocated in a 2gb address range but that memory is not reserved up
|
||||
* front. It's only reserved when it's needed by calling this function. This
|
||||
* will return false if the memory could not be reserved.
|
||||
*/
|
||||
function update_max_ptr(new_value: usize): i32 {
|
||||
if (new_value > max_ptr) {
|
||||
// if (brk(new_value)) {
|
||||
// return 0;
|
||||
// }
|
||||
let oldPages = <i32>memory.size();
|
||||
let newPages = <i32>(((new_value + 0xffff) & ~0xffff) >>> 16);
|
||||
assert(newPages > oldPages);
|
||||
if (memory.grow(newPages - oldPages) < 0) {
|
||||
return 0;
|
||||
}
|
||||
// max_ptr = new_value;
|
||||
max_ptr = <usize>newPages << 16;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Initialize a list to empty. Because these are circular lists, an "empty"
|
||||
* list is an entry where both links point to itself. This makes insertion
|
||||
* and removal simpler because they don't need any branches.
|
||||
*/
|
||||
function list_init(list: List): void {
|
||||
list.prev = list;
|
||||
list.next = list;
|
||||
}
|
||||
|
||||
/*
|
||||
* Append the provided entry to the end of the list. This assumes the entry
|
||||
* isn't in a list already because it overwrites the linked list pointers.
|
||||
*/
|
||||
function list_push(list: List, entry: List): void {
|
||||
var prev = list.prev;
|
||||
entry.prev = prev;
|
||||
entry.next = list;
|
||||
prev.next = entry;
|
||||
list.prev = entry;
|
||||
}
|
||||
|
||||
/*
|
||||
* Remove the provided entry from whichever list it's currently in. This
|
||||
* assumes that the entry is in a list. You don't need to provide the list
|
||||
* because the lists are circular, so the list's pointers will automatically
|
||||
* be updated if the first or last entries are removed.
|
||||
*/
|
||||
function list_remove(entry: List): void {
|
||||
var prev = entry.prev;
|
||||
var next = entry.next;
|
||||
prev.next = next;
|
||||
next.prev = prev;
|
||||
}
|
||||
|
||||
/*
|
||||
* Remove and return the first entry in the list or NULL if the list is empty.
|
||||
*/
|
||||
function list_pop(list: List): List | null {
|
||||
var back = list.prev;
|
||||
if (back == list) return null;
|
||||
list_remove(back);
|
||||
return back;
|
||||
}
|
||||
|
||||
/*
|
||||
* This maps from the index of a node to the address of memory that node
|
||||
* represents. The bucket can be derived from the index using a loop but is
|
||||
* required to be provided here since having them means we can avoid the loop
|
||||
* and have this function return in constant time.
|
||||
*/
|
||||
function ptr_for_node(index: usize, bucket: usize): usize {
|
||||
return base_ptr + ((index - (1 << bucket) + 1) << (MAX_ALLOC_LOG2 - bucket));
|
||||
}
|
||||
|
||||
/*
|
||||
* This maps from an address of memory to the node that represents that
|
||||
* address. There are often many nodes that all map to the same address, so
|
||||
* the bucket is needed to uniquely identify a node.
|
||||
*/
|
||||
function node_for_ptr(ptr: usize, bucket: usize): usize {
|
||||
return ((ptr - base_ptr) >> (MAX_ALLOC_LOG2 - bucket)) + (1 << bucket) - 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Given the index of a node, this returns the "is split" flag of the parent.
|
||||
*/
|
||||
function parent_is_split(index: usize): bool {
|
||||
index = (index - 1) / 2;
|
||||
return ((node_is_split$get(index / 8) >>> <i32>(index % 8)) & 1) == 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Given the index of a node, this flips the "is split" flag of the parent.
|
||||
*/
|
||||
function flip_parent_is_split(index: usize): void {
|
||||
index = (index - 1) / 2;
|
||||
var indexDiv8 = index / 8;
|
||||
node_is_split$set(indexDiv8,
|
||||
node_is_split$get(indexDiv8) ^ <i32>(1 << (index % 8))
|
||||
);
|
||||
}
|
||||
|
||||
/*
|
||||
* Given the requested size passed to "malloc", this function returns the index
|
||||
* of the smallest bucket that can fit that size.
|
||||
*/
|
||||
function bucket_for_request(request: usize): usize {
|
||||
var bucket = BUCKET_COUNT - 1;
|
||||
var size = MIN_ALLOC;
|
||||
|
||||
while (size < request) {
|
||||
bucket--;
|
||||
size *= 2;
|
||||
}
|
||||
|
||||
return bucket;
|
||||
}
|
||||
|
||||
/*
|
||||
* The tree is always rooted at the current bucket limit. This call grows the
|
||||
* tree by repeatedly doubling it in size until the root lies at the provided
|
||||
* bucket index. Each doubling lowers the bucket limit by 1.
|
||||
*/
|
||||
function lower_bucket_limit(bucket: usize): u32 {
|
||||
while (bucket < bucket_limit) {
|
||||
let root = node_for_ptr(base_ptr, bucket_limit);
|
||||
let right_child: usize;
|
||||
|
||||
/*
|
||||
* If the parent isn't SPLIT, that means the node at the current bucket
|
||||
* limit is UNUSED and our address space is entirely free. In that case,
|
||||
* clear the root free list, increase the bucket limit, and add a single
|
||||
* block with the newly-expanded address space to the new root free list.
|
||||
*/
|
||||
if (!parent_is_split(root)) {
|
||||
list_remove(changetype<List>(base_ptr));
|
||||
list_init(buckets$get(--bucket_limit));
|
||||
list_push(buckets$get(bucket_limit), changetype<List>(base_ptr));
|
||||
continue;
|
||||
}
|
||||
|
||||
/*
|
||||
* Otherwise, the tree is currently in use. Create a parent node for the
|
||||
* current root node in the SPLIT state with a right child on the free
|
||||
* list. Make sure to reserve the memory for the free list entry before
|
||||
* writing to it. Note that we do not need to flip the "is split" flag for
|
||||
* our current parent because it's already on (we know because we just
|
||||
* checked it above).
|
||||
*/
|
||||
right_child = ptr_for_node(root + 1, bucket_limit);
|
||||
if (!update_max_ptr(right_child + List.SIZE)) {
|
||||
return 0;
|
||||
}
|
||||
list_push(buckets$get(bucket_limit), changetype<List>(right_child));
|
||||
list_init(buckets$get(--bucket_limit));
|
||||
|
||||
/*
|
||||
* Set the grandparent's SPLIT flag so if we need to lower the bucket limit
|
||||
* again, we'll know that the new root node we just added is in use.
|
||||
*/
|
||||
root = (root - 1) / 2;
|
||||
if (root != 0) {
|
||||
flip_parent_is_split(root);
|
||||
}
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Memory allocator implementation
|
||||
@global namespace memory {
|
||||
|
||||
export function allocate(request: usize): usize {
|
||||
var original_bucket: usize, bucket: usize;
|
||||
|
||||
/*
|
||||
* Make sure it's possible for an allocation of this size to succeed. There's
|
||||
* a hard-coded limit on the maximum allocation size because of the way this
|
||||
* allocator works.
|
||||
*/
|
||||
if (request > MAX_ALLOC - HEADER_SIZE) unreachable();
|
||||
|
||||
/*
|
||||
* Initialize our global state if this is the first call to "malloc". At the
|
||||
* beginning, the tree has a single node that represents the smallest
|
||||
* possible allocation size. More memory will be reserved later as needed.
|
||||
*/
|
||||
if (base_ptr == 0) {
|
||||
// base_ptr = max_ptr = (uint8_t *)sbrk(0);
|
||||
base_ptr = (NODE_IS_SPLIT_END + 7) & ~7; // must be aligned
|
||||
max_ptr = <usize>memory.size() << 16; // must grow first
|
||||
bucket_limit = BUCKET_COUNT - 1;
|
||||
if (!update_max_ptr(base_ptr + List.SIZE)) {
|
||||
return 0;
|
||||
}
|
||||
list_init(buckets$get(BUCKET_COUNT - 1));
|
||||
list_push(buckets$get(BUCKET_COUNT - 1), changetype<List>(base_ptr));
|
||||
}
|
||||
|
||||
/*
|
||||
* Find the smallest bucket that will fit this request. This doesn't check
|
||||
* that there's space for the request yet.
|
||||
*/
|
||||
bucket = bucket_for_request(request + HEADER_SIZE);
|
||||
original_bucket = bucket;
|
||||
|
||||
/*
|
||||
* Search for a bucket with a non-empty free list that's as large or larger
|
||||
* than what we need. If there isn't an exact match, we'll need to split a
|
||||
* larger one to get a match.
|
||||
*/
|
||||
while (bucket + 1 != 0) {
|
||||
let size: usize, bytes_needed: usize, i: usize;
|
||||
let ptr: usize;
|
||||
|
||||
/*
|
||||
* We may need to grow the tree to be able to fit an allocation of this
|
||||
* size. Try to grow the tree and stop here if we can't.
|
||||
*/
|
||||
if (!lower_bucket_limit(bucket)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Try to pop a block off the free list for this bucket. If the free list
|
||||
* is empty, we're going to have to split a larger block instead.
|
||||
*/
|
||||
ptr = changetype<usize>(list_pop(buckets$get(bucket)));
|
||||
if (!ptr) {
|
||||
/*
|
||||
* If we're not at the root of the tree or it's impossible to grow the
|
||||
* tree any more, continue on to the next bucket.
|
||||
*/
|
||||
if (bucket != bucket_limit || bucket == 0) {
|
||||
bucket--;
|
||||
continue;
|
||||
}
|
||||
|
||||
/*
|
||||
* Otherwise, grow the tree one more level and then pop a block off the
|
||||
* free list again. Since we know the root of the tree is used (because
|
||||
* the free list was empty), this will add a parent above this node in
|
||||
* the SPLIT state and then add the new right child node to the free list
|
||||
* for this bucket. Popping the free list will give us this right child.
|
||||
*/
|
||||
if (!lower_bucket_limit(bucket - 1)) {
|
||||
return 0;
|
||||
}
|
||||
ptr = changetype<usize>(list_pop(buckets$get(bucket)));
|
||||
}
|
||||
|
||||
/*
|
||||
* Try to expand the address space first before going any further. If we
|
||||
* have run out of space, put this block back on the free list and fail.
|
||||
*/
|
||||
size = 1 << (MAX_ALLOC_LOG2 - bucket);
|
||||
bytes_needed = bucket < original_bucket ? size / 2 + List.SIZE : size;
|
||||
if (!update_max_ptr(ptr + bytes_needed)) {
|
||||
list_push(buckets$get(bucket), changetype<List>(ptr));
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* If we got a node off the free list, change the node from UNUSED to USED.
|
||||
* This involves flipping our parent's "is split" bit because that bit is
|
||||
* the exclusive-or of the UNUSED flags of both children, and our UNUSED
|
||||
* flag (which isn't ever stored explicitly) has just changed.
|
||||
*
|
||||
* Note that we shouldn't ever need to flip the "is split" bit of our
|
||||
* grandparent because we know our buddy is USED so it's impossible for our
|
||||
* grandparent to be UNUSED (if our buddy chunk was UNUSED, our parent
|
||||
* wouldn't ever have been split in the first place).
|
||||
*/
|
||||
i = node_for_ptr(ptr, bucket);
|
||||
if (i != 0) {
|
||||
flip_parent_is_split(i);
|
||||
}
|
||||
|
||||
/*
|
||||
* If the node we got is larger than we need, split it down to the correct
|
||||
* size and put the new unused child nodes on the free list in the
|
||||
* corresponding bucket. This is done by repeatedly moving to the left
|
||||
* child, splitting the parent, and then adding the right child to the free
|
||||
* list.
|
||||
*/
|
||||
while (bucket < original_bucket) {
|
||||
i = i * 2 + 1;
|
||||
bucket++;
|
||||
flip_parent_is_split(i);
|
||||
list_push(
|
||||
buckets$get(bucket),
|
||||
changetype<List>(ptr_for_node(i + 1, bucket))
|
||||
);
|
||||
}
|
||||
|
||||
/*
|
||||
* Now that we have a memory address, write the block header (just the size
|
||||
* of the allocation) and return the address immediately after the header.
|
||||
*/
|
||||
store<usize>(ptr, request);
|
||||
return ptr + HEADER_SIZE;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
export function free(ptr: usize): void {
|
||||
var bucket: usize, i: usize;
|
||||
|
||||
/*
|
||||
* Ignore any attempts to free a NULL pointer.
|
||||
*/
|
||||
if (!ptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
* We were given the address returned by "malloc" so get back to the actual
|
||||
* address of the node by subtracting off the size of the block header. Then
|
||||
* look up the index of the node corresponding to this address.
|
||||
*/
|
||||
ptr = ptr - HEADER_SIZE;
|
||||
bucket = bucket_for_request(load<usize>(ptr) + HEADER_SIZE);
|
||||
i = node_for_ptr(ptr, bucket);
|
||||
|
||||
/*
|
||||
* Traverse up to the root node, flipping USED blocks to UNUSED and merging
|
||||
* UNUSED buddies together into a single UNUSED parent.
|
||||
*/
|
||||
while (i != 0) {
|
||||
/*
|
||||
* Change this node from UNUSED to USED. This involves flipping our
|
||||
* parent's "is split" bit because that bit is the exclusive-or of the
|
||||
* UNUSED flags of both children, and our UNUSED flag (which isn't ever
|
||||
* stored explicitly) has just changed.
|
||||
*/
|
||||
flip_parent_is_split(i);
|
||||
|
||||
/*
|
||||
* If the parent is now SPLIT, that means our buddy is USED, so don't merge
|
||||
* with it. Instead, stop the iteration here and add ourselves to the free
|
||||
* list for our bucket.
|
||||
*
|
||||
* Also stop here if we're at the current root node, even if that root node
|
||||
* is now UNUSED. Root nodes don't have a buddy so we can't merge with one.
|
||||
*/
|
||||
if (parent_is_split(i) || bucket == bucket_limit) {
|
||||
break;
|
||||
}
|
||||
|
||||
/*
|
||||
* If we get here, we know our buddy is UNUSED. In this case we should
|
||||
* merge with that buddy and continue traversing up to the root node. We
|
||||
* need to remove the buddy from its free list here but we don't need to
|
||||
* add the merged parent to its free list yet. That will be done once after
|
||||
* this loop is finished.
|
||||
*/
|
||||
list_remove(changetype<List>(ptr_for_node(((i - 1) ^ 1) + 1, bucket)));
|
||||
i = (i - 1) / 2;
|
||||
bucket--;
|
||||
}
|
||||
|
||||
/*
|
||||
* Add ourselves to the free list for our bucket. We add to the back of the
|
||||
* list because "malloc" takes from the back of the list and we want a "free"
|
||||
* followed by a "malloc" of the same size to ideally use the same address
|
||||
* for better memory locality.
|
||||
*/
|
||||
list_push(buckets$get(bucket), changetype<List>(ptr_for_node(i, bucket)));
|
||||
}
|
||||
}
|
@ -1,24 +1,10 @@
|
||||
/**
|
||||
* Emscripten Memory Allocator.
|
||||
*
|
||||
* Uses Emscripten's exported _malloc and _free implementations, i.e., when linking with
|
||||
* Emscripten-compiled programs that already provide these. Differs from 'system' in that their
|
||||
* names are prefixed with an underscore.
|
||||
*
|
||||
* @module std/assembly/allocator/emscripten
|
||||
*//***/
|
||||
@unsafe declare function _malloc(size: usize): usize;
|
||||
@unsafe declare function _free(ptr: usize): void;
|
||||
|
||||
declare function _malloc(size: usize): usize;
|
||||
declare function _free(ptr: usize): void;
|
||||
|
||||
// Memory allocator implementation
|
||||
@global namespace memory {
|
||||
|
||||
@inline export function allocate(size: usize): usize {
|
||||
return _malloc(size);
|
||||
}
|
||||
|
||||
@inline export function free(ptr: usize): void {
|
||||
_free(ptr);
|
||||
}
|
||||
@unsafe @global function __memory_allocate(size: usize): usize {
|
||||
return _malloc(size);
|
||||
}
|
||||
|
||||
@unsafe @global function __memory_free(ptr: usize): void {
|
||||
_free(ptr);
|
||||
}
|
||||
|
@ -1,23 +1,10 @@
|
||||
/**
|
||||
* System Memory Allocator.
|
||||
*
|
||||
* Uses the environment's malloc and free implementations, i.e., when linking with other C-like
|
||||
* programs that already provide these.
|
||||
*
|
||||
* @module std/assembly/allocator/system
|
||||
*//***/
|
||||
@unsafe declare function malloc(size: usize): usize;
|
||||
@unsafe declare function free(ptr: usize): void;
|
||||
|
||||
declare function malloc(size: usize): usize;
|
||||
declare function free(ptr: usize): void;
|
||||
|
||||
// Memory allocator interface
|
||||
@global namespace memory {
|
||||
|
||||
@inline export function allocate(size: usize): usize {
|
||||
return malloc(size);
|
||||
}
|
||||
|
||||
@inline export function free(ptr: usize): void {
|
||||
free(ptr);
|
||||
}
|
||||
@unsafe @global function __memory_allocate(size: usize): usize {
|
||||
return malloc(size);
|
||||
}
|
||||
|
||||
@unsafe @global function __memory_free(ptr: usize): void {
|
||||
free(ptr);
|
||||
}
|
||||
|
@ -1,12 +1,3 @@
|
||||
/**
|
||||
* Two-Level Segregate Fit Memory Allocator.
|
||||
*
|
||||
* A general purpose dynamic memory allocator specifically designed to meet real-time requirements.
|
||||
* Always aligns to 8 bytes.
|
||||
*
|
||||
* @module std/assembly/allocator/tlsf
|
||||
*//***/
|
||||
|
||||
// ╒══════════════ Block size interpretation (32-bit) ═════════════╕
|
||||
// 3 2 1
|
||||
// 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 bits
|
||||
@ -16,6 +7,7 @@
|
||||
// FL: first level, SL: second level, AL: alignment, SB: small block
|
||||
|
||||
import { AL_BITS, AL_SIZE, AL_MASK } from "../util/allocator";
|
||||
import { HEAP_BASE, memory } from "../memory";
|
||||
|
||||
const SL_BITS: u32 = 5;
|
||||
const SL_SIZE: usize = 1 << <usize>SL_BITS;
|
||||
@ -429,68 +421,64 @@ function fls<T>(word: T): T {
|
||||
/** Reference to the initialized {@link Root} structure, once initialized. */
|
||||
var ROOT: Root = changetype<Root>(0);
|
||||
|
||||
// Memory allocator interface
|
||||
@global namespace memory {
|
||||
|
||||
/** Allocates a chunk of memory. */
|
||||
export function allocate(size: usize): usize {
|
||||
// initialize if necessary
|
||||
var root = ROOT;
|
||||
if (!root) {
|
||||
let rootOffset = (HEAP_BASE + AL_MASK) & ~AL_MASK;
|
||||
let pagesBefore = memory.size();
|
||||
let pagesNeeded = <i32>((((rootOffset + Root.SIZE) + 0xffff) & ~0xffff) >>> 16);
|
||||
if (pagesNeeded > pagesBefore && memory.grow(pagesNeeded - pagesBefore) < 0) unreachable();
|
||||
ROOT = root = changetype<Root>(rootOffset);
|
||||
root.tailRef = 0;
|
||||
root.flMap = 0;
|
||||
for (let fl: usize = 0; fl < FL_BITS; ++fl) {
|
||||
root.setSLMap(fl, 0);
|
||||
for (let sl: u32 = 0; sl < SL_SIZE; ++sl) {
|
||||
root.setHead(fl, sl, null);
|
||||
}
|
||||
/** Allocates a chunk of memory. */
|
||||
@unsafe @global function __memory_allocate(size: usize): usize {
|
||||
// initialize if necessary
|
||||
var root = ROOT;
|
||||
if (!root) {
|
||||
let rootOffset = (HEAP_BASE + AL_MASK) & ~AL_MASK;
|
||||
let pagesBefore = memory.size();
|
||||
let pagesNeeded = <i32>((((rootOffset + Root.SIZE) + 0xffff) & ~0xffff) >>> 16);
|
||||
if (pagesNeeded > pagesBefore && memory.grow(pagesNeeded - pagesBefore) < 0) unreachable();
|
||||
ROOT = root = changetype<Root>(rootOffset);
|
||||
root.tailRef = 0;
|
||||
root.flMap = 0;
|
||||
for (let fl: usize = 0; fl < FL_BITS; ++fl) {
|
||||
root.setSLMap(fl, 0);
|
||||
for (let sl: u32 = 0; sl < SL_SIZE; ++sl) {
|
||||
root.setHead(fl, sl, null);
|
||||
}
|
||||
root.addMemory((rootOffset + Root.SIZE + AL_MASK) & ~AL_MASK, memory.size() << 16);
|
||||
}
|
||||
|
||||
// search for a suitable block
|
||||
if (size > Block.MAX_SIZE) unreachable();
|
||||
|
||||
// 32-bit MAX_SIZE is 1 << 30 and itself aligned, hence the following can't overflow MAX_SIZE
|
||||
size = max<usize>((size + AL_MASK) & ~AL_MASK, Block.MIN_SIZE);
|
||||
|
||||
var block = root.search(size);
|
||||
if (!block) {
|
||||
|
||||
// request more memory
|
||||
let pagesBefore = memory.size();
|
||||
let pagesNeeded = <i32>(((size + 0xffff) & ~0xffff) >>> 16);
|
||||
let pagesWanted = max(pagesBefore, pagesNeeded); // double memory
|
||||
if (memory.grow(pagesWanted) < 0) {
|
||||
if (memory.grow(pagesNeeded) < 0) {
|
||||
unreachable(); // out of memory
|
||||
}
|
||||
}
|
||||
let pagesAfter = memory.size();
|
||||
root.addMemory(<usize>pagesBefore << 16, <usize>pagesAfter << 16);
|
||||
block = assert(root.search(size)); // must be found now
|
||||
}
|
||||
|
||||
assert((block.info & ~TAGS) >= size);
|
||||
return root.use(<Block>block, size);
|
||||
root.addMemory((rootOffset + Root.SIZE + AL_MASK) & ~AL_MASK, memory.size() << 16);
|
||||
}
|
||||
|
||||
/** Frees the chunk of memory at the specified address. */
|
||||
export function free(data: usize): void {
|
||||
if (data) {
|
||||
let root = ROOT;
|
||||
if (root) {
|
||||
let block = changetype<Block>(data - Block.INFO);
|
||||
let blockInfo = block.info;
|
||||
assert(!(blockInfo & FREE)); // must be used
|
||||
block.info = blockInfo | FREE;
|
||||
root.insert(changetype<Block>(data - Block.INFO));
|
||||
// search for a suitable block
|
||||
if (size > Block.MAX_SIZE) unreachable();
|
||||
|
||||
// 32-bit MAX_SIZE is 1 << 30 and itself aligned, hence the following can't overflow MAX_SIZE
|
||||
size = max<usize>((size + AL_MASK) & ~AL_MASK, Block.MIN_SIZE);
|
||||
|
||||
var block = root.search(size);
|
||||
if (!block) {
|
||||
|
||||
// request more memory
|
||||
let pagesBefore = memory.size();
|
||||
let pagesNeeded = <i32>(((size + 0xffff) & ~0xffff) >>> 16);
|
||||
let pagesWanted = max(pagesBefore, pagesNeeded); // double memory
|
||||
if (memory.grow(pagesWanted) < 0) {
|
||||
if (memory.grow(pagesNeeded) < 0) {
|
||||
unreachable(); // out of memory
|
||||
}
|
||||
}
|
||||
let pagesAfter = memory.size();
|
||||
root.addMemory(<usize>pagesBefore << 16, <usize>pagesAfter << 16);
|
||||
block = assert(root.search(size)); // must be found now
|
||||
}
|
||||
|
||||
assert((block.info & ~TAGS) >= size);
|
||||
return root.use(<Block>block, size);
|
||||
}
|
||||
|
||||
/** Frees the chunk of memory at the specified address. */
|
||||
@unsafe @global function __memory_free(data: usize): void {
|
||||
if (data) {
|
||||
let root = ROOT;
|
||||
if (root) {
|
||||
let block = changetype<Block>(data - Block.INFO);
|
||||
let blockInfo = block.info;
|
||||
assert(!(blockInfo & FREE)); // must be used
|
||||
block.info = blockInfo | FREE;
|
||||
root.insert(changetype<Block>(data - Block.INFO));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { ALLOC, REALLOC, REGISTER, LINK, FREE, ArrayBufferView } from "./runtime";
|
||||
import { runtime, ArrayBufferView } from "./runtime";
|
||||
import { ArrayBuffer } from "./arraybuffer";
|
||||
import { COMPARATOR, SORT } from "./util/sort";
|
||||
import { itoa, dtoa, itoa_stream, dtoa_stream, MAX_DOUBLE_LENGTH } from "./util/number";
|
||||
@ -36,7 +36,7 @@ export class Array<T> extends ArrayBufferView {
|
||||
const MAX_LENGTH = ArrayBufferView.MAX_BYTELENGTH >>> alignof<T>();
|
||||
if (<u32>length > <u32>MAX_LENGTH) throw new RangeError("Invalid array length");
|
||||
let newCapacity = <usize>length << alignof<T>();
|
||||
let newData = REALLOC(changetype<usize>(oldData), newCapacity); // registers on move
|
||||
let newData = runtime.realloc(changetype<usize>(oldData), newCapacity); // registers on move
|
||||
if (newData !== changetype<usize>(oldData)) {
|
||||
this.data = changetype<ArrayBuffer>(newData); // links
|
||||
this.dataStart = newData;
|
||||
@ -76,7 +76,7 @@ export class Array<T> extends ArrayBufferView {
|
||||
private __set(index: i32, value: T): void {
|
||||
this.resize(index + 1);
|
||||
store<T>(this.dataStart + (<usize>index << alignof<T>()), value);
|
||||
if (isManaged<T>()) LINK(changetype<usize>(value), changetype<usize>(this));
|
||||
if (isManaged<T>()) runtime.link(changetype<usize>(value), changetype<usize>(this));
|
||||
if (index >= this.length_) this.length_ = index + 1;
|
||||
}
|
||||
|
||||
@ -141,7 +141,7 @@ export class Array<T> extends ArrayBufferView {
|
||||
this.resize(newLength);
|
||||
this.length_ = newLength;
|
||||
store<T>(this.dataStart + (<usize>(newLength - 1) << alignof<T>()), element);
|
||||
if (isManaged<T>()) LINK(changetype<usize>(element), changetype<usize>(this));
|
||||
if (isManaged<T>()) runtime.link(changetype<usize>(element), changetype<usize>(this));
|
||||
return newLength;
|
||||
}
|
||||
|
||||
@ -156,14 +156,14 @@ export class Array<T> extends ArrayBufferView {
|
||||
for (let offset: usize = 0; offset < thisSize; offset += sizeof<T>()) {
|
||||
let element = load<T>(thisStart + offset);
|
||||
store<T>(outStart + offset, element);
|
||||
LINK(changetype<usize>(element), changetype<usize>(out));
|
||||
runtime.link(changetype<usize>(element), changetype<usize>(out));
|
||||
}
|
||||
let otherStart = other.dataStart;
|
||||
let otherSize = <usize>otherLen << alignof<T>();
|
||||
for (let offset: usize = 0; offset < otherSize; offset += sizeof<T>()) {
|
||||
let element = load<T>(otherStart + offset);
|
||||
store<T>(outStart + thisSize + offset, element);
|
||||
LINK(changetype<usize>(element), changetype<usize>(out));
|
||||
runtime.link(changetype<usize>(element), changetype<usize>(out));
|
||||
}
|
||||
} else {
|
||||
memory.copy(outStart, this.dataStart, thisSize);
|
||||
@ -221,7 +221,7 @@ export class Array<T> extends ArrayBufferView {
|
||||
let value = load<T>(this.dataStart + (<usize>index << alignof<T>()));
|
||||
let result = callbackfn(value, index, this);
|
||||
store<U>(outStart + (<usize>index << alignof<U>()), result);
|
||||
if (isManaged<U>()) LINK(changetype<usize>(result), changetype<usize>(out));
|
||||
if (isManaged<U>()) runtime.link(changetype<usize>(result), changetype<usize>(out));
|
||||
}
|
||||
return out;
|
||||
}
|
||||
@ -290,7 +290,7 @@ export class Array<T> extends ArrayBufferView {
|
||||
<usize>(newLength - 1) << alignof<T>()
|
||||
);
|
||||
store<T>(base, element);
|
||||
if (isManaged<T>()) LINK(changetype<usize>(element), changetype<usize>(this));
|
||||
if (isManaged<T>()) runtime.link(changetype<usize>(element), changetype<usize>(this));
|
||||
this.length_ = newLength;
|
||||
return newLength;
|
||||
}
|
||||
@ -307,7 +307,7 @@ export class Array<T> extends ArrayBufferView {
|
||||
let offset = <usize>i << alignof<T>();
|
||||
let element = load<T>(thisBase + offset);
|
||||
store<T>(sliceBase + offset, element);
|
||||
if (isManaged<T>()) LINK(changetype<usize>(element), changetype<usize>(slice));
|
||||
if (isManaged<T>()) runtime.link(changetype<usize>(element), changetype<usize>(slice));
|
||||
}
|
||||
return slice;
|
||||
}
|
||||
@ -323,7 +323,7 @@ export class Array<T> extends ArrayBufferView {
|
||||
for (let i = 0; i < deleteCount; ++i) {
|
||||
let element = load<T>(thisBase + (<usize>i << alignof<T>()));
|
||||
store<T>(spliceStart + (<usize>i << alignof<T>()), element);
|
||||
if (isManaged<T>()) LINK(changetype<usize>(element), changetype<usize>(splice));
|
||||
if (isManaged<T>()) runtime.link(changetype<usize>(element), changetype<usize>(splice));
|
||||
}
|
||||
memory.copy(
|
||||
splice.dataStart,
|
||||
@ -395,7 +395,7 @@ export class Array<T> extends ArrayBufferView {
|
||||
var sepLen = separator.length;
|
||||
var valueLen = 5; // max possible length of element len("false")
|
||||
var estLen = (valueLen + sepLen) * lastIndex + valueLen;
|
||||
var result = ALLOC(estLen << 1);
|
||||
var result = runtime.alloc(estLen << 1);
|
||||
var offset = 0;
|
||||
var value: bool;
|
||||
for (let i = 0; i < lastIndex; ++i) {
|
||||
@ -427,10 +427,10 @@ export class Array<T> extends ArrayBufferView {
|
||||
|
||||
if (estLen > offset) {
|
||||
let trimmed = changetype<string>(result).substring(0, offset);
|
||||
FREE(result);
|
||||
runtime.free(result);
|
||||
return trimmed; // registered in .substring
|
||||
}
|
||||
return REGISTER<string>(result);
|
||||
return runtime.register<string>(result);
|
||||
}
|
||||
|
||||
private join_int(separator: string = ","): string {
|
||||
@ -442,7 +442,7 @@ export class Array<T> extends ArrayBufferView {
|
||||
var sepLen = separator.length;
|
||||
const valueLen = (sizeof<T>() <= 4 ? 10 : 20) + <i32>isSigned<T>();
|
||||
var estLen = (valueLen + sepLen) * lastIndex + valueLen;
|
||||
var result = ALLOC(estLen << 1);
|
||||
var result = runtime.alloc(estLen << 1);
|
||||
var offset = 0;
|
||||
var value: T;
|
||||
for (let i = 0; i < lastIndex; ++i) {
|
||||
@ -461,10 +461,10 @@ export class Array<T> extends ArrayBufferView {
|
||||
offset += itoa_stream<T>(result, offset, value);
|
||||
if (estLen > offset) {
|
||||
let trimmed = changetype<string>(result).substring(0, offset);
|
||||
FREE(result);
|
||||
runtime.free(result);
|
||||
return trimmed; // registered in .substring
|
||||
}
|
||||
return REGISTER<string>(result);
|
||||
return runtime.register<string>(result);
|
||||
}
|
||||
|
||||
private join_flt(separator: string = ","): string {
|
||||
@ -476,7 +476,7 @@ export class Array<T> extends ArrayBufferView {
|
||||
const valueLen = MAX_DOUBLE_LENGTH;
|
||||
var sepLen = separator.length;
|
||||
var estLen = (valueLen + sepLen) * lastIndex + valueLen;
|
||||
var result = ALLOC(estLen << 1);
|
||||
var result = runtime.alloc(estLen << 1);
|
||||
var offset = 0;
|
||||
var value: T;
|
||||
for (let i = 0; i < lastIndex; ++i) {
|
||||
@ -495,10 +495,10 @@ export class Array<T> extends ArrayBufferView {
|
||||
offset += dtoa_stream(result, offset, value);
|
||||
if (estLen > offset) {
|
||||
let trimmed = changetype<string>(result).substring(0, offset);
|
||||
FREE(result);
|
||||
runtime.free(result);
|
||||
return trimmed; // registered in .substring
|
||||
}
|
||||
return REGISTER<string>(result);
|
||||
return runtime.register<string>(result);
|
||||
}
|
||||
|
||||
private join_str(separator: string = ","): string {
|
||||
@ -513,7 +513,7 @@ export class Array<T> extends ArrayBufferView {
|
||||
estLen += load<string>(dataStart + (<usize>i << alignof<T>())).length;
|
||||
}
|
||||
var offset = 0;
|
||||
var result = ALLOC((estLen + sepLen * lastIndex) << 1);
|
||||
var result = runtime.alloc((estLen + sepLen * lastIndex) << 1);
|
||||
var value: String;
|
||||
for (let i = 0; i < lastIndex; ++i) {
|
||||
value = load<string>(dataStart + (<usize>i << alignof<T>()));
|
||||
@ -544,7 +544,7 @@ export class Array<T> extends ArrayBufferView {
|
||||
<usize>valueLen << 1
|
||||
);
|
||||
}
|
||||
return REGISTER<string>(result);
|
||||
return runtime.register<string>(result);
|
||||
}
|
||||
|
||||
private join_arr(separator: string = ","): string {
|
||||
@ -578,7 +578,7 @@ export class Array<T> extends ArrayBufferView {
|
||||
const valueLen = 15; // max possible length of element len("[object Object]")
|
||||
var sepLen = separator.length;
|
||||
var estLen = (valueLen + sepLen) * lastIndex + valueLen;
|
||||
var result = ALLOC(estLen << 1);
|
||||
var result = runtime.alloc(estLen << 1);
|
||||
var offset = 0;
|
||||
var value: T;
|
||||
for (let i = 0; i < lastIndex; ++i) {
|
||||
@ -610,10 +610,10 @@ export class Array<T> extends ArrayBufferView {
|
||||
}
|
||||
if (estLen > offset) {
|
||||
let out = changetype<string>(result).substring(0, offset);
|
||||
FREE(result);
|
||||
runtime.free(result);
|
||||
return out; // registered in .substring
|
||||
}
|
||||
return REGISTER<string>(result);
|
||||
return runtime.register<string>(result);
|
||||
}
|
||||
|
||||
@inline
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { HEADER, ALLOC_RAW, ALLOC, REGISTER, ArrayBufferView } from "./runtime";
|
||||
import { runtime, ArrayBufferView } from "./runtime";
|
||||
|
||||
@sealed export class ArrayBuffer {
|
||||
|
||||
@ -22,11 +22,11 @@ import { HEADER, ALLOC_RAW, ALLOC, REGISTER, ArrayBufferView } from "./runtime";
|
||||
|
||||
constructor(length: i32) {
|
||||
if (<u32>length > <u32>ArrayBufferView.MAX_BYTELENGTH) throw new RangeError("Invalid array buffer length");
|
||||
return REGISTER<ArrayBuffer>(ALLOC(<usize>length));
|
||||
return runtime.register<ArrayBuffer>(runtime.alloc(<usize>length));
|
||||
}
|
||||
|
||||
get byteLength(): i32 {
|
||||
return changetype<HEADER>(changetype<usize>(this) - HEADER_SIZE).payloadSize;
|
||||
return changetype<runtime.Header>(changetype<usize>(this) - runtime.Header.SIZE).payloadSize;
|
||||
}
|
||||
|
||||
slice(begin: i32 = 0, end: i32 = ArrayBufferView.MAX_BYTELENGTH): ArrayBuffer {
|
||||
@ -34,9 +34,9 @@ import { HEADER, ALLOC_RAW, ALLOC, REGISTER, ArrayBufferView } from "./runtime";
|
||||
begin = begin < 0 ? max(length + begin, 0) : min(begin, length);
|
||||
end = end < 0 ? max(length + end , 0) : min(end , length);
|
||||
var outSize = <usize>max(end - begin, 0);
|
||||
var out = ALLOC_RAW(outSize);
|
||||
var out = runtime.allocRaw(outSize);
|
||||
memory.copy(out, changetype<usize>(this) + <usize>begin, outSize);
|
||||
return REGISTER<ArrayBuffer>(out);
|
||||
return runtime.register<ArrayBuffer>(out);
|
||||
}
|
||||
|
||||
toString(): string {
|
||||
|
@ -13,7 +13,6 @@
|
||||
@builtin export declare function isFunction<T>(value?: T): bool;
|
||||
@builtin export declare function isNullable<T>(value?: T): bool;
|
||||
@builtin export declare function isDefined(expression: void): bool;
|
||||
@builtin export declare function isImplemented(expression: void): bool;
|
||||
@builtin export declare function isConstant(expression: void): bool;
|
||||
@builtin export declare function isManaged<T>(value?: T): bool;
|
||||
@inline export function isNaN<T>(value: T): bool { return value != value; }
|
||||
|
@ -1,9 +1,3 @@
|
||||
/**
|
||||
* Incremental Tri-Color-Marking Garbage Collector.
|
||||
*
|
||||
* @module std/assembly/collector/itcm
|
||||
*//***/
|
||||
|
||||
// Largely based on Bach Le's μgc, see: https://github.com/bullno1/ugc
|
||||
|
||||
@inline const TRACE = false;
|
||||
@ -11,8 +5,8 @@
|
||||
/** Size of a managed object header. */
|
||||
@inline export const HEADER_SIZE: usize = (offsetof<ManagedObject>() + AL_MASK) & ~AL_MASK;
|
||||
|
||||
import { AL_MASK, MAX_SIZE_32 } from "../internal/allocator";
|
||||
import { __rt_iterateroots } from "../builtins";
|
||||
import { AL_MASK, MAX_SIZE_32 } from "../util/allocator";
|
||||
import { gc } from "../gc";
|
||||
|
||||
/** Collector states. */
|
||||
const enum State {
|
||||
@ -142,7 +136,7 @@ function step(): void {
|
||||
}
|
||||
case State.IDLE: {
|
||||
if (TRACE) trace("gc~step/IDLE");
|
||||
__rt_iterateroots(__gc_mark);
|
||||
gc.iterateRoots(__gc_mark);
|
||||
state = State.MARK;
|
||||
if (TRACE) trace("gc~state = MARK");
|
||||
break;
|
||||
@ -163,7 +157,7 @@ function step(): void {
|
||||
obj.hookFn(objToRef(obj));
|
||||
} else {
|
||||
if (TRACE) trace("gc~step/MARK finish");
|
||||
__rt_iterateroots(__gc_mark);
|
||||
gc.iterateRoots(__gc_mark);
|
||||
obj = iter.next;
|
||||
if (obj === toSpace) {
|
||||
let from = fromSpace;
|
||||
@ -202,9 +196,7 @@ function step(): void {
|
||||
return changetype<usize>(obj) + HEADER_SIZE;
|
||||
}
|
||||
|
||||
// Garbage collector interface
|
||||
|
||||
@global export function __gc_allocate(
|
||||
@global @unsafe export function __gc_allocate( // TODO: make this register only / reuse header
|
||||
size: usize,
|
||||
markFn: (ref: usize) => void
|
||||
): usize {
|
||||
@ -218,13 +210,13 @@ function step(): void {
|
||||
return objToRef(obj);
|
||||
}
|
||||
|
||||
@global export function __gc_link(parentRef: usize, childRef: usize): void {
|
||||
@global @unsafe export function __gc_link(parentRef: usize, childRef: usize): void {
|
||||
if (TRACE) trace("gc.link", 2, parentRef, childRef);
|
||||
var parent = refToObj(parentRef);
|
||||
if (parent.color == <i32>!white && refToObj(childRef).color == white) parent.makeGray();
|
||||
}
|
||||
|
||||
@global export function __gc_mark(ref: usize): void {
|
||||
@global @unsafe export function __gc_mark(ref: usize): void {
|
||||
if (TRACE) trace("gc.mark", 1, ref);
|
||||
if (ref) {
|
||||
let obj = refToObj(ref);
|
||||
@ -232,7 +224,7 @@ function step(): void {
|
||||
}
|
||||
}
|
||||
|
||||
@global export function __gc_collect(): void {
|
||||
@global @unsafe export function __gc_collect(): void {
|
||||
if (TRACE) trace("gc.collect");
|
||||
// begin collecting if not yet collecting
|
||||
switch (state) {
|
||||
|
@ -1,4 +1,5 @@
|
||||
import { ArrayBuffer } from "./arraybuffer";
|
||||
import { ArrayBufferView } from "./runtime";
|
||||
|
||||
export class DataView {
|
||||
|
||||
@ -12,7 +13,7 @@ export class DataView {
|
||||
byteLength: i32 = i32.MIN_VALUE // FIXME
|
||||
) {
|
||||
if (byteLength === i32.MIN_VALUE) byteLength = buffer.byteLength - byteOffset; // FIXME
|
||||
if (<u32>byteLength > <u32>ArrayBuffer.MAX_BYTELENGTH) throw new RangeError("Invalid byteLength");
|
||||
if (<u32>byteLength > <u32>ArrayBufferView.MAX_BYTELENGTH) throw new RangeError("Invalid byteLength");
|
||||
if (<u32>byteOffset + byteLength > <u32>buffer.byteLength) throw new RangeError("Invalid length");
|
||||
this.data = buffer; // links
|
||||
var dataStart = changetype<usize>(buffer) + <usize>byteOffset;
|
||||
|
@ -1,5 +1,3 @@
|
||||
/* tslint:disable */
|
||||
|
||||
@builtin export declare function ERROR(message?: void): void;
|
||||
@builtin export declare function WARNING(message?: void): void;
|
||||
@builtin export declare function INFO(message?: void): void;
|
||||
@builtin export declare function ERROR(message?: string): void;
|
||||
@builtin export declare function WARNING(message?: string): void;
|
||||
@builtin export declare function INFO(message?: string): void;
|
||||
|
@ -1,30 +1,37 @@
|
||||
/** Garbage collector interface. */
|
||||
export namespace gc {
|
||||
|
||||
/** Whether the garbage collector interface is implemented. */
|
||||
@lazy export const implemented: bool = isDefined(__gc_register);
|
||||
|
||||
/** Gets the computed unique class id of a class type. */
|
||||
@builtin @unsafe export declare function classId<T>(): u32;
|
||||
@unsafe @builtin export declare function classId<T>(): u32;
|
||||
|
||||
/** Iterates reference root objects. */
|
||||
@builtin @unsafe export declare function iterateRoots(fn: (ref: usize) => void): void;
|
||||
@unsafe @builtin export declare function iterateRoots(fn: (ref: usize) => void): void;
|
||||
|
||||
/** Registers a managed object to be tracked by the garbage collector. */
|
||||
@stub @unsafe export function register(ref: usize): void {
|
||||
ERROR("stub: missing garbage collector");
|
||||
@unsafe export function register(ref: usize): void {
|
||||
if (isDefined(__gc_register)) __gc_register(ref);
|
||||
else ERROR("missing implementation: gc.register");
|
||||
}
|
||||
|
||||
/** Links a registered object with the registered object now referencing it. */
|
||||
@stub @unsafe export function link(ref: usize, parentRef: usize): void {
|
||||
ERROR("stub: missing garbage collector");
|
||||
@unsafe export function link(ref: usize, parentRef: usize): void {
|
||||
if (isDefined(__gc_link)) __gc_link(ref, parentRef);
|
||||
else ERROR("missing implementation: gc.link");
|
||||
}
|
||||
|
||||
/** Marks an object as being reachable. */
|
||||
@stub @unsafe export function mark(ref: usize): void {
|
||||
ERROR("stub: missing garbage collector");
|
||||
@unsafe export function mark(ref: usize): void {
|
||||
if (isDefined(__gc_mark)) __gc_mark(ref);
|
||||
else ERROR("missing implementation: gc.mark");
|
||||
}
|
||||
|
||||
/** Performs a full garbage collection cycle. */
|
||||
@stub export function collect(): void {
|
||||
WARNING("stub: missing garbage collector");
|
||||
export function collect(): void {
|
||||
if (isDefined(__gc_collect)) __gc_collect();
|
||||
else WARNING("missing implementation: gc.collect");
|
||||
}
|
||||
}
|
||||
|
||||
|
9
std/assembly/index.d.ts
vendored
9
std/assembly/index.d.ts
vendored
@ -144,8 +144,6 @@ declare function isFunction<T>(value?: any): value is (...args: any) => any;
|
||||
declare function isNullable<T>(value?: any): bool;
|
||||
/** Tests if the specified expression resolves to a defined element. Compiles to a constant. */
|
||||
declare function isDefined(expression: any): bool;
|
||||
/** Tests if the specified expression resolves to an implemented (non-stub) element. Compiles to a constant. */
|
||||
declare function isImplemented(expression: any): bool;
|
||||
/** Tests if the specified expression evaluates to a constant value. Compiles to a constant. */
|
||||
declare function isConstant(expression: any): bool;
|
||||
/** Tests if the specified type *or* expression is of a managed type. Compiles to a constant. */
|
||||
@ -1519,6 +1517,13 @@ declare function inline(
|
||||
descriptor: TypedPropertyDescriptor<any>
|
||||
): TypedPropertyDescriptor<any> | void;
|
||||
|
||||
/** Annotates a method, function or constant global as unsafe. */
|
||||
declare function unsafe(
|
||||
target: any,
|
||||
propertyKey: string,
|
||||
descriptor: TypedPropertyDescriptor<any>
|
||||
): TypedPropertyDescriptor<any> | void;
|
||||
|
||||
/** Annotates an explicit external name of a function or global. */
|
||||
declare function external(namespace: string, name: string): (
|
||||
target: any,
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { LINK } from "./runtime";
|
||||
import { runtime } from "./runtime";
|
||||
import { HASH } from "./util/hash";
|
||||
|
||||
// A deterministic hash map based on CloseTable from https://github.com/jorendorff/dht
|
||||
@ -108,8 +108,8 @@ export class Map<K,V> {
|
||||
let bucketPtrBase = changetype<usize>(this.buckets) + <usize>(hashCode & this.bucketsMask) * BUCKET_SIZE;
|
||||
entry.taggedNext = load<usize>(bucketPtrBase);
|
||||
store<usize>(bucketPtrBase, changetype<usize>(entry));
|
||||
if (isManaged<K>()) LINK(changetype<usize>(key), changetype<usize>(this));
|
||||
if (isManaged<V>()) LINK(changetype<usize>(value), changetype<usize>(this));
|
||||
if (isManaged<K>()) runtime.link(changetype<usize>(key), changetype<usize>(this));
|
||||
if (isManaged<V>()) runtime.link(changetype<usize>(value), changetype<usize>(this));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,5 +1,7 @@
|
||||
import { memcmp, memmove, memset } from "./util/memory";
|
||||
|
||||
@builtin export declare const HEAP_BASE: usize;
|
||||
|
||||
/** Memory manager interface. */
|
||||
export namespace memory {
|
||||
|
||||
@ -7,15 +9,15 @@ export namespace memory {
|
||||
@builtin export declare function size(): i32;
|
||||
|
||||
/** Grows the memory by the given size in pages and returns the previous size in pages. */
|
||||
@builtin @unsafe export declare function grow(pages: i32): i32;
|
||||
@unsafe @builtin export declare function grow(pages: i32): i32;
|
||||
|
||||
/** Fills a section in memory with the specified byte value. */
|
||||
@builtin @unsafe @inline export function fill(dst: usize, c: u8, n: usize): void {
|
||||
@unsafe @builtin export function fill(dst: usize, c: u8, n: usize): void {
|
||||
memset(dst, c, n); // fallback if "bulk-memory" isn't enabled
|
||||
}
|
||||
|
||||
/** Copies a section of memory to another. Has move semantics. */
|
||||
@builtin @unsafe @inline export function copy(dst: usize, src: usize, n: usize): void {
|
||||
@unsafe @builtin export function copy(dst: usize, src: usize, n: usize): void {
|
||||
memmove(dst, src, n); // fallback if "bulk-memory" isn't enabled
|
||||
}
|
||||
|
||||
@ -30,24 +32,22 @@ export namespace memory {
|
||||
}
|
||||
|
||||
/** Dynamically allocates a section of memory and returns its address. */
|
||||
@stub @inline export function allocate(size: usize): usize {
|
||||
ERROR("stub: missing memory manager");
|
||||
@unsafe export function allocate(size: usize): usize {
|
||||
if (isDefined(__memory_allocate)) return __memory_allocate(size);
|
||||
else ERROR("missing implementation: memory.allocate");
|
||||
return <usize>unreachable();
|
||||
}
|
||||
|
||||
/** Dynamically frees a section of memory by the previously allocated address. */
|
||||
@stub @unsafe @inline export function free(ptr: usize): void {
|
||||
ERROR("stub: missing memory manager");
|
||||
@unsafe export function free(ptr: usize): void {
|
||||
if (isDefined(__memory_free)) __memory_free(ptr);
|
||||
else ERROR("missing implementation: memory.free");
|
||||
}
|
||||
|
||||
/** Resets the memory to its initial state. Arena allocator only. */
|
||||
@stub @unsafe @inline export function reset(): void {
|
||||
ERROR("stub: not supported by memory manager");
|
||||
}
|
||||
|
||||
/** Compares a section of memory to another. */
|
||||
@inline export function compare(vl: usize, vr: usize, n: usize): i32 {
|
||||
return memcmp(vl, vr, n);
|
||||
@unsafe export function reset(): void {
|
||||
if (isDefined(__memory_reset)) __memory_reset();
|
||||
else ERROR("missing implementation: memory.reset");
|
||||
}
|
||||
|
||||
/** Repeats a section of memory at a specific address. */
|
||||
@ -59,4 +59,9 @@ export namespace memory {
|
||||
index += srcLength;
|
||||
}
|
||||
}
|
||||
|
||||
/** Compares a section of memory to another. */
|
||||
@inline export function compare(vl: usize, vr: usize, n: usize): i32 {
|
||||
return memcmp(vl, vr, n);
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
export function bswap<T>(value: T): T {
|
||||
export function bswap<T extends number>(value: T): T {
|
||||
if (isInteger<T>()) {
|
||||
if (sizeof<T>() == 2) {
|
||||
return <T>((value << 8) | ((value >> 8) & <T>0x00FF));
|
||||
@ -25,7 +25,7 @@ export function bswap<T>(value: T): T {
|
||||
return value;
|
||||
}
|
||||
|
||||
@inline export function bswap16<T>(value: T): T {
|
||||
@inline export function bswap16<T extends number>(value: T): T {
|
||||
if (isInteger<T>() && sizeof<T>() <= 4) {
|
||||
if (sizeof<T>() == 2) {
|
||||
return <T>((value << 8) | ((value >> 8) & <T>0x00FF));
|
||||
|
@ -1,139 +1,143 @@
|
||||
import { AL_MASK, MAX_SIZE_32 } from "./util/allocator";
|
||||
import { HEAP_BASE, memory } from "./memory";
|
||||
import { gc } from "./gc";
|
||||
|
||||
@builtin export declare const HEAP_BASE: usize;
|
||||
/** Common runtime. */
|
||||
export namespace runtime {
|
||||
|
||||
/** Common runtime header of all objects. */
|
||||
@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
|
||||
}
|
||||
/** Common runtime header of all objects. */
|
||||
@unmanaged export class Header {
|
||||
|
||||
// Note that header data and layout isn't quite optimal depending on which allocator one
|
||||
// decides to use, but it's done this way for maximum flexibility. Also remember that the
|
||||
// runtime will most likely change significantly once reftypes and WASM GC are a thing.
|
||||
/** Size of a runtime header. */
|
||||
@lazy @inline static readonly 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
|
||||
|
||||
/** Whether a GC is present or not. */
|
||||
@inline export const GC = isImplemented(gc.register);
|
||||
/** Magic value used to validate runtime headers. */
|
||||
@lazy @inline static readonly MAGIC: u32 = 0xA55E4B17;
|
||||
|
||||
/** Size of the common runtime header. */
|
||||
@inline export const HEADER_SIZE: usize = GC
|
||||
? (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
|
||||
|
||||
/** Magic value used to validate common runtime headers. */
|
||||
@inline export const HEADER_MAGIC: u32 = 0xA55E4B17;
|
||||
|
||||
/** Adjusts an allocation to actual block size. Primarily targets TLSF. */
|
||||
export function ADJUST(payloadSize: usize): usize {
|
||||
// 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. */
|
||||
@unsafe export function ALLOC_RAW(payloadSize: u32): usize {
|
||||
var header = changetype<HEADER>(memory.allocate(ADJUST(payloadSize)));
|
||||
header.classId = HEADER_MAGIC;
|
||||
header.payloadSize = payloadSize;
|
||||
if (GC) {
|
||||
header.gc1 = 0;
|
||||
header.gc2 = 0;
|
||||
/** 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
|
||||
}
|
||||
return changetype<usize>(header) + HEADER_SIZE;
|
||||
}
|
||||
|
||||
/** Allocates a new object and returns a pointer to its payload. Fills with zeroes.*/
|
||||
@unsafe export function ALLOC(payloadSize: u32): usize {
|
||||
var ref = ALLOC_RAW(payloadSize);
|
||||
memory.fill(ref, 0, payloadSize);
|
||||
return ref;
|
||||
}
|
||||
// Note that header data and layout isn't quite optimal depending on which allocator one
|
||||
// decides to use, but it's done this way for maximum flexibility. Also remember that the
|
||||
// runtime will most likely change significantly once reftypes and WASM GC are a thing.
|
||||
|
||||
/** Reallocates an object if necessary. Returns a pointer to its (moved) payload. */
|
||||
@unsafe export function REALLOC(ref: usize, newPayloadSize: u32): usize {
|
||||
// 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) {
|
||||
let newAdjustedSize = ADJUST(newPayloadSize);
|
||||
if (select(ADJUST(payloadSize), 0, ref > HEAP_BASE) < newAdjustedSize) {
|
||||
// move if the allocation isn't large enough or not a heap object
|
||||
let newHeader = changetype<HEADER>(memory.allocate(newAdjustedSize));
|
||||
newHeader.classId = HEADER_MAGIC;
|
||||
if (GC) {
|
||||
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) {
|
||||
// if previously registered, register again
|
||||
gc.register(ref);
|
||||
}
|
||||
header = newHeader;
|
||||
ref = newRef;
|
||||
} else {
|
||||
// otherwise just clear additional memory within this block
|
||||
memory.fill(ref + payloadSize, 0, newPayloadSize - payloadSize);
|
||||
/** Adjusts an allocation to actual block size. Primarily targets TLSF. */
|
||||
function adjust(payloadSize: usize): usize {
|
||||
// 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. */
|
||||
@unsafe export function allocRaw(payloadSize: u32): usize {
|
||||
var header = changetype<Header>(memory.allocate(adjust(payloadSize)));
|
||||
header.classId = Header.MAGIC;
|
||||
header.payloadSize = payloadSize;
|
||||
if (gc.implemented) {
|
||||
header.gc1 = 0;
|
||||
header.gc2 = 0;
|
||||
}
|
||||
} 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.
|
||||
return changetype<usize>(header) + Header.SIZE;
|
||||
}
|
||||
|
||||
/** Allocates a new object and returns a pointer to its payload. Fills with zeroes.*/
|
||||
@unsafe export function alloc(payloadSize: u32): usize {
|
||||
var ref = allocRaw(payloadSize);
|
||||
memory.fill(ref, 0, payloadSize);
|
||||
return ref;
|
||||
}
|
||||
|
||||
/** Reallocates an object if necessary. Returns a pointer to its (moved) payload. */
|
||||
@unsafe export function realloc(ref: usize, newPayloadSize: u32): usize {
|
||||
// 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) {
|
||||
let newAdjustedSize = adjust(newPayloadSize);
|
||||
if (select(adjust(payloadSize), 0, ref > HEAP_BASE) < newAdjustedSize) {
|
||||
// move if the allocation isn't large enough or not a heap object
|
||||
let newHeader = changetype<Header>(memory.allocate(newAdjustedSize));
|
||||
newHeader.classId = Header.MAGIC;
|
||||
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
|
||||
gc.register(ref);
|
||||
}
|
||||
header = newHeader;
|
||||
ref = newRef;
|
||||
} else {
|
||||
// otherwise just clear additional memory within this block
|
||||
memory.fill(ref + payloadSize, 0, newPayloadSize - payloadSize);
|
||||
}
|
||||
} 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.
|
||||
}
|
||||
header.payloadSize = newPayloadSize;
|
||||
return ref;
|
||||
}
|
||||
|
||||
function unref(ref: usize): Header {
|
||||
assert(ref >= HEAP_BASE + Header.SIZE); // must be a heap object
|
||||
var header = changetype<Header>(ref - Header.SIZE);
|
||||
assert(header.classId == Header.MAGIC); // must be unregistered
|
||||
return header;
|
||||
}
|
||||
|
||||
/** Frees an object. Must not have been registered with GC yet. */
|
||||
@unsafe @inline export function free<T>(ref: T): void {
|
||||
memory.free(changetype<usize>(unref(changetype<usize>(ref))));
|
||||
}
|
||||
|
||||
/** Registers a managed object. Cannot be free'd anymore afterwards. */
|
||||
@unsafe @inline export function register<T>(ref: usize): T {
|
||||
if (!isReference<T>()) ERROR("reference expected");
|
||||
// see comment in REALLOC why this is useful. also inline this because
|
||||
// it's generic so we don't get a bunch of functions.
|
||||
unref(ref).classId = gc.classId<T>();
|
||||
if (gc.implemented) gc.register(ref);
|
||||
return changetype<T>(ref);
|
||||
}
|
||||
|
||||
/** Links a managed object with its managed parent. */
|
||||
@unsafe @inline export function link<T, TParent>(ref: T, parentRef: TParent): void {
|
||||
assert(changetype<usize>(ref) >= HEAP_BASE + Header.SIZE); // must be a heap object
|
||||
var header = changetype<Header>(changetype<usize>(ref) - Header.SIZE);
|
||||
assert(header.classId != Header.MAGIC && header.gc1 != 0 && header.gc2 != 0); // must be registered
|
||||
if (gc.implemented) gc.link(changetype<usize>(ref), changetype<usize>(parentRef)); // tslint:disable-line
|
||||
}
|
||||
header.payloadSize = newPayloadSize;
|
||||
return ref;
|
||||
}
|
||||
|
||||
function unref(ref: usize): HEADER {
|
||||
assert(ref >= HEAP_BASE + HEADER_SIZE); // must be a heap object
|
||||
var header = changetype<HEADER>(ref - HEADER_SIZE);
|
||||
assert(header.classId == HEADER_MAGIC); // must be unregistered
|
||||
return header;
|
||||
}
|
||||
|
||||
/** Frees an object. Must not have been registered with GC yet. */
|
||||
@unsafe @inline export function FREE<T>(ref: T): void {
|
||||
memory.free(changetype<usize>(unref(changetype<usize>(ref))));
|
||||
}
|
||||
|
||||
/** Registers a managed object. Cannot be free'd anymore afterwards. */
|
||||
@unsafe @inline export function REGISTER<T>(ref: usize): T {
|
||||
if (!isReference<T>()) ERROR("reference expected");
|
||||
// see comment in REALLOC why this is useful. also inline this because
|
||||
// it's generic so we don't get a bunch of functions.
|
||||
unref(ref).classId = gc.classId<T>();
|
||||
if (GC) gc.register(ref);
|
||||
return changetype<T>(ref);
|
||||
}
|
||||
|
||||
/** Links a managed object with its managed parent. */
|
||||
@unsafe @inline export function LINK<T, TParent>(ref: T, parentRef: TParent): void {
|
||||
assert(changetype<usize>(ref) >= HEAP_BASE + HEADER_SIZE); // must be a heap object
|
||||
var header = changetype<HEADER>(changetype<usize>(ref) - HEADER_SIZE);
|
||||
assert(header.classId != HEADER_MAGIC && header.gc1 != 0 && header.gc2 != 0); // must be registered
|
||||
if (GC) gc.link(changetype<usize>(ref), changetype<usize>(parentRef)); // tslint:disable-line
|
||||
}
|
||||
import { ArrayBuffer } from "./arraybuffer";
|
||||
|
||||
export abstract class ArrayBufferView {
|
||||
@lazy static readonly MAX_BYTELENGTH: i32 = MAX_SIZE_32 - HEADER_SIZE;
|
||||
@lazy static readonly MAX_BYTELENGTH: i32 = MAX_SIZE_32 - runtime.Header.SIZE;
|
||||
|
||||
[key: number]: number;
|
||||
|
||||
@ -158,7 +162,7 @@ export abstract class ArrayBufferView {
|
||||
}
|
||||
|
||||
get length(): i32 {
|
||||
ERROR("concrete implementation must provide this");
|
||||
ERROR("missing implementation: [T extends ArrayBufferView]#length");
|
||||
return unreachable();
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { LINK } from "./runtime";
|
||||
import { runtime } from "./runtime";
|
||||
import { HASH } from "./util/hash";
|
||||
|
||||
// A deterministic hash set based on CloseTable from https://github.com/jorendorff/dht
|
||||
@ -98,7 +98,7 @@ export class Set<K> {
|
||||
let bucketPtrBase = changetype<usize>(this.buckets) + <usize>(hashCode & this.bucketsMask) * BUCKET_SIZE;
|
||||
entry.taggedNext = load<usize>(bucketPtrBase);
|
||||
store<usize>(bucketPtrBase, changetype<usize>(entry));
|
||||
if (isManaged<K>()) LINK(changetype<usize>(key), changetype<usize>(this)); // tslint:disable-line
|
||||
if (isManaged<K>()) runtime.link(changetype<usize>(key), changetype<usize>(this)); // tslint:disable-line
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,25 +1,25 @@
|
||||
import { HEADER, HEADER_SIZE, ALLOC, REGISTER, ArrayBufferView } from "./runtime";
|
||||
import { runtime } from "./runtime";
|
||||
import { MAX_SIZE_32 } from "./util/allocator";
|
||||
import { compareImpl, parse, CharCode, isWhiteSpaceOrLineTerminator } from "./util/string";
|
||||
|
||||
@sealed export abstract class String {
|
||||
@lazy static readonly MAX_LENGTH: i32 = (MAX_SIZE_32 - HEADER_SIZE) >> alignof<u16>();
|
||||
@lazy static readonly MAX_LENGTH: i32 = (MAX_SIZE_32 - runtime.Header.SIZE) >> alignof<u16>();
|
||||
|
||||
get length(): i32 {
|
||||
return changetype<HEADER>(changetype<usize>(this) - HEADER_SIZE).payloadSize >> 1;
|
||||
return changetype<runtime.Header>(changetype<usize>(this) - runtime.Header.SIZE).payloadSize >> 1;
|
||||
}
|
||||
|
||||
// TODO Add and handle second argument
|
||||
static fromCharCode(code: i32): String {
|
||||
var out = ALLOC(2);
|
||||
var out = runtime.allocRaw(2);
|
||||
store<u16>(out, <u16>code);
|
||||
return REGISTER<String>(out);
|
||||
return runtime.register<String>(out);
|
||||
}
|
||||
|
||||
static fromCodePoint(code: i32): String {
|
||||
assert(<u32>code <= 0x10FFFF);
|
||||
var sur = code > 0xFFFF;
|
||||
var out = ALLOC((<i32>sur + 1) << 1);
|
||||
var out = runtime.allocRaw((<i32>sur + 1) << 1);
|
||||
if (!sur) {
|
||||
store<u16>(out, <u16>code);
|
||||
} else {
|
||||
@ -28,15 +28,15 @@ import { compareImpl, parse, CharCode, isWhiteSpaceOrLineTerminator } from "./ut
|
||||
let lo: u32 = (code & 0x3FF) + 0xDC00;
|
||||
store<u32>(out, (hi << 16) | lo);
|
||||
}
|
||||
return REGISTER<String>(out);
|
||||
return runtime.register<String>(out);
|
||||
}
|
||||
|
||||
@operator("[]") charAt(pos: i32): String {
|
||||
assert(this !== null);
|
||||
if (<u32>pos >= <u32>this.length) return changetype<String>("");
|
||||
var out = ALLOC(2);
|
||||
var out = runtime.allocRaw(2);
|
||||
store<u16>(out, load<u16>(changetype<usize>(this) + (<usize>pos << 1)));
|
||||
return REGISTER<String>(out);
|
||||
return runtime.register<String>(out);
|
||||
}
|
||||
|
||||
charCodeAt(pos: i32): i32 {
|
||||
@ -67,10 +67,10 @@ import { compareImpl, parse, CharCode, isWhiteSpaceOrLineTerminator } from "./ut
|
||||
var otherSize: isize = other.length << 1;
|
||||
var outSize: usize = thisSize + otherSize;
|
||||
if (outSize == 0) return changetype<String>("");
|
||||
var out = ALLOC(outSize);
|
||||
var out = runtime.allocRaw(outSize);
|
||||
memory.copy(out, changetype<usize>(this), thisSize);
|
||||
memory.copy(out + thisSize, changetype<usize>(other), otherSize);
|
||||
return REGISTER<String>(out);
|
||||
return runtime.register<String>(out);
|
||||
}
|
||||
|
||||
endsWith(searchString: String, endPosition: i32 = String.MAX_LENGTH): bool {
|
||||
@ -173,9 +173,9 @@ import { compareImpl, parse, CharCode, isWhiteSpaceOrLineTerminator } from "./ut
|
||||
if (intStart < 0) intStart = max(size + intStart, 0);
|
||||
var resultLength = min(max(end, 0), size - intStart);
|
||||
if (resultLength <= 0) return changetype<String>("");
|
||||
var out = ALLOC(resultLength << 1);
|
||||
var out = runtime.allocRaw(resultLength << 1);
|
||||
memory.copy(out, changetype<usize>(this) + intStart, resultLength);
|
||||
return REGISTER<String>(out);
|
||||
return runtime.register<String>(out);
|
||||
}
|
||||
|
||||
substring(start: i32, end: i32 = i32.MAX_VALUE): String {
|
||||
@ -188,9 +188,9 @@ import { compareImpl, parse, CharCode, isWhiteSpaceOrLineTerminator } from "./ut
|
||||
len = toPos - fromPos;
|
||||
if (!len) return changetype<String>("");
|
||||
if (!fromPos && toPos == this.length << 1) return this;
|
||||
var out = ALLOC(len);
|
||||
var out = runtime.allocRaw(len);
|
||||
memory.copy(out, changetype<usize>(this) + fromPos, len);
|
||||
return REGISTER<String>(out);
|
||||
return runtime.register<String>(out);
|
||||
}
|
||||
|
||||
trim(): String {
|
||||
@ -216,9 +216,9 @@ import { compareImpl, parse, CharCode, isWhiteSpaceOrLineTerminator } from "./ut
|
||||
}
|
||||
if (!size) return changetype<String>("");
|
||||
if (!start && size == length << 1) return this;
|
||||
var out = ALLOC(size);
|
||||
var out = runtime.allocRaw(size);
|
||||
memory.copy(out, changetype<usize>(this) + offset, size);
|
||||
return REGISTER<String>(out);
|
||||
return runtime.register<String>(out);
|
||||
}
|
||||
|
||||
@inline
|
||||
@ -246,9 +246,9 @@ import { compareImpl, parse, CharCode, isWhiteSpaceOrLineTerminator } from "./ut
|
||||
if (!offset) return this;
|
||||
size -= offset;
|
||||
if (!size) return changetype<String>("");
|
||||
var out = ALLOC(size);
|
||||
var out = runtime.allocRaw(size);
|
||||
memory.copy(out, changetype<usize>(this) + offset, size);
|
||||
return REGISTER<String>(out);
|
||||
return runtime.register<String>(out);
|
||||
}
|
||||
|
||||
trimEnd(): String {
|
||||
@ -265,9 +265,9 @@ import { compareImpl, parse, CharCode, isWhiteSpaceOrLineTerminator } from "./ut
|
||||
}
|
||||
if (!size) return changetype<String>("");
|
||||
if (size == originalSize) return this;
|
||||
var out = ALLOC(size);
|
||||
var out = runtime.allocRaw(size);
|
||||
memory.copy(out, changetype<usize>(this), size);
|
||||
return REGISTER<String>(out);
|
||||
return runtime.register<String>(out);
|
||||
}
|
||||
|
||||
padStart(targetLength: i32, padString: string = " "): String {
|
||||
@ -277,7 +277,7 @@ import { compareImpl, parse, CharCode, isWhiteSpaceOrLineTerminator } from "./ut
|
||||
var padSize = <usize>padString.length << 1;
|
||||
if (targetSize < thisSize || !padSize) return this;
|
||||
var prependSize = targetSize - thisSize;
|
||||
var out = ALLOC(targetSize);
|
||||
var out = runtime.allocRaw(targetSize);
|
||||
if (prependSize > padSize) {
|
||||
let repeatCount = (prependSize - 2) / padSize;
|
||||
let restBase = repeatCount * padSize;
|
||||
@ -288,7 +288,7 @@ import { compareImpl, parse, CharCode, isWhiteSpaceOrLineTerminator } from "./ut
|
||||
memory.copy(out, changetype<usize>(padString), prependSize);
|
||||
}
|
||||
memory.copy(out + prependSize, changetype<usize>(this), thisSize);
|
||||
return REGISTER<String>(out);
|
||||
return runtime.register<String>(out);
|
||||
}
|
||||
|
||||
padEnd(targetLength: i32, padString: string = " "): String {
|
||||
@ -298,7 +298,7 @@ import { compareImpl, parse, CharCode, isWhiteSpaceOrLineTerminator } from "./ut
|
||||
var padSize = <usize>padString.length << 1;
|
||||
if (targetSize < thisSize || !padSize) return this;
|
||||
var appendSize = targetSize - thisSize;
|
||||
var out = ALLOC(targetSize);
|
||||
var out = runtime.allocRaw(targetSize);
|
||||
memory.copy(out, changetype<usize>(this), thisSize);
|
||||
if (appendSize > padSize) {
|
||||
let repeatCount = (appendSize - 2) / padSize;
|
||||
@ -309,7 +309,7 @@ import { compareImpl, parse, CharCode, isWhiteSpaceOrLineTerminator } from "./ut
|
||||
} else {
|
||||
memory.copy(out + thisSize, changetype<usize>(padString), appendSize);
|
||||
}
|
||||
return REGISTER<String>(out);
|
||||
return runtime.register<String>(out);
|
||||
}
|
||||
|
||||
repeat(count: i32 = 0): String {
|
||||
@ -323,9 +323,9 @@ import { compareImpl, parse, CharCode, isWhiteSpaceOrLineTerminator } from "./ut
|
||||
|
||||
if (count == 0 || !length) return changetype<String>("");
|
||||
if (count == 1) return this;
|
||||
var out = ALLOC((length * count) << 1);
|
||||
var out = runtime.allocRaw((length * count) << 1);
|
||||
memory.repeat(out, changetype<usize>(this), <usize>length << 1, count);
|
||||
return REGISTER<String>(out);
|
||||
return runtime.register<String>(out);
|
||||
}
|
||||
|
||||
slice(beginIndex: i32, endIndex: i32 = i32.MAX_VALUE): String {
|
||||
@ -334,9 +334,9 @@ import { compareImpl, parse, CharCode, isWhiteSpaceOrLineTerminator } from "./ut
|
||||
var end = endIndex < 0 ? max(endIndex + len, 0) : min(endIndex, len);
|
||||
len = end - begin;
|
||||
if (len <= 0) return changetype<String>("");
|
||||
var out = ALLOC(len << 1);
|
||||
var out = runtime.allocRaw(len << 1);
|
||||
memory.copy(out, changetype<usize>(this) + (<usize>begin << 1), <usize>len << 1);
|
||||
return REGISTER<String>(out);
|
||||
return runtime.register<String>(out);
|
||||
}
|
||||
|
||||
split(separator: String | null = null, limit: i32 = i32.MAX_VALUE): String[] {
|
||||
@ -354,7 +354,7 @@ import { compareImpl, parse, CharCode, isWhiteSpaceOrLineTerminator } from "./ut
|
||||
let buffer = unreachable(); // TODO
|
||||
// let buffer = <ArrayBuffer>result.buffer_;
|
||||
for (let i: isize = 0; i < length; ++i) {
|
||||
let char = ALLOC(2);
|
||||
let char = runtime.allocRaw(2);
|
||||
store<u16>(
|
||||
changetype<usize>(char),
|
||||
load<u16>(
|
||||
@ -374,9 +374,9 @@ import { compareImpl, parse, CharCode, isWhiteSpaceOrLineTerminator } from "./ut
|
||||
while ((end = this.indexOf(separator!, start)) != -1) {
|
||||
let len = end - start;
|
||||
if (len > 0) {
|
||||
let out = ALLOC(<usize>len << 1);
|
||||
let out = runtime.allocRaw(<usize>len << 1);
|
||||
memory.copy(out, changetype<usize>(this) + (<usize>start << 1), <usize>len << 1);
|
||||
result.push(REGISTER<String>(out));
|
||||
result.push(runtime.register<String>(out));
|
||||
} else {
|
||||
result.push(changetype<String>(""));
|
||||
}
|
||||
@ -390,9 +390,9 @@ import { compareImpl, parse, CharCode, isWhiteSpaceOrLineTerminator } from "./ut
|
||||
}
|
||||
var len = length - start;
|
||||
if (len > 0) {
|
||||
let out = ALLOC(<usize>len << 1);
|
||||
let out = runtime.allocRaw(<usize>len << 1);
|
||||
memory.copy(out, changetype<usize>(this) + (<usize>start << 1), <usize>len << 1);
|
||||
result.push(REGISTER<String>(out));
|
||||
result.push(runtime.register<String>(out));
|
||||
} else {
|
||||
result.push(changetype<String>(""));
|
||||
}
|
||||
@ -464,10 +464,10 @@ import { compareImpl, parse, CharCode, isWhiteSpaceOrLineTerminator } from "./ut
|
||||
}
|
||||
}
|
||||
assert(ptrPos == len);
|
||||
var out = ALLOC(bufPos);
|
||||
var out = runtime.allocRaw(bufPos);
|
||||
memory.copy(changetype<usize>(out), buf, bufPos);
|
||||
memory.free(buf);
|
||||
return REGISTER<string>(out);
|
||||
return runtime.register<string>(out);
|
||||
}
|
||||
|
||||
toUTF8(): usize {
|
||||
|
@ -1,16 +1,14 @@
|
||||
export namespace table {
|
||||
|
||||
// export function copy(dst: u32, src: u32, n: u32): void {
|
||||
// __table_copy(dst, src, n);
|
||||
// }
|
||||
export function copy(dst: u32, src: u32, n: u32): void {
|
||||
ERROR("not implemented: table.copy");
|
||||
}
|
||||
|
||||
// Passive elements
|
||||
export function init(elementIndex: u32, srcOffset: u32, dstOffset: u32, n: u32): void {
|
||||
ERROR("not implemented: table.init");
|
||||
}
|
||||
|
||||
// export function init(elementIndex: u32, srcOffset: u32, dstOffset: u32, n: u32): void {
|
||||
// __table_init(elementIndex, srcOffset, dstOffset, n);
|
||||
// }
|
||||
|
||||
// export function drop(elementIndex: u32): void {
|
||||
// __table_drop(elementIndex);
|
||||
// }
|
||||
export function drop(elementIndex: u32): void {
|
||||
ERROR("not implemented: table.drop");
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { ALLOC, REGISTER, LINK, ArrayBufferView } from "./runtime";
|
||||
import { runtime, ArrayBufferView } from "./runtime";
|
||||
import { COMPARATOR, SORT as SORT_IMPL } from "./util/sort";
|
||||
|
||||
function clampToByte(value: i32): i32 {
|
||||
@ -720,11 +720,11 @@ export class Float64Array extends ArrayBufferView {
|
||||
else begin = min(begin, length);
|
||||
if (end < 0) end = max(length + end, begin);
|
||||
else end = max(min(end, length), begin);
|
||||
var out = ALLOC(offsetof<TArray>());
|
||||
var out = runtime.allocRaw(offsetof<TArray>());
|
||||
store<usize>(out, buffer, offsetof<TArray>("buffer"));
|
||||
store<usize>(out, array.dataStart + (<usize>begin << alignof<T>()) , offsetof<TArray>("dataStart"));
|
||||
store<usize>(out, array.dataEnd + (<usize>(end - begin) << alignof<T>()), offsetof<TArray>("dataEnd"));
|
||||
LINK(buffer, REGISTER<TArray>(out)); // register first, then link
|
||||
runtime.link(buffer, runtime.register<TArray>(out)); // register first, then link
|
||||
return changetype<TArray>(out);
|
||||
}
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { ALLOC, REGISTER, FREE } from "../runtime";
|
||||
import { runtime } from "../runtime";
|
||||
import { CharCode } from "./string";
|
||||
|
||||
@inline export const MAX_DOUBLE_LENGTH = 28;
|
||||
@ -254,10 +254,10 @@ export function utoa32(value: u32): String {
|
||||
if (!value) return "0";
|
||||
|
||||
var decimals = decimalCount32(value);
|
||||
var out = ALLOC(decimals << 1);
|
||||
var out = runtime.alloc(decimals << 1);
|
||||
|
||||
utoa32_core(changetype<usize>(out), value, decimals);
|
||||
return REGISTER<String>(out);
|
||||
return runtime.register<String>(out);
|
||||
}
|
||||
|
||||
export function itoa32(value: i32): String {
|
||||
@ -267,12 +267,12 @@ export function itoa32(value: i32): String {
|
||||
if (sign) value = -value;
|
||||
|
||||
var decimals = decimalCount32(value) + <u32>sign;
|
||||
var out = ALLOC(decimals << 1);
|
||||
var out = runtime.alloc(decimals << 1);
|
||||
|
||||
utoa32_core(changetype<usize>(out), value, decimals);
|
||||
if (sign) store<u16>(changetype<usize>(out), CharCode.MINUS);
|
||||
|
||||
return REGISTER<String>(out);
|
||||
return runtime.register<String>(out);
|
||||
}
|
||||
|
||||
export function utoa64(value: u64): String {
|
||||
@ -282,14 +282,14 @@ export function utoa64(value: u64): String {
|
||||
if (value <= u32.MAX_VALUE) {
|
||||
let val32 = <u32>value;
|
||||
let decimals = decimalCount32(val32);
|
||||
out = ALLOC(decimals << 1);
|
||||
out = runtime.alloc(decimals << 1);
|
||||
utoa32_core(out, val32, decimals);
|
||||
} else {
|
||||
let decimals = decimalCount64(value);
|
||||
out = ALLOC(decimals << 1);
|
||||
out = runtime.alloc(decimals << 1);
|
||||
utoa64_core(changetype<usize>(out), value, decimals);
|
||||
}
|
||||
return REGISTER<String>(out);
|
||||
return runtime.register<String>(out);
|
||||
}
|
||||
|
||||
export function itoa64(value: i64): String {
|
||||
@ -302,16 +302,16 @@ export function itoa64(value: i64): String {
|
||||
if (<u64>value <= <u64>u32.MAX_VALUE) {
|
||||
let val32 = <u32>value;
|
||||
let decimals = decimalCount32(val32) + <u32>sign;
|
||||
out = ALLOC(decimals << 1);
|
||||
out = runtime.alloc(decimals << 1);
|
||||
utoa32_core(changetype<usize>(out), val32, decimals);
|
||||
} else {
|
||||
let decimals = decimalCount64(value) + <u32>sign;
|
||||
out = ALLOC(decimals << 1);
|
||||
out = runtime.alloc(decimals << 1);
|
||||
utoa64_core(changetype<usize>(out), value, decimals);
|
||||
}
|
||||
if (sign) store<u16>(changetype<usize>(out), CharCode.MINUS);
|
||||
|
||||
return REGISTER<String>(out);
|
||||
return runtime.register<String>(out);
|
||||
}
|
||||
|
||||
export function itoa<T>(value: T): String {
|
||||
@ -594,10 +594,10 @@ export function dtoa(value: f64): String {
|
||||
if (isNaN(value)) return "NaN";
|
||||
return select<String>("-Infinity", "Infinity", value < 0);
|
||||
}
|
||||
var temp = ALLOC(MAX_DOUBLE_LENGTH << 1);
|
||||
var temp = runtime.alloc(MAX_DOUBLE_LENGTH << 1);
|
||||
var length = dtoa_core(temp, value);
|
||||
var result = changetype<String>(temp).substring(0, length);
|
||||
FREE(temp);
|
||||
runtime.free(temp);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user