知识共享许可协议
本作品采用知识共享署名-非商业性使用 3.0 未本地化版本许可协议进行许可。

Node.js v0.10.18 手册 & 文档


Smalloc#

稳定度: 1 - 实验性

smalloc.alloc(length[, receiver][, type])#

  • length Number <= smalloc.kMaxLength
  • receiver Object, Optional, Default: new Object
  • type Enum, Optional, Default: Uint8

  • length Number <= smalloc.kMaxLength

  • receiver Object 可选,缺省为 new Object
  • type Enum 可选,缺省为 Uint8

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

返回 receiver 及所分配的外部数组数据。如果未传入 receiver 则会创建并返回一个新的 Object。

Buffers are backed by a simple allocator that only handles the assignation of external raw memory. Smalloc exposes that functionality.

Buffer 后端为一个只处理外部原始内存的分配的简易分配器所支撑。Smalloc 暴露了该功能。

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).

这可用于创建你自己的类似 Buffer 的类。由于不会设置其它属性,因此使用者需要自行跟踪其它所需信息(比如所分配的长度 length)。

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.

它只检查 receiver 是否为一个非 Array 的 Object。因此,可以分配外部数组数据的不止纯 Object。

// { [Function allocMe] '0': 0, '1': 0, '2': 0 }

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

V8 不支持向一个 Array 分配外部数组数据,如果这么做将会抛出异常。

It's possible is to specify the type of external array data you would like. All possible options are listed in smalloc.Types. Example usage:

您可以指定您想要的外部数组数据的类型。所有可取的值都已在 smalloc.Types 中列出。使用示例:

// { '0': 0, '1': 0.1, '2': 0.2 }

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

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

  • source 分配了外部数组的来源对象

  • sourceStart 从这个位置开始拷贝
  • dest 分配了外部数组的目标对象
  • destStart 拷贝到这个位置
  • copyLength 拷贝的长度

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

从一个外部数组向另一个拷贝内存。所有参数都是必填,否则将会抛出异常。

// { '0': 4, '1': 6, '2': 2, '3': 3 }

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

copyOnto 会在内部自动检测分配的长度,因此无需对此给出额外的参数。

smalloc.dispose(obj)#

  • obj Object

  • obj 对象

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

释放已使用 smalloc.alloc 分配到一个对象的内存。

// {}

This is useful to reduce strain on the garbage collector, but developers must be careful. Cryptic errors may arise in applications that are difficult to trace.

这对于减轻垃圾回收器的负担有所帮助,但开发者务必小心。难以跟踪的应用程序可能会发生奇怪的错误。

// 将导致:
// Error: source has no external array data

dispose() does not support Buffers, and will throw if passed.

dispose() 不支持 Buffer,传入将会抛出异常。

smalloc.kMaxLength#

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

最大的分配大小。该值同时也适用于 Buffer 的创建。

smalloc.Types#

Enum of possible external array types. Contains:

外部数组类型的可取值,包含:

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