UNPKG

@arcgis/core

Version:

ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API

6 lines (5 loc) 2.16 kB
/* All material copyright ESRI, All Rights Reserved, unless otherwise specified. See https://js.arcgis.com/4.32/esri/copyright.txt for details. */ import has from"../../../../core/has.js";import{assertIsSome as t}from"../../../../core/maybe.js";import{FreeList as e}from"./cpuMapped/FreeList.js";const r=has("esri-2d-log-allocations");class s{static create(t,e){const r=e.acquireUint32Array(t);return new s(r,e)}constructor(t,e){this._array=t,this._pool=e}get array(){return this._array}get length(){return this._array.length}getUint32View(t,e){return new Uint32Array(this._array.buffer,t+this._array.byteOffset,e)}expand(t){if(t<=this._array.byteLength)return;const e=this._pool.acquireUint32Array(t);e.set(this._array),this._pool.releaseUint32Array(this._array),this._array=e}destroy(){this._pool.releaseUint32Array(this._array)}}class a{constructor(){this._data=new ArrayBuffer(a.BYTE_LENGTH),this._freeList=new e({start:0,end:this._data.byteLength})}static get BYTE_LENGTH(){return 16e6}get buffer(){return this._data}acquireUint32Array(t){const e=this._freeList.firstFit(t);return null==e?null:new Uint32Array(this._data,e,t/Uint32Array.BYTES_PER_ELEMENT)}releaseUint32Array(t){this._freeList.free(t.byteOffset,t.byteLength)}}class i{constructor(){this._pages=[],this._pagesByBuffer=new Map,this._bytesAllocated=0}destroy(){this._pages=[],this._pagesByBuffer=null}get _bytesTotal(){return this._pages.length*a.BYTE_LENGTH}acquireUint32Array(e){if(this._bytesAllocated+=e,r&&console.log(`Allocating ${e}, (${this._bytesAllocated} / ${this._bytesTotal})`),e>=a.BYTE_LENGTH)return new Uint32Array(e/Uint32Array.BYTES_PER_ELEMENT);for(const t of this._pages){const r=t.acquireUint32Array(e);if(null!=r)return r}const s=this._addPage().acquireUint32Array(e);return t(s,"Expected to allocate page"),s}releaseUint32Array(t){this._bytesAllocated-=t.byteLength,r&&console.log(`Freeing ${t.byteLength}, (${this._bytesAllocated} / ${this._bytesTotal})`);const e=this._pagesByBuffer.get(t.buffer);e&&e.releaseUint32Array(t)}_addPage(){const t=new a;return this._pages.push(t),this._pagesByBuffer.set(t.buffer,t),t}}export{i as ArrayBufferPool,s as PooledUint32Array};