Skip to content

Commit 3a2f273

Browse files
committed
buffer: use smalloc as backing data store
Memory allocations are now done through smalloc. The Buffer cc class has been removed completely, but for backwards compatibility have left the namespace as Buffer. The .parent attribute is only set if the Buffer is a slice of an allocation. Which is then set to the alloc object (not a Buffer). The .offset attribute is now a ReadOnly set to 0, for backwards compatibility. I'd like to remove it in the future (pre v1.0). A few alterations have been made to how arguments are either coerced or thrown. All primitives will now be coerced to their respective values, and (most) all out of range index requests will throw. The indexes that are coerced were left for backwards compatibility. For example: Buffer slice operates more like Array slice, and coerces instead of throwing out of range indexes. This may change in the future. The reason for wanting to throw for out of range indexes is because giving js access to raw memory has high potential risk. To mitigate that it's easier to make sure the developer is always quickly alerted to the fact that their code is attempting to access beyond memory bounds. Because SlowBuffer will be deprecated, and simply returns a new Buffer instance, all tests on SlowBuffer have been removed. Heapdumps will now show usage under "smalloc" instead of "Buffer". ParseArrayIndex was added to node_internals to support proper uint argument checking/coercion for external array data indexes. SlabAllocator had to be updated since handle_ no longer exists.
1 parent 252cdfa commit 3a2f273

20 files changed

+695
-1286
lines changed

doc/api/buffer.markdown

Lines changed: 65 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,69 @@ Allocates a new buffer containing the given `str`.
8080
Returns true if the `encoding` is a valid encoding argument, or false
8181
otherwise.
8282

83+
### Class Method: Buffer.isBuffer(obj)
84+
85+
* `obj` Object
86+
* Return: Boolean
87+
88+
Tests if `obj` is a `Buffer`.
89+
90+
### Class Method: Buffer.byteLength(string, [encoding])
91+
92+
* `string` String
93+
* `encoding` String, Optional, Default: 'utf8'
94+
* Return: Number
95+
96+
Gives the actual byte length of a string. `encoding` defaults to `'utf8'`.
97+
This is not the same as `String.prototype.length` since that returns the
98+
number of *characters* in a string.
99+
100+
Example:
101+
102+
str = '\u00bd + \u00bc = \u00be';
103+
104+
console.log(str + ": " + str.length + " characters, " +
105+
Buffer.byteLength(str, 'utf8') + " bytes");
106+
107+
// ½ + ¼ = ¾: 9 characters, 12 bytes
108+
109+
### Class Method: Buffer.concat(list, [totalLength])
110+
111+
* `list` {Array} List of Buffer objects to concat
112+
* `totalLength` {Number} Total length of the buffers when concatenated
113+
114+
Returns a buffer which is the result of concatenating all the buffers in
115+
the list together.
116+
117+
If the list has no items, or if the totalLength is 0, then it returns a
118+
zero-length buffer.
119+
120+
If the list has exactly one item, then the first item of the list is
121+
returned.
122+
123+
If the list has more than one item, then a new Buffer is created.
124+
125+
If totalLength is not provided, it is read from the buffers in the list.
126+
However, this adds an additional loop to the function, so it is faster
127+
to provide the length explicitly.
128+
129+
### buf.length
130+
131+
* Number
132+
133+
The size of the buffer in bytes. Note that this is not necessarily the size
134+
of the contents. `length` refers to the amount of memory allocated for the
135+
buffer object. It does not change when the contents of the buffer are changed.
136+
137+
buf = new Buffer(1234);
138+
139+
console.log(buf.length);
140+
buf.write("some string", 0, "ascii");
141+
console.log(buf.length);
142+
143+
// 1234
144+
// 1234
145+
83146
### buf.write(string, [offset], [length], [encoding])
84147

85148
* `string` String - data to be written to buffer
@@ -155,69 +218,6 @@ Example: copy an ASCII string into a buffer, one byte at a time:
155218

156219
// node.js
157220

158-
### Class Method: Buffer.isBuffer(obj)
159-
160-
* `obj` Object
161-
* Return: Boolean
162-
163-
Tests if `obj` is a `Buffer`.
164-
165-
### Class Method: Buffer.byteLength(string, [encoding])
166-
167-
* `string` String
168-
* `encoding` String, Optional, Default: 'utf8'
169-
* Return: Number
170-
171-
Gives the actual byte length of a string. `encoding` defaults to `'utf8'`.
172-
This is not the same as `String.prototype.length` since that returns the
173-
number of *characters* in a string.
174-
175-
Example:
176-
177-
str = '\u00bd + \u00bc = \u00be';
178-
179-
console.log(str + ": " + str.length + " characters, " +
180-
Buffer.byteLength(str, 'utf8') + " bytes");
181-
182-
// ½ + ¼ = ¾: 9 characters, 12 bytes
183-
184-
### Class Method: Buffer.concat(list, [totalLength])
185-
186-
* `list` {Array} List of Buffer objects to concat
187-
* `totalLength` {Number} Total length of the buffers when concatenated
188-
189-
Returns a buffer which is the result of concatenating all the buffers in
190-
the list together.
191-
192-
If the list has no items, or if the totalLength is 0, then it returns a
193-
zero-length buffer.
194-
195-
If the list has exactly one item, then the first item of the list is
196-
returned.
197-
198-
If the list has more than one item, then a new Buffer is created.
199-
200-
If totalLength is not provided, it is read from the buffers in the list.
201-
However, this adds an additional loop to the function, so it is faster
202-
to provide the length explicitly.
203-
204-
### buf.length
205-
206-
* Number
207-
208-
The size of the buffer in bytes. Note that this is not necessarily the size
209-
of the contents. `length` refers to the amount of memory allocated for the
210-
buffer object. It does not change when the contents of the buffer are changed.
211-
212-
buf = new Buffer(1234);
213-
214-
console.log(buf.length);
215-
buf.write("some string", 0, "ascii");
216-
console.log(buf.length);
217-
218-
// 1234
219-
// 1234
220-
221221
### buf.copy(targetBuffer, [targetStart], [sourceStart], [sourceEnd])
222222

223223
* `targetBuffer` Buffer object - Buffer to copy into
@@ -694,11 +694,8 @@ Note that this is a property on the buffer module returned by
694694

695695
## Class: SlowBuffer
696696

697-
This class is primarily for internal use. JavaScript programs should
698-
use Buffer instead of using SlowBuffer.
697+
Deprecated. SlowBuffer now returns an instance of Buffer.
699698

700699
In order to avoid the overhead of allocating many C++ Buffer objects for
701700
small blocks of memory in the lifetime of a server, Node allocates memory
702-
in 8Kb (8192 byte) chunks. If a buffer is smaller than this size, then it
703-
will be backed by a parent SlowBuffer object. If it is larger than this,
704-
then Node will allocate a SlowBuffer slab for it directly.
701+
in 8Kb (8192 byte) chunks. This is now handled by Smalloc.

0 commit comments

Comments
 (0)