@woosh/meep-engine
Version:
Pure JavaScript game engine. Fully featured and production ready.
125 lines • 3.92 kB
TypeScript
export const ALLOCATOR_NO_SPACE: 4294967295;
/**
* Fast hard realtime O(1) offset allocator with minimal fragmentation.
*
* Uses 256 bins with 8 bit floating point distribution (3 bit mantissa + 5 bit exponent) and a two level bitfield to find the next available bin using 2x LZCNT instructions to make all operations O(1). Bin sizes following the floating point distribution ensures hard bounds for memory overhead percentage regardless of size class. Pow2 bins would waste up to +100% memory (+50% on average). Our float bins waste up to +12.5% (+6.25% on average).
*
* The allocation metadata is stored in a separate data structure, making this allocator suitable for sub-allocating any resources, such as GPU heaps, buffers and arrays. Returns an offset to the first element of the allocated contiguous range.
*
* @see "A comparison of memory allocators for real-time applications" by Miguel Masmano et al
* @example
* const allocator = new Allocator(12345); // Allocator with 12345 contiguous elements in total
*
* const a = allocator.allocate(1337); // Allocate a 1337 element contiguous range
* const offset_a = a.offset; // Provides offset to the first element of the range
* do_something(offset_a);
*
* const b = allocator.allocate(123); // Allocate a 123 element contiguous range
* const offset_b = b.offset; // Provides offset to the first element of the range
* do_something(offset_b);
*
* allocator.free(a); // Free allocation a
* allocator.free(b); // Free allocation b
*/
export class OffsetAllocator {
/**
*
* @param {number} size amount of contiguous elements managed by this allocator (typically bytes)
* @param {number} [maxAllocs]
*/
constructor(size: number, maxAllocs?: number);
/**
* Total managed space
* uint32
* @type {number}
*/
size: number;
/**
* Limit on number of allocations
* uint32
* @type {number}
*/
maxAllocs: number;
/**
* uint32
* @type {number}
*/
freeStorage: number;
/**
* uint32
* @type {number}
*/
usedBinsTop: number;
/**
*
* @type {Uint8Array}
*/
usedBins: Uint8Array;
/**
*
* @type {Uint32Array}
*/
binIndices: Uint32Array;
/**
*
* @type {RowFirstTable}
*/
nodes: RowFirstTable;
/**
* @type {Uint32Array}
*/
freeNodes: Uint32Array;
/**
* uint32
* @type {number}
*/
freeOffset: number;
/**
*
* @param {number} size
* @returns {Allocation}
*/
allocate(size: number): Allocation;
/**
* Direct method of releasing an allocation.
* Allows the user to skip holding an object reference in memory.
* {@link node_index} can be read from {@link Allocation.metadata}
*
* @param {number} node_index
* @see free
*/
free_node(node_index: number): void;
/**
* @param {Allocation} allocation
* @see free_node
*/
free(allocation: Allocation): void;
/**
* Drop all allocations, effectively deallocating everything and restoring allocator into initial state
*/
reset(): void;
/**
* Useful for debug and UI
* @return {StorageReport}
*/
storageReport(): StorageReport;
#private;
}
import { RowFirstTable } from "../../collection/table/RowFirstTable.js";
declare class Allocation {
/**
*
* @param {number} offset
* @param {number} metadata
* @return {Allocation}
*/
static from(offset: number, metadata: number): Allocation;
offset: number;
metadata: number;
}
declare class StorageReport {
totalFreeSpace: number;
largestFreeRegion: number;
}
export {};
//# sourceMappingURL=OffsetAllocator.d.ts.map