-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddon.js
More file actions
96 lines (84 loc) · 3.02 KB
/
Copy pathaddon.js
File metadata and controls
96 lines (84 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
var addon = require('bindings')('node_shared_mem');
/**
* Access permissions for shared memory mappings.
* @enum {number}
*/
const MemoryAccess = Object.freeze({
READ: addon.MemoryAccess.READ,
WRITE: addon.MemoryAccess.WRITE,
EXECUTE: addon.MemoryAccess.EXECUTE
});
/**
* Represents a shared memory mapping.
*/
class SharedMemory {
/**
* Opens the shared memory mapping with the given name.
* @param {string} name Name of the mapping.
* @param {number} length Length of the mapping in bytes.
* @param {MemoryAccess} [access] Access permissions of the mapping. Default is {@link MemoryAccess.READ} and {@link MemoryAccess.WRITE}.
*/
constructor (name, length, access) {
const native = new addon.SharedMemory(name, length, access);
this._native = native;
/**
* Name of the mapping.
* @type {string}
*/
this.name = native.name;
/**
* Length of the mapping in bytes.
* @type {string}
*/
this.length = native.length;
/**
* Access permissions of the mapping.
* @type {MemoryAccess}
*/
this.access = native.access;
/**
* Underlying array buffer of the mapping.
* @type {ArrayBuffer}
*/
this.buffer = native.buffer;
/**
* Indicates whether data must be explicitly synchronized between shared memory and the array buffer.
* If `false`, the array buffer is backed by the shared memory mapping.
* @type {boolean}
*/
this.requiresCopy = native.requiresCopy;
}
/**
* Copy data from shared memory to the underlying array buffer.
* @remarks Only required if the buffer is not backed by shared memory as indicated by `requiresCopy`.
* @param {*} [srcOffset] Byte offset into the shared memory mapping. Default is 0.
* @param {*} [dstOffset] Byte offset into the array buffer. Default is 0.
* @param {*} [length] Number of bytes to copy. Defaults to the number of available bytes.
*/
copyFrom(srcOffset, dstOffset, length) {
this._native.copyFrom(srcOffset, dstOffset, length);
}
/**
* Copy data the underlying array buffer to shared memory.
* @remarks Only required if the buffer is not backed by shared memory as indicated by `requiresCopy`.
* @param {*} [srcOffset] Byte offset into the array buffer. Default is 0.
* @param {*} [dstOffset] Byte offset into the shared memory mapping. Default is 0.
* @param {*} [length] Number of bytes to copy. Defaults to the number of available bytes.
*/
copyTo(srcOffset, dstOffset, length) {
this._native.copyTo(srcOffset, dstOffset, length);
}
/**
* Closes the mapping.
*/
close() {
this._native.close();
delete this.name;
delete this.length;
delete this.access;
delete this.buffer;
delete this.requiresCopy;
delete this._native;
}
}
module.exports = { SharedMemory, MemoryAccess };