Skip to content

Commit 82e66e7

Browse files
improved consistency and clarity around allocation functions (#7029)
* more thorough analysis * linter update * terminal newline * restore terminal newline * restore terminal newline * restore terminal newlines * restore terminal newline --------- Co-authored-by: Michael Chirico <[email protected]>
1 parent 6725de1 commit 82e66e7

File tree

21 files changed

+118
-112
lines changed

21 files changed

+118
-112
lines changed

.ci/linters/cocci/malloc_return_value_cast.cocci

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,9 @@ expression E;
44
@@
55
- (T)
66
malloc(E)
7+
8+
- (T)
9+
calloc(_, E)
10+
11+
- (T)
12+
realloc(_, E)

src/assign.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ SEXP assign(SEXP dt, SEXP rows, SEXP cols, SEXP newcolnames, SEXP values)
400400
// FR #2077 - set able to add new cols by reference
401401
if (isString(cols)) {
402402
PROTECT(tmp = chmatch(cols, names, 0)); protecti++;
403-
buf = (int *) R_alloc(length(cols), sizeof(int));
403+
buf = (int *) R_alloc(length(cols), sizeof(*buf));
404404
int k=0;
405405
for (int i=0; i<length(cols); ++i) {
406406
if (INTEGER(tmp)[i] == 0) buf[k++] = i;
@@ -699,7 +699,7 @@ SEXP assign(SEXP dt, SEXP rows, SEXP cols, SEXP newcolnames, SEXP values)
699699
}
700700
if (ndelete) {
701701
// delete any columns assigned NULL (there was a 'continue' earlier in loop above)
702-
int *tt = (int *)R_alloc(ndelete, sizeof(int));
702+
int *tt = (int *)R_alloc(ndelete, sizeof(*tt));
703703
const int *colsd=INTEGER(cols), ncols=length(cols), ndt=length(dt);
704704
for (int i=0, k=0; i<ncols; ++i) { // find which ones to delete and put them in tt
705705
// Aside: a new column being assigned NULL (something odd to do) would have been warned above, added above, and now deleted. Just
@@ -1055,7 +1055,7 @@ const char *memrecycle(const SEXP target, const SEXP where, const int start, con
10551055
case RAWSXP: BODY(Rbyte, RAW, int, val!=0, td[i]=cval)
10561056
case LGLSXP:
10571057
if (mc) {
1058-
memcpy(td, LOGICAL_RO(source), slen*sizeof(int)); break;
1058+
memcpy(td, LOGICAL_RO(source), slen*sizeof(*td)); break;
10591059
} else BODY(int, LOGICAL, int, val, td[i]=cval)
10601060
case INTSXP: BODY(int, INTEGER, int, val==NA_INTEGER ? NA_LOGICAL : val!=0, td[i]=cval)
10611061
case REALSXP:
@@ -1072,7 +1072,7 @@ const char *memrecycle(const SEXP target, const SEXP where, const int start, con
10721072
case LGLSXP: // same as INTSXP ...
10731073
case INTSXP:
10741074
if (mc) {
1075-
memcpy(td, INTEGER_RO(source), slen*sizeof(int)); break;
1075+
memcpy(td, INTEGER_RO(source), slen*sizeof(*td)); break;
10761076
} else BODY(int, INTEGER, int, val, td[i]=cval)
10771077
case REALSXP:
10781078
if (sourceIsI64)
@@ -1092,7 +1092,7 @@ const char *memrecycle(const SEXP target, const SEXP where, const int start, con
10921092
case REALSXP:
10931093
if (sourceIsI64) {
10941094
if (mc) {
1095-
memcpy(td, (const int64_t *)REAL_RO(source), slen*sizeof(int64_t)); break;
1095+
memcpy(td, (const int64_t *)REAL_RO(source), slen*sizeof(*td)); break;
10961096
} else BODY(int64_t, REAL, int64_t, val, td[i]=cval)
10971097
} else BODY(double, REAL, int64_t, within_int64_repres(val) ? val : NA_INTEGER64, td[i]=cval)
10981098
case CPLXSXP: BODY(Rcomplex, COMPLEX, int64_t, ISNAN(val.r) ? NA_INTEGER64 : (int64_t)val.r, td[i]=cval)
@@ -1291,14 +1291,14 @@ void savetl(SEXP s)
12911291
internal_error(__func__, "reached maximum %d items for savetl", nalloc); // # nocov
12921292
}
12931293
nalloc = nalloc>(INT_MAX/2) ? INT_MAX : nalloc*2;
1294-
char *tmp = (char *)realloc(saveds, nalloc*sizeof(SEXP));
1294+
char *tmp = realloc(saveds, sizeof(SEXP)*nalloc);
12951295
if (tmp==NULL) {
12961296
// C spec states that if realloc() fails the original block is left untouched; it is not freed or moved. We rely on that here.
12971297
savetl_end(); // # nocov free(saveds) happens inside savetl_end
12981298
error(_("Failed to realloc saveds to %d items in savetl"), nalloc); // # nocov
12991299
}
13001300
saveds = (SEXP *)tmp;
1301-
tmp = (char *)realloc(savedtl, nalloc*sizeof(R_len_t));
1301+
tmp = realloc(savedtl, sizeof(R_len_t)*nalloc);
13021302
if (tmp==NULL) {
13031303
savetl_end(); // # nocov
13041304
error(_("Failed to realloc savedtl to %d items in savetl"), nalloc); // # nocov

src/chmatch.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ static SEXP chmatchMain(SEXP x, SEXP table, int nomatch, bool chin, bool chmatch
9696
// For example: A,B,C,B,D,E,A,A => A(TL=1),B(2),C(3),D(4),E(5) => dupMap 1 2 3 5 6 | 8 7 4
9797
// dupLink 7 8 | 6 (blank=0)
9898
unsigned int mapsize = tablelen+nuniq; // lto compilation warning #5760 // +nuniq to store a 0 at the end of each group
99-
int *counts = (int *)calloc(nuniq, sizeof(int));
100-
int *map = (int *)calloc(mapsize, sizeof(int));
99+
int *counts = calloc(nuniq, sizeof(*counts));
100+
int *map = calloc(mapsize, sizeof(*map));
101101
if (!counts || !map) {
102102
// # nocov start
103103
free(counts); free(map);

src/cj.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ SEXP cj(SEXP base_list) {
3636
}
3737
#pragma omp parallel for num_threads(getDTthreads(ncopy*blocklen, true))
3838
for (int i=1; i<ncopy; ++i) {
39-
memcpy(targetP + i*blocklen, targetP, blocklen*sizeof(int));
39+
memcpy(targetP + i*blocklen, targetP, blocklen*sizeof(*targetP));
4040
}
4141
} break;
4242
case REALSXP: {

src/coalesce.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ SEXP coalesce(SEXP x, SEXP inplaceArg) {
5353
first = PROTECT(copyAsPlain(first)); nprotect++;
5454
if (verbose) Rprintf(_("coalesce copied first item (inplace=FALSE)\n"));
5555
}
56-
const void **valP = (const void **)R_alloc(nval, sizeof(void *));
56+
const void **valP = (const void **)R_alloc(nval, sizeof(*valP));
5757
switch(TYPEOF(first)) {
5858
case LGLSXP:
5959
case INTSXP: {

src/dogroups.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ SEXP dogroups(SEXP dt, SEXP dtcols, SEXP groups, SEXP grpcols, SEXP jiscols, SEX
139139
SEXP names = PROTECT(getAttrib(SDall, R_NamesSymbol)); nprotect++;
140140
if (length(names) != length(SDall))
141141
internal_error(__func__, "length(names)!=length(SD)"); // # nocov
142-
SEXP *nameSyms = (SEXP *)R_alloc(length(names), sizeof(SEXP));
142+
SEXP *nameSyms = (SEXP *)R_alloc(length(names), sizeof(*nameSyms));
143143

144144
for(int i=0; i<length(SDall); ++i) {
145145
SEXP this = VECTOR_ELT(SDall, i);
@@ -156,7 +156,7 @@ SEXP dogroups(SEXP dt, SEXP dtcols, SEXP groups, SEXP grpcols, SEXP jiscols, SEX
156156
SEXP xknames = PROTECT(getAttrib(xSD, R_NamesSymbol)); nprotect++;
157157
if (length(xknames) != length(xSD))
158158
internal_error(__func__, "length(xknames)!=length(xSD)"); // # nocov
159-
SEXP *xknameSyms = (SEXP *)R_alloc(length(xknames), sizeof(SEXP));
159+
SEXP *xknameSyms = (SEXP *)R_alloc(length(xknames), sizeof(*xknameSyms));
160160
for(int i=0; i<length(xSD); ++i) {
161161
if (SIZEOF(VECTOR_ELT(xSD, i))==0)
162162
internal_error(__func__, "type %d in .xSD column %d should have been caught by now", TYPEOF(VECTOR_ELT(xSD, i)), i); // # nocov

src/fmelt.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ SEXP set_diff(SEXP x, int n) {
2020
SEXP table = PROTECT(seq_int(n, 1)); // TODO: using match to 1:n seems odd here, why use match at all
2121
SEXP xmatch = PROTECT(match(x, table, 0)); // Old comment:took a while to realise: matches vec against x - thanks to comment from Matt in assign.c!
2222
const int *ixmatch = INTEGER(xmatch);
23-
int *buf = (int *) R_alloc(n, sizeof(int));
23+
int *buf = (int *) R_alloc(n, sizeof(*buf));
2424
int j=0;
2525
for (int i=0; i<n; ++i) {
2626
if (ixmatch[i] == 0) {
@@ -40,7 +40,7 @@ SEXP which(SEXP x, Rboolean val) {
4040
SEXP ans;
4141
if (!isLogical(x)) error(_("Argument to 'which' must be logical"));
4242
const int *ix = LOGICAL(x);
43-
int *buf = (int *) R_alloc(n, sizeof(int));
43+
int *buf = (int *) R_alloc(n, sizeof(*buf));
4444
for (int i=0; i<n; ++i) {
4545
if (ix[i] == val) {
4646
buf[j++] = i+1;
@@ -317,10 +317,10 @@ static void preprocess(SEXP DT, SEXP id, SEXP measure, SEXP varnames, SEXP valna
317317
}
318318
if (length(varnames) != 1)
319319
error(_("'variable.name' must be a character/integer vector of length 1."));
320-
data->leach = (int *)R_alloc(data->lvalues, sizeof(int));
321-
data->isidentical = (int *)R_alloc(data->lvalues, sizeof(int));
322-
data->isfactor = (int *)R_alloc(data->lvalues, sizeof(int));
323-
data->maxtype = (SEXPTYPE *)R_alloc(data->lvalues, sizeof(SEXPTYPE));
320+
data->leach = (int *)R_alloc(data->lvalues, sizeof(*data->leach));
321+
data->isidentical = (int *)R_alloc(data->lvalues, sizeof(*data->isidentical));
322+
data->isfactor = (int *)R_alloc(data->lvalues, sizeof(*data->isfactor));
323+
data->maxtype = (SEXPTYPE *)R_alloc(data->lvalues, sizeof(*data->maxtype));
324324
// first find max type of each output column.
325325
for (int i=0; i<data->lvalues; ++i) { // for each output column.
326326
tmp = VECTOR_ELT(data->valuecols, i);
@@ -401,7 +401,7 @@ static SEXP combineFactorLevels(SEXP factorLevels, SEXP target, int * factorType
401401
if (!isString(target)) internal_error(__func__, "expects a character target to factorize"); // # nocov
402402
int nrow = length(target);
403403
SEXP ans = PROTECT(allocVector(INTSXP, nrow));
404-
SEXP *levelsRaw = (SEXP *)R_alloc(maxlevels, sizeof(SEXP)); // allocate for worst-case all-unique levels
404+
SEXP *levelsRaw = (SEXP *)R_alloc(maxlevels, sizeof(*levelsRaw)); // allocate for worst-case all-unique levels
405405
int *ansd = INTEGER(ans);
406406
const SEXP *targetd = STRING_PTR_RO(target);
407407
savetl_init();
@@ -492,7 +492,7 @@ SEXP getvaluecols(SEXP DT, SEXP dtnames, Rboolean valfactor, Rboolean verbose, s
492492
data->totlen = data->nrow * data->lmax;
493493
}
494494
SEXP flevels = PROTECT(allocVector(VECSXP, data->lmax));
495-
Rboolean *isordered = (Rboolean *)R_alloc(data->lmax, sizeof(Rboolean));
495+
Rboolean *isordered = (Rboolean *)R_alloc(data->lmax, sizeof(*isordered));
496496
SEXP ansvals = PROTECT(allocVector(VECSXP, data->lvalues));
497497
for (int i=0; i<data->lvalues; ++i) {//for each output/value column.
498498
bool thisvalfactor = (data->maxtype[i] == VECSXP) ? false : valfactor;

0 commit comments

Comments
 (0)