2019-03-27 17:21:52 +01:00
|
|
|
/// <reference path="./allocator/index.d.ts" />
|
|
|
|
|
2019-03-13 22:35:47 +01:00
|
|
|
import { memcmp, memmove, memset } from "./util/memory";
|
2019-03-28 11:07:47 +01:00
|
|
|
import { E_NOTIMPLEMENTED } from "./util/error";
|
2019-03-13 22:35:47 +01:00
|
|
|
|
2019-03-14 05:11:03 +01:00
|
|
|
// @ts-ignore: decorator
|
2019-03-14 06:09:49 +01:00
|
|
|
@builtin
|
|
|
|
export declare const HEAP_BASE: usize;
|
2019-03-14 04:33:58 +01:00
|
|
|
|
2019-03-13 22:35:47 +01:00
|
|
|
/** Memory manager interface. */
|
|
|
|
export namespace memory {
|
|
|
|
|
2019-03-28 11:07:47 +01:00
|
|
|
/** Whether the memory managed interface is implemented. */
|
|
|
|
// @ts-ignore: decorator
|
|
|
|
@lazy
|
|
|
|
export const implemented: bool = isDefined(__mem_allocate);
|
|
|
|
|
2019-03-13 22:35:47 +01:00
|
|
|
/** Gets the size of the memory in pages. */
|
2019-03-14 05:11:03 +01:00
|
|
|
// @ts-ignore: decorator
|
2019-03-14 06:09:49 +01:00
|
|
|
@builtin
|
|
|
|
export declare function size(): i32;
|
2019-03-13 22:35:47 +01:00
|
|
|
|
|
|
|
/** Grows the memory by the given size in pages and returns the previous size in pages. */
|
2019-03-14 05:11:03 +01:00
|
|
|
// @ts-ignore: decorator
|
2019-03-14 06:09:49 +01:00
|
|
|
@unsafe @builtin
|
|
|
|
export declare function grow(pages: i32): i32;
|
2019-03-13 22:35:47 +01:00
|
|
|
|
|
|
|
/** Fills a section in memory with the specified byte value. */
|
2019-03-14 05:11:03 +01:00
|
|
|
// @ts-ignore: decorator
|
2019-03-14 06:09:49 +01:00
|
|
|
@unsafe @builtin
|
|
|
|
export function fill(dst: usize, c: u8, n: usize): void {
|
2019-03-13 22:35:47 +01:00
|
|
|
memset(dst, c, n); // fallback if "bulk-memory" isn't enabled
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Copies a section of memory to another. Has move semantics. */
|
2019-03-14 05:11:03 +01:00
|
|
|
// @ts-ignore: decorator
|
2019-03-14 06:09:49 +01:00
|
|
|
@unsafe @builtin
|
|
|
|
export function copy(dst: usize, src: usize, n: usize): void {
|
2019-03-13 22:35:47 +01:00
|
|
|
memmove(dst, src, n); // fallback if "bulk-memory" isn't enabled
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Initializes a memory segment. */
|
2019-03-14 05:11:03 +01:00
|
|
|
// @ts-ignore: decorator
|
2019-03-14 06:09:49 +01:00
|
|
|
@unsafe
|
|
|
|
export function init(segmentIndex: u32, srcOffset: usize, dstOffset: usize, n: usize): void {
|
2019-03-28 11:07:47 +01:00
|
|
|
throw new Error(E_NOTIMPLEMENTED);
|
2019-03-13 22:35:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Drops a memory segment. */
|
2019-03-14 05:11:03 +01:00
|
|
|
// @ts-ignore: decorator
|
2019-03-14 06:09:49 +01:00
|
|
|
@unsafe
|
|
|
|
export function drop(segmentIndex: u32): void {
|
2019-03-28 11:07:47 +01:00
|
|
|
throw new Error(E_NOTIMPLEMENTED);
|
2019-03-13 22:35:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Dynamically allocates a section of memory and returns its address. */
|
2019-03-14 05:11:03 +01:00
|
|
|
// @ts-ignore: decorator
|
2019-03-14 06:09:49 +01:00
|
|
|
@unsafe
|
|
|
|
export function allocate(size: usize): usize {
|
2019-03-27 17:21:52 +01:00
|
|
|
if (isDefined(__mem_allocate)) return __mem_allocate(size);
|
2019-03-28 11:07:47 +01:00
|
|
|
else throw new Error(E_NOTIMPLEMENTED);
|
2019-03-13 22:35:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Dynamically frees a section of memory by the previously allocated address. */
|
2019-03-14 05:11:03 +01:00
|
|
|
// @ts-ignore: decorator
|
2019-03-14 06:09:49 +01:00
|
|
|
@unsafe
|
|
|
|
export function free(ptr: usize): void {
|
2019-03-27 17:21:52 +01:00
|
|
|
if (isDefined(__mem_free)) __mem_free(ptr);
|
2019-03-28 11:07:47 +01:00
|
|
|
else throw new Error(E_NOTIMPLEMENTED);
|
2019-03-13 22:35:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Resets the memory to its initial state. Arena allocator only. */
|
2019-03-14 05:11:03 +01:00
|
|
|
// @ts-ignore: decorator
|
2019-03-14 06:09:49 +01:00
|
|
|
@unsafe
|
|
|
|
export function reset(): void {
|
2019-03-27 17:21:52 +01:00
|
|
|
if (isDefined(__mem_reset)) __mem_reset();
|
2019-03-28 11:07:47 +01:00
|
|
|
else throw new Error(E_NOTIMPLEMENTED);
|
2019-03-13 22:35:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Repeats a section of memory at a specific address. */
|
2019-03-14 05:11:03 +01:00
|
|
|
// @ts-ignore: decorator
|
2019-03-14 06:09:49 +01:00
|
|
|
@unsafe
|
|
|
|
export function repeat(dst: usize, src: usize, srcLength: usize, count: usize): void {
|
2019-03-13 22:35:47 +01:00
|
|
|
var index: usize = 0;
|
|
|
|
var total = srcLength * count;
|
|
|
|
while (index < total) {
|
|
|
|
memory.copy(dst + index, src, srcLength);
|
|
|
|
index += srcLength;
|
|
|
|
}
|
|
|
|
}
|
2019-03-14 04:33:58 +01:00
|
|
|
|
|
|
|
/** Compares a section of memory to another. */
|
2019-03-14 05:11:03 +01:00
|
|
|
// @ts-ignore: decorator
|
2019-03-14 06:09:49 +01:00
|
|
|
@inline
|
|
|
|
export function compare(vl: usize, vr: usize, n: usize): i32 {
|
2019-03-14 04:33:58 +01:00
|
|
|
return memcmp(vl, vr, n);
|
|
|
|
}
|
2019-03-13 22:35:47 +01:00
|
|
|
}
|