From 15e71abdc5d1b870c78d264f92d58b9be566e0a7 Mon Sep 17 00:00:00 2001 From: Emil Bay Date: Fri, 26 Jun 2020 05:58:58 +0200 Subject: [PATCH] Detach buffers by sending to an empty message channel --- memory.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/memory.js b/memory.js index e641d6b..662970b 100644 --- a/memory.js +++ b/memory.js @@ -1,14 +1,22 @@ /* eslint-disable camelcase */ +if (!window) var { MessageChannel } = require('worker' + '_threads') function sodium_malloc (n) { return new Uint8Array(n) } +const sink = new MessageChannel() +function sodium_free (n) { + sodium_memzero(n) + sink.port1.postMessage(n.buffer, [n.buffer]) +} + function sodium_memzero (arr) { arr.fill(0) } module.exports = { sodium_malloc, + sodium_free, sodium_memzero }