(byteSize: number, retry = true)
| 188 | } |
| 189 | |
| 190 | private alloc(byteSize: number, retry = true): number { |
| 191 | const idx = OFFSET_FREE_PTR >> 2; |
| 192 | const currentPtr = Atomics.load(this.u32, idx); |
| 193 | |
| 194 | // Align to 8 bytes for Float64 performance |
| 195 | const nextPtr = currentPtr + byteSize; |
| 196 | const alignedNext = (nextPtr + 7) & ~7; |
| 197 | |
| 198 | if (alignedNext > this.buffer.byteLength) { |
| 199 | if (retry) { |
| 200 | this.collectGarbage(); |
| 201 | this.propertyHints.clear(); |
| 202 | return this.alloc(byteSize, false); |
| 203 | } |
| 204 | throw new Error( |
| 205 | `SharedJsonBuffer OOM: Used ${alignedNext} of ${this.buffer.byteLength}`, |
| 206 | ); |
| 207 | } |
| 208 | |
| 209 | Atomics.store(this.u32, idx, alignedNext); |
| 210 | return currentPtr; |
| 211 | } |
| 212 | |
| 213 | private collectGarbage() { |
| 214 | const tempBuffer = new ArrayBuffer(this.buffer.byteLength); |
no test coverage detected