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 = [ ] ) { this . buffers = buffers ; trace ( "new %s" , this ) ; } toString ( ) { return format ( "MultiSegmentArena_segments:%d" , getNumSegments ( this ) ) ; } } export function allocate ( minSize , m ) { 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 , m ) { 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 ) { return m . buffers . length ; }