diff options
| author | akiyamn | 2023-09-24 23:22:21 +1000 |
|---|---|---|
| committer | akiyamn | 2023-09-24 23:22:21 +1000 |
| commit | 4e87195739f2a5d9a05451b48773c8afdc680765 (patch) | |
| tree | 9cba501844a4a11dcbdffc4050ed8189561c55ed /node_modules/capnp-ts/src/serialization/arena/multi-segment-arena.ts | |
| download | price-tracker-worker-4e87195739f2a5d9a05451b48773c8afdc680765.tar.gz price-tracker-worker-4e87195739f2a5d9a05451b48773c8afdc680765.zip | |
Initial commit (by create-cloudflare CLI)
Diffstat (limited to 'node_modules/capnp-ts/src/serialization/arena/multi-segment-arena.ts')
| -rw-r--r-- | node_modules/capnp-ts/src/serialization/arena/multi-segment-arena.ts | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/node_modules/capnp-ts/src/serialization/arena/multi-segment-arena.ts b/node_modules/capnp-ts/src/serialization/arena/multi-segment-arena.ts new file mode 100644 index 0000000..7a53e1b --- /dev/null +++ b/node_modules/capnp-ts/src/serialization/arena/multi-segment-arena.ts @@ -0,0 +1,51 @@ +/** + * @author jdiaz5513 + */ + +import initTrace from "debug"; +import { DEFAULT_BUFFER_SIZE } from "../../constants"; +import { SEG_ID_OUT_OF_BOUNDS } from "../../errors"; +import { padToWord, format } from "../../util"; +import { ArenaAllocationResult } from "./arena-allocation-result"; +import { ArenaKind } from "./arena-kind"; + +const trace = initTrace("capnp:arena:multi"); +trace("load"); + +export class MultiSegmentArena { + static readonly allocate = allocate; + static readonly getBuffer = getBuffer; + static readonly getNumSegments = getNumSegments; + + readonly buffers: ArrayBuffer[]; + readonly kind = ArenaKind.MULTI_SEGMENT; + + constructor(buffers: ArrayBuffer[] = []) { + this.buffers = buffers; + + trace("new %s", this); + } + + toString(): string { + return format("MultiSegmentArena_segments:%d", getNumSegments(this)); + } +} + +export function allocate(minSize: number, m: MultiSegmentArena): ArenaAllocationResult { + const b = new ArrayBuffer(padToWord(Math.max(minSize, DEFAULT_BUFFER_SIZE))); + m.buffers.push(b); + + return new ArenaAllocationResult(m.buffers.length - 1, b); +} + +export function getBuffer(id: number, m: MultiSegmentArena): ArrayBuffer { + if (id < 0 || id >= m.buffers.length) { + throw new Error(format(SEG_ID_OUT_OF_BOUNDS, id)); + } + + return m.buffers[id]; +} + +export function getNumSegments(m: MultiSegmentArena): number { + return m.buffers.length; +} |
