May 10, 2021 Node.js
稳定性: 1 - 试验
Represents a cache that can be supported by a simple memory allocator that handles the allocation of extended raw memory, as shown by the functions available to Malloc:
length
{Number}
<= smalloc.kMaxLength
receiver
default:
new Object
type
(Enum) Default:
Uint8
The function serves to return a receiver object that contains allocated
receiver
data.
If no receiver
receiver
in, a new object is created and returned.
This can be used to create your own buffer-like classes. No other properties are set, so consumers need to keep track of other required information, such as the length of the assignment.
function SimpleData(n) {
this.length = n;
smalloc.alloc(this.length, this);
}
SimpleData.prototype = { /* ... */ };
Check only if
receiver
is a non-array object.
Therefore, extended data data can be assigned, not just ordinary objects.
function allocMe() { }
smalloc.alloc(3, allocMe);
// { [Function allocMe] '0': 0, '1': 0, '2': 0 }
v8 does not support assigning extended array objects to arrays, and if you do so, it is thrown.
You can specify the type of external array data in
smalloc.Types
the types of external array data available, such as:
var doubleArr = smalloc.alloc(3, smalloc.Types.Double);
for (var i = 0; i < 3; i++)
doubleArr = i / 10;
// { '0': 0, '1': 0.1, '2': 0.2 }
With
Object.freeze
Object.seal
Object.preventExtensions
freeze, block, or prevent objects from using extended data extensions.
source
of the external array is assigned to the source
sourceStart
responsible for the starting position
dest
external array is assigned by the dest .Object
destStart
copied to the target's starting position
copyLength
Copying memory from an external array allocation to another array is optional, or an exception is thrown.
var a = smalloc.alloc(4);
var b = smalloc.alloc(4);
for (var i = 0; i < 4; i++) {
a[i] = i;
b[i] = i * 2;
}
// { '0': 0, '1': 1, '2': 2, '3': 3 }
// { '0': 0, '1': 2, '2': 4, '3': 6 }
smalloc.copyOnto(b, 2, a, 0, 2);
// { '0': 4, '1': 6, '2': 2, '3': 3 }
copyOnto
detects the length of the internal allocation, so no additional parameters need to be set.
obj
Object
Frees up the memory allocated to the object through
smalloc.alloc
var a = {};
smalloc.alloc(3, a);
// { '0': 0, '1': 0, '2': 0 }
smalloc.dispose(a);
// {}
This is effective in reducing the burden on garbage collectors, but be careful when developing programs that can be difficult to track.
var a = smalloc.alloc(4);
var b = smalloc.alloc(4);
// perform this somewhere along the line
smalloc.dispose(b);
// now trying to copy some data out
smalloc.copyOnto(b, 2, a, 0, 2);
// now results in:
// RangeError: copy_length > source_length
Call
dispose()
the object still has external data,
smalloc.hasExternalData()
true
dispose()
support caching and will be thrown if it is passed in.
obj
{Object}
If
obj
externally allocated memory, true
true
The maximum number that can be assigned. The same applies to buffer creation.
The type of external array that contains:
Int8
Uint8
Int16
Uint16
Int32
Uint32
Float
Double
Uint8Clamped