Skip to content

Commit 61cc473

Browse files
committed
Introduce TAKR_PTR and use it
1 parent dab2134 commit 61cc473

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

src/netlog/netlog-manager.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -484,8 +484,6 @@ int manager_new(Manager **ret, const char *state_file, const char *cursor) {
484484
if (r < 0)
485485
return r;
486486

487-
*ret = m;
488-
m = NULL;
489-
487+
*ret = TAKE_PTR(m);
490488
return 0;
491489
}

src/netlog/netlog-network.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ int manager_open_network_socket(Manager *m) {
207207
if (SYSLOG_TRANSMISSION_PROTOCOL_TCP == m->protocol) {
208208
union sockaddr_union sa;
209209
socklen_t salen;
210+
210211
switch (m->address.sockaddr.sa.sa_family) {
211212
case AF_INET:
212213
sa = (union sockaddr_union) {
@@ -228,6 +229,7 @@ int manager_open_network_socket(Manager *m) {
228229
r = -EAFNOSUPPORT;
229230
goto fail;
230231
}
232+
231233
r = connect(m->socket, &m->address.sockaddr.sa, salen);
232234
if (r < 0 && errno != EINPROGRESS) {
233235
r = -errno;

src/share/macro.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,4 +399,15 @@ static inline unsigned long ALIGN_POWER2(unsigned long u) {
399399
} \
400400
struct __useless_struct_to_allow_trailing_semicolon__
401401

402+
/* Takes inspiration from Rust's Option::take() method: reads and returns a pointer, but at the same time
403+
* resets it to NULL. See: https://doc.rust-lang.org/std/option/enum.Option.html#method.take */
404+
#define TAKE_PTR(ptr) \
405+
({ \
406+
typeof(ptr) *_pptr_ = &(ptr); \
407+
typeof(ptr) _ptr_ = *_pptr_; \
408+
*_pptr_ = NULL; \
409+
_ptr_; \
410+
})
411+
412+
402413
#include "log.h"

0 commit comments

Comments
 (0)