From 4e87195739f2a5d9a05451b48773c8afdc680765 Mon Sep 17 00:00:00 2001 From: akiyamn Date: Sun, 24 Sep 2023 23:22:21 +1000 Subject: Initial commit (by create-cloudflare CLI) --- .../serialization/arena/single-segment-arena.ts | 67 ++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 node_modules/capnp-ts/src/serialization/arena/single-segment-arena.ts (limited to 'node_modules/capnp-ts/src/serialization/arena/single-segment-arena.ts') diff --git a/node_modules/capnp-ts/src/serialization/arena/single-segment-arena.ts b/node_modules/capnp-ts/src/serialization/arena/single-segment-arena.ts new file mode 100644 index 0000000..250568c --- /dev/null +++ b/node_modules/capnp-ts/src/serialization/arena/single-segment-arena.ts @@ -0,0 +1,67 @@ +/** + * @author jdiaz5513 + */ + +import initTrace from "debug"; +import { DEFAULT_BUFFER_SIZE, MIN_SINGLE_SEGMENT_GROWTH } from "../../constants"; +import { SEG_GET_NON_ZERO_SINGLE, SEG_NOT_WORD_ALIGNED } from "../../errors"; +import { format, padToWord } from "../../util"; +import { Segment } from "../segment"; +import { ArenaAllocationResult } from "./arena-allocation-result"; +import { ArenaKind } from "./arena-kind"; + +const trace = initTrace("capnp:arena:single"); +trace("load"); + +export class SingleSegmentArena { + static readonly allocate = allocate; + static readonly getBuffer = getBuffer; + static readonly getNumSegments = getNumSegments; + + buffer: ArrayBuffer; + readonly kind = ArenaKind.SINGLE_SEGMENT; + + constructor(buffer = new ArrayBuffer(DEFAULT_BUFFER_SIZE)) { + if ((buffer.byteLength & 7) !== 0) { + throw new Error(format(SEG_NOT_WORD_ALIGNED, buffer.byteLength)); + } + + this.buffer = buffer; + + trace("new %s", this); + } + + toString(): string { + return format("SingleSegmentArena_len:%x", this.buffer.byteLength); + } +} + +export function allocate(minSize: number, segments: Segment[], s: SingleSegmentArena): ArenaAllocationResult { + trace("Allocating %x bytes for segment 0 in %s.", minSize, s); + + const srcBuffer = segments.length > 0 ? segments[0].buffer : s.buffer; + + if (minSize < MIN_SINGLE_SEGMENT_GROWTH) { + minSize = MIN_SINGLE_SEGMENT_GROWTH; + } else { + minSize = padToWord(minSize); + } + + s.buffer = new ArrayBuffer(srcBuffer.byteLength + minSize); + + // PERF: Assume that the source and destination buffers are word-aligned and use Float64Array to copy them one word + // at a time. + new Float64Array(s.buffer).set(new Float64Array(srcBuffer)); + + return new ArenaAllocationResult(0, s.buffer); +} + +export function getBuffer(id: number, s: SingleSegmentArena): ArrayBuffer { + if (id !== 0) throw new Error(format(SEG_GET_NON_ZERO_SINGLE, id)); + + return s.buffer; +} + +export function getNumSegments(): number { + return 1; +} -- cgit v1.2.3