MCPcopy Create free account
hub / github.com/chronoxor/FastBinaryEncoding / allocate

Method allocate

projects/JavaScript/proto/fbe.js:240–260  ·  view source on GitHub ↗

* Allocate memory in the current write buffer and return offset to the allocated memory block * @this {!WriteBuffer} * @param {!number} size Allocation size * @returns {!number} Allocated memory offset

(size)

Source from the content-addressed store, hash-verified

238 * @returns {!number} Allocated memory offset
239 */
240 allocate (size) {
241 if (size < 0) {
242 throw new Error('Invalid allocation size!')
243 }
244
245 let offset = this._size
246
247 // Calculate a new buffer size
248 let total = this._size + size
249
250 if (total <= this._buffer.length) {
251 this._size = total
252 return offset
253 }
254
255 let data = new Uint8Array(Math.max(total, 2 * this._buffer.length))
256 data.set(this._buffer)
257 this._buffer = data
258 this._size = total
259 return offset
260 }
261
262 /**
263 * Remove some memory of the given size from the current write buffer

Callers

nothing calls this directly

Calls 1

setMethod · 0.45

Tested by

no test coverage detected