Skip to content

Commit 20bb736

Browse files
committed
finished upgrading allocation syntax
1 parent 4f2c94a commit 20bb736

File tree

6 files changed

+27
-27
lines changed

6 files changed

+27
-27
lines changed

src/assign.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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/forder.c

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ static void push(const int *x, const int n) {
117117
int newn = gs_thread_n[me] + n;
118118
if (gs_thread_alloc[me] < newn) {
119119
gs_thread_alloc[me] = (newn < nrow/3) ? (1+(newn*2)/4096)*4096 : nrow; // [2|3] to not overflow and 3 not 2 to avoid allocating close to nrow (nrow groups occurs when all size 1 groups)
120-
gs_thread[me] = realloc(gs_thread[me], gs_thread_alloc[me]*sizeof(int));
120+
gs_thread[me] = realloc(gs_thread[me], sizeof(*gs_thread[me])*gs_thread_alloc[me]);
121121
if (gs_thread[me]==NULL) STOP(_("Failed to realloc thread private group size buffer to %d*4bytes"), (int)gs_thread_alloc[me]);
122122
}
123123
memcpy(gs_thread[me]+gs_thread_n[me], x, n*sizeof(int));
@@ -131,10 +131,10 @@ static void flush(void) {
131131
int newn = gs_n + n;
132132
if (gs_alloc < newn) {
133133
gs_alloc = (newn < nrow/3) ? (1+(newn*2)/4096)*4096 : nrow;
134-
gs = realloc(gs, gs_alloc*sizeof(int));
134+
gs = realloc(gs, sizeof(*gs)*gs_alloc);
135135
if (gs==NULL) STOP(_("Failed to realloc group size result to %d*4bytes"), (int)gs_alloc);
136136
}
137-
memcpy(gs+gs_n, gs_thread[me], n*sizeof(int));
137+
memcpy(gs+gs_n, gs_thread[me], sizeof(int)*n);
138138
gs_n += n;
139139
gs_thread_n[me] = 0;
140140
}
@@ -316,7 +316,7 @@ static void range_str(const SEXP *x, int n, uint64_t *out_min, uint64_t *out_max
316316
if (ustr_alloc<=ustr_n) {
317317
ustr_alloc = (ustr_alloc==0) ? 16384 : ustr_alloc*2; // small initial guess, negligible time to alloc 128KB (32 pages)
318318
if (ustr_alloc>n) ustr_alloc = n; // clamp at n. Reaches n when fully unique (no dups)
319-
ustr = realloc(ustr, ustr_alloc * sizeof(SEXP));
319+
ustr = realloc(ustr, sizeof(SEXP) * ustr_alloc);
320320
if (ustr==NULL) STOP(_("Unable to realloc %d * %d bytes in range_str"), ustr_alloc, (int)sizeof(SEXP)); // # nocov
321321
}
322322
ustr[ustr_n++] = s;
@@ -345,7 +345,7 @@ static void range_str(const SEXP *x, int n, uint64_t *out_min, uint64_t *out_max
345345
SEXP *ustr3 = malloc(sizeof(*ustr3) * ustr_n);
346346
if (!ustr3)
347347
STOP(_("Failed to alloc ustr3 when converting strings to UTF8")); // # nocov
348-
memcpy(ustr3, STRING_PTR_RO(ustr2), ustr_n*sizeof(SEXP));
348+
memcpy(ustr3, STRING_PTR_RO(ustr2), sizeof(SEXP) * ustr_n);
349349
// need to reset ustr_maxlen because we need ustr_maxlen for utf8 strings
350350
ustr_maxlen = 0;
351351
for (int i=0; i<ustr_n; i++) {
@@ -551,9 +551,9 @@ SEXP forder(SEXP DT, SEXP by, SEXP retGrpArg, SEXP retStatsArg, SEXP sortGroupsA
551551

552552
int ncol=length(by);
553553
int keyAlloc = (ncol+n_cplx)*8 + 1; // +1 for NULL to mark end; calloc to initialize with NULLs
554-
key = calloc(keyAlloc, sizeof(uint8_t *)); // needs to be before loop because part II relies on part I, column-by-column.
554+
key = calloc(keyAlloc, sizeof(*key)); // needs to be before loop because part II relies on part I, column-by-column.
555555
if (!key)
556-
STOP(_("Unable to allocate %"PRIu64" bytes of working memory"), (uint64_t)keyAlloc*sizeof(uint8_t *)); // # nocov
556+
STOP(_("Unable to allocate %"PRIu64" bytes of working memory"), (uint64_t)keyAlloc*sizeof(*key)); // # nocov
557557
nradix=0; // the current byte we're writing this column to; might be squashing into it (spare>0)
558558
int spare=0; // the amount of bits remaining on the right of the current nradix byte
559559
bool isReal=false;
@@ -665,10 +665,10 @@ SEXP forder(SEXP DT, SEXP by, SEXP retGrpArg, SEXP retStatsArg, SEXP sortGroupsA
665665

666666
for (int b=0; b<nbyte; b++) {
667667
if (key[nradix+b]==NULL) {
668-
uint8_t *tt = calloc(nrow, sizeof(uint8_t)); // 0 initialize so that NA's can just skip (NA is always the 0 offset)
668+
uint8_t *tt = calloc(nrow, sizeof(*tt)); // 0 initialize so that NA's can just skip (NA is always the 0 offset)
669669
if (!tt) {
670670
free(key); // # nocov
671-
STOP(_("Unable to allocate %"PRIu64" bytes of working memory"), (uint64_t)nrow*sizeof(uint8_t)); // # nocov
671+
STOP(_("Unable to allocate %"PRIu64" bytes of working memory"), (uint64_t)nrow*sizeof(*tt)); // # nocov
672672
}
673673
key[nradix+b] = tt;
674674
}
@@ -796,9 +796,9 @@ SEXP forder(SEXP DT, SEXP by, SEXP retGrpArg, SEXP retStatsArg, SEXP sortGroupsA
796796
}
797797

798798
if (retgrp) {
799-
gs_thread = calloc(nth, sizeof(int *)); // thread private group size buffers
800-
gs_thread_alloc = calloc(nth, sizeof(int));
801-
gs_thread_n = calloc(nth, sizeof(int));
799+
gs_thread = calloc(nth, sizeof(*gs_thread)); // thread private group size buffers
800+
gs_thread_alloc = calloc(nth, sizeof(*gs_thread_alloc));
801+
gs_thread_n = calloc(nth, sizeof(*gs_thread_n));
802802
if (!gs_thread || !gs_thread_alloc || !gs_thread_n) {
803803
free(gs_thread); free(gs_thread_alloc); free(gs_thread_n); // # nocov
804804
STOP(_("Could not allocate (very tiny) group size thread buffers")); // # nocov
@@ -1242,9 +1242,9 @@ void radix_r(const int from, const int to, const int radix) {
12421242
// the counts are uint16_t but the cumulate needs to be int32_t (or int64_t in future) to hold the offsets
12431243
// If skip==true and we're already done, we still need the first row of this cummulate (diff to get total group sizes) to push() or recurse below
12441244

1245-
int *starts = calloc(nBatch*256, sizeof(int)); // keep starts the same shape and ugrp order as counts
1245+
int *starts = calloc(nBatch*256, sizeof(*starts)); // keep starts the same shape and ugrp order as counts
12461246
if (!starts)
1247-
STOP(_("Failed to allocate %d bytes for '%s'."), (int)(nBatch*256*sizeof(int)), "starts"); // # nocov
1247+
STOP(_("Failed to allocate %d bytes for '%s'."), (int)(nBatch*256*sizeof(*starts)), "starts"); // # nocov
12481248
for (int j=0, sum=0; j<ngrp; j++) { // iterate through columns (ngrp bytes)
12491249
uint16_t *tmp1 = counts+ugrp[j];
12501250
int *tmp2 = starts+ugrp[j];

src/fsort.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ SEXP fsort(SEXP x, SEXP verboseArg) {
273273
{
274274
// each thread has its own small stack of counts
275275
// don't use VLAs here: perhaps too big for stack yes but more that VLAs apparently fail with schedule(dynamic)
276-
uint64_t *restrict mycounts = calloc((toBit/8 + 1)*256, sizeof(uint64_t));
276+
uint64_t *restrict mycounts = calloc((toBit/8 + 1)*256, sizeof(*mycounts));
277277
if (!mycounts) {
278278
failed=true; alloc_fail=true; // # nocov
279279
}

src/gsumm.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ SEXP gforce(SEXP env, SEXP jsub, SEXP o, SEXP f, SEXP l, SEXP irowsArg) {
117117
int bitshift = MAX(nb-8, 0); // TODO: experiment nb/2. Here it doesn't have to be /2 currently.
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);
120-
int *counts = calloc(nBatch*highSize, sizeof(int)); // TODO: cache-line align and make highSize a multiple of 64
120+
int *counts = calloc(nBatch*highSize, sizeof(*counts)); // TODO: cache-line align and make highSize a multiple of 64
121121
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
@@ -625,7 +625,7 @@ SEXP gmean(SEXP x, SEXP narmArg)
625625
for (int i=0; i<ngrp; i++) ansp[i] /= grpsize[i];
626626
} else {
627627
// narm==true and anyNA==true
628-
int *restrict nna_counts = calloc(ngrp, sizeof(int));
628+
int *restrict nna_counts = calloc(ngrp, sizeof(*nna_counts));
629629
if (!nna_counts)
630630
error(_("Unable to allocate %d * %zu bytes for non-NA counts in gmean na.rm=TRUE"), ngrp, sizeof(int)); // # nocov
631631
#pragma omp parallel for num_threads(getDTthreads(highSize, false))
@@ -678,8 +678,8 @@ SEXP gmean(SEXP x, SEXP narmArg)
678678
}
679679
} else {
680680
// narm==true and anyNA==true
681-
int *restrict nna_counts_r = calloc(ngrp, sizeof(int));
682-
int *restrict nna_counts_i = calloc(ngrp, sizeof(int));
681+
int *restrict nna_counts_r = calloc(ngrp, sizeof(*nna_counts_r));
682+
int *restrict nna_counts_i = calloc(ngrp, sizeof(*nna_counts_i));
683683
if (!nna_counts_r || !nna_counts_i) {
684684
// # nocov start
685685
free(nna_counts_r); free(nna_counts_i);

src/rbindlist.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,11 @@ SEXP rbindlist(SEXP l, SEXP usenamesArg, SEXP fillArg, SEXP idcolArg, SEXP ignor
9292
SET_TRUELENGTH(s,-nuniq);
9393
}
9494
}
95-
if (nuniq>0) uniq = realloc(uniq, nuniq*sizeof(SEXP)); // shrink to only what we need to release the spare
95+
if (nuniq>0) uniq = realloc(uniq, sizeof(SEXP)*nuniq); // shrink to only what we need to release the spare
9696

9797
// now count the dups (if any) and how they're distributed across the items
98-
int *counts = (int *)calloc(nuniq, sizeof(int)); // counts of names for each colnames
99-
int *maxdup = (int *)calloc(nuniq, sizeof(int)); // the most number of dups for any name within one colname vector
98+
int *counts = calloc(nuniq, sizeof(*counts)); // counts of names for each colnames
99+
int *maxdup = calloc(nuniq, sizeof(*maxdup)); // the most number of dups for any name within one colname vector
100100
if (!counts || !maxdup) {
101101
// # nocov start
102102
for (int i=0; i<nuniq; ++i) SET_TRUELENGTH(uniq[i], 0);
@@ -445,7 +445,7 @@ SEXP rbindlist(SEXP l, SEXP usenamesArg, SEXP fillArg, SEXP idcolArg, SEXP ignor
445445
if (allocLevel<INT_MAX) {
446446
int64_t new = (int64_t)allocLevel+n-k+1024; // if all remaining levels in this item haven't been seen before, plus 1024 margin in case of many very short levels
447447
allocLevel = (new>(int64_t)INT_MAX) ? INT_MAX : (int)new;
448-
tt = (SEXP *)realloc(levelsRaw, allocLevel*sizeof(SEXP)); // first time levelsRaw==NULL and realloc==malloc in that case
448+
tt = realloc(levelsRaw, sizeof(SEXP)*allocLevel); // first time levelsRaw==NULL and realloc==malloc in that case
449449
}
450450
if (tt==NULL) {
451451
// # nocov start

0 commit comments

Comments
 (0)