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