calloc
v1.0.0
Published
C-style memory allocation simulation with ArrayBuffer support
Maintainers
Readme
calloc
C-style memory allocation simulation for JavaScript with ArrayBuffer support.
Installation
npm install callocFeatures
C-style memory allocation: malloc(), calloc(), realloc()
Multiple data types: bytes, words, dwords (signed/unsigned)
ArrayBuffer backed: Real memory simulation
Zero-initialization: calloc() behavior
Flexible initialization: Numbers, strings, arrays
Memory operations: Copy, resize, fill, inspect
Quick Start
const { malloc, calloc } = require('calloc');
// Allocate 10 unsigned bytes
let mem1 = malloc('byte', 10);
// Allocate 5 signed words zero-initialized
let mem2 = calloc('sw', 5);
// Default type (unsigned bytes)
let mem3 = malloc(10); // 10 bytes
// Initialize with data
let mem4 = calloc('b', 8, [1, 2, 3, 4]);
let mem5 = malloc('sb', 16, "Hello"); // C-stringAPI Reference
Allocation Functions
malloc(type, quantity, initialData?)
Allocates memory, and you should not except it's always zero.
calloc(type, quantity, initialData?)
Allocates zero-initialized memory.
Type Aliases
| Full Name | Aliases | Size | Description |
|----------------|---------|---------|---------------------|
| byte | b | 1 byte | Unsigned 8-bit |
| signed_byte | sb | 1 byte | Signed 8-bit |
| word | w | 2 bytes | Unsigned 16-bit |
| signed_word | sw | 2 bytes | Signed 16-bit |
| dword | d | 4 bytes | Unsigned 32-bit |
| signed_dword | sd | 4 bytes | Signed 32-bit |
Initialization Data
undefined: Unknown values (malloc) or zeros (calloc)Number: Set first elementString: Copy as ASCII (null-terminated)Array: Copy array valuesArrayBuffer/TypedArray: Copy raw bytes
Memory Object Methods
.get(index) - Get value at index
.set(index, value) - Set value at index
.fill(value) - Fill with value
.slice(start, end) - Create slice
.resize(newSize) - Resize memory
.copy() - Create copy
.toArray() - Convert to array
.toHex() - Get hex representation (each byte in hex string)
.toString() - Decode as string
.free() - Free memory
.inspect() - Get memory info
Examples
const { malloc, calloc, memcpy } = require('calloc');
// Basic allocation
let arr = calloc('d', 4, [10, 20, 30, 40]);
console.log(arr.toArray()); // [10, 20, 30, 40]
// String initialization
let str = calloc('b', 16, "Hello World!");
console.log(str.toString()); // "Hello World!"
// Memory operations
let copy = arr.copy();
arr.fill(0);
memcpy(arr, copy, 4);
// Inspection
console.log(arr.inspect());C Compatibility
The package simulates these C functions:
malloc()- Memory allocationcalloc()- Cleared allocationrealloc()- Re-allocationmemcpy()- Memory copymemset()- Memory set
License
MIT
Disclaimer
This package and all memory allocation packages simulates C-style memory allocation, but does not is a perfect allocation. It uses JavaScript's methods which can have overhead. Move your project to C/C++ if you really need perfect memory allocation.
Made with love 💖 for JavaScript community since low-level programming should never forgotten.
