Skip to content

Commit b5b4e1b

Browse files
authored
Merge pull request #200 from r-lib/upkeep/avoid-Rf_allocSExp
2 parents ac99704 + 734b4f1 commit b5b4e1b

File tree

4 files changed

+22
-6
lines changed

4 files changed

+22
-6
lines changed

inst/tools/error-codes.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ code <- '
77
88
SEXP ps__define_errno(void) {
99
10-
SEXP env = PROTECT(Rf_allocSExp(ENVSXP));
10+
SEXP env = PROTECT(ps_new_env());
1111
1212
#define PS_ADD_ERRNO(err,str,val) \\
1313
defineVar( \\

src/common.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,29 @@
55
#define R_USE_C99_IN_CXX 1
66
#include <Rinternals.h>
77
#include <R_ext/Rdynload.h>
8+
#include <Rversion.h>
89

910
#include "ps-internal.h"
1011

1112
/* ---------------------------------------------------------------------*/
1213
/* Internals */
1314
/* ---------------------------------------------------------------------*/
1415

16+
static R_INLINE SEXP ps_new_env(void) {
17+
SEXP env;
18+
#if R_VERSION >= R_Version(4, 1, 0)
19+
PROTECT(env = R_NewEnv(R_EmptyEnv, 1, 29));
20+
#else
21+
PROTECT(env = Rf_allocSExp(ENVSXP));
22+
SET_FRAME(env, R_NilValue);
23+
SET_ENCLOS(env, R_EmptyEnv);
24+
SET_HASHTAB(env, R_NilValue);
25+
SET_ATTRIB(env, R_NilValue);
26+
#endif
27+
UNPROTECT(1);
28+
return env;
29+
}
30+
1531
extern int PS__TESTING;
1632
extern int PS__DEBUG;
1733

src/posix.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ SEXP psp__waitpid(SEXP r_pid) {
224224
}
225225
SEXP ps__define_signals(void) {
226226

227-
SEXP signalenv = PROTECT(Rf_allocSExp(ENVSXP));
227+
SEXP signalenv = PROTECT(ps_new_env());
228228

229229
#define PS_ADD_SIGNAL(sig) \
230230
defineVar(install(#sig), PROTECT(ScalarInteger(sig)), signalenv); \
@@ -358,7 +358,7 @@ SEXP ps__define_signals(void) {
358358

359359

360360
SEXP ps__define_socket_address_families(void) {
361-
SEXP afenv = PROTECT(Rf_allocSExp(ENVSXP));
361+
SEXP afenv = PROTECT(ps_new_env());
362362

363363
#define PS_ADD_AF(af) \
364364
defineVar(install(#af), PROTECT(ScalarInteger(af)), afenv); \
@@ -508,7 +508,7 @@ SEXP ps__define_socket_address_families(void) {
508508
}
509509

510510
SEXP ps__define_socket_types(void) {
511-
SEXP env = PROTECT(Rf_allocSExp(ENVSXP));
511+
SEXP env = PROTECT(ps_new_env());
512512

513513
#define PS_ADD_SOCKET_TYPE(type) \
514514
defineVar(install(#type), PROTECT(ScalarInteger(type)), env); \

src/windows.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,7 @@ SEXP ps__define_tcp_statuses(void) {
763763
}
764764

765765
SEXP ps__define_socket_address_families() {
766-
SEXP afenv = PROTECT(Rf_allocSExp(ENVSXP));
766+
SEXP afenv = PROTECT(ps_new_env());
767767

768768
defineVar(install("AF_INET"), PROTECT(ScalarInteger(AF_INET)), afenv);
769769
UNPROTECT(1);
@@ -776,7 +776,7 @@ SEXP ps__define_socket_address_families() {
776776
}
777777

778778
SEXP ps__define_socket_types() {
779-
SEXP env = PROTECT(Rf_allocSExp(ENVSXP));
779+
SEXP env = PROTECT(ps_new_env());
780780

781781
defineVar(install("SOCK_STREAM"), PROTECT(ScalarInteger(SOCK_STREAM)), env);
782782
UNPROTECT(1);

0 commit comments

Comments
 (0)