Skip to content

Commit b07703f

Browse files
authored
C level cleanups (#165)
* Cleaner unreached code paths * Better organize compilation units * Mark later2() inline
1 parent 7ac1ed4 commit b07703f

File tree

6 files changed

+53
-51
lines changed

6 files changed

+53
-51
lines changed

NEWS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
#### Updates
44

5-
* Fixes rare cases of `unresolvedValue` being returned by fulfilled promises (#163).
5+
* Fixes very rare cases of `unresolvedValue` being returned by fulfilled promises (#163).
66

77
# nanonext 1.6.1
88

src/aio.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,8 @@ SEXP nano_aio_get_msg(SEXP env) {
290290
return mk_error_aio(res, env);
291291
break;
292292
default:
293-
return R_MissingArg;
293+
/* not reached */
294+
res = 0;
294295
}
295296

296297
return create_aio_msg(env, aio, raio, res);
@@ -353,7 +354,8 @@ SEXP rnng_aio_get_msg(SEXP env) {
353354

354355
break;
355356
default:
356-
return R_MissingArg;
357+
/* not reached */
358+
res = 0;
357359
}
358360

359361
return create_aio_msg(env, aio, raio, res);

src/core.c

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -231,44 +231,10 @@ inline SEXP R_mkClosure(SEXP formals, SEXP body, SEXP env) {
231231

232232
#endif
233233

234-
inline SEXP nano_PreserveObject(const SEXP x) {
235-
236-
SEXP tail = CDR(nano_precious);
237-
SEXP node = Rf_cons(nano_precious, tail);
238-
SETCDR(nano_precious, node);
239-
if (tail != R_NilValue)
240-
SETCAR(tail, node);
241-
SET_TAG(node, x);
242-
243-
return node;
244-
245-
}
246-
247-
inline void nano_ReleaseObject(SEXP node) {
248-
249-
SET_TAG(node, R_NilValue);
250-
SEXP head = CAR(node);
251-
SEXP tail = CDR(node);
252-
SETCDR(head, tail);
253-
if (tail != R_NilValue)
254-
SETCAR(tail, head);
255-
256-
}
257-
258-
void later2(void (*fun)(void *), void *data) {
234+
inline void later2(void (*fun)(void *), void *data) {
259235
eln2(fun, data, 0, 0);
260236
}
261237

262-
void raio_invoke_cb(void *arg) {
263-
264-
SEXP call, node = (SEXP) arg, x = TAG(node);
265-
PROTECT(call = Rf_lcons(nano_ResolveSymbol, Rf_cons(nano_aio_get_msg(x), R_NilValue)));
266-
Rf_eval(call, NANO_ENCLOS(x));
267-
UNPROTECT(1);
268-
nano_ReleaseObject(node);
269-
270-
}
271-
272238
inline int nano_integer(const SEXP x) {
273239
return (TYPEOF(x) == INTSXP || TYPEOF(x) == LGLSXP) ? NANO_INTEGER(x) : Rf_asInteger(x);
274240
}

src/nanonext.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,13 +285,12 @@ SEXP R_NewEnv(SEXP, int, int);
285285
#if R_VERSION < R_Version(4, 5, 0)
286286
SEXP R_mkClosure(SEXP, SEXP, SEXP);
287287
#endif
288-
SEXP nano_PreserveObject(const SEXP);
289-
void nano_ReleaseObject(SEXP);
290288
void dialer_finalizer(SEXP);
291289
void listener_finalizer(SEXP);
292290
void socket_finalizer(SEXP);
293291
void later2(void (*)(void *), void *);
294292
void raio_invoke_cb(void *);
293+
void haio_invoke_cb(void *);
295294
int nano_integer(const SEXP);
296295
SEXP mk_error(const int);
297296
SEXP mk_error_data(const int);

src/ncurl.c

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -69,17 +69,6 @@ static nano_buf nano_char_buf(const SEXP data) {
6969

7070
// aio completion callbacks ----------------------------------------------------
7171

72-
static void haio_invoke_cb(void *arg) {
73-
74-
SEXP call, status, node = (SEXP) arg, x = TAG(node);
75-
status = nano_aio_http_status(x);
76-
PROTECT(call = Rf_lcons(nano_ResolveSymbol, Rf_cons(status, R_NilValue)));
77-
Rf_eval(call, NANO_ENCLOS(x));
78-
UNPROTECT(1);
79-
nano_ReleaseObject(node);
80-
81-
}
82-
8372
static void haio_complete(void *arg) {
8473

8574
nano_aio *haio = (nano_aio *) arg;

src/sync.c

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,54 @@ static void nano_load_later(void) {
1616

1717
}
1818

19+
static inline SEXP nano_PreserveObject(const SEXP x) {
20+
21+
SEXP tail = CDR(nano_precious);
22+
SEXP node = Rf_cons(nano_precious, tail);
23+
SETCDR(nano_precious, node);
24+
if (tail != R_NilValue)
25+
SETCAR(tail, node);
26+
SET_TAG(node, x);
27+
28+
return node;
29+
30+
}
31+
32+
static inline void nano_ReleaseObject(SEXP node) {
33+
34+
SET_TAG(node, R_NilValue);
35+
SEXP head = CAR(node);
36+
SEXP tail = CDR(node);
37+
SETCDR(head, tail);
38+
if (tail != R_NilValue)
39+
SETCAR(tail, head);
40+
41+
}
42+
1943
// aio completion callbacks ----------------------------------------------------
2044

45+
46+
void raio_invoke_cb(void *arg) {
47+
48+
SEXP call, node = (SEXP) arg, x = TAG(node);
49+
PROTECT(call = Rf_lcons(nano_ResolveSymbol, Rf_cons(nano_aio_get_msg(x), R_NilValue)));
50+
Rf_eval(call, NANO_ENCLOS(x));
51+
UNPROTECT(1);
52+
nano_ReleaseObject(node);
53+
54+
}
55+
56+
void haio_invoke_cb(void *arg) {
57+
58+
SEXP call, status, node = (SEXP) arg, x = TAG(node);
59+
status = nano_aio_http_status(x);
60+
PROTECT(call = Rf_lcons(nano_ResolveSymbol, Rf_cons(status, R_NilValue)));
61+
Rf_eval(call, NANO_ENCLOS(x));
62+
UNPROTECT(1);
63+
nano_ReleaseObject(node);
64+
65+
}
66+
2167
static void sendaio_complete(void *arg) {
2268

2369
nng_aio *aio = ((nano_saio *) arg)->aio;

0 commit comments

Comments
 (0)