-
Notifications
You must be signed in to change notification settings - Fork 2
/
shm.cpp
73 lines (54 loc) · 1.78 KB
/
shm.cpp
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
#include <node.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
using namespace std;
void unlink(const v8::FunctionCallbackInfo<v8::Value>& args)
{
auto* isolate = args.GetIsolate();
if (args.Length() != 1) {
isolate->ThrowException(
v8::Exception::Error(
v8::String::NewFromUtf8(isolate, "shm_unlink() takes 1 arguments: name.")));
return;
}
v8::String::Utf8Value str(args[0]->ToString());
char* name = (char*)calloc(1, str.length() + 1);
strcpy(name, (char*)*str);
int retVal = shm_unlink(name);
free(name);
args.GetReturnValue().Set(retVal);
}
void open(const v8::FunctionCallbackInfo<v8::Value>& args)
{
auto* isolate = args.GetIsolate();
if (args.Length() != 3) {
isolate->ThrowException(
v8::Exception::Error(
v8::String::NewFromUtf8(isolate, "shm_open() takes 3 arguments: name, oflag and mode.")));
return;
}
const int oflag = args[1]->ToInteger()->Value();
const mode_t mode = args[2]->ToInteger()->Value();
v8::String::Utf8Value str(args[0]->ToString());
char* name = (char*)calloc(1, str.length());
strcpy(name, (char*)*str);
int retVal = shm_open(name, oflag, mode);
free(name);
args.GetReturnValue().Set(retVal);
}
static void RegisterModule(v8::Local<v8::Object> exports)
{
NODE_SET_METHOD(exports, "shm_open", open);
NODE_SET_METHOD(exports, "shm_unlink", unlink);
NODE_DEFINE_CONSTANT(exports, O_RDONLY);
NODE_DEFINE_CONSTANT(exports, O_RDWR);
NODE_DEFINE_CONSTANT(exports, O_CREAT);
NODE_DEFINE_CONSTANT(exports, O_EXCL);
NODE_DEFINE_CONSTANT(exports, O_TRUNC);
}
NODE_MODULE(mmap, RegisterModule);