95 lines
2.8 KiB
TypeScript
Raw Normal View History

2019-03-13 22:35:47 +01:00
import { memcmp, memmove, memset } from "./util/memory";
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 {
/** 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-13 22:35:47 +01:00
ERROR("not implemented");
}
/** 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-13 22:35:47 +01:00
ERROR("not implemented");
}
/** 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 {
// @ts-ignore: stub
2019-03-14 04:33:58 +01:00
if (isDefined(__memory_allocate)) return __memory_allocate(size);
2019-03-15 13:13:48 +01:00
else WARNING("missing implementation: memory.allocate");
2019-03-13 22:35:47 +01:00
return <usize>unreachable();
}
/** 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 {
// @ts-ignore: stub
2019-03-14 04:33:58 +01:00
if (isDefined(__memory_free)) __memory_free(ptr);
2019-03-15 13:13:48 +01:00
else WARNING("missing implementation: memory.free");
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 {
// @ts-ignore: stub
2019-03-14 04:33:58 +01:00
if (isDefined(__memory_reset)) __memory_reset();
2019-03-15 13:13:48 +01:00
else WARNING("missing implementation: memory.reset");
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
}