From 6bf400533009a503e560cc06072ff50b6a4e9067 Mon Sep 17 00:00:00 2001 From: Michael Chirico Date: Mon, 19 Aug 2024 19:17:00 -0700 Subject: [PATCH] Use recommended R_ prefixed versions of Calloc,Realloc,Free (#6382) * Use recommended R_ prefixed versions of Calloc,Realloc,Free * Also remove !defined(R_VERSION) guard --- inst/tests/tests.Rraw | 2 +- src/bmerge.c | 18 +++++++++--------- src/data.table.h | 7 +++++-- src/forder.c | 2 +- src/ijoin.c | 6 +++--- src/init.c | 2 +- src/uniqlist.c | 14 +++++++------- 7 files changed, 27 insertions(+), 24 deletions(-) diff --git a/inst/tests/tests.Rraw b/inst/tests/tests.Rraw index fc36c67a9..f86ec3ee4 100644 --- a/inst/tests/tests.Rraw +++ b/inst/tests/tests.Rraw @@ -13178,7 +13178,7 @@ options(datatable.use.index=FALSE) test(1942.10, DT[,sum(v),keyby=id1,verbose=TRUE], data.table(id1=c("D","A","C"), V1=INT(1,6,8), key="id1"), output="Finding groups using forderv") options(datatable.use.index=TRUE) -# test coverage in uniqlist.c of Realloc when over initial 1000 uniqs, and use double to cover numeric tolerance (both in one-column case and >1 column branch) +# test coverage in uniqlist.c of R_Realloc when over initial 1000 uniqs, and use double to cover numeric tolerance (both in one-column case and >1 column branch) set.seed(2) DT = data.table(real=sample((1:1500)/1000, 10000, replace=TRUE), id=sample(letters, 1000, replace=TRUE), value=1:10000) setkey(DT,id,real) diff --git a/src/bmerge.c b/src/bmerge.c index f12a56f01..64b0cafa7 100644 --- a/src/bmerge.c +++ b/src/bmerge.c @@ -130,9 +130,9 @@ SEXP bmerge(SEXP idt, SEXP xdt, SEXP icolsArg, SEXP xcolsArg, SEXP xoArg, SEXP r if (nqmaxgrp>1 && mult == ALL) { // non-equi case with mult=ALL, may need reallocation anslen = 1.1 * ((iN > 1000) ? iN : 1000); - retFirst = Calloc(anslen, int); // anslen is set above - retLength = Calloc(anslen, int); - retIndex = Calloc(anslen, int); + retFirst = R_Calloc(anslen, int); // anslen is set above + retLength = R_Calloc(anslen, int); + retIndex = R_Calloc(anslen, int); // initialise retIndex here directly, as next loop is meant for both equi and non-equi joins for (int j=0; j 1 && mult == ALL) { - Free(retFirst); - Free(retLength); - Free(retIndex); + R_Free(retFirst); + R_Free(retLength); + R_Free(retIndex); } if (verbose) Rprintf("bmerge: took %.3fs\n", omp_get_wtime()-tic); @@ -416,9 +416,9 @@ void bmerge_r(int xlowIn, int xuppIn, int ilowIn, int iuppIn, int col, int thisg ++ctr; if (ctr+ilen >= anslen) { anslen = 1.1*anslen; - retFirst = Realloc(retFirst, anslen, int); // if fails, it fails inside Realloc with R error - retLength = Realloc(retLength, anslen, int); - retIndex = Realloc(retIndex, anslen, int); + retFirst = R_Realloc(retFirst, anslen, int); // if fails, it fails inside R_Realloc with R error + retLength = R_Realloc(retLength, anslen, int); + retIndex = R_Realloc(retIndex, anslen, int); } } else if (mult == FIRST) { retFirst[k] = (XIND(retFirst[k]-1) > XIND(xlow+1)) ? xlow+2 : retFirst[k]; diff --git a/src/data.table.h b/src/data.table.h index 49f3f1634..4d4bed225 100644 --- a/src/data.table.h +++ b/src/data.table.h @@ -2,12 +2,15 @@ #include "dt_stdio.h" // PRId64 and PRIu64 #include #include -#if !defined(R_VERSION) || R_VERSION < R_Version(3, 5, 0) // R-exts$6.14 +#if R_VERSION < R_Version(3, 5, 0) // R-exts$6.14 # define ALTREP(x) 0 // #2866 # define USE_RINTERNALS // #3301 # define DATAPTR_RO(x) ((const void *)DATAPTR(x)) +# define R_Calloc(x, y) Calloc(x, y) // #6380 +# define R_Realloc(x, y, z) Realloc(x, y, z) +# define R_Free(x) Free(x) #endif -#if !defined(R_VERSION) || R_VERSION < R_Version(3, 4, 0) +#if R_VERSION < R_Version(3, 4, 0) # define SET_GROWABLE_BIT(x) // #3292 #endif #include diff --git a/src/forder.c b/src/forder.c index db2197443..7e53c4a75 100644 --- a/src/forder.c +++ b/src/forder.c @@ -65,7 +65,7 @@ static char msg[1001]; /* Using OS realloc() in this file to benefit from (often) in-place realloc() to save copy * We have to trap on exit anyway to call savetl_end(). * NB: R_alloc() would be more convenient (fails within) and robust (auto free) but there is no R_realloc(). Implementing R_realloc() would be an alloc and copy, iiuc. - * Calloc/Realloc needs to be Free'd, even before error() [R-exts$6.1.2]. An oom within Calloc causes a previous Calloc to leak so Calloc would still needs to be trapped anyway. + * R_Calloc/R_Realloc needs to be R_Free'd, even before error() [R-exts$6.1.2]. An oom within R_Calloc causes a previous R_Calloc to leak so R_Calloc would still needs to be trapped anyway. * Therefore, using <> approach to cleanup() on error. */ diff --git a/src/ijoin.c b/src/ijoin.c index b4f0a4b08..efb88c858 100644 --- a/src/ijoin.c +++ b/src/ijoin.c @@ -141,7 +141,7 @@ SEXP lookup(SEXP ux, SEXP xlen, SEXP indices, SEXP gaps, SEXP overlaps, SEXP mul Rprintf(_("Second pass on allocation in lookup ... done in %8.3f seconds\n"), 1.0*(pass2)/CLOCKS_PER_SEC); // generate lookup start = clock(); - idx = Calloc(uxrows, R_len_t); // resets bits, =0 + idx = R_Calloc(uxrows, R_len_t); // resets bits, =0 switch (type) { case ANY: case START: case END: case WITHIN: for (int i=0; i=isize) { \ isize = MIN(nrow, (size_t)(1.1*(double)isize*((double)nrow/i))); \ - iidx = Realloc(iidx, isize, int); \ + iidx = R_Realloc(iidx, isize, int); \ } \ } \ prev = elem; \ @@ -134,14 +134,14 @@ SEXP uniqlist(SEXP l, SEXP order) iidx[len++] = i+1; if (len >= isize) { isize = MIN(nrow, (size_t)(1.1*(double)isize*((double)nrow/i))); - iidx = Realloc(iidx, isize, int); + iidx = R_Realloc(iidx, isize, int); } } } } PROTECT(ans = allocVector(INTSXP, len)); memcpy(INTEGER(ans), iidx, sizeof(int)*len); // sizeof is of type size_t - no integer overflow issues - Free(iidx); + R_Free(iidx); UNPROTECT(1); return(ans); } @@ -259,7 +259,7 @@ SEXP nestedid(SEXP l, SEXP cols, SEXP order, SEXP grps, SEXP resetvals, SEXP mul R_len_t nrows = length(VECTOR_ELT(l,0)), ncols = length(cols); if (nrows==0) return(allocVector(INTSXP, 0)); R_len_t thisi, previ, ansgrpsize=1000, nansgrp=0; - R_len_t *ansgrp = Calloc(ansgrpsize, R_len_t), starts, grplen; // #3401 fix. Needs to be Calloc due to Realloc below .. else segfaults. + R_len_t *ansgrp = R_Calloc(ansgrpsize, R_len_t), starts, grplen; // #3401 fix. Needs to be R_Calloc due to R_Realloc below .. else segfaults. R_len_t ngrps = length(grps); bool *i64 = (bool *)R_alloc(ncols, sizeof(bool)); if (ngrps==0) error(_("Internal error: nrows[%d]>0 but ngrps==0"), nrows); // # nocov @@ -335,14 +335,14 @@ SEXP nestedid(SEXP l, SEXP cols, SEXP order, SEXP grps, SEXP resetvals, SEXP mul } if (nansgrp >= ansgrpsize) { ansgrpsize = MIN(nrows, (size_t)(1.1*(double)ansgrpsize*((double)nrows/i))); - ansgrp = Realloc(ansgrp, ansgrpsize, int); + ansgrp = R_Realloc(ansgrp, ansgrpsize, int); } for (int j=0; j