Smalloc

    • {Number} <= smalloc.kMaxLength
    • receiver {Object} Default: new Object
    • type {Enum} Default: Uint8

    Returns receiver with allocated external array data. If no receiver is
    passed then a new Object will be created and returned.

    This can be used to create your own Buffer-like classes. No other properties are
    set, so the user will need to keep track of other necessary information (e.g.
    length of the allocation).

    1. function SimpleData(n) {
    2. this.length = n;
    3. smalloc.alloc(this.length, this);
    4. }
    5. SimpleData.prototype = { /* ... */ };

    It only checks if the receiver is an Object, and also not an Array. Because of
    this it is possible to allocate external array data to more than a plain Object.

    v8 does not support allocating external array data to an Array, and if passed
    will throw.

    1. var doubleArr = smalloc.alloc(3, smalloc.Types.Double);
    2. doubleArr = i / 10;
    3. // { '0': 0, '1': 0.1, '2': 0.2 }

    It is not possible to freeze, seal and prevent extensions of objects with
    external data using Object.freeze, Object.seal and
    Object.preventExtensions respectively.

    smalloc.copyOnto(source, sourceStart, dest, destStart, copyLength);

    • source {Object} with external array allocation
    • sourceStart {Number} Position to begin copying from
    • dest {Object} with external array allocation
    • destStart {Number} Position to begin copying onto
    • copyLength {Number} Length of copy

    Copy memory from one external array allocation to another. No arguments are
    optional, and any violation will throw.

    copyOnto automatically detects the length of the allocation internally, so no
    need to set any additional properties for this to work.

    • obj Object

    Free memory that has been allocated to an object via smalloc.alloc.

    1. var a = {};
    2. // { '0': 0, '1': 0, '2': 0 }
    3. smalloc.dispose(a);
    4. // {}

    After dispose() is called object still behaves as one with external data, for
    example smalloc.hasExternalData() returns true.
    dispose() does not support Buffers, and will throw if passed.

    smalloc.hasExternalData(obj)

    • obj {Object}

    Returns true if the obj has externally allocated memory.

    Size of maximum allocation. This is also applicable to Buffer creation.

    smalloc.Types

    Enum of possible external array types. Contains:

    • Int8
    • Uint8
    • Int16
    • Uint16
    • Int32
    • Uint32
    • Float