From c0c254f14d0d65a8b7ec50720ed8d98b5a04919a Mon Sep 17 00:00:00 2001 From: Piotr HaƂaczkiewicz Date: Mon, 22 Jul 2013 22:50:45 +0200 Subject: Binary heap fix & improvement. Fixed a bug when removing last element of binary heap (its parent would be removed instead if it had the same value). Binary heap now allows custom swapper function/macro. Added `swap_ptr` macro to swap two pointers in place (`swap` is not suitable for pointers). This allows to store pointers in binary heap. --- src/common/timer.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src/common/timer.c') diff --git a/src/common/timer.c b/src/common/timer.c index 955a971c8..f019c6927 100644 --- a/src/common/timer.c +++ b/src/common/timer.c @@ -195,7 +195,7 @@ unsigned int timer_gettick(void) { /// Adds a timer to the timer_heap static void push_timer_heap(int tid) { BHEAP_ENSURE(timer_heap, 1, 256); - BHEAP_PUSH(timer_heap, tid, DIFFTICK_MINTOPCMP); + BHEAP_PUSH(timer_heap, tid, DIFFTICK_MINTOPCMP, swap); } /*========================== @@ -322,9 +322,9 @@ int timer_settick(int tid, unsigned int tick) { return (int)tick;// nothing to do, already in propper position // pop and push adjusted timer - BHEAP_POPINDEX(timer_heap, i, DIFFTICK_MINTOPCMP); + BHEAP_POPINDEX(timer_heap, i, DIFFTICK_MINTOPCMP, swap); timer_data[tid].tick = tick; - BHEAP_PUSH(timer_heap, tid, DIFFTICK_MINTOPCMP); + BHEAP_PUSH(timer_heap, tid, DIFFTICK_MINTOPCMP, swap); return (int)tick; } @@ -342,7 +342,7 @@ int do_timer(unsigned int tick) { break; // no more expired timers to process // remove timer - BHEAP_POP(timer_heap, DIFFTICK_MINTOPCMP); + BHEAP_POP(timer_heap, DIFFTICK_MINTOPCMP, swap); timer_data[tid].type |= TIMER_REMOVE_HEAP; if( timer_data[tid].func ) { -- cgit v1.2.3-60-g2f50