Skip to content

Commit

Permalink
Initial implementation if ugc, see #16; Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodeIO committed Jan 18, 2018
1 parent 461daab commit 9cdfa35
Show file tree
Hide file tree
Showing 24 changed files with 715 additions and 173 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ A few early examples to get an idea:
A PSON decoder implemented in AssemblyScript.

* **[TLSF memory allocator](./examples/tlsf)**<br />
An early port of TLSF to AssemblyScript.
An port of TLSF to AssemblyScript.

* **[μgc garbage collector](./examples/ugc)**<br />
An port of μgc to AssemblyScript.

Or browse the [compiler tests](./tests/compiler) for a more in-depth overview of what's supported already. One of them is a [showcase](./tests/compiler/showcase.ts).

Expand Down
4 changes: 2 additions & 2 deletions examples/tlsf/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
TLSF
====
TLSF memory allocator
=====================

A port of [Matt Conte's implementation](https://github.com/mattconte/tlsf) of the [TLSF](http://www.gii.upv.es/tlsf/) memory allocator to AssemblyScript.

Expand Down
2 changes: 1 addition & 1 deletion examples/tlsf/assembly/LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
tlsf.ts is based on:
tlsf.ts is based on https://github.com/mattconte/tlsf

Two Level Segregated Fit memory allocator, version 3.1.
Written by Matthew Conte
Expand Down
16 changes: 8 additions & 8 deletions examples/tlsf/assembly/tlsf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class BlockHeader {
static readonly OVERHEAD: usize = sizeof<usize>();

// User data starts directly after the size field in a used block.
static readonly USERDATA_OFFSET: usize = sizeof<usize>() + sizeof<usize>();
static readonly DATA_OFFSET: usize = sizeof<usize>() + sizeof<usize>();

// A free block must be large enough to store its header minus the size of
// the prev_phys_block field, and no larger than the number of addressable
Expand Down Expand Up @@ -114,12 +114,12 @@ class BlockHeader {

/** Gets the block header matching the specified data pointer. */
static fromDataPtr(ptr: usize): BlockHeader {
return changetype<BlockHeader>(ptr - BlockHeader.USERDATA_OFFSET);
return changetype<BlockHeader>(ptr - BlockHeader.DATA_OFFSET);
}

/** Returns the address of this block's data. */
toDataPtr(): usize {
return changetype<usize>(this) + BlockHeader.USERDATA_OFFSET;
return changetype<usize>(this) + BlockHeader.DATA_OFFSET;
}

/** Gets the next block after this one using the specified size. */
Expand Down Expand Up @@ -174,7 +174,7 @@ class BlockHeader {
return this.size >= BlockHeader.SIZE + size;
}

/* Splits a block into two, the second of which is free. */
/** Splits a block into two, the second of which is free. */
split(size: usize): BlockHeader {
// Calculate the amount of space left in the remaining block.
var remain = BlockHeader.fromOffset(
Expand All @@ -194,7 +194,7 @@ class BlockHeader {
return remain;
}

/* Absorb a free block's storage into this (adjacent previous) free block. */
/** Absorb a free block's storage into this (adjacent previous) free block. */
absorb(block: BlockHeader): void {
assert(!this.isLast,
"previous block can't be last"
Expand All @@ -205,7 +205,7 @@ class BlockHeader {
}
}

/* The TLSF control structure. */
/** The TLSF control structure. */
@explicit
class Control extends BlockHeader { // Empty lists point here, indicating free

Expand Down Expand Up @@ -289,7 +289,7 @@ class Control extends BlockHeader { // Empty lists point here, indicating free
this.insertFreeBlock(block, fl_out, sl_out);
}

/* Inserts a free block into the free block list. */
/** Inserts a free block into the free block list. */
insertFreeBlock(block: BlockHeader, fl: i32, sl: i32): void {
var current = this.blocks(fl, sl);
assert(current,
Expand All @@ -311,7 +311,7 @@ class Control extends BlockHeader { // Empty lists point here, indicating free
this.sl_bitmap_set(fl, this.sl_bitmap(fl) | (1 << sl))
}

/* Removes a free block from the free list.*/
/** Removes a free block from the free list.*/
removeFreeBlock(block: BlockHeader, fl: i32, sl: i32): void {
var prev = block.prev_free;
var next = block.next_free;
Expand Down
20 changes: 20 additions & 0 deletions examples/ugc/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
μgc garbage collector
=====================

A port of [Bach Le's μgc garbage collector library](https://github.com/bullno1/ugc) to AssemblyScript.

Instructions
------------

To build [assembly/ugc.ts](./assembly/ugc.ts) to an untouched and an optimized `.wasm` including their respective `.wast` representations, run:

```
$> npm run build
```

Afterwards, to run the included [test](./tests/index.js):

```
$> npm install
$> npm test
```
25 changes: 25 additions & 0 deletions examples/ugc/assembly/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
ugc.ts is based on https://github.com/bullno1/ugc

Copyright (c) 2017, Bach Le
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
6 changes: 6 additions & 0 deletions examples/ugc/assembly/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "../../../std/assembly.json",
"include": [
"./**/*.ts"
]
}
Loading

0 comments on commit 9cdfa35

Please sign in to comment.