From 5c22b636b315776ea3cd5d279344aac7f5a47548 Mon Sep 17 00:00:00 2001 From: Haru Date: Sun, 4 Oct 2015 20:36:36 +0200 Subject: Changed VECTOR/BHEAP/ARRAY macros to discourage usage of unsigned loop counters. Signed-off-by: Haru --- src/common/cbasetypes.h | 2 +- src/common/db.h | 34 +++++++++++++++++----------------- src/common/timer.c | 34 +++++++++++++++++++++++++--------- src/map/path.c | 2 +- 4 files changed, 44 insertions(+), 28 deletions(-) (limited to 'src') diff --git a/src/common/cbasetypes.h b/src/common/cbasetypes.h index 575428f96..64f21f7e0 100644 --- a/src/common/cbasetypes.h +++ b/src/common/cbasetypes.h @@ -391,7 +391,7 @@ typedef char bool; ////////////////////////////////////////////////////////////////////////// // length of a static array -#define ARRAYLENGTH(A) ( sizeof(A)/sizeof((A)[0]) ) +#define ARRAYLENGTH(A) ( (int)(sizeof(A)/sizeof((A)[0])) ) ////////////////////////////////////////////////////////////////////////// // Make sure va_copy exists diff --git a/src/common/db.h b/src/common/db.h index 90204ad3a..afec62d86 100644 --- a/src/common/db.h +++ b/src/common/db.h @@ -1043,8 +1043,8 @@ HPShared struct db_interface *DB; */ #define VECTOR_DECL(_type) \ struct { \ - size_t _max_; \ - size_t _len_; \ + int _max_; \ + int _len_; \ _type *_data_; \ } @@ -1056,8 +1056,8 @@ HPShared struct db_interface *DB; */ #define VECTOR_STRUCT_DECL(_name, _type) \ struct _name { \ - size_t _max_; \ - size_t _len_; \ + int _max_; \ + int _len_; \ _type *_data_; \ } @@ -1191,7 +1191,7 @@ HPShared struct db_interface *DB; */ #define VECTOR_ENSURE(_vec, _n, _step) \ do { \ - size_t _empty_ = VECTOR_CAPACITY(_vec)-VECTOR_LENGTH(_vec); \ + int _empty_ = VECTOR_CAPACITY(_vec)-VECTOR_LENGTH(_vec); \ if ((_n) > _empty_) { \ while ((_n) > _empty_) \ _empty_ += (_step); \ @@ -1499,11 +1499,11 @@ HPShared struct db_interface *DB; */ #define BHEAP_PUSH(_heap, _val, _topcmp, _swp) \ do { \ - size_t _i_ = VECTOR_LENGTH(_heap); \ + int _i_ = VECTOR_LENGTH(_heap); \ VECTOR_PUSH(_heap, _val); /* insert at end */ \ while (_i_ > 0) { \ /* restore heap property in parents */ \ - size_t _parent_ = (_i_-1)/2; \ + int _parent_ = (_i_-1)/2; \ if (_topcmp(VECTOR_INDEX(_heap, _parent_), VECTOR_INDEX(_heap, _i_)) < 0) \ break; /* done */ \ _swp(VECTOR_INDEX(_heap, _parent_), VECTOR_INDEX(_heap, _i_)); \ @@ -1523,7 +1523,7 @@ HPShared struct db_interface *DB; */ #define BHEAP_PUSH2(_heap, _val, _topcmp, _swp) \ do { \ - size_t _i_ = VECTOR_LENGTH(_heap); \ + int _i_ = VECTOR_LENGTH(_heap); \ VECTOR_PUSH(_heap, _val); /* insert at end */ \ BHEAP_SIFTDOWN(_heap, 0, _i_, _topcmp, _swp); \ } while(false) @@ -1579,13 +1579,13 @@ HPShared struct db_interface *DB; */ #define BHEAP_POPINDEX(_heap, _idx, _topcmp, _swp) \ do { \ - size_t _i_ = _idx; \ + int _i_ = _idx; \ VECTOR_INDEX(_heap, _idx) = VECTOR_POP(_heap); /* put last at index */ \ if (_i_ >= VECTOR_LENGTH(_heap)) /* removed last, nothing to do */ \ break; \ while (_i_ > 0) { \ /* restore heap property in parents */ \ - size_t _parent_ = (_i_-1)/2; \ + int _parent_ = (_i_-1)/2; \ if (_topcmp(VECTOR_INDEX(_heap, _parent_), VECTOR_INDEX(_heap, _i_)) < 0) \ break; /* done */ \ _swp(VECTOR_INDEX(_heap, _parent_), VECTOR_INDEX(_heap, _i_)); \ @@ -1593,8 +1593,8 @@ HPShared struct db_interface *DB; } \ while (_i_ < VECTOR_LENGTH(_heap)) { \ /* restore heap property in children */ \ - size_t _lchild_ = _i_*2 + 1; \ - size_t _rchild_ = _i_*2 + 2; \ + int _lchild_ = _i_*2 + 1; \ + int _rchild_ = _i_*2 + 2; \ if ((_lchild_ >= VECTOR_LENGTH(_heap) || _topcmp(VECTOR_INDEX(_heap, _i_), VECTOR_INDEX(_heap, _lchild_)) <= 0) \ && (_rchild_ >= VECTOR_LENGTH(_heap) || _topcmp(VECTOR_INDEX(_heap, _i_), VECTOR_INDEX(_heap, _rchild_)) <= 0)) { \ break; /* done */ \ @@ -1624,10 +1624,10 @@ HPShared struct db_interface *DB; */ #define BHEAP_SIFTDOWN(_heap, _startidx, _idx, _topcmp, _swp) \ do { \ - size_t _i2_ = _idx; \ + int _i2_ = _idx; \ while (_i2_ > _startidx) { \ /* restore heap property in parents */ \ - size_t _parent_ = (_i2_-1)/2; \ + int _parent_ = (_i2_-1)/2; \ if (_topcmp(VECTOR_INDEX(_heap, _parent_), VECTOR_INDEX(_heap, _i2_)) <= 0) \ break; /* done */ \ _swp(VECTOR_INDEX(_heap, _parent_), VECTOR_INDEX(_heap, _i2_)); \ @@ -1645,11 +1645,11 @@ HPShared struct db_interface *DB; */ #define BHEAP_SIFTUP(_heap, _idx, _topcmp, _swp) \ do { \ - size_t _i_ = _idx; \ - size_t _lchild_ = _i_*2 + 1; \ + int _i_ = _idx; \ + int _lchild_ = _i_*2 + 1; \ while (_lchild_ < VECTOR_LENGTH(_heap)) { \ /* restore heap property in children */ \ - size_t _rchild_ = _i_*2 + 2; \ + int _rchild_ = _i_*2 + 2; \ if (_rchild_ >= VECTOR_LENGTH(_heap) || _topcmp(VECTOR_INDEX(_heap, _lchild_), VECTOR_INDEX(_heap, _rchild_)) < 0) { \ /* left child */ \ _swp(VECTOR_INDEX(_heap, _i_), VECTOR_INDEX(_heap, _lchild_)); \ diff --git a/src/common/timer.c b/src/common/timer.c index 793706511..f6ce00d54 100644 --- a/src/common/timer.c +++ b/src/common/timer.c @@ -239,6 +239,10 @@ int64 timer_gettick(void) { /// Adds a timer to the timer_heap static void push_timer_heap(int tid) { BHEAP_ENSURE(timer_heap, 1, 256); +#ifdef __clang_analyzer__ // Clang's static analyzer warns that BHEAP_ENSURE might set BHEAP_DATA(timer_heap) to NULL. +#include "assert.h" + assert(BHEAP_DATA(timer_heap) != NULL); +#endif // __clang_analyzer__ BHEAP_PUSH(timer_heap, tid, DIFFTICK_MINTOPCMP, swap); } @@ -348,14 +352,21 @@ int64 timer_addtick(int tid, int64 tick) { return timer->settick(tid, timer_data[tid].tick+tick); } -/// Modifies a timer's expiration time (an alternative to deleting a timer and starting a new one). -/// Returns the new tick value, or -1 if it fails. -int64 timer_settick(int tid, int64 tick) { - size_t i; +/** + * Modifies a timer's expiration time (an alternative to deleting a timer and starting a new one). + * + * @param tid The timer ID. + * @param tick New expiration time. + * @return The new tick value. + * @retval -1 in case of failure. + */ +int64 timer_settick(int tid, int64 tick) +{ + int i; // search timer position ARR_FIND(0, BHEAP_LENGTH(timer_heap), i, BHEAP_DATA(timer_heap)[i] == tid); - if( i == BHEAP_LENGTH(timer_heap) ) { + if (i == BHEAP_LENGTH(timer_heap)) { ShowError("timer_settick: no such timer %d (%p(%s))\n", tid, timer_data[tid].func, search_timer_func_list(timer_data[tid].func)); return -1; } @@ -373,13 +384,18 @@ int64 timer_settick(int tid, int64 tick) { return tick; } -/// Executes all expired timers. -/// Returns the value of the smallest non-expired timer (or 1 second if there aren't any). -int do_timer(int64 tick) { +/** + * Executes all expired timers. + * + * @param tick The current tick. + * @return The value of the smallest non-expired timer (or 1 second if there aren't any). + */ +int do_timer(int64 tick) +{ int64 diff = TIMER_MAX_INTERVAL; // return value // process all timers one by one - while( BHEAP_LENGTH(timer_heap) ) { + while (BHEAP_LENGTH(timer_heap) > 0) { int tid = BHEAP_PEEK(timer_heap);// top element in heap (smallest tick) diff = DIFF_TICK(timer_data[tid].tick, tick); diff --git a/src/map/path.c b/src/map/path.c index a482fc473..29701d445 100644 --- a/src/map/path.c +++ b/src/map/path.c @@ -45,7 +45,7 @@ struct path_node { }; /// Binary heap of path nodes -BHEAP_STRUCT_DECL(node_heap, struct path_node*); +BHEAP_STRUCT_DECL(node_heap, struct path_node *); /// Comparator for binary heap of path nodes (minimum cost at top) #define NODE_MINTOPCMP(i,j) ((i)->f_cost - (j)->f_cost) -- cgit v1.2.3-70-g09d2