File size: 1,589 Bytes
5641073
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
 * @author jdiaz5513
 */

import initTrace from "debug";
import { assertNever } from "../../errors";
import { Segment } from "../segment";
import { AnyArena } from "./any-arena";
import { ArenaAllocationResult } from "./arena-allocation-result";
import { ArenaKind } from "./arena-kind";
import { MultiSegmentArena } from "./multi-segment-arena";
import { SingleSegmentArena } from "./single-segment-arena";

const trace = initTrace("capnp:arena");
trace("load");

export abstract class Arena {
  static readonly allocate = allocate;
  static readonly getBuffer = getBuffer;
  static readonly getNumSegments = getNumSegments;
}

export function allocate(minSize: number, segments: Segment[], a: AnyArena): ArenaAllocationResult {
  switch (a.kind) {
    case ArenaKind.MULTI_SEGMENT:
      return MultiSegmentArena.allocate(minSize, a);

    case ArenaKind.SINGLE_SEGMENT:
      return SingleSegmentArena.allocate(minSize, segments, a);

    default:
      return assertNever(a);
  }
}

export function getBuffer(id: number, a: AnyArena): ArrayBuffer {
  switch (a.kind) {
    case ArenaKind.MULTI_SEGMENT:
      return MultiSegmentArena.getBuffer(id, a);

    case ArenaKind.SINGLE_SEGMENT:
      return SingleSegmentArena.getBuffer(id, a);

    default:
      return assertNever(a);
  }
}

export function getNumSegments(a: AnyArena): number {
  switch (a.kind) {
    case ArenaKind.MULTI_SEGMENT:
      return MultiSegmentArena.getNumSegments(a);

    case ArenaKind.SINGLE_SEGMENT:
      return SingleSegmentArena.getNumSegments();

    default:
      return assertNever(a);
  }
}