-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmmapIPC.cpp
186 lines (158 loc) · 6.66 KB
/
mmapIPC.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
* @Ref <br>
* 1- https://github.com/nodejs/node-addon-api/blob/main/doc/object_wrap.md <br>
* 2- https://github.com/nodejs/node-addon-api/blob/main/doc/object.md
* 3- https://github.com/nodejs/node-addon-api/blob/main/doc/class_property_descriptor.md
*/
#include <stdlib.h>
#include <unistd.h>
#include <sys/mman.h>
#include <fcntl.h> /* For O_* constants */
#include <sys/file.h> /* For flock(2)*/
//#include <cstdio>
#include <errno.h>
#include <napi.h>
//-----------------------CLASS----------------------//
class shm_metadata
{
public:
std::size_t mmapLength;
std::string shm_fileName;
int shm_fd;
};
class mmapIPC : public Napi::ObjectWrap<mmapIPC>
{
public:
static Napi::Object Init(Napi::Env env, Napi::Object exports); //These three functions are required by Node!
mmapIPC(const Napi::CallbackInfo& info);
static Napi::Value CreateNewItem(const Napi::CallbackInfo& info);
private:
//-----------members
shm_metadata shm_hint;
Napi::Reference<Napi::Buffer<uint8_t>> memoryBuffer;
//-----------funcs
Napi::Value buffer(const Napi::CallbackInfo& info);
Napi::Value acquireReadLock(const Napi::CallbackInfo& info);
Napi::Value acquireWriteLock(const Napi::CallbackInfo& info);
Napi::Value removeLock(const Napi::CallbackInfo& info);
//-----------statics
static void freeCallback(Napi::Env, void* memoryAddress, shm_metadata* hint);
static int createShmFile( const char *name, std::size_t memLenght);
static void* mapMemory( int shm_fd, std::size_t regionLength, bool lockPagesToRam );
};
//-----------------------CLASS----------------------//
//-------------------Static-funcs-------------------//
void mmapIPC::freeCallback(Napi::Env, void* memoryAddress, shm_metadata* hint)
{
munmap(memoryAddress, hint->mmapLength);
close(hint->shm_fd);
shm_unlink(hint->shm_fileName.c_str());
//free(hint); //Seemingly, JS frees this automatically, so it causes double free error! UPDATE: Silly me, shm_hint defined on the stack!
}
int mmapIPC::createShmFile( const char *name, std::size_t memLenght)
{
errno = 0;
int shm_fd;
shm_fd = shm_open(name, O_RDWR | O_CREAT | O_EXCL, 00777);
if(shm_fd == -1 && errno == EEXIST)
{
errno = 0;
shm_fd = shm_open(name, O_RDWR | O_CREAT, 00777);
}
else
{
ftruncate(shm_fd, memLenght);
}
return shm_fd;
}
void* mmapIPC::mapMemory( int shm_fd, std::size_t regionLength, bool lockPagesToRam )
{
void* memoryAddress = mmap( (void*)0, regionLength, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
//fprintf(stderr, "memoryAddress: %p\n", memoryAddress);
if( lockPagesToRam )
{
mlock2(memoryAddress, regionLength, MLOCK_ONFAULT);
}
return memoryAddress;
}
//-------------------Static-funcs-------------------//
//-----------------JS-Requirements-----------------//
Napi::Object mmapIPC::Init(Napi::Env env, Napi::Object exports)
{
// This method is used to hook the accessor and method callbacks
Napi::Function functionList = DefineClass(env, "mmapIPC",
{
InstanceMethod<&mmapIPC::buffer>("buffer", static_cast<napi_property_attributes>(napi_writable | napi_configurable)),
InstanceMethod<&mmapIPC::acquireReadLock>("acquireReadLock", static_cast<napi_property_attributes>(napi_writable | napi_configurable)),
InstanceMethod<&mmapIPC::acquireWriteLock>("acquireWriteLock", static_cast<napi_property_attributes>(napi_writable | napi_configurable)),
InstanceMethod<&mmapIPC::removeLock>("removeLock", static_cast<napi_property_attributes>(napi_writable | napi_configurable)),
StaticMethod<&mmapIPC::CreateNewItem>("CreateNewItem", static_cast<napi_property_attributes>(napi_writable | napi_configurable))
});
Napi::FunctionReference* constructor = new Napi::FunctionReference();
*constructor = Napi::Persistent(functionList);
exports.Set("mmapIPC", functionList);
env.SetInstanceData<Napi::FunctionReference>(constructor);
return exports;
}
Napi::Value mmapIPC::CreateNewItem(const Napi::CallbackInfo& info)
{
Napi::FunctionReference* constructor = info.Env().GetInstanceData<Napi::FunctionReference>();
return constructor->New({ info[0], info[1], info[2] }); //TODO: check: wrong; CreateNewItem is used only if we want to create a new JS object inside c++
}
Napi::Object Init (Napi::Env env, Napi::Object exports)
{
mmapIPC::Init(env, exports);
return exports;
}
NODE_API_MODULE(NODE_GYP_MODULE_NAME, Init)
//-----------------JS-Requirements-----------------//
//===========================================================================================================================================================================================
mmapIPC::mmapIPC(const Napi::CallbackInfo& info) : Napi::ObjectWrap<mmapIPC>(info), shm_hint()
{
Napi::Env env = info.Env();
this->shm_hint.shm_fileName = "/" + std::string(info[0].ToString());
this->shm_hint.mmapLength = (std::size_t)info[1].As<Napi::Number>().Int64Value();
bool lockMemory = info[2].As<Napi::Boolean>().Value();
this->shm_hint.shm_fd = createShmFile(this->shm_hint.shm_fileName.c_str(), this->shm_hint.mmapLength);
void* externalData = mapMemory(this->shm_hint.shm_fd, this->shm_hint.mmapLength, lockMemory);
this->memoryBuffer = Napi::Persistent(Napi::Buffer<uint8_t>::New(
env,
static_cast<uint8_t*>(externalData),
this->shm_hint.mmapLength,
freeCallback,
&this->shm_hint ));
/*this->memoryBuffer = Napi::Buffer<void>::New(
env,
externalData,
this->shm_hint.mmapLength,
freeCallback,
&this->shm_hint);*/
}
Napi::Value mmapIPC::buffer(const Napi::CallbackInfo& info)
{
return this->memoryBuffer.Value();
}
Napi::Value mmapIPC::acquireReadLock(const Napi::CallbackInfo& info)
{
Napi::Env env = info.Env();
if( flock(this->shm_hint.shm_fd, LOCK_SH) == 0 )
{ return Napi::Boolean::New(env, true); }
else
{ return Napi::Boolean::New(env, false); }
}
Napi::Value mmapIPC::acquireWriteLock(const Napi::CallbackInfo& info)
{
Napi::Env env = info.Env();
if( flock(this->shm_hint.shm_fd, LOCK_EX) == 0 )
{ return Napi::Boolean::New(env, true); }
else
{ return Napi::Boolean::New(env, false); }
}
Napi::Value mmapIPC::removeLock(const Napi::CallbackInfo& info)
{
Napi::Env env = info.Env();
if( flock(this->shm_hint.shm_fd, LOCK_UN) == 0 )
{ return Napi::Boolean::New(env, true); }
else
{ return Napi::Boolean::New(env, false); }
}