Skip to content

Commit 820aaf5

Browse files
bnoordhuisindutny
authored andcommitted
src: replace CONTAINER_OF with type-safe function
Replace the CONTAINER_OF macro with a template function that is as type-safe as a reinterpret_cast<> of an arbitrary pointer can be made. Signed-off-by: Fedor Indutny <fedor@indutny.com>
1 parent c7b0203 commit 820aaf5

12 files changed

Lines changed: 61 additions & 34 deletions

src/cares_wrap.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ static void ares_timeout(uv_timer_t* handle) {
8888

8989

9090
static void ares_poll_cb(uv_poll_t* watcher, int status, int events) {
91-
ares_task_t* task = CONTAINER_OF(watcher, ares_task_t, poll_watcher);
91+
ares_task_t* task = ContainerOf(&ares_task_t::poll_watcher, watcher);
9292
Environment* env = task->env;
9393

9494
/* Reset the idle timer */
@@ -109,7 +109,8 @@ static void ares_poll_cb(uv_poll_t* watcher, int status, int events) {
109109

110110

111111
static void ares_poll_close_cb(uv_handle_t* watcher) {
112-
ares_task_t* task = CONTAINER_OF(watcher, ares_task_t, poll_watcher);
112+
ares_task_t* task = ContainerOf(&ares_task_t::poll_watcher,
113+
reinterpret_cast<uv_poll_t*>(watcher));
113114
free(task);
114115
}
115116

src/env-inl.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ inline bool Environment::in_domain() const {
276276

277277
inline Environment* Environment::from_immediate_check_handle(
278278
uv_check_t* handle) {
279-
return CONTAINER_OF(handle, Environment, immediate_check_handle_);
279+
return ContainerOf(&Environment::immediate_check_handle_, handle);
280280
}
281281

282282
inline uv_check_t* Environment::immediate_check_handle() {
@@ -289,15 +289,15 @@ inline uv_idle_t* Environment::immediate_idle_handle() {
289289

290290
inline Environment* Environment::from_idle_prepare_handle(
291291
uv_prepare_t* handle) {
292-
return CONTAINER_OF(handle, Environment, idle_prepare_handle_);
292+
return ContainerOf(&Environment::idle_prepare_handle_, handle);
293293
}
294294

295295
inline uv_prepare_t* Environment::idle_prepare_handle() {
296296
return &idle_prepare_handle_;
297297
}
298298

299299
inline Environment* Environment::from_idle_check_handle(uv_check_t* handle) {
300-
return CONTAINER_OF(handle, Environment, idle_check_handle_);
300+
return ContainerOf(&Environment::idle_check_handle_, handle);
301301
}
302302

303303
inline uv_check_t* Environment::idle_check_handle() {
@@ -345,7 +345,7 @@ inline void Environment::set_printed_error(bool value) {
345345
}
346346

347347
inline Environment* Environment::from_cares_timer_handle(uv_timer_t* handle) {
348-
return CONTAINER_OF(handle, Environment, cares_timer_handle_);
348+
return ContainerOf(&Environment::cares_timer_handle_, handle);
349349
}
350350

351351
inline uv_timer_t* Environment::cares_timer_handle() {

src/node.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1523,7 +1523,7 @@ static void GetActiveRequests(const FunctionCallbackInfo<Value>& args) {
15231523
int i = 0;
15241524

15251525
QUEUE_FOREACH(q, &req_wrap_queue) {
1526-
ReqWrap<uv_req_t>* w = CONTAINER_OF(q, ReqWrap<uv_req_t>, req_wrap_queue_);
1526+
ReqWrap<uv_req_t>* w = ContainerOf(&ReqWrap<uv_req_t>::req_wrap_queue_, q);
15271527
if (w->persistent().IsEmpty())
15281528
continue;
15291529
ary->Set(i++, w->object());
@@ -1546,7 +1546,7 @@ void GetActiveHandles(const FunctionCallbackInfo<Value>& args) {
15461546
Local<String> owner_sym = env->owner_string();
15471547

15481548
QUEUE_FOREACH(q, &handle_wrap_queue) {
1549-
HandleWrap* w = CONTAINER_OF(q, HandleWrap, handle_wrap_queue_);
1549+
HandleWrap* w = ContainerOf(&HandleWrap::handle_wrap_queue_, q);
15501550
if (w->persistent().IsEmpty() || (w->flags_ & HandleWrap::kUnref))
15511551
continue;
15521552
Local<Object> object = w->object();

src/node_crypto.cc

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4027,7 +4027,6 @@ class PBKDF2Request : public AsyncWrap {
40274027
error_ = err;
40284028
}
40294029

4030-
// TODO(trevnorris): Make private and make work with CONTAINER_OF macro.
40314030
uv_work_t work_req_;
40324031

40334032
private:
@@ -4059,7 +4058,7 @@ void EIO_PBKDF2(PBKDF2Request* req) {
40594058

40604059

40614060
void EIO_PBKDF2(uv_work_t* work_req) {
4062-
PBKDF2Request* req = CONTAINER_OF(work_req, PBKDF2Request, work_req_);
4061+
PBKDF2Request* req = ContainerOf(&PBKDF2Request::work_req_, work_req);
40634062
EIO_PBKDF2(req);
40644063
}
40654064

@@ -4078,7 +4077,7 @@ void EIO_PBKDF2After(PBKDF2Request* req, Local<Value> argv[2]) {
40784077

40794078
void EIO_PBKDF2After(uv_work_t* work_req, int status) {
40804079
assert(status == 0);
4081-
PBKDF2Request* req = CONTAINER_OF(work_req, PBKDF2Request, work_req_);
4080+
PBKDF2Request* req = ContainerOf(&PBKDF2Request::work_req_, work_req);
40824081
Environment* env = req->env();
40834082
HandleScope handle_scope(env->isolate());
40844083
Context::Scope context_scope(env->context());
@@ -4257,7 +4256,6 @@ class RandomBytesRequest : public AsyncWrap {
42574256
error_ = err;
42584257
}
42594258

4260-
// TODO(trevnorris): Make private and make work with CONTAINER_OF macro.
42614259
uv_work_t work_req_;
42624260

42634261
private:
@@ -4269,9 +4267,8 @@ class RandomBytesRequest : public AsyncWrap {
42694267

42704268
template <bool pseudoRandom>
42714269
void RandomBytesWork(uv_work_t* work_req) {
4272-
RandomBytesRequest* req = CONTAINER_OF(work_req,
4273-
RandomBytesRequest,
4274-
work_req_);
4270+
RandomBytesRequest* req =
4271+
ContainerOf(&RandomBytesRequest::work_req_, work_req);
42754272
int r;
42764273

42774274
// Ensure that OpenSSL's PRNG is properly seeded.
@@ -4317,9 +4314,8 @@ void RandomBytesCheck(RandomBytesRequest* req, Local<Value> argv[2]) {
43174314

43184315
void RandomBytesAfter(uv_work_t* work_req, int status) {
43194316
assert(status == 0);
4320-
RandomBytesRequest* req = CONTAINER_OF(work_req,
4321-
RandomBytesRequest,
4322-
work_req_);
4317+
RandomBytesRequest* req =
4318+
ContainerOf(&RandomBytesRequest::work_req_, work_req);
43234319
Environment* env = req->env();
43244320
HandleScope handle_scope(env->isolate());
43254321
Context::Scope context_scope(env->context());

src/node_http_parser.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,15 @@ const uint32_t kOnMessageComplete = 3;
7777

7878
#define HTTP_CB(name) \
7979
static int name(http_parser* p_) { \
80-
Parser* self = CONTAINER_OF(p_, Parser, parser_); \
80+
Parser* self = ContainerOf(&Parser::parser_, p_); \
8181
return self->name##_(); \
8282
} \
8383
int name##_()
8484

8585

8686
#define HTTP_DATA_CB(name) \
8787
static int name(http_parser* p_, const char* at, size_t length) { \
88-
Parser* self = CONTAINER_OF(p_, Parser, parser_); \
88+
Parser* self = ContainerOf(&Parser::parser_, p_); \
8989
return self->name##_(at, length); \
9090
} \
9191
int name##_(const char* at, size_t length)

src/node_v8.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ void Environment::IsolateData::AfterGarbageCollection(GCType type,
8383
q = QUEUE_HEAD(&queue);
8484
QUEUE_REMOVE(q);
8585
QUEUE_INSERT_TAIL(&gc_tracker_queue_, q);
86-
Environment* env = CONTAINER_OF(q, Environment, gc_tracker_queue_);
86+
Environment* env = ContainerOf(&Environment::gc_tracker_queue_, q);
8787
env->AfterGarbageCollectionCallback(&gc_info_before_, &gc_info_after_);
8888
}
8989
}

src/node_zlib.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ class ZCtx : public AsyncWrap {
246246
// for a single write() call, until all of the input bytes have
247247
// been consumed.
248248
static void Process(uv_work_t* work_req) {
249-
ZCtx *ctx = CONTAINER_OF(work_req, ZCtx, work_req_);
249+
ZCtx *ctx = ContainerOf(&ZCtx::work_req_, work_req);
250250

251251
// If the avail_out is left at 0, then it means that it ran out
252252
// of room. If there was avail_out left over, then it means
@@ -320,7 +320,7 @@ class ZCtx : public AsyncWrap {
320320
static void After(uv_work_t* work_req, int status) {
321321
assert(status == 0);
322322

323-
ZCtx* ctx = CONTAINER_OF(work_req, ZCtx, work_req_);
323+
ZCtx* ctx = ContainerOf(&ZCtx::work_req_, work_req);
324324
Environment* env = ctx->env();
325325

326326
HandleScope handle_scope(env->isolate());

src/signal_wrap.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ class SignalWrap : public HandleWrap {
105105
}
106106

107107
static void OnSignal(uv_signal_t* handle, int signum) {
108-
SignalWrap* wrap = CONTAINER_OF(handle, SignalWrap, handle_);
108+
SignalWrap* wrap = ContainerOf(&SignalWrap::handle_, handle);
109109
Environment* env = wrap->env();
110110
HandleScope handle_scope(env->isolate());
111111
Context::Scope context_scope(env->context());

src/stream_wrap.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ void StreamWrap::SetBlocking(const FunctionCallbackInfo<Value>& args) {
510510
}
511511

512512
void StreamWrap::AfterWrite(uv_write_t* req, int status) {
513-
WriteWrap* req_wrap = CONTAINER_OF(req, WriteWrap, req_);
513+
WriteWrap* req_wrap = ContainerOf(&WriteWrap::req_, req);
514514
StreamWrap* wrap = req_wrap->wrap();
515515
Environment* env = wrap->env();
516516

src/tls_wrap.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ TLSCallbacks::~TLSCallbacks() {
114114
QUEUE* q = QUEUE_HEAD(&pending_write_items_);
115115
QUEUE_REMOVE(q);
116116

117-
WriteItem* wi = QUEUE_DATA(q, WriteItem, member_);
117+
WriteItem* wi = ContainerOf(&WriteItem::member_, q);
118118
delete wi;
119119
}
120120
}
@@ -145,7 +145,7 @@ bool TLSCallbacks::InvokeQueued(int status) {
145145
QUEUE* q = QUEUE_HEAD(&pending_write_items_);
146146
QUEUE_REMOVE(q);
147147

148-
WriteItem* wi = QUEUE_DATA(q, WriteItem, member_);
148+
WriteItem* wi = ContainerOf(&WriteItem::member_, q);
149149
wi->cb_(&wi->w_->req_, status);
150150
delete wi;
151151
}

0 commit comments

Comments
 (0)