MCPcopy Create free account
hub / github.com/W4G1/multithreading / alloc

Method alloc

src/lib/json_buffer.ts:190–211  ·  view source on GitHub ↗
(byteSize: number, retry = true)

Source from the content-addressed store, hash-verified

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

Callers 6

writeValueMethod · 0.95
allocObjectMethod · 0.95
allocArrayMethod · 0.95
objectSetMethod · 0.95
arrayEnsureCapacityMethod · 0.95
arraySetMethod · 0.95

Calls 1

collectGarbageMethod · 0.95

Tested by

no test coverage detected