Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update for node v16 #11

Merged
merged 3 commits into from
Sep 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ node_modules/
npm-debug.log
package-lock.json
.vscode
.cache
compile_commands.json
1 change: 0 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';
const shm = require('./build/Release/shm.node');
const cluster = require('cluster');

const uint32Max = Math.pow(2,32) - 1;
const keyMin = 1;
Expand Down
33 changes: 23 additions & 10 deletions src/node_shm.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "node_shm.h"
#include "node.h"

//-------------------------------

Expand Down Expand Up @@ -28,8 +29,8 @@ namespace Buffer {


MaybeLocal<Object> NewTyped(
Isolate* isolate,
char* data,
Isolate* isolate,
char* data,
size_t count
#if NODE_MODULE_VERSION > IOJS_2_0_MODULE_VERSION
, node::Buffer::FreeCallback callback
Expand All @@ -51,9 +52,9 @@ namespace Buffer {
Local<ArrayBuffer> ab = arr->Buffer();
*/

Local<ArrayBuffer> ab = ArrayBuffer::New(isolate, data, length,
Local<ArrayBuffer> ab = ArrayBuffer::New(isolate, data, length,
ArrayBufferCreationMode::kExternalized);

Local<Object> ui;
switch(type) {
case SHMBT_INT8:
Expand Down Expand Up @@ -159,7 +160,11 @@ namespace node_shm {
static bool hasShmSegmentInfo(int resId);
static bool removeShmSegmentInfo(int resId);
static void FreeCallback(char* data, void* hint);
#if NODE_MODULE_VERSION < NODE_16_0_MODULE_VERSION
static void Init(Local<Object> target);
#else
static void Init(Local<Object> target, Local<Value> module, void* priv);
#endif
static void AtNodeExit(void*);


Expand Down Expand Up @@ -231,10 +236,10 @@ namespace node_shm {
if (i == shmSegmentsCnt-1) {
//removing last element
} else {
std::copy(shmSegmentsIds + i + 1,
shmSegmentsIds + shmSegmentsCnt,
std::copy(shmSegmentsIds + i + 1,
shmSegmentsIds + shmSegmentsCnt,
shmSegmentsIds + i);
std::copy(shmSegmentsAddrs + i + 1,
std::copy(shmSegmentsAddrs + i + 1,
shmSegmentsAddrs + shmSegmentsCnt,
shmSegmentsAddrs + i);
}
Expand Down Expand Up @@ -307,7 +312,7 @@ namespace node_shm {
ShmBufferType type = (ShmBufferType) Nan::To<int32_t>(info[4]).FromJust();
size_t size = count * getSize1ForShmBufferType(type);
bool isCreate = (size > 0);

int resId = shmget(key, size, shmflg);
if (resId == -1) {
switch(errno) {
Expand All @@ -330,7 +335,7 @@ namespace node_shm {
} else
return Nan::ThrowError(strerror(errno));
}

void* res = shmat(resId, NULL, at_shmflg);
if (res == (void *)-1)
return Nan::ThrowError(strerror(errno));
Expand Down Expand Up @@ -399,9 +404,13 @@ namespace node_shm {
}

// Init module
#if NODE_MODULE_VERSION < NODE_16_0_MODULE_VERSION
static void Init(Local<Object> target) {
#else
static void Init(Local<Object> target, Local<Value> module, void* priv) {
#endif
initShmSegmentsInfo();

Nan::SetMethod(target, "get", get);
Nan::SetMethod(target, "detach", detach);
Nan::SetMethod(target, "detachAll", detachAll);
Expand All @@ -424,7 +433,11 @@ namespace node_shm {
Nan::Set(target, Nan::New("SHMBT_FLOAT32").ToLocalChecked(), Nan::New<Number>(SHMBT_FLOAT32));
Nan::Set(target, Nan::New("SHMBT_FLOAT64").ToLocalChecked(), Nan::New<Number>(SHMBT_FLOAT64));

#if NODE_MODULE_VERSION < NODE_16_0_MODULE_VERSION
node::AtExit(AtNodeExit);
#else
node::AddEnvironmentCleanupHook(target->GetIsolate(), AtNodeExit, nullptr);
#endif
}

}
Expand Down