Skip to content

Instantly share code, notes, and snippets.

@ragingwind
Last active August 17, 2020 08:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ragingwind/be144f32c550cc46e7028819cca9365b to your computer and use it in GitHub Desktop.
Save ragingwind/be144f32c550cc46e7028819cca9365b to your computer and use it in GitHub Desktop.
Allocate memory with typed array
function TypedArrayToHeap(arr) {
const { name, BYTES_PER_ELEMENT } = arr.constructor;
const prefix = name.charAt(0).replace(/I|B/, '');
const heap = Module[`HEAP${prefix}${BYTES_PER_ELEMENT << 3}`];
if (!heap) {
throw new Error(`Unknow typed array ${heap}`);
}
const ptr = Module._malloc(arr.length * BYTES_PER_ELEMENT);
heap.set(arr, ptr / BYTES_PER_ELEMENT);
return ptr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment