malloc-append

0.0.1 • Public • Published

malloc-append

An append-only memory allocator with the same interface as malloc, useful for implementing append only logs. Has no free() but does not enforce write-once.

Build Status

What?

It lets you allocate a large, contiguous slab of memory up front and then alloc() within that buffer.

It is mostly useful in conjunction with things like mmap.js.

It's developed using design by contract, so you might find the library's own code style a bit unusual, but it doesn't affect usage or performance.

Installation

Install via npm.

Usage

import Allocator from "malloc-append";
 
const heap = new Buffer(1024 * 1024);
const allocator = new Allocator(heap); // heap could also be an ArrayBuffer
console.log(allocator.inspect());
 
let firstAddress = 0;
let lastAddress = 0;
 
for (let i = 0; i < 100; i++) {
  const address = allocator.alloc(64);
  if (firstAddress === 0) {
    firstAddress = address;
  }
  if (lastAddress !== 0) {
    // do something with the address
    heap.writeUInt32LE(address, lastAddress);
  }
}
 
let address = firstAddress;
for (let i = 0; i < 100; i++) {
  console.log('Reading address', address);
  address = heap.readUInt32LE(address);
}
 

License

Published by codemix under a permissive MIT License, see LICENSE.md.

Dependencies (0)

    Dev Dependencies (14)

    Package Sidebar

    Install

    npm i malloc-append

    Weekly Downloads

    3

    Version

    0.0.1

    License

    MIT

    Last publish

    Collaborators

    • charlespick