Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Define STRICT_R_HEADERS, use R_{Calloc,Realloc,Free} #1822

Merged
merged 1 commit into from
May 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Makevars
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PKG_CPPFLAGS = -DSTRICT_R_HEADERS
1 change: 1 addition & 0 deletions src/Makevars.win
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PKG_CPPFLAGS = -DSTRICT_R_HEADERS
22 changes: 11 additions & 11 deletions src/ldc_ad2cp_in_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,11 @@ List do_ldc_ad2cp_in_file(CharacterVector filename, IntegerVector from, IntegerV
unsigned short header_checksum;
} header;
unsigned int dbuflen = 10000; // may be increased later
unsigned char *dbuf = (unsigned char *)Calloc((size_t)dbuflen, unsigned char);
unsigned char *dbuf = (unsigned char *)R_Calloc((size_t)dbuflen, unsigned char);
unsigned int nchunk = 100000;
unsigned int *index_buf = (unsigned int*)Calloc((size_t)nchunk, unsigned int);
unsigned int *length_buf = (unsigned int*)Calloc((size_t)nchunk, unsigned int);
unsigned int *id_buf = (unsigned int*)Calloc((size_t)nchunk, unsigned int);
unsigned int *index_buf = (unsigned int*)R_Calloc((size_t)nchunk, unsigned int);
unsigned int *length_buf = (unsigned int*)R_Calloc((size_t)nchunk, unsigned int);
unsigned int *id_buf = (unsigned int*)R_Calloc((size_t)nchunk, unsigned int);
int early_EOF = 0;
int reset_cindex = 0; // set to 1 if we skipped to find a new header start, after a bad checksum
while (chunk < to_value) { // FIXME: use whole file here
Expand All @@ -220,9 +220,9 @@ List do_ldc_ad2cp_in_file(CharacterVector filename, IntegerVector from, IntegerV
if (debug)
Rprintf(" increasing 'index_buf' size from %d", nchunk);
nchunk = (unsigned int) floor(chunk * 1.4); // increase buffer size by sqrt(2)
index_buf = (unsigned int*)Realloc(index_buf, nchunk, unsigned int);
length_buf = (unsigned int*)Realloc(length_buf, nchunk, unsigned int);
id_buf = (unsigned int*)Realloc(id_buf, nchunk, unsigned int);
index_buf = (unsigned int*)R_Realloc(index_buf, nchunk, unsigned int);
length_buf = (unsigned int*)R_Realloc(length_buf, nchunk, unsigned int);
id_buf = (unsigned int*)R_Realloc(id_buf, nchunk, unsigned int);
if (debug)
Rprintf(" to %d\n", nchunk);
}
Expand Down Expand Up @@ -311,7 +311,7 @@ List do_ldc_ad2cp_in_file(CharacterVector filename, IntegerVector from, IntegerV
Rprintf(" *BUG*: cindex is out of synch with ftell()\n");

dbuflen = header.data_size;
dbuf = (unsigned char *)Realloc(dbuf, dbuflen, unsigned char);
dbuf = (unsigned char *)R_Realloc(dbuf, dbuflen, unsigned char);
}
// Read the data
bytes_read = fread(dbuf, 1, header.data_size, fp);
Expand Down Expand Up @@ -402,9 +402,9 @@ List do_ldc_ad2cp_in_file(CharacterVector filename, IntegerVector from, IntegerV
length[i] = length_buf[i];
id[i] = id_buf[i];
}
Free(index_buf);
Free(length_buf);
Free(id_buf);
R_Free(index_buf);
R_Free(length_buf);
R_Free(id_buf);
if (debug)
Rprintf("} # do_ldc_ad2cp_in_file()\n");
return(List::create(Named("index")=index,
Expand Down
44 changes: 22 additions & 22 deletions src/ldc_rdi_in_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ List do_ldc_rdi_in_file(StringVector filename, IntegerVector from, IntegerVector
// 'obuf' is a growable C buffer to hold the output, which eventually
// gets saved in the R item "buf".
unsigned long int nobuf = 100000; // BUFFER SIZE
unsigned char *obuf = (unsigned char *)Calloc((size_t)nobuf, unsigned char);
unsigned char *obuf = (unsigned char *)R_Calloc((size_t)nobuf, unsigned char);
unsigned long int iobuf = 0;

// 'ensembles', 'times' and 'sec100s' are growable buffers of equal length, with one
Expand All @@ -237,12 +237,12 @@ List do_ldc_rdi_in_file(StringVector filename, IntegerVector from, IntegerVector
// Note that we do not check the Calloc() results because the R docs say that
// Calloc() performs its won tests, and that R will handle any problems.
unsigned long int nensembles = 100000; // BUFFER SIZE
unsigned int *ensemble_in_files = (unsigned int *)Calloc((size_t)nensembles, unsigned int);
int *ensembles = (int *)Calloc((size_t)nensembles, int);
int *times = (int *)Calloc((size_t)nensembles, int);
int *sec100s = (int *)Calloc((size_t)nensembles, int);
unsigned int *ensemble_in_files = (unsigned int *)R_Calloc((size_t)nensembles, unsigned int);
int *ensembles = (int *)R_Calloc((size_t)nensembles, int);
int *times = (int *)R_Calloc((size_t)nensembles, int);
int *sec100s = (int *)R_Calloc((size_t)nensembles, int);
unsigned long int nebuf = 50000; // BUFFER SIZE
unsigned char *ebuf = (unsigned char *)Calloc((size_t)nebuf, unsigned char);
unsigned char *ebuf = (unsigned char *)R_Calloc((size_t)nebuf, unsigned char);

unsigned long int in_ensemble = 1, out_ensemble = 0;
int b1, b2;
Expand Down Expand Up @@ -291,10 +291,10 @@ List do_ldc_rdi_in_file(StringVector filename, IntegerVector from, IntegerVector
if (debug_value > 0)
Rprintf("\nbytes_to_check=%d based on b1=%d(0x%02x) and b2=%d(0x%02x)\n", bytes_to_check, b1, b1, b2, b2);
if (bytes_to_check < 5) { // this will only happen in error; we check so bytes_to_read won't be crazy
Free(ensembles);
Free(times);
Free(sec100s);
Free(ebuf);
R_Free(ensembles);
R_Free(times);
R_Free(sec100s);
R_Free(ebuf);
::Rf_error("cannot decode the length of ensemble number %d", in_ensemble);
}
if (bytes_to_check < 4)
Expand All @@ -305,7 +305,7 @@ List do_ldc_rdi_in_file(StringVector filename, IntegerVector from, IntegerVector
if (bytes_to_read > nebuf) {
if (debug_value > 0)
Rprintf("Increasing 'ebuf' buffer size from %d bytes to %d bytes\n", nebuf, bytes_to_read);
ebuf = (unsigned char *)Realloc(ebuf, bytes_to_read, unsigned char);
ebuf = (unsigned char *)R_Realloc(ebuf, bytes_to_read, unsigned char);
nebuf = bytes_to_read;
}
// Read the bytes in one operation, because fgetc() is too slow.
Expand Down Expand Up @@ -351,10 +351,10 @@ List do_ldc_rdi_in_file(StringVector filename, IntegerVector from, IntegerVector
nensembles = 3 * nensembles / 2;
if (debug_value > -1)
Rprintf("Increasing ensembles,times,sec100s storage to %d elements ...\n", nensembles);
ensemble_in_files = (unsigned int *) Realloc(ensemble_in_files, nensembles, unsigned int);
ensembles = (int *) Realloc(ensembles, nensembles, int);
times = (int *) Realloc(times, nensembles, int);
sec100s = (int *)Realloc(sec100s, nensembles, int);
ensemble_in_files = (unsigned int *) R_Realloc(ensemble_in_files, nensembles, unsigned int);
ensembles = (int *) R_Realloc(ensembles, nensembles, int);
times = (int *) R_Realloc(times, nensembles, int);
sec100s = (int *)R_Realloc(sec100s, nensembles, int);
}
// We will decide whether to keep this ensemble, based on ensemble
// number, if mode_value==0 or on time, if mode_value==1. That
Expand Down Expand Up @@ -407,7 +407,7 @@ List do_ldc_rdi_in_file(StringVector filename, IntegerVector from, IntegerVector
nobuf = nobuf + 100 + bytes_to_read + nobuf / 2;
if (debug_value > 0)
Rprintf("about to enlarge obuf storage to %d elements ...\n", nobuf);
obuf = (unsigned char *)Realloc(obuf, nobuf, unsigned char);
obuf = (unsigned char *)R_Realloc(obuf, nobuf, unsigned char);
if (debug_value > 0)
Rprintf(" ... allocation was successful\n");
}
Expand Down Expand Up @@ -540,15 +540,15 @@ List do_ldc_rdi_in_file(StringVector filename, IntegerVector from, IntegerVector
sec100[i] = sec100s[i];
//Rprintf("i=%d ensemble=%d time=%d sec100=%d\n", i, ensemble[i], time[i], (int)sec100[i]);
}
Free(ensemble_in_files);
Free(ensembles);
Free(times);
Free(sec100s);
Free(ebuf);
R_Free(ensemble_in_files);
R_Free(ensembles);
R_Free(times);
R_Free(sec100s);
R_Free(ebuf);
for (unsigned long int i = 0; i < iobuf; i++) {
buf[i] = obuf[i];
}
Free(obuf);
R_Free(obuf);
if (debug_value > 0)
Rprintf("Returning from C++ function named do_ldc_rdi_in_file.\n");
return(List::create(Named("ensembleStart")=ensemble, Named("time")=time,
Expand Down
12 changes: 6 additions & 6 deletions src/map.c
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,8 @@ SEXP map_check_polygons(SEXP x, SEXP y, SEXP z, SEXP xokspan, SEXP usr) // retur
if (j > (clen - 2)) { \
/*Rprintf("INCREASE storage from %d to %d [a]\n", clen, (int)(100 + clen));*/ \
clen += 100; \
xbp = (double*)Realloc(xbp, clen, double); \
ybp = (double*)Realloc(ybp, clen, double); \
xbp = (double*)R_Realloc(xbp, clen, double); \
ybp = (double*)R_Realloc(ybp, clen, double); \
} \
j++;

Expand All @@ -268,8 +268,8 @@ SEXP map_clip_xy_OLD_BROKEN(SEXP x, SEXP y, SEXP usr) // returns list with new x
error("must have at least two 'x' and 'y' pairs");
// xbp and xbp are growable buffers
int clen = xlen + 100; // the 100 may save reallocs
double *xbp = (double*)Calloc((size_t)clen, double);
double *ybp = (double*)Calloc((size_t)clen, double);
double *xbp = (double*)R_Calloc((size_t)clen, double);
double *ybp = (double*)R_Calloc((size_t)clen, double);
#ifdef DEBUG
double distMIN = 10e6; // FIXME: temporary to find problem in Greenland
#endif
Expand Down Expand Up @@ -388,8 +388,8 @@ SEXP map_clip_xy(SEXP x, SEXP y, SEXP usr) // returns list with new x and y vect
error("must have at least two 'x' and 'y' pairs");
// xb and yb are growable buffers; we copy to xc and yc near the end.
int clen = xlen + 100; // the 100 may save reallocs
double *xbp = (double*)Calloc((size_t)clen, double);
double *ybp = (double*)Calloc((size_t)clen, double);
double *xbp = (double*)R_Calloc((size_t)clen, double);
double *ybp = (double*)R_Calloc((size_t)clen, double);
#ifdef DEBUG
double distMIN = 10e6; // FIXME: temporary to find problem in Greenland
#endif
Expand Down