Skip to content

Commit

Permalink
avoids DATAPTR
Browse files Browse the repository at this point in the history
  • Loading branch information
shikokuchuo committed Jun 23, 2024
1 parent 2ff6ad7 commit 28905e2
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 44 deletions.
30 changes: 15 additions & 15 deletions src/aio.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ static inline SEXP R_mkClosure(SEXP formals, SEXP body, SEXP env) {

#endif

inline int R_Integer(SEXP x) {
inline int nano_integer(SEXP x) {
int out;
switch (TYPEOF(x)) {
case INTSXP:
case LGLSXP:
out = *(int *) DATAPTR_RO(x);
out = NANO_INTEGER(x);
break;
default:
out = Rf_asInteger(x);
Expand Down Expand Up @@ -576,7 +576,7 @@ SEXP rnng_aio_call(SEXP x) {
case VECSXP: ;
const R_xlen_t xlen = Rf_xlength(x);
for (R_xlen_t i = 0; i < xlen; i++) {
rnng_aio_call(R_VECTOR(x)[i]);
rnng_aio_call(NANO_VECTOR(x)[i]);
}
break;
}
Expand All @@ -599,7 +599,7 @@ static SEXP rnng_aio_collect_impl(SEXP x, SEXP (*const func)(SEXP)) {
const R_xlen_t xlen = Rf_xlength(x);
PROTECT(out = Rf_allocVector(VECSXP, xlen));
for (R_xlen_t i = 0; i < xlen; i++) {
env = func(R_VECTOR(x)[i]);
env = func(NANO_VECTOR(x)[i]);
if (TYPEOF(env) != ENVSXP) goto exit;
env = Rf_findVarInFrame(env, nano_ValueSymbol);
if (env == R_UnboundValue) goto exit;
Expand Down Expand Up @@ -642,7 +642,7 @@ SEXP rnng_aio_stop(SEXP x) {
case VECSXP: ;
const R_xlen_t xlen = Rf_xlength(x);
for (R_xlen_t i = 0; i < xlen; i++) {
rnng_aio_stop(R_VECTOR(x)[i]);
rnng_aio_stop(NANO_VECTOR(x)[i]);
}
break;
}
Expand Down Expand Up @@ -696,7 +696,7 @@ SEXP rnng_unresolved(SEXP x) {
case VECSXP: ;
const R_xlen_t xlen = Rf_xlength(x);
for (R_xlen_t i = 0; i < xlen; i++) {
if (rnng_unresolved_impl(R_VECTOR(x)[i]))
if (rnng_unresolved_impl(NANO_VECTOR(x)[i]))
return Rf_ScalarLogical(1);
}
}
Expand Down Expand Up @@ -728,7 +728,7 @@ SEXP rnng_unresolved2(SEXP x) {
int xc = 0;
const R_xlen_t xlen = Rf_xlength(x);
for (R_xlen_t i = 0; i < xlen; i++) {
xc += rnng_unresolved2_impl(R_VECTOR(x)[i]);
xc += rnng_unresolved2_impl(NANO_VECTOR(x)[i]);
}
return Rf_ScalarInteger(xc);
}
Expand All @@ -741,7 +741,7 @@ SEXP rnng_unresolved2(SEXP x) {

SEXP rnng_send_aio(SEXP con, SEXP data, SEXP mode, SEXP timeout, SEXP clo) {

const nng_duration dur = timeout == R_NilValue ? NNG_DURATION_DEFAULT : (nng_duration) R_Integer(timeout);
const nng_duration dur = timeout == R_NilValue ? NNG_DURATION_DEFAULT : (nng_duration) nano_integer(timeout);
nano_aio *saio;
SEXP aio;
nano_buf buf;
Expand Down Expand Up @@ -837,7 +837,7 @@ SEXP rnng_send_aio(SEXP con, SEXP data, SEXP mode, SEXP timeout, SEXP clo) {

SEXP rnng_recv_aio(SEXP con, SEXP mode, SEXP timeout, SEXP cvar, SEXP bytes, SEXP clo) {

const nng_duration dur = timeout == R_NilValue ? NNG_DURATION_DEFAULT : (nng_duration) R_Integer(timeout);
const nng_duration dur = timeout == R_NilValue ? NNG_DURATION_DEFAULT : (nng_duration) nano_integer(timeout);
const int signal = TAG(cvar) == nano_CvSymbol;
nano_cv *ncv = signal ? (nano_cv *) R_ExternalPtrAddr(cvar) : NULL;
nano_aio *raio;
Expand Down Expand Up @@ -866,7 +866,7 @@ SEXP rnng_recv_aio(SEXP con, SEXP mode, SEXP timeout, SEXP cvar, SEXP bytes, SEX
} else if (ptrtag == nano_StreamSymbol) {

mod = nano_matchargs(mode);
const size_t xlen = (size_t) R_Integer(bytes);
const size_t xlen = (size_t) nano_integer(bytes);
nng_stream **sp = (nng_stream **) R_ExternalPtrAddr(con);
nng_iov iov;

Expand Down Expand Up @@ -922,7 +922,7 @@ SEXP rnng_ncurl_aio(SEXP http, SEXP convert, SEXP method, SEXP headers, SEXP dat

const char *httr = CHAR(STRING_ELT(http, 0));
const char *mthd = method != R_NilValue ? CHAR(STRING_ELT(method, 0)) : NULL;
const nng_duration dur = timeout == R_NilValue ? NNG_DURATION_DEFAULT : (nng_duration) R_Integer(timeout);
const nng_duration dur = timeout == R_NilValue ? NNG_DURATION_DEFAULT : (nng_duration) nano_integer(timeout);
if (tls != R_NilValue && TAG(tls) != nano_TlsSymbol)
Rf_error("'tls' is not a valid TLS Configuration");
nano_aio *haio = R_Calloc(1, nano_aio);
Expand Down Expand Up @@ -1101,7 +1101,7 @@ static SEXP rnng_aio_http_impl(SEXP env, const int typ) {
} else {
vec = Rf_allocVector(RAWSXP, sz);
if (dat != NULL)
memcpy(DATAPTR(vec), dat, sz);
memcpy(NANO_DATAPTR(vec), dat, sz);
}
Rf_defineVar(nano_ValueSymbol, vec, env);

Expand Down Expand Up @@ -1135,7 +1135,7 @@ SEXP rnng_ncurl_session(SEXP http, SEXP convert, SEXP method, SEXP headers, SEXP

const char *httr = CHAR(STRING_ELT(http, 0));
const char *mthd = method != R_NilValue ? CHAR(STRING_ELT(method, 0)) : NULL;
const nng_duration dur = timeout == R_NilValue ? NNG_DURATION_DEFAULT : (nng_duration) R_Integer(timeout);
const nng_duration dur = timeout == R_NilValue ? NNG_DURATION_DEFAULT : (nng_duration) nano_integer(timeout);
if (tls != R_NilValue && TAG(tls) != nano_TlsSymbol)
Rf_error("'tls' is not a valid TLS Configuration");

Expand Down Expand Up @@ -1292,7 +1292,7 @@ SEXP rnng_ncurl_transact(SEXP session) {
} else {
vec = Rf_allocVector(RAWSXP, sz);
if (dat != NULL)
memcpy(DATAPTR(vec), dat, sz);
memcpy(NANO_DATAPTR(vec), dat, sz);
}
SET_VECTOR_ELT(out, 2, vec);

Expand Down Expand Up @@ -1324,7 +1324,7 @@ SEXP rnng_request(SEXP con, SEXP data, SEXP sendmode, SEXP recvmode, SEXP timeou
if (TAG(con) != nano_ContextSymbol)
Rf_error("'con' is not a valid Context");

const nng_duration dur = timeout == R_NilValue ? NNG_DURATION_DEFAULT : (nng_duration) R_Integer(timeout);
const nng_duration dur = timeout == R_NilValue ? NNG_DURATION_DEFAULT : (nng_duration) nano_integer(timeout);
const int mod = nano_matcharg(recvmode);
int signal, drop;
if (cvar == R_NilValue) {
Expand Down
38 changes: 19 additions & 19 deletions src/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ SEXP rawToChar(const unsigned char *buf, const size_t sz) {
if (sz - i > 1) {
REprintf("data could not be converted to a character string\n");
out = Rf_allocVector(RAWSXP, sz);
memcpy(DATAPTR(out), buf, sz);
memcpy(NANO_DATAPTR(out), buf, sz);
return out;
}

Expand Down Expand Up @@ -152,7 +152,7 @@ static SEXP nano_inHook(SEXP x, SEXP fun) {
PROTECT(newlist = Rf_allocVector(VECSXP, xlen + 1));
PROTECT(newnames = Rf_allocVector(STRSXP, xlen + 1));
for (R_xlen_t i = 0; i < xlen; i++) {
SET_VECTOR_ELT(newlist, i, R_VECTOR(list)[i]);
SET_VECTOR_ELT(newlist, i, NANO_VECTOR(list)[i]);
SET_STRING_ELT(newnames, i, STRING_ELT(names, i));
}
SET_VECTOR_ELT(newlist, xlen, x);
Expand All @@ -169,7 +169,7 @@ static SEXP nano_inHook(SEXP x, SEXP fun) {
static SEXP nano_outHook(SEXP x, SEXP fun) {

const long i = atol(CHAR(*(SEXP *) DATAPTR_RO(x))) - 1;
return R_VECTOR(fun)[i];
return NANO_VECTOR(fun)[i];

}

Expand Down Expand Up @@ -260,7 +260,7 @@ void nano_serialize_next(nano_buf *buf, const SEXP object) {
buf->cur += sizeof(R_xlen_t);

for (R_xlen_t i = 0; i < llen; i++) {
PROTECT(call = Rf_lcons(func, Rf_cons(R_VECTOR(refList)[i], R_NilValue)));
PROTECT(call = Rf_lcons(func, Rf_cons(NANO_VECTOR(refList)[i], R_NilValue)));
PROTECT(out = R_UnwindProtect(eval_safe, call, rl_reset, NULL, NULL));
if (TYPEOF(out) == RAWSXP) {
R_xlen_t xlen = XLENGTH(out);
Expand Down Expand Up @@ -306,7 +306,7 @@ SEXP nano_unserialize(unsigned char *buf, const size_t sz) {
SEXP raw, call;
if (reg == 1) {
PROTECT(raw = Rf_allocVector(RAWSXP, sz - offset));
memcpy(DATAPTR(raw), buf + offset, sz - offset);
memcpy(NANO_DATAPTR(raw), buf + offset, sz - offset);
PROTECT(call = Rf_lcons(CADR(nano_refHook), Rf_cons(raw, R_NilValue)));
reflist = Rf_eval(call, R_GlobalEnv);
SET_TAG(nano_refHook, reflist);
Expand All @@ -322,7 +322,7 @@ SEXP nano_unserialize(unsigned char *buf, const size_t sz) {
memcpy(&xlen, buf + cur, sizeof(R_xlen_t));
cur += sizeof(R_xlen_t);
PROTECT(raw = Rf_allocVector(RAWSXP, xlen));
memcpy(DATAPTR(raw), buf + cur, xlen);
memcpy(NANO_DATAPTR(raw), buf + cur, xlen);
cur += xlen;
PROTECT(call = Rf_lcons(func, Rf_cons(raw, R_NilValue)));
out = Rf_eval(call, R_GlobalEnv);
Expand Down Expand Up @@ -589,7 +589,7 @@ SEXP nano_decode(unsigned char *buf, const size_t sz, const int mod) {
return data;
}

memcpy(DATAPTR(data), buf, sz);
memcpy(NANO_DATAPTR(data), buf, sz);
return data;

}
Expand Down Expand Up @@ -903,7 +903,7 @@ SEXP rnng_listener_close(SEXP listener) {

SEXP rnng_send(SEXP con, SEXP data, SEXP mode, SEXP block) {

const int flags = block == R_NilValue ? NNG_DURATION_DEFAULT : TYPEOF(block) == LGLSXP ? 0 : R_Integer(block);
const int flags = block == R_NilValue ? NNG_DURATION_DEFAULT : TYPEOF(block) == LGLSXP ? 0 : nano_integer(block);
nano_buf buf;
int xc;

Expand Down Expand Up @@ -1046,7 +1046,7 @@ SEXP rnng_send(SEXP con, SEXP data, SEXP mode, SEXP block) {

SEXP rnng_recv(SEXP con, SEXP mode, SEXP block, SEXP bytes) {

const int flags = block == R_NilValue ? NNG_DURATION_DEFAULT : TYPEOF(block) == LGLSXP ? 0 : R_Integer(block);
const int flags = block == R_NilValue ? NNG_DURATION_DEFAULT : TYPEOF(block) == LGLSXP ? 0 : nano_integer(block);
int mod, xc;
unsigned char *buf;
size_t sz;
Expand Down Expand Up @@ -1131,7 +1131,7 @@ SEXP rnng_recv(SEXP con, SEXP mode, SEXP block, SEXP bytes) {
} else if (ptrtag == nano_StreamSymbol) {

mod = nano_matchargs(mode);
const size_t xlen = (size_t) R_Integer(bytes);
const size_t xlen = (size_t) nano_integer(bytes);
nng_stream **sp = (nng_stream **) R_ExternalPtrAddr(con);
nng_iov iov;
nng_aio *aiop;
Expand Down Expand Up @@ -1196,7 +1196,7 @@ SEXP rnng_set_opt(SEXP object, SEXP opt, SEXP value) {
break;
case REALSXP:
case INTSXP:
val = R_Integer(value);
val = nano_integer(value);
xc = nng_socket_set_ms(*sock, op, (nng_duration) val);
if (xc == 0) break;
xc = nng_socket_set_size(*sock, op, (size_t) val);
Expand Down Expand Up @@ -1224,7 +1224,7 @@ SEXP rnng_set_opt(SEXP object, SEXP opt, SEXP value) {
break;
case REALSXP:
case INTSXP:
val = R_Integer(value);
val = nano_integer(value);
xc = nng_ctx_set_ms(*ctx, op, (nng_duration) val);
if (xc == 0) break;
xc = nng_ctx_set_size(*ctx, op, (size_t) val);
Expand Down Expand Up @@ -1252,7 +1252,7 @@ SEXP rnng_set_opt(SEXP object, SEXP opt, SEXP value) {
break;
case REALSXP:
case INTSXP:
val = R_Integer(value);
val = nano_integer(value);
xc = nng_stream_set_ms(*st, op, (nng_duration) val);
if (xc == 0) break;
xc = nng_stream_set_size(*st, op, (size_t) val);
Expand Down Expand Up @@ -1280,7 +1280,7 @@ SEXP rnng_set_opt(SEXP object, SEXP opt, SEXP value) {
break;
case REALSXP:
case INTSXP:
val = R_Integer(value);
val = nano_integer(value);
xc = nng_listener_set_ms(*list, op, (nng_duration) val);
if (xc == 0) break;
xc = nng_listener_set_size(*list, op, (size_t) val);
Expand Down Expand Up @@ -1308,7 +1308,7 @@ SEXP rnng_set_opt(SEXP object, SEXP opt, SEXP value) {
break;
case REALSXP:
case INTSXP:
val = R_Integer(value);
val = nano_integer(value);
xc = nng_dialer_set_ms(*dial, op, (nng_duration) val);
if (xc == 0) break;
xc = nng_dialer_set_size(*dial, op, (size_t) val);
Expand Down Expand Up @@ -1562,8 +1562,8 @@ SEXP rnng_next_config(SEXP refhook, SEXP klass, SEXP list, SEXP mark) {
case VECSXP:
if (Rf_xlength(refhook) != 2)
return nano_refHook;
typ1 = TYPEOF(R_VECTOR(refhook)[0]);
typ2 = TYPEOF(R_VECTOR(refhook)[1]);
typ1 = TYPEOF(NANO_VECTOR(refhook)[0]);
typ2 = TYPEOF(NANO_VECTOR(refhook)[1]);
plist = 0;
break;
case NILSXP:
Expand All @@ -1577,8 +1577,8 @@ SEXP rnng_next_config(SEXP refhook, SEXP klass, SEXP list, SEXP mark) {
if ((typ1 == CLOSXP || typ1 == SPECIALSXP || typ1 == BUILTINSXP) &&
(typ2 == CLOSXP || typ2 == SPECIALSXP || typ2 == BUILTINSXP)) {

SETCAR(nano_refHook, plist ? CAR(refhook) : R_VECTOR(refhook)[0]);
SETCADR(nano_refHook, plist ? CADR(refhook) : R_VECTOR(refhook)[1]);
SETCAR(nano_refHook, plist ? CAR(refhook) : NANO_VECTOR(refhook)[0]);
SETCADR(nano_refHook, plist ? CADR(refhook) : NANO_VECTOR(refhook)[1]);
SETCAR(nano_klassString, STRING_ELT(klass, 0));

registered = NANO_INTEGER(list) ? 1 : 2;
Expand Down
9 changes: 5 additions & 4 deletions src/nanonext.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,10 @@ typedef struct nano_thread_duo_s {
#include <Rversion.h>
#include <R_ext/Visibility.h>

#define R_VECTOR(x) ((const SEXP *) DATAPTR_RO(x))
#define NANO_DATAPTR(x) (void *) DATAPTR_RO(x)
#define NANO_VECTOR(x) ((const SEXP *) DATAPTR_RO(x))
#define NANO_INTEGER(x) *(int *) DATAPTR_RO(x)
#define NANO_ERROR(x) { Rf_error(x); return R_NilValue; }

#define ERROR_OUT(xc) Rf_error("%d | %s", xc, nng_strerror(xc))
#define ERROR_RET(xc) { Rf_warning("%d | %s", xc, nng_strerror(xc)); return mk_error(xc); }
Expand All @@ -203,8 +206,6 @@ typedef struct nano_thread_duo_s {
(x)->len = 0; \
(x)->cur = sz
#define NANO_FREE(x) if (x.len) R_Free(x.buf)
#define NANO_INTEGER(x) *(int *) DATAPTR_RO(x)
#define NANO_ERROR(x) { Rf_error(x); return R_NilValue; }
#define NANO_CLASS2(x, cls1, cls2) \
SEXP klass = Rf_allocVector(STRSXP, 2); \
Rf_classgets(x, klass); \
Expand All @@ -220,7 +221,7 @@ void later2(void (*)(void *), void *);
extern void (*eln2)(void (*)(void *), void *, double, int);
void eln2dummy(void (*)(void *), void *, double, int);

int R_Integer(SEXP);
int nano_integer(SEXP);
SEXP nano_PreserveObject(SEXP);
void nano_ReleaseObject(SEXP);
SEXP mk_error(const int);
Expand Down
2 changes: 1 addition & 1 deletion src/thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ SEXP rnng_wait_thread_create(SEXP x) {

const R_xlen_t xlen = Rf_xlength(x);
for (R_xlen_t i = 0; i < xlen; i++) {
rnng_wait_thread_create(R_VECTOR(x)[i]);
rnng_wait_thread_create(NANO_VECTOR(x)[i]);
}

}
Expand Down
2 changes: 1 addition & 1 deletion src/tls.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ SEXP rnng_random(SEXP n, SEXP convert) {
out = nano_hash_char(buf, sz);
} else {
out = Rf_allocVector(RAWSXP, sz);
memcpy(DATAPTR(out), buf, sz);
memcpy(NANO_DATAPTR(out), buf, sz);
}

return out;
Expand Down
8 changes: 4 additions & 4 deletions src/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ static void tls_finalizer(SEXP xptr) {

SEXP rnng_strerror(SEXP error) {

const int xc = R_Integer(error);
const int xc = nano_integer(error);
char nano_errbuf[NANONEXT_ERR_STRLEN];
snprintf(nano_errbuf, NANONEXT_ERR_STRLEN, "%d | %s", xc, nng_strerror(xc));

Expand Down Expand Up @@ -161,7 +161,7 @@ SEXP rnng_ncurl(SEXP http, SEXP convert, SEXP follow, SEXP method, SEXP headers,

const char *addr = CHAR(STRING_ELT(http, 0));
const char *mthd = method != R_NilValue ? CHAR(STRING_ELT(method, 0)) : NULL;
const nng_duration dur = timeout == R_NilValue ? NNG_DURATION_DEFAULT : (nng_duration) R_Integer(timeout);
const nng_duration dur = timeout == R_NilValue ? NNG_DURATION_DEFAULT : (nng_duration) nano_integer(timeout);
if (tls != R_NilValue && TAG(tls) != nano_TlsSymbol)
Rf_error("'tls' is not a valid TLS Configuration");
int chk_resp = response != R_NilValue && TYPEOF(response) == STRSXP;
Expand Down Expand Up @@ -302,7 +302,7 @@ SEXP rnng_ncurl(SEXP http, SEXP convert, SEXP follow, SEXP method, SEXP headers,
} else {
vec = Rf_allocVector(RAWSXP, sz);
if (dat != NULL)
memcpy(DATAPTR(vec), dat, sz);
memcpy(NANO_DATAPTR(vec), dat, sz);
}
SET_VECTOR_ELT(out, 2, vec);

Expand Down Expand Up @@ -532,7 +532,7 @@ SEXP rnng_stream_close(SEXP stream) {

SEXP rnng_status_code(SEXP x) {

const int status = R_Integer(x);
const int status = nano_integer(x);
char *code;
switch (status) {
case 100: code = "Continue"; break;
Expand Down

0 comments on commit 28905e2

Please sign in to comment.