• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/// <reference path="../bindings/embind.d.ts" />
2/// <reference path="public_api.d.ts" />
3// eslint-disable-next-line @typescript-eslint/no-unused-vars
4namespace memory {
5  declare const Module: embind.EmbindModule;
6  export const nullptr = 0;
7
8  export const copy1dArray = (arr: number[] | public_api.TypedArray | null,
9                              dest: Heaps, ptr?: number): number => {
10    if (!arr || !arr.length) {
11      return nullptr;
12    }
13    const bytesPerElement = Module[dest].BYTES_PER_ELEMENT;
14    ptr ||= Module._malloc(arr.length * bytesPerElement);
15    Module[dest].set(arr, ptr / bytesPerElement);
16    return ptr;
17  };
18
19  export const freeIfNecessary = (ptr: number, arr: any[] | public_api.TypedArray): void => {
20    if (arr && !(arr as any)._ck) {
21      Module._free(ptr);
22    }
23  };
24
25  // Recommended over enums by Effective Typescript pg 197.
26  export type Heaps = 'HEAPF32' | 'HEAPU8' | 'HEAPU16' | 'HEAPU32' | 'HEAP8' | 'HEAP16' | 'HEAP32';
27}
28