Coder Social home page Coder Social logo

dreamtworld / cmem-wasm Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 26 KB

Single-page C memory management in WebAssembly

License: BSD 2-Clause "Simplified" License

Shell 3.02% HTML 33.12% WebAssembly 63.87%
malloc calloc free heap libc lightweight memcpy memmove memset realloc

cmem-wasm's Introduction

cmem-wasm

This library implements libc memory allocation functions in WebAssembly. The implementation assumes a single page of memory, that is, a 64 KiB buffer.

Available libc functions

  • malloc
  • calloc
  • free
  • realloc
  • memset
  • memcpy
  • memmove

Usage

Initialize memory with cmem_init(start), where start is the 16 bit address of the first byte of the memory region to use. Proceed to use memory allocation functions as usual. Exceeding available memory will trap.

Additional functionality

cmem_end() returns a past-the-end pointer to the last buffer in memory, effectively retrieving the current length of memory. Intended use is backing up memory states at minimal size.

Build

build.sh runs the source code through the C language preprocessor, then translates it with wat2wasm from the wabt software package.

The result is an object file that can be linked using wasm-ld, as well as a WebAssembly module.

The script also produces an optimized module using wasm-opt -O3 from the binaryen package.

Build options

  • EXPORT_ALL exports the memory and functions other than cmem_init and cmem_end, including memory and list management functions.
  • PREFIX_LIBC prepends cmem_ to libc function names.
  • FIRST_FIT employs a first-fit instead of a best-fit strategy for allocation.
  • BULK_MEMORY enables use of bulk memory instructions, decreasing embedder compatibility.

Build dependencies

Implementation

Best-fit allocation. Doubly linked list to navigate buffers. 16-bit addresses constrict memory length to 64 KiB.

cmem-wasm's People

Contributors

llucinat avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.