https://github.com/fabricio-p/as-malloc
A memory allocator written for AssemblyScript
https://github.com/fabricio-p/as-malloc
allocator assemblyscript memory wasm webassembly
Last synced: 5 months ago
JSON representation
A memory allocator written for AssemblyScript
- Host: GitHub
- URL: https://github.com/fabricio-p/as-malloc
- Owner: fabricio-p
- Created: 2021-05-09T12:34:26.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2021-08-11T16:26:18.000Z (over 3 years ago)
- Last Synced: 2024-11-09T17:02:23.616Z (5 months ago)
- Topics: allocator, assemblyscript, memory, wasm, webassembly
- Language: TypeScript
- Homepage:
- Size: 13.7 KB
- Stars: 24
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- awesome-assemblyscript - fabriciopashajas-malloc - Lightweight implementation of `malloc`, `realloc`, and `free` functions (Packages)
README
# as-malloc
This library provides a ligtweight implementation of the holy trinity of
dynamic memory allocation: `malloc`, `realloc`, `free`.
## Usage
If you have never heard of the functions above, you probably lived in a cave.
You just import the functions:
```ts
import {malloc, realloc, free} from "as-malloc";
```
Declare some unmanaged class (for easier memory access:
```ts
@unmanaged
class Foo {
bar: i32;
baz: f64;
}
```
Allocate the memory for that class:
```ts
const foo = changetype(malloc(offsetof()));
```
And then do whatever you want with it:
```ts
foo.bar = 35433;
foo.baz = 45.89117345;
```
## Things to remember
- as-malloc uses only one page of WebAssembly memory (64kB). This may be
changed on the future versions
- If you are using any managed runtime, you should compile your program with
`--memoryBase 65536`.