mirror of
https://github.com/fluencelabs/assemblyscript
synced 2025-06-22 19:21:47 +00:00
unify mem/ref interface
This commit is contained in:
21
std/assembly/allocator/README.md
Normal file
21
std/assembly/allocator/README.md
Normal file
@ -0,0 +1,21 @@
|
||||
Memory manager interface
|
||||
========================
|
||||
|
||||
A memory manager for AssemblyScript must implement the following common and may implement any number of optional interfaces:
|
||||
|
||||
Common
|
||||
------
|
||||
|
||||
* **__mem_allocate**(size: `usize`): `usize`<br />
|
||||
Dynamically allocates a chunk of memory of at least the specified size and returns its address.
|
||||
Alignment must be guaranteed to be at least 8 bytes, but there are considerations to increase
|
||||
alignment to 16 bytes to fit SIMD v128 values.
|
||||
|
||||
* **__mem_free**(ref: `usize`): `void`<br />
|
||||
Frees a dynamically allocated chunk of memory by its address.
|
||||
|
||||
Optional
|
||||
--------
|
||||
|
||||
* **__mem_reset**(ref: `usize`, parentRef: `usize`)<br />
|
||||
Resets dynamic memory to its initial state. Used by the arena allocator.
|
@ -10,8 +10,8 @@ var startOffset: usize = (HEAP_BASE + AL_MASK) & ~AL_MASK;
|
||||
var offset: usize = startOffset;
|
||||
|
||||
// @ts-ignore: decorator
|
||||
@unsafe @global @inline
|
||||
function __memory_allocate(size: usize): usize {
|
||||
@unsafe @global
|
||||
function __mem_allocate(size: usize): usize {
|
||||
if (size > MAX_SIZE_32) unreachable();
|
||||
var ptr = offset;
|
||||
var newPtr = (ptr + max<usize>(size, 1) + AL_MASK) & ~AL_MASK;
|
||||
@ -30,12 +30,12 @@ function __memory_allocate(size: usize): usize {
|
||||
}
|
||||
|
||||
// @ts-ignore: decorator
|
||||
@unsafe @global @inline
|
||||
function __memory_free(ptr: usize): void {
|
||||
@unsafe @global
|
||||
function __mem_free(ptr: usize): void {
|
||||
}
|
||||
|
||||
// @ts-ignore: decorator
|
||||
@unsafe @global @inline
|
||||
function __memory_reset(): void {
|
||||
@unsafe @global
|
||||
function __mem_reset(): void {
|
||||
offset = startOffset;
|
||||
}
|
||||
|
@ -7,13 +7,13 @@ declare function _malloc(size: usize): usize;
|
||||
declare function _free(ptr: usize): void;
|
||||
|
||||
// @ts-ignore: decorator
|
||||
@unsafe @global @inline
|
||||
function __memory_allocate(size: usize): usize {
|
||||
@unsafe @global
|
||||
function __mem_allocate(size: usize): usize {
|
||||
return _malloc(size);
|
||||
}
|
||||
|
||||
// @ts-ignore: decorator
|
||||
@unsafe @global @inline
|
||||
function __memory_free(ptr: usize): void {
|
||||
@unsafe @global
|
||||
function __mem_free(ptr: usize): void {
|
||||
_free(ptr);
|
||||
}
|
||||
|
3
std/assembly/allocator/index.d.ts
vendored
Normal file
3
std/assembly/allocator/index.d.ts
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
declare function __mem_allocate(size: usize): usize;
|
||||
declare function __mem_free(ref: usize): void;
|
||||
declare function __mem_reset(): void;
|
@ -7,13 +7,13 @@ declare function malloc(size: usize): usize;
|
||||
declare function free(ptr: usize): void;
|
||||
|
||||
// @ts-ignore: decorator
|
||||
@unsafe @global @inline
|
||||
function __memory_allocate(size: usize): usize {
|
||||
@unsafe @global
|
||||
function __mem_allocate(size: usize): usize {
|
||||
return malloc(size);
|
||||
}
|
||||
|
||||
// @ts-ignore: decorator
|
||||
@unsafe @global @inline
|
||||
function __memory_free(ptr: usize): void {
|
||||
@unsafe @global
|
||||
function __mem_free(ptr: usize): void {
|
||||
free(ptr);
|
||||
}
|
||||
|
@ -428,8 +428,8 @@ var ROOT: Root = changetype<Root>(0);
|
||||
|
||||
/** Allocates a chunk of memory. */
|
||||
// @ts-ignore: decorator
|
||||
@unsafe @global @inline
|
||||
function __memory_allocate(size: usize): usize {
|
||||
@unsafe @global
|
||||
function __mem_allocate(size: usize): usize {
|
||||
// initialize if necessary
|
||||
var root = ROOT;
|
||||
if (!root) {
|
||||
@ -478,8 +478,8 @@ function __memory_allocate(size: usize): usize {
|
||||
|
||||
/** Frees the chunk of memory at the specified address. */
|
||||
// @ts-ignore: decorator
|
||||
@unsafe @global @inline
|
||||
function __memory_free(data: usize): void {
|
||||
@unsafe @global
|
||||
function __mem_free(data: usize): void {
|
||||
if (data) {
|
||||
let root = ROOT;
|
||||
if (root) {
|
||||
|
Reference in New Issue
Block a user