https://github.com/codemix/malloc
Simple malloc() & free() implementation for node.js, built on top of array buffers.
https://github.com/codemix/malloc
Last synced: 8 months ago
JSON representation
Simple malloc() & free() implementation for node.js, built on top of array buffers.
- Host: GitHub
- URL: https://github.com/codemix/malloc
- Owner: codemix
- License: mit
- Created: 2015-11-16T23:02:54.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2016-02-10T23:47:06.000Z (about 10 years ago)
- Last Synced: 2025-06-23T18:50:39.244Z (9 months ago)
- Language: JavaScript
- Size: 40 KB
- Stars: 25
- Watchers: 4
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# malloc
A skip-list based memory allocator built on top of typed arrays (and node buffers).
[](https://travis-ci.org/codemix/malloc)
## What?
It lets you allocate a large, contiguous slab of memory up front and then `alloc()` and `free()` within that buffer.
It is mostly useful in conjunction with things like [mmap.js](https://github.com/indutny/mmap.js).
It's developed using [design by contract](https://github.com/codemix/babel-plugin-contracts), so you might find the library's own code style a bit unusual, but it doesn't affect usage.
## Installation
Install via [npm](https://npmjs.org/package/malloc).
## Usage
```js
import Allocator from "malloc";
const heap = new Buffer(1024 * 1024);
const allocator = new Allocator(heap); // heap could also be an ArrayBuffer
console.log(allocator.inspect());
const input = "Hello World";
const offset = allocator.alloc(Buffer.byteLength(input));
heap.write(input, offset);
console.log(allocator.inspect());
console.log(allocator.sizeOf(offset));
console.log('freed', allocator.free(offset), 'bytes');
```
## License
Published by [codemix](http://codemix.com/) under a permissive MIT License, see [LICENSE.md](./LICENSE.md).