MCPcopy Create free account
hub / github.com/nodejs/node-addon-api / CustomAllocWorker

Class CustomAllocWorker

test/async_worker.cc:55–79  ·  view source on GitHub ↗

Using default std::allocator impl, but assuming user can define their own allocate/deallocate methods

Source from the content-addressed store, hash-verified

53// Using default std::allocator impl, but assuming user can define their own
54// allocate/deallocate methods
55class CustomAllocWorker : public AsyncWorker {
56 using Allocator = std::allocator<CustomAllocWorker>;
57
58 public:
59 CustomAllocWorker(Function& cb) : AsyncWorker(cb){};
60 static void DoWork(const CallbackInfo& info) {
61 Function cb = info[0].As<Function>();
62 Allocator allocator;
63 CustomAllocWorker* newWorker = allocator.allocate(1);
64 std::allocator_traits<Allocator>::construct(allocator, newWorker, cb);
65 newWorker->Queue();
66 }
67
68 protected:
69 void Execute() override {}
70 void Destroy() override {
71 assert(this->_secretVal == 24);
72 Allocator allocator;
73 std::allocator_traits<Allocator>::destroy(allocator, this);
74 allocator.deallocate(this, 1);
75 }
76
77 private:
78 int _secretVal = 24;
79};
80
81class TestWorker : public AsyncWorker {
82 public:

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected