/* COPYRIGHT Esri - https://js.arcgis.com/5.0.18/LICENSE.txt */
import has from"../../../../core/has.js";import{FreeList as t}from"./cpuMapped/FreeList.js";const e=has("esri-2d-log-allocations");class r{static create(t,e){const s=e.acquireUint32Array(t);return new r(s,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 s{constructor(){this._data=new ArrayBuffer(s.BYTE_LENGTH),this._freeList=new t({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 a{constructor(){this._pages=[],this._pagesByBuffer=new Map,this._bytesAllocated=0}destroy(){this._pages=[],this._pagesByBuffer=null}get _bytesTotal(){return this._pages.length*s.BYTE_LENGTH}acquireUint32Array(t){return this._bytesAllocated+=t,e&&console.log(`Allocating ${t}, (${this._bytesAllocated} / ${this._bytesTotal})`),new Uint32Array(t/Uint32Array.BYTES_PER_ELEMENT)}releaseUint32Array(t){this._bytesAllocated-=t.byteLength,e&&console.log(`Freeing ${t.byteLength}, (${this._bytesAllocated} / ${this._bytesTotal})`)}_addPage(){const t=new s;return this._pages.push(t),this._pagesByBuffer.set(t.buffer,t),t}}export{a as ArrayBufferPool,r as PooledUint32Array};
