Skip to content

Commit 95caa95

Browse files
committed
renamed _const_cast and __halt
1 parent 73d79ed commit 95caa95

File tree

3 files changed

+25
-26
lines changed

3 files changed

+25
-26
lines changed

src/fread.c

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ static void Field(FieldParseContext *ctx);
127127
* Drops `const` qualifier from a `const char*` variable, equivalent of
128128
* `const_cast<char*>` in C++.
129129
*/
130-
static char* _const_cast(const char *ptr) {
130+
static char* const_cast(const char *ptr) {
131131
union { const char *a; char *b; } tmp = { ptr };
132132
return tmp.b;
133133
}
@@ -190,7 +190,7 @@ static inline uint64_t umin(uint64_t a, uint64_t b) { return a < b ? a : b; }
190190
static inline int64_t imin( int64_t a, int64_t b) { return a < b ? a : b; }
191191

192192
/** Return value of `x` clamped to the range [upper, lower] */
193-
static inline int64_t clamp_i64t(int64_t x, int64_t lower, int64_t upper) {
193+
static inline int64_t clamp_szt(int64_t x, int64_t lower, int64_t upper) {
194194
return x < lower ? lower : x > upper? upper : x;
195195
}
196196

@@ -211,7 +211,7 @@ static const char* strlim(const char *ch, size_t limit) {
211211
char *ptr = buf + 501 * flip;
212212
flip = 1 - flip;
213213
char *ch2 = ptr;
214-
limit = imin(limit, 500);
214+
if (limit>500) limit=500;
215215
size_t width = 0;
216216
while ((*ch>'\r' || (*ch!='\0' && *ch!='\r' && *ch!='\n')) && width++<limit) {
217217
*ch2++ = *ch++;
@@ -923,7 +923,7 @@ static void parse_double_hexadecimal(FieldParseContext *ctx)
923923
acc = (acc << 4) + digit;
924924
ch++;
925925
}
926-
ptrdiff_t ndigits = ch - ch0;
926+
size_t ndigits = (uint_fast8_t)(ch - ch0);
927927
if (ndigits > 13 || !(*ch=='p' || *ch=='P')) return;
928928
acc <<= (13 - ndigits) * 4;
929929
ch += 1 + (Eneg = ch[1]=='-') + (ch[1]=='+');
@@ -1570,7 +1570,7 @@ int freadMain(freadMainArgs _args) {
15701570
DTPRINT(_("Avoidable file copy in RAM took %.3f seconds. %s.\n"), time_taken, msg); // # nocov. not warning as that could feasibly cause CRAN tests to fail, say, if test machine is heavily loaded
15711571
}
15721572
}
1573-
*_const_cast(eof) = '\0'; // cow page
1573+
*const_cast(eof) = '\0'; // cow page
15741574
}
15751575
// else char* input already guaranteed to end with \0. We do not modify direct char* input at all, ever.
15761576
// We have now ensured the input ends on eof and that *eof=='\0' too. Normally, lastEOLreplaced will be true.
@@ -1862,8 +1862,8 @@ int freadMain(freadMainArgs _args) {
18621862
if (verbose) DTPRINT(_(" 1-column file ends with 2 or more end-of-line. Restoring last eol using extra byte in cow page.\n"));
18631863
eof++;
18641864
}
1865-
*_const_cast(eof-1) = eol_one_r ? '\r' : '\n';
1866-
*_const_cast(eof) = '\0';
1865+
*const_cast(eof-1) = eol_one_r ? '\r' : '\n';
1866+
*const_cast(eof) = '\0';
18671867
}
18681868
}
18691869

@@ -1876,7 +1876,7 @@ int freadMain(freadMainArgs _args) {
18761876
int64_t estnrow=1;
18771877
int64_t allocnrow=0; // Number of rows in the allocated DataTable
18781878
double meanLineLen=0.0; // Average length (in bytes) of a single line in the input file
1879-
ptrdiff_t bytesRead=0; // Bytes in the data section (i.e. excluding column names, header and footer, if any)
1879+
size_t bytesRead=0; // Bytes in the data section (i.e. excluding column names, header and footer, if any)
18801880
{
18811881
if (verbose) DTPRINT(_("[07] Detect column types, dec, good nrow estimate and whether first row is column names\n"));
18821882
if (verbose && args.header!=NA_BOOL8) DTPRINT(_(" 'header' changed by user from 'auto' to %s\n"), args.header?"true":"false");
@@ -1940,8 +1940,8 @@ int freadMain(freadMainArgs _args) {
19401940
}
19411941
firstRowStart = ch;
19421942
} else {
1943-
ch = (jump == nJumps-1) ? eof - (ptrdiff_t)(0.5*jump0size) : // to almost-surely sample the last line
1944-
pos + jump*((eof-pos)/(nJumps-1));
1943+
ch = (jump == nJumps-1) ? eof - (size_t)(0.5*jump0size) : // to almost-surely sample the last line
1944+
pos + (size_t)jump*((size_t)(eof-pos)/(size_t)(nJumps-1));
19451945
ch = nextGoodLine(ch, ncol);
19461946
}
19471947
if (ch<lastRowEnd) ch=lastRowEnd; // Overlap when apx 1,200 lines (just over 11*100) with short lines at the beginning and longer lines near the end, #2157
@@ -1978,7 +1978,7 @@ int freadMain(freadMainArgs _args) {
19781978
if (jump==0 && bumped) {
19791979
// apply bumps after each line in the first jump from the start in case invalid line stopped early on is in the first 100 lines.
19801980
// otherwise later jumps must complete fully before their bumps are applied. Invalid lines in those are more likely to be due to bad jump start.
1981-
memcpy(type, tmpType, ncol);
1981+
memcpy(type, tmpType, (size_t)ncol);
19821982
bumped = false; // detect_types() only updates &bumped when it's true. So reset to false here.
19831983
}
19841984
}
@@ -1992,7 +1992,7 @@ int freadMain(freadMainArgs _args) {
19921992
if (bumped) {
19931993
// when jump>0, apply the bumps (if any) at the end of the successfully completed jump sample
19941994
ASSERT(jump>0, "jump(%d)>0", jump);
1995-
memcpy(type, tmpType, ncol);
1995+
memcpy(type, tmpType, (size_t)ncol);
19961996
}
19971997
if (verbose && (bumped || jump==0 || jump==nJumps-1)) {
19981998
DTPRINT(_(" Type codes (jump %03d) : %s Quote rule %d\n"), jump, typesAsString(ncol), quoteRule);
@@ -2102,11 +2102,11 @@ int freadMain(freadMainArgs _args) {
21022102
if (verbose) DTPRINT(_(" All rows were sampled since file is small so we know nrow=%"PRIu64" exactly\n"), (uint64_t)sampleLines);
21032103
estnrow = allocnrow = sampleLines;
21042104
} else {
2105-
bytesRead = eof - firstRowStart;
2105+
bytesRead = (size_t)(eof - firstRowStart);
21062106
meanLineLen = (double)sumLen/sampleLines;
21072107
estnrow = CEIL(bytesRead/meanLineLen); // only used for progress meter and verbose line below
21082108
double sd = sqrt( (sumLenSq - (sumLen*sumLen)/sampleLines)/(sampleLines-1) );
2109-
allocnrow = clamp_i64t(bytesRead / fmax(meanLineLen - 2*sd, minLen),
2109+
allocnrow = clamp_szt((size_t)(bytesRead / fmax(meanLineLen - 2*sd, minLen)),
21102110
(size_t)(1.1*estnrow), 2*estnrow);
21112111
// sd can be very close to 0.0 sometimes, so apply a +10% minimum
21122112
// blank lines have length 1 so for fill=true apply a +100% maximum. It'll be grown if needed.
@@ -2187,7 +2187,7 @@ int freadMain(freadMainArgs _args) {
21872187
{
21882188
if (verbose) DTPRINT(_("[09] Apply user overrides on column types\n"));
21892189
ch = pos;
2190-
memcpy(tmpType, type, ncol) ;
2190+
memcpy(tmpType, type, (size_t)ncol) ;
21912191
if (!userOverride(type, colNames, colNamesAnchor, ncol)) { // colNames must not be changed but type[] can be
21922192
if (verbose) DTPRINT(_(" Cancelled by user: userOverride() returned false.")); // # nocov
21932193
freadCleanup(); // # nocov
@@ -2274,7 +2274,7 @@ int freadMain(freadMainArgs _args) {
22742274
nJumps = (int)(bytesRead/chunkBytes);
22752275
if (nJumps==0) nJumps=1;
22762276
else if (nJumps>nth) nJumps = nth*(1+(nJumps-1)/nth);
2277-
chunkBytes = bytesRead / nJumps;
2277+
chunkBytes = bytesRead / (size_t)nJumps;
22782278
} else {
22792279
ASSERT(nJumps==1 /*when nrowLimit supplied*/ || nJumps==2 /*small files*/, "nJumps (%d) != 1|2", nJumps);
22802280
nJumps=1;
@@ -2372,10 +2372,10 @@ int freadMain(freadMainArgs _args) {
23722372
}
23732373
}
23742374

2375-
const char *tch = jump==jump0 ? headPos : nextGoodLine(pos+jump*chunkBytes, ncol);
2375+
const char *tch = jump==jump0 ? headPos : nextGoodLine(pos+(size_t)jump*chunkBytes, ncol);
23762376
const char *thisJumpStart = tch; // "this" for prev/this/next adjective used later, rather than a (mere) t prefix for thread-local.
23772377
const char *tLineStart = tch;
2378-
const char *nextJumpStart = jump<nJumps-1 ? nextGoodLine(pos+(jump+1)*chunkBytes, ncol) : eof;
2378+
const char *nextJumpStart = jump<nJumps-1 ? nextGoodLine(pos+(size_t)(jump+1)*chunkBytes, ncol) : eof;
23792379

23802380
void *targets[9] = {NULL, ctx.buff1, NULL, NULL, ctx.buff4, NULL, NULL, NULL, ctx.buff8};
23812381
FieldParseContext fctx = {
@@ -2800,4 +2800,4 @@ int freadMain(freadMainArgs _args) {
28002800
}
28012801
freadCleanup();
28022802
return 1;
2803-
}
2803+
}

src/freadR.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,7 @@ void progress(int p, int eta) {
708708
}
709709
// # nocov end
710710

711-
void __halt(bool warn, const char *format, ...) {
711+
void halt__(bool warn, const char *format, ...) {
712712
// Solves: http://stackoverflow.com/questions/18597123/fread-data-table-locks-files
713713
// TODO: always include fnam in the STOP message. For log files etc.
714714
va_list args;
@@ -726,4 +726,4 @@ void __halt(bool warn, const char *format, ...) {
726726
void prepareThreadContext(ThreadLocalFreadParsingContext *ctx) {}
727727
void postprocessBuffer(ThreadLocalFreadParsingContext *ctx) {}
728728
void orderBuffer(ThreadLocalFreadParsingContext *ctx) {}
729-
void freeThreadContext(ThreadLocalFreadParsingContext *ctx) {}
729+
void freeThreadContext(ThreadLocalFreadParsingContext *ctx) {}

src/freadR.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,11 @@
1919
// However, msg has to be manually constructed first (rather than simply leaving construction to snprintf inside warning()
2020
// or error()) because the msg usually points to substrings from the mmp (which is invalid after close).
2121
// Where no halt is happening, we can just use raw Rprintf() or warning()
22-
void __halt(bool warn, const char *format, ...); // see freadR.c
23-
#define STOP(...) __halt(0, __VA_ARGS__)
22+
void halt__(bool warn, const char *format, ...); // see freadR.c
23+
#define STOP(...) halt__(0, __VA_ARGS__)
2424
static char internal_error_buff[1001] __attribute__((unused)); // match internalErrSize // todo: fix imports such that compiler warns correctly #6468
25-
#define INTERNAL_STOP(...) do {snprintf(internal_error_buff, 1000, __VA_ARGS__); __halt(0, "%s %s: %s. %s", _("Internal error in"), __func__, internal_error_buff, _("Please report to the data.table issues tracker"));} while (0)
25+
#define INTERNAL_STOP(...) do {snprintf(internal_error_buff, 1000, __VA_ARGS__); halt__(0, "%s %s: %s. %s", _("Internal error in"), __func__, internal_error_buff, _("Please report to the data.table issues tracker"));} while (0)
2626
#define DTPRINT Rprintf
27-
#define DTWARN(...) warningsAreErrors ? __halt(1, __VA_ARGS__) : warning(__VA_ARGS__)
27+
#define DTWARN(...) warningsAreErrors ? halt__(1, __VA_ARGS__) : warning(__VA_ARGS__)
2828

2929
#endif
30-

0 commit comments

Comments
 (0)