summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTobias Klauser <tklauser@distanz.ch>2016-04-28 10:12:52 +0200
committerTobias Klauser <tklauser@distanz.ch>2016-04-28 10:18:21 +0200
commit0afeed389c9b3e310e4f70b0b3b7fc89b1688fa1 (patch)
tree932ce107899a7190eb3db21dfb58ceb0c0241bcb
parent2061043d3d277012bd98b4107a5add53c1919254 (diff)
Avoid realloc on every call of sf_push()
Currently, every call to sf_push() realloc()'s _sf_stack, even if the maximum size _sf_max wasn't changed. As the indentation beneath the "if" clause already indicates, the realloc() should only be executed if _sf_max was increased. Found by compiling flex with the -Wmisleading-indentation flags of gcc, which leads to the following warning: scanflags.c: In function ‘sf_push’: scanflags.c:42:5: warning: this ‘if’ clause does not guard... [-Wmisleading-indentation] if (_sf_top_ix + 1 >= _sf_max) ^~ scanflags.c:44:9: note: ...this statement, but the latter is misleadingly indented as if it is guarded by the ‘if’ _sf_stk = realloc(_sf_stk, sizeof(scanflags_t) * _sf_max); ^~~~~~~
-rw-r--r--src/scanflags.c3
1 files changed, 2 insertions, 1 deletions
diff --git a/src/scanflags.c b/src/scanflags.c
index aa3de8a..9699a31 100644
--- a/src/scanflags.c
+++ b/src/scanflags.c
@@ -39,9 +39,10 @@ size_t _sf_top_ix=0, _sf_max=0;
void
sf_push (void)
{
- if (_sf_top_ix + 1 >= _sf_max)
+ if (_sf_top_ix + 1 >= _sf_max) {
_sf_max += 32;
_sf_stk = realloc(_sf_stk, sizeof(scanflags_t) * _sf_max);
+ }
// copy the top element
_sf_stk[_sf_top_ix + 1] = _sf_stk[_sf_top_ix];