summaryrefslogtreecommitdiff
path: root/node_modules/capnp-ts/src/serialization/arena/single-segment-arena.ts
diff options
context:
space:
mode:
authorakiyamn2023-09-24 23:22:21 +1000
committerakiyamn2023-09-24 23:22:21 +1000
commit4e87195739f2a5d9a05451b48773c8afdc680765 (patch)
tree9cba501844a4a11dcbdffc4050ed8189561c55ed /node_modules/capnp-ts/src/serialization/arena/single-segment-arena.ts
downloadprice-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/single-segment-arena.ts')
-rw-r--r--node_modules/capnp-ts/src/serialization/arena/single-segment-arena.ts67
1 files changed, 67 insertions, 0 deletions
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;
+}