Skip to content

Commit 442ccde

Browse files
committed
First commit, version 1.0.0
0 parents  commit 442ccde

File tree

8 files changed

+204
-0
lines changed

8 files changed

+204
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
build/
2+
node_modules/
3+

README.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# shared-buffer
2+
3+
A minimal node.js package that provides an ArrayBuffer that can be used across processes for interprocess-communication.
4+
5+
***
6+
7+
## Installation
8+
9+
```bash
10+
npm install shared-buffer --save
11+
```
12+
13+
## Usage
14+
15+
```typescript
16+
declare function createSharedBuffer(key: number, size: number, initialize?: boolean): ArrayBuffer;
17+
```
18+
19+
* key: Any number to identify the memory segment we're allocating. Must be an integer.
20+
* size: The size of the segment in bytes. Must be an integer.
21+
* initialize: (optional) If true, will initialize a new segment, if false, will acquire an already existing one.
22+
23+
## Example
24+
25+
```js
26+
27+
const cluster = require('cluster');
28+
const { createSharedBuffer } = require('shared-buffer');
29+
30+
const SEGMENT_ID = 1024;
31+
const SEGMENT_ELEMENTS = 10;
32+
const SEGMENT_SIZE = SEGMENT_ELEMENTS * Float64Array.BYTES_PER_ELEMENT;
33+
34+
if (cluster.isMaster) {
35+
const sharedBuffer = createSharedBuffer(SEGMENT_ID, SEGMENT_SIZE, true);
36+
const sharedArray = new Float64Array(sharedBuffer);
37+
for(let i = 0; i < SEGMENT_ELEMENTS; i++)
38+
sharedArray[i] = 1/i;
39+
cluster.fork();
40+
} else {
41+
const sharedBuffer = createSharedBuffer(SEGMENT_ID, SEGMENT_SIZE);
42+
const sharedArray = new Float64Array(sharedBuffer);
43+
console.log(sharedArray);
44+
process.exit(0);
45+
}
46+
47+
/**
48+
* Output:
49+
* Float64Array [
50+
* Infinity,
51+
* 1,
52+
* 0.5,
53+
* 0.3333333333333333,
54+
* 0.25,
55+
* 0.2,
56+
* 0.16666666666666666,
57+
* 0.14285714285714285,
58+
* 0.125,
59+
* 0.1111111111111111 ]
60+
*/
61+
```
62+
63+
## Compatibility
64+
65+
POSIX only for now, sorry.
66+
67+
## License
68+
69+
Copyright 2017, Abdullah Ali
70+
71+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
72+
73+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
74+
75+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

binding.gyp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"targets": [
3+
{
4+
"include_dirs": [
5+
"<!(node -e \"require('nan')\")"
6+
],
7+
"target_name": "shared-buffer",
8+
"sources": [
9+
"src/shared-buffer.cxx"
10+
]
11+
}
12+
]
13+
}

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('bindings')('shared-buffer');

package-lock.json

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "shared-buffer",
3+
"version": "1.0.0",
4+
"description": "A minimal package that provides an ArrayBuffer that can be used across processes for interprocess-communication.",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "node test.js",
8+
"install": "node-gyp rebuild"
9+
},
10+
"keywords": [
11+
"ipc",
12+
"process",
13+
"cluster",
14+
"shared",
15+
"memory"
16+
],
17+
"author": "Abdullah Ali",
18+
"license": "MIT",
19+
"gypfile": true,
20+
"dependencies": {
21+
"bindings": "^1.2.1",
22+
"nan": "^2.6.2"
23+
},
24+
"repository": {
25+
"type" : "git",
26+
"url" : "https://github.com/voodooattack/shared-buffer.git"
27+
}
28+
}

src/shared-buffer.cxx

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include <nan.h>
2+
#include <string.h>
3+
#include <sys/ipc.h>
4+
#include <sys/shm.h>
5+
6+
NAN_METHOD(createSharedBuffer) {
7+
v8::Isolate* isolate = v8::Isolate::GetCurrent();
8+
v8::HandleScope scope(isolate);
9+
10+
if (!info[0]->IsUint32()) {
11+
Nan::ThrowError("key must be an integer");
12+
return;
13+
} else if (!info[1]->IsUint32()) {
14+
Nan::ThrowError("size must be an integer");
15+
return;
16+
}
17+
18+
key_t key = info[0]->Uint32Value();
19+
size_t size = info[1]->Uint32Value();
20+
bool initialize = info[2]->BooleanValue();
21+
22+
key_t shmId = shmget(key, size, initialize ? IPC_CREAT | 0666 : 0666);
23+
24+
if (shmId < 0) {
25+
Nan::ThrowError(strerror(errno));
26+
return;
27+
}
28+
29+
char * data = (char *)shmat(shmId, NULL, 0);
30+
31+
if (data == (char *)-1) {
32+
Nan::ThrowError(strerror(errno));
33+
return;
34+
}
35+
36+
if (initialize)
37+
memset(data, 0, size);
38+
39+
auto buffer = Nan::New<v8::ArrayBuffer>(v8::ArrayBuffer::New(isolate, (void*)data, size));
40+
41+
info.GetReturnValue().Set(buffer);
42+
}
43+
44+
NAN_MODULE_INIT(Initialize) {
45+
NAN_EXPORT(target, createSharedBuffer);
46+
}
47+
48+
NODE_MODULE(sharedbuffer, Initialize)

test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const cluster = require('cluster');
2+
const { createSharedBuffer } = require('./index.js');
3+
4+
const SEGMENT_ID = 1024;
5+
const SEGMENT_ELEMENTS = 10;
6+
const SEGMENT_SIZE = SEGMENT_ELEMENTS * Float64Array.BYTES_PER_ELEMENT;
7+
8+
if (cluster.isMaster) {
9+
const sharedBuffer = createSharedBuffer(SEGMENT_ID, SEGMENT_SIZE, true);
10+
const sharedArray = new Float64Array(sharedBuffer);
11+
for(let i = 0; i < SEGMENT_ELEMENTS; i++)
12+
sharedArray[i] = 1/i;
13+
cluster.fork();
14+
} else {
15+
const sharedBuffer = createSharedBuffer(SEGMENT_ID, SEGMENT_SIZE);
16+
const sharedArray = new Float64Array(sharedBuffer);
17+
console.log(sharedArray);
18+
process.exit(0);
19+
}

0 commit comments

Comments
 (0)