@daneren2005/shared-memory-objects 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +44 -0
- package/package.json +1 -1
- package/src/main.ts +6 -5
- package/src/shared-list.ts +3 -0
package/README.md
CHANGED
|
@@ -8,6 +8,50 @@ Each allocated memory location can be stored as an int pointer. You can use `ge
|
|
|
8
8
|
|
|
9
9
|
When passing memory to another thread you can either pass a pointer or a serialized version of the buffer position/byte offset in order to re-create the object in the other thread.
|
|
10
10
|
|
|
11
|
+
## Getting Started
|
|
12
|
+
`npm install @daneren2005/shared-memory-objects`
|
|
13
|
+
|
|
14
|
+
Example to update blocks of memory from a thread.
|
|
15
|
+
```
|
|
16
|
+
let heap = new MemoryHeap();
|
|
17
|
+
let memory = heap.allocUI32(4);
|
|
18
|
+
|
|
19
|
+
// Pass memory to another thread
|
|
20
|
+
thread.postMessage({
|
|
21
|
+
heap: heap.getSharedMemory(),
|
|
22
|
+
memory: memory.getSharedMemory()
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
// From worker thread re-construct memory and change it
|
|
26
|
+
self.onmessage = (e) => {
|
|
27
|
+
let heap = new MemoryHeap(e.data.heap);
|
|
28
|
+
let memory = new AllocatedMemory(heap, e.data.memory);
|
|
29
|
+
memory.data[2] = 5;
|
|
30
|
+
};
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
// Example to work with data structures from a thread. When constructing a new structure you just pass the heap. When re-creating a structure from an already initialized memory location pass the heap and the shared memory location for it.
|
|
34
|
+
```
|
|
35
|
+
let heap = new MemoryHeap();
|
|
36
|
+
let list = new SharedList(heap);
|
|
37
|
+
|
|
38
|
+
// Pass memory to another thread
|
|
39
|
+
thread.postMessage({
|
|
40
|
+
heap: heap.getSharedMemory(),
|
|
41
|
+
list: list.getSharedMemory()
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
// From worker thread re-construct memory and change it
|
|
45
|
+
self.onmessage = (e) => {
|
|
46
|
+
let heap = new MemoryHeap(e.data.heap);
|
|
47
|
+
let list = new SharedList(heap, e.data.list);
|
|
48
|
+
|
|
49
|
+
list.push(5);
|
|
50
|
+
};
|
|
51
|
+
```
|
|
52
|
+
let mainList = new SharedList(memory);
|
|
53
|
+
let secondList = new SharedList(memory, mainList.getSharedMemory());
|
|
54
|
+
|
|
11
55
|
## Data Structures
|
|
12
56
|
- SharedList
|
|
13
57
|
- SharedVector
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@daneren2005/shared-memory-objects",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.3",
|
|
4
4
|
"author": "daneren2005@gmail.com",
|
|
5
5
|
"description": "Creating objects with a SharedArrayBuffer",
|
|
6
6
|
"homepage": "https://github.com/daneren2005/shared-memory-objects#readme",
|
package/src/main.ts
CHANGED
|
@@ -3,12 +3,13 @@ import MemoryBuffer from './memory-buffer';
|
|
|
3
3
|
import MemoryHeap, { type MemoryHeapMemory } from './memory-heap';
|
|
4
4
|
|
|
5
5
|
import SharedList, { type SharedListMemory } from './shared-list';
|
|
6
|
-
import SharedMap from './shared-map';
|
|
6
|
+
import SharedMap, { type SharedMapMemory } from './shared-map';
|
|
7
7
|
import SharedPointerList from './shared-pointer-list';
|
|
8
8
|
import SharedString from './shared-string';
|
|
9
|
-
import SharedVector from './shared-vector';
|
|
9
|
+
import SharedVector, { type SharedVectorMemory } from './shared-vector';
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
export * from './interfaces/typed-array';
|
|
12
|
+
export * from './interfaces/typed-array-constructor';
|
|
12
13
|
|
|
13
14
|
export * from './utils/16-from-32-array';
|
|
14
15
|
export * from './utils/16-from-64-array';
|
|
@@ -27,9 +28,9 @@ export {
|
|
|
27
28
|
SharedList,
|
|
28
29
|
type SharedListMemory,
|
|
29
30
|
SharedMap,
|
|
31
|
+
type SharedMapMemory,
|
|
30
32
|
SharedPointerList,
|
|
31
33
|
SharedString,
|
|
32
34
|
SharedVector,
|
|
33
|
-
|
|
34
|
-
type TypedArrayConstructor
|
|
35
|
+
type SharedVectorMemory
|
|
35
36
|
};
|
package/src/shared-list.ts
CHANGED
|
@@ -67,6 +67,7 @@ export default class SharedList<T extends Uint32Array | Int32Array | Float32Arra
|
|
|
67
67
|
} else {
|
|
68
68
|
if(config && config.initWithBlock) {
|
|
69
69
|
this.firstBlock = new AllocatedMemory(memory, config.initWithBlock);
|
|
70
|
+
console.log(`init block at ${this.firstBlock.data.byteOffset}`);
|
|
70
71
|
} else {
|
|
71
72
|
this.firstBlock = memory.allocUI32(FIRST_BLOCK_RECORD_KEEPING_COUNT);
|
|
72
73
|
}
|
|
@@ -124,6 +125,7 @@ export default class SharedList<T extends Uint32Array | Int32Array | Float32Arra
|
|
|
124
125
|
} else {
|
|
125
126
|
// First item - store on first block
|
|
126
127
|
storeRawPointer(this.firstBlock.data, 0, newBlockPointer);
|
|
128
|
+
console.log(`insert first block ${newBlockPointer} at ${this.firstBlock.data.byteOffset}`);
|
|
127
129
|
}
|
|
128
130
|
|
|
129
131
|
// Always update new last buffer position and length
|
|
@@ -170,6 +172,7 @@ export default class SharedList<T extends Uint32Array | Int32Array | Float32Arra
|
|
|
170
172
|
*[Symbol.iterator]() {
|
|
171
173
|
let currentIndex = 0;
|
|
172
174
|
let { bufferPosition: nextBlockPosition, bufferByteOffset: nextBlockByteOffset } = loadPointer(this.firstBlock.data, 0);
|
|
175
|
+
console.log(`checking first block: ${nextBlockByteOffset} at ${this.firstBlock.data.byteOffset}`);
|
|
173
176
|
let lastBlockData = this.firstBlock.data;
|
|
174
177
|
let lastBlockPosition = 0;
|
|
175
178
|
let lastBlockByteOffset = 0;
|