Skip to content

Commit 0855d0a

Browse files
Merge pull request #6951 from badasahog/devBranch1
changed malloc to use objectwise sizeof and exclude explicit pointer type cast
2 parents f1723e7 + 289a51f commit 0855d0a

File tree

9 files changed

+52
-46
lines changed

9 files changed

+52
-46
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@malloc_return_value_cast expression@
2+
type T;
3+
expression E;
4+
@@
5+
- (T)
6+
malloc(E)

src/assign.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,7 @@ SEXP assign(SEXP dt, SEXP rows, SEXP cols, SEXP newcolnames, SEXP values)
646646
if (!*tc1) internal_error(__func__, "index name ends with trailing __"); // # nocov
647647
// check the position of the first appearance of an assigned column in the index.
648648
// the new index will be truncated to this position.
649-
char *s4 = (char*) malloc(strlen(c1) + 3);
649+
char *s4 = malloc(strlen(c1) + 3);
650650
if (!s4) {
651651
internal_error(__func__, "Couldn't allocate memory for s4"); // # nocov
652652
}
@@ -656,7 +656,7 @@ SEXP assign(SEXP dt, SEXP rows, SEXP cols, SEXP newcolnames, SEXP values)
656656
int newKeyLength = strlen(c1);
657657
for(int i = 0; i < xlength(assignedNames); i++){
658658
tc2 = CHAR(STRING_ELT(assignedNames, i));
659-
char *s5 = (char*) malloc(strlen(tc2) + 5); //4 * '_' + \0
659+
char *s5 = malloc(strlen(tc2) + 5); //4 * '_' + \0
660660
if (!s5) {
661661
free(s4); // # nocov
662662
internal_error(__func__, "Couldn't allocate memory for s5"); // # nocov
@@ -873,7 +873,7 @@ const char *memrecycle(const SEXP target, const SEXP where, const int start, con
873873
for (int k=0; k<nTargetLevels; ++k) SET_TRUELENGTH(targetLevelsD[k], 0); // don't need those anymore
874874
if (nAdd) {
875875
// cannot grow the levels yet as that would be R call which could fail to alloc and we have no hook to clear up
876-
SEXP *temp = (SEXP *)malloc(nAdd * sizeof(SEXP *));
876+
SEXP *temp = malloc(sizeof(*temp) * nAdd);
877877
if (!temp) {
878878
// # nocov start
879879
for (int k=0; k<nSourceLevels; ++k) SET_TRUELENGTH(sourceLevelsD[k], 0);
@@ -1282,8 +1282,8 @@ void savetl_init(void) {
12821282
}
12831283
nsaved = 0;
12841284
nalloc = 100;
1285-
saveds = (SEXP *)malloc(nalloc * sizeof(SEXP));
1286-
savedtl = (R_len_t *)malloc(nalloc * sizeof(R_len_t));
1285+
saveds = malloc(sizeof(*saveds) * nalloc);
1286+
savedtl = malloc(sizeof(*savedtl) * nalloc);
12871287
if (!saveds || !savedtl) {
12881288
free(saveds); free(savedtl); // # nocov
12891289
savetl_end(); // # nocov

src/forder.c

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,8 @@ static void cradix_r(SEXP *xsub, int n, int radix)
280280

281281
static void cradix(SEXP *x, int n)
282282
{
283-
cradix_counts = (int *)calloc(ustr_maxlen*256, sizeof(int)); // counts for the letters of left-aligned strings
284-
cradix_xtmp = (SEXP *)malloc(ustr_n*sizeof(SEXP));
283+
cradix_counts = calloc(ustr_maxlen*256, sizeof(*cradix_counts)); // counts for the letters of left-aligned strings
284+
cradix_xtmp = malloc(sizeof(*cradix_xtmp) * ustr_n);
285285
if (!cradix_counts || !cradix_xtmp) {
286286
free(cradix_counts); free(cradix_xtmp); // # nocov
287287
STOP(_("Failed to alloc cradix_counts and/or cradix_tmp")); // # nocov
@@ -342,7 +342,7 @@ static void range_str(const SEXP *x, int n, uint64_t *out_min, uint64_t *out_max
342342
if (anynotutf8) {
343343
SEXP ustr2 = PROTECT(allocVector(STRSXP, ustr_n));
344344
for (int i=0; i<ustr_n; i++) SET_STRING_ELT(ustr2, i, ENC2UTF8(ustr[i]));
345-
SEXP *ustr3 = (SEXP *)malloc(ustr_n * sizeof(SEXP));
345+
SEXP *ustr3 = malloc(sizeof(*ustr3) * ustr_n);
346346
if (!ustr3)
347347
STOP(_("Failed to alloc ustr3 when converting strings to UTF8")); // # nocov
348348
memcpy(ustr3, STRING_PTR_RO(ustr2), ustr_n*sizeof(SEXP));
@@ -361,7 +361,7 @@ static void range_str(const SEXP *x, int n, uint64_t *out_min, uint64_t *out_max
361361
SET_TRUELENGTH(ustr3[i], --o);
362362
}
363363
// now use the 1-1 mapping from ustr to ustr2 to get the ordering back into original ustr, being careful to reset tl to 0
364-
int *tl = (int *)malloc(ustr_n * sizeof(int));
364+
int *tl = malloc(sizeof(*tl) * ustr_n);
365365
if (!tl) {
366366
free(ustr3); // # nocov
367367
STOP(_("Failed to alloc tl when converting strings to UTF8")); // # nocov
@@ -788,8 +788,8 @@ SEXP forder(SEXP DT, SEXP by, SEXP retGrpArg, SEXP retStatsArg, SEXP sortGroupsA
788788

789789
// global nth, TMP & UGRP
790790
nth = getDTthreads(nrow, true); // this nth is relied on in cleanup(); throttle=true/false debated for #5077
791-
TMP = (int *)malloc(nth*UINT16_MAX*sizeof(int)); // used by counting sort (my_n<=65536) in radix_r()
792-
UGRP = (uint8_t *)malloc(nth*256); // TODO: align TMP and UGRP to cache lines (and do the same for stack allocations too)
791+
TMP = malloc(sizeof(*TMP)*nth*UINT16_MAX); // used by counting sort (my_n<=65536) in radix_r()
792+
UGRP = malloc(sizeof(*UGRP)*nth*256); // TODO: align TMP and UGRP to cache lines (and do the same for stack allocations too)
793793
if (!TMP || !UGRP /*|| TMP%64 || UGRP%64*/) {
794794
free(TMP); free(UGRP); // # nocov
795795
STOP(_("Failed to allocate TMP or UGRP or they weren't cache line aligned: nth=%d"), nth); // # nocov
@@ -917,7 +917,7 @@ void radix_r(const int from, const int to, const int radix) {
917917
#endif
918918

919919
uint8_t *restrict my_key = key[radix]+from; // safe to write as we don't use this radix again
920-
uint8_t *o = (uint8_t *)malloc(my_n * sizeof(uint8_t));
920+
uint8_t *o = malloc(sizeof(*o) * my_n);
921921
if (!o)
922922
STOP(_("Failed to allocate %d bytes for '%s'."), (int)(my_n * sizeof(uint8_t)), "o"); // # nocov
923923
// if last key (i.e. radix+1==nradix) there are no more keys to reorder so we could reorder osub by reference directly and save allocating and populating o just
@@ -986,10 +986,10 @@ void radix_r(const int from, const int to, const int radix) {
986986
}
987987
if (!skip) {
988988
// reorder osub and each remaining ksub
989-
int *TMP = malloc(my_n * sizeof(int));
989+
int *TMP = malloc(sizeof(*TMP) * my_n);
990990
if (!TMP) {
991991
free(o); // # nocov
992-
STOP(_("Failed to allocate %d bytes for '%s'."), (int)(my_n * sizeof(int)), "TMP"); // # nocov
992+
STOP(_("Failed to allocate %d bytes for '%s'."), (int)(sizeof(*TMP) * my_n), "TMP"); // # nocov
993993
}
994994
const int *restrict osub = anso+from;
995995
for (int i=0; i<my_n; i++) TMP[i] = osub[o[i]];
@@ -1009,9 +1009,9 @@ void radix_r(const int from, const int to, const int radix) {
10091009
return;
10101010
}
10111011
int ngrp=0; //minor TODO: could know number of groups with certainty up above
1012-
int *my_gs = malloc(my_n * sizeof(int));
1012+
int *my_gs = malloc(sizeof(*my_gs) * my_n);
10131013
if (!my_gs)
1014-
STOP(_("Failed to allocate %d bytes for '%s'."), (int)(my_n * sizeof(int)), "my_gs"); // # nocov
1014+
STOP(_("Failed to allocate %d bytes for '%s'."), (int)(sizeof(*my_gs) * my_n), "my_gs"); // # nocov
10151015
my_gs[ngrp]=1;
10161016
for (int i=1; i<my_n; i++) {
10171017
if (my_key[i]!=my_key[i-1]) my_gs[++ngrp] = 1;
@@ -1111,7 +1111,7 @@ void radix_r(const int from, const int to, const int radix) {
11111111
if (!retgrp && radix+1==nradix) {
11121112
return; // we're done. avoid allocating and populating very last group sizes for last key
11131113
}
1114-
int *my_gs = malloc((ngrp==0 ? 256 : ngrp) * sizeof(int)); // ngrp==0 when sort and skip==true; we didn't count the non-zeros in my_counts yet in that case
1114+
int *my_gs = malloc(sizeof(*my_gs) * (ngrp==0 ? 256 : ngrp)); // ngrp==0 when sort and skip==true; we didn't count the non-zeros in my_counts yet in that case
11151115
if (!my_gs)
11161116
STOP(_("Failed to allocate %d bytes for '%s'."), (int)((ngrp==0 ? 256 : ngrp) * sizeof(int)), "my_gs"); // # nocov
11171117
if (sortType!=0) {
@@ -1139,9 +1139,9 @@ void radix_r(const int from, const int to, const int radix) {
11391139
int batchSize = MIN(UINT16_MAX, 1+my_n/getDTthreads(my_n, true)); // (my_n-1)/nBatch + 1; //UINT16_MAX == 65535
11401140
int nBatch = (my_n-1)/batchSize + 1; // TODO: make nBatch a multiple of nThreads?
11411141
int lastBatchSize = my_n - (nBatch-1)*batchSize;
1142-
uint16_t *counts = calloc(nBatch*256,sizeof(uint16_t));
1143-
uint8_t *ugrps = malloc(nBatch*256*sizeof(uint8_t));
1144-
int *ngrps = calloc(nBatch ,sizeof(int));
1142+
uint16_t *counts = calloc(nBatch*256,sizeof(*counts));
1143+
uint8_t *ugrps = malloc(sizeof(*ugrps)*nBatch*256);
1144+
int *ngrps = calloc(nBatch ,sizeof(*ngrps));
11451145
if (!counts || !ugrps || !ngrps) {
11461146
free(counts); free(ugrps); free(ngrps); // # nocov
11471147
STOP(_("Failed to allocate parallel counts. my_n=%d, nBatch=%d"), my_n, nBatch); // # nocov
@@ -1152,11 +1152,11 @@ void radix_r(const int from, const int to, const int radix) {
11521152
TEND(16)
11531153
#pragma omp parallel num_threads(getDTthreads(nBatch, false))
11541154
{
1155-
int *my_otmp = malloc(batchSize * sizeof(int)); // thread-private write
1156-
uint8_t *my_ktmp = malloc(batchSize * sizeof(uint8_t) * n_rem);
1155+
int *my_otmp = malloc(sizeof(*my_otmp) * batchSize); // thread-private write
1156+
uint8_t *my_ktmp = malloc(sizeof(*my_ktmp) * batchSize * n_rem);
11571157
if (!my_otmp || !my_ktmp) {
11581158
free(my_otmp); free(my_ktmp);
1159-
STOP(_("Failed to allocate 'my_otmp' and/or 'my_ktmp' arrays (%d bytes)."), (int)(batchSize*(sizeof(int) + sizeof(uint8_t))));
1159+
STOP(_("Failed to allocate 'my_otmp' and/or 'my_ktmp' arrays (%d bytes)."), (int)((sizeof(*my_otmp) + sizeof(*my_ktmp)) * batchSize));
11601160
}
11611161
// TODO: move these up above and point restrict[me] to them. Easier to Error that way if failed to alloc.
11621162
#pragma omp for
@@ -1259,7 +1259,7 @@ void radix_r(const int from, const int to, const int radix) {
12591259

12601260
TEND(18 + notFirst*3)
12611261
if (!skip) {
1262-
int *TMP = malloc(my_n * sizeof(int));
1262+
int *TMP = malloc(sizeof(*TMP) * my_n);
12631263
if (!TMP)
12641264
STOP(_("Unable to allocate TMP for my_n=%d items in parallel batch counting"), my_n); // # nocov
12651265
#pragma omp parallel for num_threads(getDTthreads(nBatch, false))
@@ -1298,7 +1298,7 @@ void radix_r(const int from, const int to, const int radix) {
12981298
TEND(19 + notFirst*3)
12991299
notFirst = true;
13001300

1301-
int *my_gs = malloc(ngrp * sizeof(int));
1301+
int *my_gs = malloc(sizeof(*my_gs) * ngrp);
13021302
if (!my_gs)
13031303
STOP(_("Failed to allocate %d bytes for '%s'."), (int)(ngrp * sizeof(int)), "my_gs"); // # nocov
13041304
for (int i=1; i<ngrp; i++) my_gs[i-1] = starts[ugrp[i]] - starts[ugrp[i-1]]; // use the first row of starts to get totals

src/fread.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ static const char* filesize_to_str(size_t fsize)
441441
double copyFile(size_t fileSize) // only called in very very rare cases
442442
{
443443
double tt = wallclock();
444-
mmp_copy = (char *)malloc(fileSize + 1 /* extra \0 */);
444+
mmp_copy = malloc(fileSize + 1 /* extra \0 */);
445445
if (!mmp_copy)
446446
return -1.0; // # nocov
447447
memcpy(mmp_copy, mmp, fileSize);
@@ -1884,8 +1884,8 @@ int freadMain(freadMainArgs _args) {
18841884
if (verbose) DTPRINT(_("[07] Detect column types, dec, good nrow estimate and whether first row is column names\n"));
18851885
if (verbose && args.header!=NA_BOOL8) DTPRINT(_(" 'header' changed by user from 'auto' to %s\n"), args.header?"true":"false");
18861886

1887-
type = (int8_t *)malloc((size_t)ncol * sizeof(int8_t));
1888-
tmpType = (int8_t *)malloc((size_t)ncol * sizeof(int8_t)); // used i) in sampling to not stop on errors when bad jump point and ii) when accepting user overrides
1887+
type = malloc(sizeof(*type) * (size_t)ncol);
1888+
tmpType = malloc(sizeof(*tmpType) * (size_t)ncol); // used i) in sampling to not stop on errors when bad jump point and ii) when accepting user overrides
18891889
if (!type || !tmpType) {
18901890
free(type); free(tmpType); // # nocov
18911891
STOP(_("Failed to allocate 2 x %d bytes for type and tmpType: %s"), ncol, strerror(errno)); // # nocov
@@ -2201,9 +2201,9 @@ int freadMain(freadMainArgs _args) {
22012201
rowSize1 = 0;
22022202
rowSize4 = 0;
22032203
rowSize8 = 0;
2204-
size = (int8_t *)malloc((size_t)ncol * sizeof(int8_t)); // TODO: remove size[] when we implement Pasha's idea to += size inside processor
2204+
size = malloc(sizeof(*size) * (size_t)ncol); // TODO: remove size[] when we implement Pasha's idea to += size inside processor
22052205
if (!size)
2206-
STOP(_("Failed to allocate %d bytes for '%s': %s"), (int)(ncol * sizeof(int8_t)), "size", strerror(errno)); // # nocov
2206+
STOP(_("Failed to allocate %d bytes for '%s': %s"), (int)(sizeof(*size) * (size_t)ncol), "size", strerror(errno)); // # nocov
22072207
nStringCols = 0;
22082208
nNonStringCols = 0;
22092209
for (int j=0; j<ncol; j++) {
@@ -2642,7 +2642,7 @@ int freadMain(freadMainArgs _args) {
26422642
DTPRINT(_(" Provided number of fill columns: %d but only found %d\n"), ncol, max_col);
26432643
DTPRINT(_(" Dropping %d overallocated columns\n"), ndropFill);
26442644
}
2645-
dropFill = (int *)malloc((size_t)ndropFill * sizeof(int));
2645+
dropFill = malloc(sizeof(*dropFill) * (size_t)ndropFill);
26462646
if (!dropFill)
26472647
STOP(_("Failed to allocate %d bytes for '%s'."), (int)(ndropFill * sizeof(int)), "dropFill"); // # nocov
26482648
int i=0;

src/frolladaptive.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ void fadaptiverollmeanFast(double *x, uint64_t nx, ans_t *ans, int *k, double fi
2929
snprintf(end(ans->message[0]), 500, _("%s: running for input length %"PRIu64", hasna %d, narm %d\n"), "fadaptiverollmeanFast", (uint64_t)nx, hasna, (int) narm);
3030
bool truehasna = hasna>0; // flag to re-run if NAs detected
3131
long double w = 0.0;
32-
double *cs = malloc(nx*sizeof(double)); // cumsum vector, same as double cs[nx] but no segfault
32+
double *cs = malloc(sizeof(*cs) * nx); // cumsum vector, same as double cs[nx] but no segfault
3333
if (!cs) { // # nocov start
3434
ans->status = 3; // raise error
3535
snprintf(ans->message[3], 500, _("%s: Unable to allocate memory for cumsum"), __func__);
@@ -65,7 +65,7 @@ void fadaptiverollmeanFast(double *x, uint64_t nx, ans_t *ans, int *k, double fi
6565
}
6666
if (truehasna) {
6767
uint64_t nc = 0; // running NA counter
68-
uint64_t *cn = malloc(nx*sizeof(uint64_t)); // cumulative NA counter, used the same way as cumsum, same as uint64_t cn[nx] but no segfault
68+
uint64_t *cn = malloc(sizeof(*cn) * nx); // cumulative NA counter, used the same way as cumsum, same as uint64_t cn[nx] but no segfault
6969
if (!cn) { // # nocov start
7070
ans->status = 3; // raise error
7171
snprintf(ans->message[3], 500, _("%s: Unable to allocate memory for cum NA counter"), __func__);
@@ -218,7 +218,7 @@ void fadaptiverollsumFast(double *x, uint64_t nx, ans_t *ans, int *k, double fil
218218
snprintf(end(ans->message[0]), 500, _("%s: running for input length %"PRIu64", hasna %d, narm %d\n"), "fadaptiverollsumFast", (uint64_t)nx, hasna, (int) narm);
219219
bool truehasna = hasna>0;
220220
long double w = 0.0;
221-
double *cs = malloc(nx*sizeof(double));
221+
double *cs = malloc(sizeof(*cs) * nx);
222222
if (!cs) { // # nocov start
223223
ans->status = 3;
224224
snprintf(ans->message[3], 500, _("%s: Unable to allocate memory for cumsum"), __func__);
@@ -254,7 +254,7 @@ void fadaptiverollsumFast(double *x, uint64_t nx, ans_t *ans, int *k, double fil
254254
}
255255
if (truehasna) {
256256
uint64_t nc = 0;
257-
uint64_t *cn = malloc(nx*sizeof(uint64_t));
257+
uint64_t *cn = malloc(sizeof(*cn) * nx);
258258
if (!cn) { // # nocov start
259259
ans->status = 3;
260260
snprintf(ans->message[3], 500, _("%s: Unable to allocate memory for cum NA counter"), __func__);

src/fsort.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,8 @@ SEXP fsort(SEXP x, SEXP verboseArg) {
140140
// and ii) for small vectors with just one batch
141141

142142
t[1] = wallclock();
143-
double *mins = (double *)malloc(nBatch * sizeof(double));
144-
double *maxs = (double *)malloc(nBatch * sizeof(double));
143+
double *mins = malloc(sizeof(*mins) * nBatch);
144+
double *maxs = malloc(sizeof(*maxs) * nBatch);
145145
if (!mins || !maxs) {
146146
free(mins); free(maxs); // # nocov
147147
error(_("Failed to allocate %d bytes in fsort()."), (int)(2 * nBatch * sizeof(double))); // # nocov
@@ -300,7 +300,7 @@ SEXP fsort(SEXP x, SEXP verboseArg) {
300300
uint64_t thisN = msbCounts[order[msb]];
301301

302302
if (myworking==NULL) {
303-
myworking = malloc(thisN * sizeof(double));
303+
myworking = malloc(sizeof(*myworking) * thisN);
304304
if (!myworking) {
305305
failed=true; alloc_fail=true; continue; // # nocov
306306
}

src/gsumm.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ SEXP gforce(SEXP env, SEXP jsub, SEXP o, SEXP f, SEXP l, SEXP irowsArg) {
118118
int highSize = ((nrow-1)>>bitshift) + 1;
119119
//Rprintf(_("When assigning grp[o] = g, highSize=%d nb=%d bitshift=%d nBatch=%d\n"), highSize, nb, bitshift, nBatch);
120120
int *counts = calloc(nBatch*highSize, sizeof(int)); // TODO: cache-line align and make highSize a multiple of 64
121-
int *TMP = malloc(nrow*2l*sizeof(int)); // must multiple the long int otherwise overflow may happen, #4295
121+
int *TMP = malloc(sizeof(*TMP) * nrow*2l); // must multiple the long int otherwise overflow may happen, #4295
122122
if (!counts || !TMP ) {
123123
free(counts); free(TMP); // # nocov
124124
error(_("Failed to allocate counts or TMP when assigning g in gforce")); // # nocov
@@ -1124,7 +1124,7 @@ SEXP gprod(SEXP x, SEXP narmArg) {
11241124
const int n = nosubset ? length(x) : irowslen;
11251125
//clock_t start = clock();
11261126
if (nrow != n) error(_("nrow [%d] != length(x) [%d] in %s"), nrow, n, "gprod");
1127-
long double *s = malloc(ngrp * sizeof(long double));
1127+
long double *s = malloc(sizeof(*s) * ngrp);
11281128
if (!s)
11291129
error(_("Unable to allocate %d * %zu bytes for gprod"), ngrp, sizeof(long double)); // # nocov
11301130
for (int i=0; i<ngrp; ++i) s[i] = 1.0;

0 commit comments

Comments
 (0)