diff --git a/src/common/evsched.c b/src/common/evsched.c index fe642983e343b2d81b9a88b72985b696fd8e39f3..5a69c09697b422d596dccfd0081e5039b9ea5397 100644 --- a/src/common/evsched.c +++ b/src/common/evsched.c @@ -26,10 +26,10 @@ #define OPENBSD_SLAB_BROKEN #endif -static int compare_event_heap_nodes(event_t *e1, event_t *e2) +/* According to libucw heap, x<y should return non-zero else zero value. */ +static int compare_event_heap_nodes(event_t **e1, event_t **e2) { - if (timercmp(&e1->tv, &e2->tv, <)) return -1; - if (timercmp(&e1->tv, &e2->tv, >)) return 1; + if (timercmp(&(*e1)->tv, &(*e2)->tv, <)) return 1; return 0; } @@ -144,6 +144,11 @@ void evsched_event_free(evsched_t *s, event_t *ev) if (!s || !ev) { return; } + + int found = 0; + if ((found = heap_find(&s->heap, &ev))) { + heap_delete(&s->heap, found); + } #ifndef OPENBSD_SLAB_BROKEN pthread_mutex_lock(&s->cache.lock); diff --git a/src/common/heap.c b/src/common/heap.c index fe06f5418fa23a742d012c30a26abcfb7273ab35..71b681c6e90f53da17a1319e3595d238a891ae25 100644 --- a/src/common/heap.c +++ b/src/common/heap.c @@ -46,8 +46,8 @@ void _def_swap(struct heap *h, void *e1, void *e2) { + if (e1 == e2) return; void *tmp = HTEMPELEMENT(h); - memcpy(tmp, e1, h->elm_size); memcpy(e1, e2, h->elm_size); memcpy(e2, tmp, h->elm_size); @@ -75,8 +75,8 @@ static inline void _heap_bubble_down(struct heap *h, int e) { e1 = 2*e; if(e1 > h->num) break; - if((h->cmp(HELEMENT(h, e),HELEMENT(h,e1)) < 0) && (e1 == h->num) || (h->cmp(HELEMENT(h, e),HELEMENT(h,e1+1)) < 0)) break; - if(e1 != h->num && (h->cmp(HELEMENT(h, e1+1),HELEMENT(h,e1)) < 0)) e1++; + if(((h->cmp(HELEMENT(h, e),HELEMENT(h,e1)) < 0) && (e1 == h->num)) || (h->cmp(HELEMENT(h, e),HELEMENT(h,e1+1)) < 0)) break; + if((e1 != h->num) && (h->cmp(HELEMENT(h, e1+1), HELEMENT(h,e1)) < 0)) e1++; h->swap(h,HELEMENT(h,e),HELEMENT(h,e1)); } }