UNPKG

@jawis/shared-page-heap

Version:
115 lines (114 loc) 2.53 kB
import { TypedArray, TypedArrayContructor } from "^jab"; import { FixedSizeHeap } from "^shared-algs"; export type SharedListHeapDeps = { maxSize: number; dataSize: number; sharedArray: Int32Array; initialized?: boolean; verifyAfterOperations: boolean; }; /** * - Fixed size doubly linked list * - Links are kept in the header section * - Pages contains only user data, so dataSize === pageSize * - Must initialize all memory itself, because this is the most basic data structure and the sharedArray could be reused. * * impl * - Pages are 0-indexed * - It's possible to determine if a page is allocated because NODE_ALLOCATED is stored in its `next slot` * * notes * - Free list only need to be a singly linked list. * * memory layout * meta data * 4 bytes alloc count * 4 bytes free list head * repeated n times * 4 bytes ref to next page * 4 bytes padding (optional depending on n) * n pages (sequentially in memory) * * page layout * x bytes data * * structure of reference (heighest bits first) * 32 bits page index * */ export declare class SharedListHeap implements FixedSizeHeap { deps: SharedListHeapDeps; readonly dataSize: number; private META_DATA_BYTES; /** * */ constructor(deps: SharedListHeapDeps); /** * */ static getExpectedByteSize: (n: number, dataSize: number) => number; /** * partial */ pack: () => { maxSize: number; dataSize: number; sharedArray: Int32Array; verifyAfterOperations: boolean; initialized: true; }; /** * */ get count(): number; /** * */ private set count(value); /** * */ getPageByteOffset: (pageIndex: number) => number; /** * */ get: <A extends TypedArray>(ref: number, TypedArray: TypedArrayContructor<A>) => A; /** * */ allocate: <A extends TypedArray>(TypedArray: TypedArrayContructor<A>, zeroFill?: boolean) => { ref: number; array: A; }; /** * Get a free page from the free list. */ private _allocate; /** * * - The deallocated node becomes the new free head. * */ deallocate: (ref: number) => void; /** * */ private pushFreeNode; /** * */ private getNext; /** * */ private setNext; /** * */ private _invariant; /** * */ toString: () => never; }