/** * Resource Tracking and Memory Optimization for MicroPDF * * This module provides: * - FinalizationRegistry-based handle tracking for leak detection * - Object pools for geometry types (Point, Rect, Matrix, Quad) * - WeakRef-based leak detection warnings * - Buffer optimization utilities * * @module resource-tracking */ import { Point, Rect, Matrix, Quad } from './geometry.js'; /** * Resource types that can be tracked */ export declare enum ResourceType { Context = "Context", Buffer = "Buffer", Stream = "Stream", Pixmap = "Pixmap", Document = "Document", Page = "Page", Font = "Font", Image = "Image", Path = "Path", Text = "Text", Device = "Device", DisplayList = "DisplayList", Colorspace = "Colorspace", PdfObject = "PdfObject", Annotation = "Annotation", Link = "Link", Outline = "Outline", Cookie = "Cookie", Archive = "Archive", StextPage = "StextPage", Other = "Other" } /** * Information about a tracked allocation */ export interface AllocationInfo { handle: number; type: ResourceType; createdAt: number; stackTrace?: string | undefined; tag?: string | undefined; } /** * Statistics for a resource type */ export interface TypeStats { currentCount: number; totalAllocated: number; totalDeallocated: number; } /** * Global tracking statistics */ export interface GlobalStats { totalHandlesCreated: number; totalHandlesDestroyed: number; currentHandles: number; peakHandles: number; leakWarnings: number; } /** * Global handle registry for tracking unreleased resources */ declare class HandleRegistry { private registry; private allocations; private weakRefs; private stats; private typeStats; private enabled; private captureStacks; private leakWarnings; constructor(); /** * Enable or disable resource tracking */ setEnabled(enabled: boolean): void; /** * Check if tracking is enabled */ isEnabled(): boolean; /** * Enable or disable stack trace capture (expensive) */ setCaptureStacks(capture: boolean): void; /** * Register an object for tracking */ register(target: object, handle: number, type: ResourceType, tag?: string): void; /** * Unregister an object (called when properly disposed) */ unregister(target: object, handle: number): void; /** * Called when an object is garbage collected without being properly disposed */ private handleFinalized; /** * Get current global statistics */ getStats(): GlobalStats; /** * Get statistics by resource type */ getTypeStats(): Map; /** * Get all current allocations */ getAllocations(): AllocationInfo[]; /** * Get potential leaks (allocations older than threshold) */ getPotentialLeaks(minAgeMs: number): AllocationInfo[]; /** * Get and clear leak warnings */ getLeakWarnings(): string[]; /** * Reset all tracking data */ reset(): void; } /** * Global handle registry instance */ export declare const handleRegistry: HandleRegistry; /** * Enable resource tracking */ export declare function enableTracking(enabled: boolean): void; /** * Enable stack trace capture for allocations */ export declare function enableStackTraces(enabled: boolean): void; /** * Check if tracking is enabled */ export declare function isTrackingEnabled(): boolean; /** * Generic object pool */ declare class ObjectPool { private pool; private maxSize; private factory; private reset; private hits; private misses; private returns; constructor(factory: () => T, reset: (obj: T) => void, maxSize?: number); /** * Get an object from the pool or create a new one */ acquire(): T; /** * Return an object to the pool */ release(obj: T): void; /** * Get pool statistics */ getStats(): { size: number; hits: number; misses: number; returns: number; hitRate: number; }; /** * Clear the pool */ clear(): void; } /** * Mutable Point for pooling (internal use only) */ declare class MutablePoint { x: number; y: number; reset(): void; toPoint(): Point; } /** * Mutable Rect for pooling (internal use only) */ declare class MutableRect { x0: number; y0: number; x1: number; y1: number; reset(): void; toRect(): Rect; } /** * Mutable Matrix for pooling (internal use only) */ declare class MutableMatrix { a: number; b: number; c: number; d: number; e: number; f: number; reset(): void; toMatrix(): Matrix; } /** * Mutable Quad for pooling (internal use only) */ declare class MutableQuad { ulX: number; ulY: number; urX: number; urY: number; llX: number; llY: number; lrX: number; lrY: number; reset(): void; toQuad(): Quad; } /** * Acquire a mutable point from the pool */ export declare function acquirePoint(): MutablePoint; /** * Release a mutable point back to the pool */ export declare function releasePoint(p: MutablePoint): void; /** * Acquire a mutable rect from the pool */ export declare function acquireRect(): MutableRect; /** * Release a mutable rect back to the pool */ export declare function releaseRect(r: MutableRect): void; /** * Acquire a mutable matrix from the pool */ export declare function acquireMatrix(): MutableMatrix; /** * Release a mutable matrix back to the pool */ export declare function releaseMatrix(m: MutableMatrix): void; /** * Acquire a mutable quad from the pool */ export declare function acquireQuad(): MutableQuad; /** * Release a mutable quad back to the pool */ export declare function releaseQuad(q: MutableQuad): void; /** * Get pool statistics */ export declare function getPoolStats(): Record['getStats']>>; /** * Clear all pools */ export declare function clearPools(): void; /** * Pool for reusable byte arrays */ declare class ByteArrayPool { private pools; private sizes; private maxPerSize; /** * Get the size class for a given length */ private getSizeClass; /** * Acquire a byte array with at least the specified length */ acquire(minLength: number): Uint8Array; /** * Release a byte array back to the pool */ release(arr: Uint8Array): void; /** * Clear all pools */ clear(): void; } /** * Global byte array pool */ export declare const byteArrayPool: ByteArrayPool; /** * Convert a Uint8Array to string efficiently * * Uses TextDecoder for larger buffers (> 1KB) which is more efficient, * and falls back to simple string conversion for smaller buffers. */ export declare function uint8ArrayToString(data: Uint8Array): string; /** * Convert a Buffer to string efficiently * * Uses TextDecoder for larger buffers (> 1KB) which is more efficient. */ export declare function bufferToString(data: globalThis.Buffer, encoding?: BufferEncoding): string; /** * Reusable number array pool for hot paths */ declare class NumberArrayPool { private pools; private maxPerSize; acquire(size: number): number[]; release(arr: number[]): void; clear(): void; } /** * Global number array pool */ export declare const numberArrayPool: NumberArrayPool; /** * Generate a comprehensive leak report */ export declare function generateLeakReport(minAgeMs?: number): string; /** * Print leak report to console */ export declare function printLeakReport(minAgeMs?: number): void; export {}; //# sourceMappingURL=resource-tracking.d.ts.map